]> git.argeo.org Git - gpl/argeo-slc.git/commitdiff
@update:79; Wait for one agent
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 7 Jun 2009 16:26:44 +0000 (16:26 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 7 Jun 2009 16:26:44 +0000 (16:26 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@2511 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

integration-tests/org.argeo.slc.it.webapp/.classpath
integration-tests/org.argeo.slc.it.webapp/pom.xml
integration-tests/org.argeo.slc.it.webapp/src/test/java/org/argeo/slc/it/webapp/AgentTest.java
integration-tests/org.argeo.slc.it.webapp/src/test/resources/log4j.properties [new file with mode: 0644]
runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/server/client/HttpServicesClient.java [new file with mode: 0644]
runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/server/client/SlcServerHttpClient.java
runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/server/client/impl/AbstractHttpServicesClient.java [new file with mode: 0644]
runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/server/client/impl/SlcServerHttpClientImpl.java [new file with mode: 0644]
runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/server/unit/AbstractHttpClientTestCase.java
runtime/org.argeo.slc.server/src/main/resources/org/argeo/slc/server/client/spring.xml
runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/Condition.java [new file with mode: 0644]

index 04257bbcb6fb43a5454dae149db0cc8ec1f3529c..9bedaab94670ce601c4dcd2047dd06f61c380416 100644 (file)
@@ -1,8 +1,11 @@
 <?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>
index b9879ddab142bfc436a8bd578b4bff1d0d871c26..ef22873797e2a08bab81fdea3e7934521509cf64 100644 (file)
@@ -2,12 +2,11 @@
        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>
index e4515d27588ca918009f530f5e90091b5631e8d3..a66eca4806ed5f5eb88cf782f71fdbd54727a44d 100644 (file)
@@ -1,20 +1,16 @@
 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());
        }
 
 }
diff --git a/integration-tests/org.argeo.slc.it.webapp/src/test/resources/log4j.properties b/integration-tests/org.argeo.slc.it.webapp/src/test/resources/log4j.properties
new file mode 100644 (file)
index 0000000..bd20b8e
--- /dev/null
@@ -0,0 +1,14 @@
+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
diff --git a/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/server/client/HttpServicesClient.java b/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/server/client/HttpServicesClient.java
new file mode 100644 (file)
index 0000000..9bdcd5f
--- /dev/null
@@ -0,0 +1,27 @@
+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);
+
+}
index 7102a749c014dc6d43a9d411416efe13bde6932b..2113438f8c06566bdcef903c8b464053d9380958 100644 (file)
@@ -1,121 +1,9 @@
 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();
 }
diff --git a/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/server/client/impl/AbstractHttpServicesClient.java b/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/server/client/impl/AbstractHttpServicesClient.java
new file mode 100644 (file)
index 0000000..ba4e584
--- /dev/null
@@ -0,0 +1,126 @@
+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;
+       }
+}
diff --git a/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/server/client/impl/SlcServerHttpClientImpl.java b/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/server/client/impl/SlcServerHttpClientImpl.java
new file mode 100644 (file)
index 0000000..75e4e8e
--- /dev/null
@@ -0,0 +1,37 @@
+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;
+       }
+
+}
index 5d5dcea5ec77add2f6332fa4df94747c12f119ec..f0142ba50f38f53dc0fc5edfd6bb7cf6e52b27b6 100644 (file)
@@ -13,11 +13,12 @@ public abstract class AbstractHttpClientTestCase extends AbstractSpringTestCase
        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);
        }
index e67633eec48a25d589e190b58973d954c6a52a39..8f63be9b1dec2bcc059baf1ef302baf0b082c258 100644 (file)
@@ -10,7 +10,7 @@
        <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/" />
diff --git a/runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/Condition.java b/runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/Condition.java
new file mode 100644 (file)
index 0000000..f476961
--- /dev/null
@@ -0,0 +1,11 @@
+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);
+}