<?xml version="1.0" encoding="UTF-8"?>
<classpath>
- <classpathentry kind="src" path="src/test/java"/>
- <classpathentry kind="src" path="src/test/resources"/>
- <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
- <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
- <classpathentry kind="output" path="target/classes"/>
+ <classpathentry kind="src" path="src/test/java" output="target/test-classes" />
+ <classpathentry kind="src" path="src/test/resources"
+ output="target/test-classes" />
+ <classpathentry kind="con"
+ path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5" />
+ <classpathentry kind="con"
+ path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER" />
+ <classpathentry kind="output" path="target/classes" />
</classpath>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
- <!--
- <groupId>org.argeo.slc.server</groupId>
- <artifactId>deploy</artifactId>
- <version>0.11.4-SNAPSHOT</version>
- <relativePath>../../server/org.argeo.slc.siteserver</relativePath>
- -->
+ <!--
+ <groupId>org.argeo.slc.server</groupId>
+ <artifactId>deploy</artifactId> <version>0.11.4-SNAPSHOT</version>
+ <relativePath>../../server/org.argeo.slc.siteserver</relativePath>
+ -->
<groupId>org.argeo.slc</groupId>
<artifactId>integration-tests</artifactId>
<version>0.11.4-SNAPSHOT</version>
<groupId>org.argeo.slc.maven</groupId>
<artifactId>maven-argeo-osgi-plugin</artifactId>
<version>${version.maven-argeo-osgi}</version>
+ <configuration>
+ <argsToAppend>
+ <arg>-clean</arg>
+ </argsToAppend>
+ <systemProperties>
+ <slc.osgi.bundles>
+ ${basedir}/../../demo/site;in=*;ex=target,
+ ${basedir}/../../server/org.argeo.slc.siteserver/bundles;in=*
+ </slc.osgi.bundles>
+ <slc.osgi.start>
+ org.argeo.dep.osgi.catalina.start,
+ org.springframework.osgi.extender,
+ org.springframework.osgi.web.extender,
+ org.springframework.osgi.samples.simplewebapp,
+ org.argeo.slc.server.activemq,
+ org.argeo.slc.server.hsqldb,
+ org.argeo.slc.server.hibernate,
+ org.argeo.slc.server.services,
+ org.argeo.slc.server.jms,
+ org.argeo.slc.webapp,
+ org.argeo.slc.ria,
+ org.argeo.slc.agent
+ </slc.osgi.start>
+ </systemProperties>
+ </configuration>
<executions>
<execution>
<id>equinox-start</id>
</goals>
<configuration>
<wait>false</wait>
- <argsToAppend>
- <arg>-clean</arg>
- </argsToAppend>
- <systemProperties>
- <slc.osgi.bundles>
- ${basedir}/../../demo/site;in=*;ex=target,
- ${basedir}/../../server/org.argeo.slc.siteserver/bundles;in=*
- </slc.osgi.bundles>
- <slc.osgi.start>
- org.argeo.dep.osgi.catalina.start,
- org.springframework.osgi.extender,
- org.springframework.osgi.web.extender,
- org.springframework.osgi.samples.simplewebapp,
- org.argeo.slc.server.activemq,
- org.argeo.slc.server.hsqldb,
- org.argeo.slc.server.hibernate,
- org.argeo.slc.server.services,
- org.argeo.slc.server.jms,
- org.argeo.slc.webapp,
- org.argeo.slc.ria
- </slc.osgi.start>
- </systemProperties>
</configuration>
</execution>
<execution>
package org.argeo.slc.it.webapp;
-import org.argeo.slc.msg.ObjectList;
-import org.argeo.slc.server.client.SlcServerHttpClient;
+import java.net.InetAddress;
+
+import org.argeo.slc.runtime.SlcAgentDescriptor;
import org.argeo.slc.server.unit.AbstractHttpClientTestCase;
-public class AgentTest extends AbstractHttpClientTestCase{
+public class AgentTest extends AbstractHttpClientTestCase {
public void testListAgents() throws Exception {
- waitForServerToBeReady();
- SlcServerHttpClient httpClient = getBean(SlcServerHttpClient.class);
- ObjectList objectList = httpClient.callService("listAgents.service",
- null);
- assertEquals(0, objectList.getObjects().size());
-// SlcAgentDescriptor slcAgentDescriptor = (SlcAgentDescriptor) objectList
-// .getObjects().get(0);
-// assertEquals(InetAddress.getLocalHost().getHostName(),
-// slcAgentDescriptor.getHost());
+ SlcAgentDescriptor slcAgentDescriptor = getHttpClient()
+ .waitForOneAgent();
+ assertEquals(InetAddress.getLocalHost().getHostName(),
+ slcAgentDescriptor.getHost());
}
}
--- /dev/null
+log4j.rootLogger=WARN, console\r
+\r
+## Levels\r
+# SLC\r
+log4j.logger.org.argeo=DEBUG\r
+\r
+## Appenders\r
+# A1 is set to be a ConsoleAppender.\r
+log4j.appender.console=org.apache.log4j.ConsoleAppender\r
+\r
+# A1 uses PatternLayout.\r
+log4j.appender.console.layout=org.apache.log4j.PatternLayout\r
+log4j.appender.console.layout.ConversionPattern= %-5p %d{ISO8601} %m - %c%n\r
+\r
--- /dev/null
+package org.argeo.slc.server.client;
+
+import java.util.Map;
+
+import org.argeo.slc.Condition;
+
+/** Abstraction of the access to HTTP services . */
+public interface HttpServicesClient {
+ /** Call service, failing if it is not available. */
+ public <T> T callService(String path, Map<String, String> parameters);
+
+ /**
+ * Call service, waiting and retrying until the timeout is reached if it is
+ * not immediately available.
+ *
+ * @param path
+ * service path
+ * @param condition
+ * if not null, a condition to be applied on received object,
+ * keep trying if it returns false.
+ * @param timeout
+ * timeout after which an exception is thrown
+ */
+ public <T> T callServiceSafe(String path, Map<String, String> parameters,
+ Condition<T> condition, Long timeout);
+
+}
package org.argeo.slc.server.client;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import java.util.Map;
-
-import javax.xml.transform.stream.StreamSource;
-
-import org.apache.commons.io.IOUtils;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.argeo.slc.SlcException;
-import org.springframework.oxm.Unmarshaller;
-import org.springframework.util.Assert;
-
-public class SlcServerHttpClient {
- private final static Log log = LogFactory.getLog(SlcServerHttpClient.class);
-
- private Unmarshaller unmarshaller;
- private String baseUrl;
-
- private Long retryPeriod = 1000l;
-
- @SuppressWarnings(value = { "unchecked" })
- public <T> T callService(String path, Map<String, String> parameters) {
- try {
- return (T) callServiceLowLevel(path, parameters);
- } catch (Exception e) {
- throw new SlcException("Cannot call service " + path + " on "
- + baseUrl, e);
- }
- }
-
- @SuppressWarnings(value = { "unchecked" })
- public <T> T callServiceSafe(String path, Map<String, String> parameters,
- long timeout) {
- long begin = System.currentTimeMillis();
- try {
- Object obj = null;
- long duration = System.currentTimeMillis() - begin;
- while (duration < timeout) {
- try {
- obj = callServiceLowLevel(path, parameters);
- } catch (IOException e) {
- if (log.isTraceEnabled())
- log.trace("Exception when calling service " + path
- + " on " + baseUrl, e);
- }
-
- if (obj != null)
- break;
-
- // wait a bit
- try {
- Thread.sleep(retryPeriod);
- } catch (InterruptedException e) {
- // silent
- }
- }
-
- if (obj == null)
- throw new SlcException(
- "Service "
- + path
- + " on "
- + baseUrl
- + " did not return an answer after calling it safely for "
- + duration + " ms.");
- return (T) obj;
- } catch (Exception e) {
- throw new SlcException(
- "Unexpected exception when safely calling service " + path
- + " on " + baseUrl, e);
- }
- }
-
- protected Object callServiceLowLevel(String path,
- Map<String, String> parameters) throws IOException {
- Assert.notNull(baseUrl, "base url");
- InputStream in = null;
- try {
- URL url = new URL(baseUrl + path);
- HttpURLConnection connection = (HttpURLConnection) url
- .openConnection();
- if (parameters != null) {
- for (String key : parameters.keySet()) {
- connection.addRequestProperty(key, parameters.get(key));
- }
- }
-
- connection.connect();
-
- in = connection.getInputStream();
- StreamSource source = new StreamSource(in);
- Object obj = unmarshaller.unmarshal(source);
- return obj;
- } finally {
- IOUtils.closeQuietly(in);
- }
- }
-
- public void setUnmarshaller(Unmarshaller unmarshaller) {
- this.unmarshaller = unmarshaller;
- }
-
- public void setBaseUrl(String baseUrl) {
- this.baseUrl = baseUrl;
- }
-
- public Long getRetryPeriod() {
- return retryPeriod;
- }
-
- /** Retry period in ms when accessing service safely. Default is 1000 ms. */
- public void setRetryPeriod(Long retryPeriod) {
- this.retryPeriod = retryPeriod;
- }
+import org.argeo.slc.runtime.SlcAgentDescriptor;
+/** Abstraction of the access to HTTP services of an SLC Server. */
+public interface SlcServerHttpClient extends HttpServicesClient {
+ /** Wait for one agent to be available. */
+ public SlcAgentDescriptor waitForOneAgent();
}
--- /dev/null
+package org.argeo.slc.server.client.impl;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.Map;
+
+import javax.xml.transform.stream.StreamSource;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.argeo.slc.Condition;
+import org.argeo.slc.SlcException;
+import org.argeo.slc.server.client.HttpServicesClient;
+import org.springframework.oxm.Unmarshaller;
+import org.springframework.util.Assert;
+
+public abstract class AbstractHttpServicesClient implements HttpServicesClient {
+ private final static Log log = LogFactory
+ .getLog(AbstractHttpServicesClient.class);
+ private Unmarshaller unmarshaller;
+ private String baseUrl;
+
+ private Long retryPeriod = 1000l;
+ @SuppressWarnings(value = { "unchecked" })
+ public <T> T callService(String path, Map<String, String> parameters) {
+ try {
+ return (T) callServiceLowLevel(path, parameters);
+ } catch (Exception e) {
+ throw new SlcException("Cannot call service " + path + " on "
+ + baseUrl, e);
+ }
+ }
+
+ @SuppressWarnings(value = { "unchecked" })
+ public <T> T callServiceSafe(String path, Map<String, String> parameters,
+ Condition<T> condition, Long timeout) {
+ long begin = System.currentTimeMillis();
+ try {
+ Object obj = null;
+ long duration = System.currentTimeMillis() - begin;
+ while (duration < timeout) {
+ try {
+ obj = callServiceLowLevel(path, parameters);
+ } catch (IOException e) {
+ if (log.isTraceEnabled())
+ log.trace("Exception when calling service " + path
+ + " on " + baseUrl, e);
+ }
+
+ if (obj != null) {
+ if (condition == null)
+ break;
+ else {
+ if (condition.check((T) obj))
+ break;
+ }
+ }
+ // wait a bit
+ try {
+ Thread.sleep(retryPeriod);
+ } catch (InterruptedException e) {
+ // silent
+ }
+ }
+
+ if (obj == null)
+ throw new SlcException(
+ "Service "
+ + path
+ + " on "
+ + baseUrl
+ + " did not return an answer after calling it safely for "
+ + duration + " ms.");
+ return (T) obj;
+ } catch (Exception e) {
+ throw new SlcException(
+ "Unexpected exception when safely calling service " + path
+ + " on " + baseUrl, e);
+ }
+ }
+
+ protected Object callServiceLowLevel(String path,
+ Map<String, String> parameters) throws IOException {
+ Assert.notNull(baseUrl, "base url");
+ InputStream in = null;
+ try {
+ URL url = new URL(baseUrl + path);
+ HttpURLConnection connection = (HttpURLConnection) url
+ .openConnection();
+ if (parameters != null) {
+ for (String key : parameters.keySet()) {
+ connection.addRequestProperty(key, parameters.get(key));
+ }
+ }
+
+ connection.connect();
+
+ in = connection.getInputStream();
+ StreamSource source = new StreamSource(in);
+ Object obj = unmarshaller.unmarshal(source);
+ return obj;
+ } finally {
+ IOUtils.closeQuietly(in);
+ }
+ }
+
+ public void setUnmarshaller(Unmarshaller unmarshaller) {
+ this.unmarshaller = unmarshaller;
+ }
+
+ public void setBaseUrl(String baseUrl) {
+ this.baseUrl = baseUrl;
+ }
+
+ public Long getRetryPeriod() {
+ return retryPeriod;
+ }
+
+ /** Retry period in ms when accessing service safely. Default is 1000 ms. */
+ public void setRetryPeriod(Long retryPeriod) {
+ this.retryPeriod = retryPeriod;
+ }
+}
--- /dev/null
+package org.argeo.slc.server.client.impl;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.argeo.slc.Condition;
+import org.argeo.slc.msg.ObjectList;
+import org.argeo.slc.runtime.SlcAgentDescriptor;
+import org.argeo.slc.server.client.SlcServerHttpClient;
+
+public class SlcServerHttpClientImpl extends AbstractHttpServicesClient
+ implements SlcServerHttpClient {
+ public final static String LIST_AGENTS = "listAgents.service";
+
+ private final static Log log = LogFactory
+ .getLog(SlcServerHttpClientImpl.class);
+
+ private Long retryTimeout = 60 * 1000l;
+
+ public SlcAgentDescriptor waitForOneAgent() {
+ ObjectList objectList = callServiceSafe(LIST_AGENTS, null,
+ new Condition<ObjectList>() {
+ public Boolean check(ObjectList obj) {
+ int size = obj.getObjects().size();
+ if (log.isTraceEnabled())
+ log.trace("Object list size: " + size);
+ return size == 1;
+ }
+ }, retryTimeout);
+ return (SlcAgentDescriptor) objectList.getObjects().get(0);
+ }
+
+ /** Timeout in ms after which a safe call will throw an exception. */
+ public void setRetryTimeout(Long retryTimeout) {
+ this.retryTimeout = retryTimeout;
+ }
+
+}
protected void setUp() throws Exception {
super.setUp();
httpClient = createHttpClient();
+ waitForServerToBeReady();
}
protected void waitForServerToBeReady() {
ExecutionAnswer answer = httpClient.callServiceSafe(
- isServerReadyService, null, getServerReadyTimeout());
+ isServerReadyService, null, null, getServerReadyTimeout());
if (!answer.isOk())
throw new SlcException("Server is not ready: " + answer);
}
<import resource="classpath:org/argeo/slc/castor/spring.xml" />
<!-- Http client -->
- <bean id="slcDefault.serverHttpClient" class="org.argeo.slc.server.client.SlcServerHttpClient"
+ <bean id="slcDefault.serverHttpClient" class="org.argeo.slc.server.client.impl.SlcServerHttpClientImpl"
lazy-init="true">
<property name="unmarshaller" ref="slcDefault.castor.marshaller" />
<property name="baseUrl" value="http://localhost:7070/org.argeo.slc.webapp/" />
--- /dev/null
+package org.argeo.slc;
+
+/** Binary check on an arbitrary object. */
+public interface Condition<T> {
+ /**
+ * Checks the condition.
+ *
+ * @return true, if the condition is verified, false if not.
+ */
+ public Boolean check(T obj);
+}