]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/server/client/SlcServerHttpClient.java
Start implementing OSGi integration tests
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / server / client / SlcServerHttpClient.java
index 7a77175cf4a6de3a5e236c7f3c3acc8af14070df..7102a749c014dc6d43a9d411416efe13bde6932b 100644 (file)
@@ -1,5 +1,6 @@
 package org.argeo.slc.server.client;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.net.HttpURLConnection;
 import java.net.URL;
@@ -8,16 +9,76 @@ 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 = "http://localhost:7070/org.argeo.slc.webapp/";
+       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);
@@ -34,10 +95,7 @@ public class SlcServerHttpClient {
                        in = connection.getInputStream();
                        StreamSource source = new StreamSource(in);
                        Object obj = unmarshaller.unmarshal(source);
-                       return (T) obj;
-               } catch (Exception e) {
-                       throw new SlcException("Cannot call service " + path + " on "
-                                       + baseUrl, e);
+                       return obj;
                } finally {
                        IOUtils.closeQuietly(in);
                }
@@ -51,4 +109,13 @@ public class SlcServerHttpClient {
                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;
+       }
+
 }