From d52fc40d455dfda8ad1a217e94cf8eba46311f0f Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Mon, 17 May 2010 13:45:09 +0000
Subject: [PATCH] LMS-1527 Refactoring: Moving stuff from OpenBisServiceFactory
 to a new class: ServiceFinder in project server-common. Adapt build to create
 Raw Data API dist

SVN: 16001
---
 openbis/build/build.xml                       |   3 +-
 .../shared/DefaultLimsServiceStubFactory.java |  35 -----
 .../generic/shared/OpenBisServiceFactory.java | 143 ++----------------
 .../openbis/generic/shared/ResourceNames.java |   2 +
 .../query/client/api/v1/FacadeFactory.java    |  31 ++--
 .../shared/OpenBisServiceFactoryTest.java     | 105 -------------
 6 files changed, 27 insertions(+), 292 deletions(-)
 delete mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/DefaultLimsServiceStubFactory.java
 delete mode 100644 openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/OpenBisServiceFactoryTest.java

diff --git a/openbis/build/build.xml b/openbis/build/build.xml
index cff040d69fe..c0335a51d69 100644
--- a/openbis/build/build.xml
+++ b/openbis/build/build.xml
@@ -469,7 +469,8 @@
       <zipfileset src="${jar.file}">
         <include name="ch/systemsx/cisd/common/exceptions/**/*.class" />
         <include name="ch/systemsx/cisd/common/spring/HttpInvokerUtils.class" />
-        <include name="ch/systemsx/cisd/openbis/**/api/**/*.class" />
+        <include name="ch/systemsx/cisd/common/api/*.class" />
+        <include name="ch/systemsx/cisd/openbis/plugin/query/**/api/**/*.class" />
         <exclude name="ch/systemsx/cisd/openbis/**/server/api/**/*.class" />
         <include name="**/*BuildAndEnvironmentInfo.class" />
         <include name="*.INFO" />
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/DefaultLimsServiceStubFactory.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/DefaultLimsServiceStubFactory.java
deleted file mode 100644
index 7d424a82093..00000000000
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/DefaultLimsServiceStubFactory.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * Copyright 2010 ETH Zuerich, CISD
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package ch.systemsx.cisd.openbis.generic.shared;
-
-import ch.systemsx.cisd.common.spring.HttpInvokerUtils;
-import ch.systemsx.cisd.openbis.generic.shared.OpenBisServiceFactory.ILimsServiceStubFactory;
-
-/**
- * @author Chandrasekhar Ramakrishnan
- */
-public class DefaultLimsServiceStubFactory implements ILimsServiceStubFactory
-{
-    public static final int SERVER_TIMEOUT_MIN = 5;
-
-    public IETLLIMSService createServiceStub(String serverUrl)
-    {
-        return HttpInvokerUtils.createServiceStub(IETLLIMSService.class, serverUrl,
-                SERVER_TIMEOUT_MIN);
-    }
-
-}
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/OpenBisServiceFactory.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/OpenBisServiceFactory.java
index a0803763a1d..256c0bd4db0 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/OpenBisServiceFactory.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/OpenBisServiceFactory.java
@@ -16,64 +16,23 @@
 
 package ch.systemsx.cisd.openbis.generic.shared;
 
+import ch.systemsx.cisd.common.api.client.IServicePinger;
+import ch.systemsx.cisd.common.api.client.ServiceFinder;
+
 /**
  * A factory for creating proxies to the openBIS application server.
  * <p>
  * The OpenBisServiceFactory will create a proxy by trying several possible locations for the
  * service.
- * <p>
- * After calling {@link #createService}, you can get the url createService used by calling the
- * {@link #getUsedServerUrl} method.
  * 
  * @author Chandrasekhar Ramakrishnan
  */
 public class OpenBisServiceFactory
 {
-    /**
-     * An interface that can create a {@link IETLLIMSService} proxy to a service located at a given
-     * a URL.
-     * 
-     * @author Chandrasekhar Ramakrishnan
-     */
-    public static interface ILimsServiceStubFactory
-    {
-        /**
-         * Create a proxy to the service located at the serverURL. Implementations should not alter
-         * the url, e.g., by appending something to it.
-         * 
-         * @param serverUrl The URL of of the IETLLIMSService service
-         * @return IETLLIMSService The service located at the serverUrl
-         */
-        public IETLLIMSService createServiceStub(String serverUrl);
-    }
-
     private final String initialServerUrl;
 
     private final String urlServiceSuffix;
 
-    private final ILimsServiceStubFactory stubFactory;
-
-    private String usedServerUrl;
-
-    /**
-     * Constructor for the OpenBisServiceFactory. The service factory works best when the serverUrl
-     * is simply the protocol://machine:port of the openBIS application server. It will
-     * automatically append likely locations of the openBIS service to the url.
-     * <p>
-     * Examples:
-     * <ul>
-     * <li>OpenBisServiceFactory("http://localhost:8888/", "/rmi-etl", stubFactory)</li>
-     * <li>OpenBisServiceFactory("https://openbis.ethz.ch:8443/", "/rmi-etl", stubFactory)</li>
-     * </ul>
-     * 
-     * @param serverUrl The Url where the openBIS server is assumed to be.
-     * @param stubFactory A factory that, given a url, returns an IETLLIMSService proxy to the url
-     */
-    public OpenBisServiceFactory(String serverUrl, ILimsServiceStubFactory stubFactory)
-    {
-        this(serverUrl, "/rmi-etl", stubFactory);
-    }
-
     /**
      * Constructor for the OpenBisServiceFactory. The service factory works best when the serverUrl
      * is simply the protocol://machine:port of the openBIS application server. It will
@@ -87,15 +46,11 @@ public class OpenBisServiceFactory
      * 
      * @param serverUrl The Url where the openBIS server is assumed to be.
      * @param urlServiceSuffix The suffix appended to the url to refer to the service.
-     * @param stubFactory A factory that, given a url, returns an IETLLIMSService proxy to the url
      */
-    public OpenBisServiceFactory(String serverUrl, String urlServiceSuffix,
-            ILimsServiceStubFactory stubFactory)
+    public OpenBisServiceFactory(String serverUrl, String urlServiceSuffix)
     {
         this.initialServerUrl = serverUrl;
         this.urlServiceSuffix = urlServiceSuffix;
-        this.stubFactory = stubFactory;
-        this.usedServerUrl = "";
     }
 
     /**
@@ -105,87 +60,15 @@ public class OpenBisServiceFactory
      */
     public IETLLIMSService createService()
     {
-        IETLLIMSService service;
-        usedServerUrl = computeOpenbisOpenbisServerUrl(initialServerUrl);
-        // Try the url that ends in openbis/openbis
-        service = stubFactory.createServiceStub(usedServerUrl + urlServiceSuffix);
-        if (canConnectToService(service))
-        {
-            return service;
-        }
-
-        // Try the url that ends in just one openbis
-        usedServerUrl = computeOpenbisServerUrl(initialServerUrl);
-        service = stubFactory.createServiceStub(usedServerUrl + urlServiceSuffix);
-        if (canConnectToService(service))
-        {
-            return service;
-        }
-
-        // Try the url as provided
-        usedServerUrl = initialServerUrl;
-        service = stubFactory.createServiceStub(usedServerUrl + urlServiceSuffix);
-        return service;
+        ServiceFinder serviceFinder = new ServiceFinder("openbis", urlServiceSuffix);
+        return serviceFinder.createService(IETLLIMSService.class, initialServerUrl,
+                new IServicePinger<IETLLIMSService>()
+                    {
+                        public void ping(IETLLIMSService service)
+                        {
+                            service.getVersion();
+                }
+            });
     }
 
-    /**
-     * Return the serverUrl used by the createService method. The result of this method only makes
-     * sense after calling createService.
-     */
-    public String getUsedServerUrl()
-    {
-        return usedServerUrl;
-    }
-
-    private boolean canConnectToService(IETLLIMSService service)
-    {
-        try
-        {
-            service.getVersion();
-        } catch (Exception e)
-        {
-            return false;
-        }
-        return true;
-    }
-
-    private String computeOpenbisOpenbisServerUrl(String serverUrl)
-    {
-        if (serverUrl.endsWith("/openbis/openbis"))
-        {
-            return serverUrl;
-        }
-
-        if (serverUrl.endsWith("/openbis"))
-        {
-            return serverUrl + "/openbis";
-        }
-
-        String myServerUrl = serverUrl;
-        if (false == serverUrl.endsWith("/"))
-        {
-            myServerUrl = myServerUrl + "/";
-        }
-        return myServerUrl + "openbis/openbis";
-    }
-
-    private String computeOpenbisServerUrl(String serverUrl)
-    {
-        if (serverUrl.endsWith("/openbis/openbis"))
-        {
-            return serverUrl.substring(0, serverUrl.length() - "/openbis".length());
-        }
-
-        if (serverUrl.endsWith("/openbis"))
-        {
-            return serverUrl;
-        }
-
-        String myServerUrl = serverUrl;
-        if (false == serverUrl.endsWith("/"))
-        {
-            myServerUrl = myServerUrl + "/";
-        }
-        return myServerUrl + "openbis";
-    }
 }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/ResourceNames.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/ResourceNames.java
index e04ebbb7d43..ebf6640fb30 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/ResourceNames.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/ResourceNames.java
@@ -44,6 +44,8 @@ public final class ResourceNames
     }
 
     public final static String ETL_SERVICE = "etl-service";
+    
+    public final static String ETL_SERVICE_URL = "/rmi-etl";
 
     public final static String COMMON_SERVICE = "common-service";
 
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/client/api/v1/FacadeFactory.java b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/client/api/v1/FacadeFactory.java
index 185d6502e3b..42006302572 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/client/api/v1/FacadeFactory.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/client/api/v1/FacadeFactory.java
@@ -16,9 +16,7 @@
 
 package ch.systemsx.cisd.openbis.plugin.query.client.api.v1;
 
-import ch.systemsx.cisd.common.spring.HttpInvokerUtils;
-import ch.systemsx.cisd.openbis.generic.shared.DefaultLimsServiceStubFactory;
-import ch.systemsx.cisd.openbis.generic.shared.OpenBisServiceFactory;
+import ch.systemsx.cisd.common.api.client.ServiceFinder;
 import ch.systemsx.cisd.openbis.plugin.query.server.api.v1.ResourceNames;
 import ch.systemsx.cisd.openbis.plugin.query.shared.api.v1.IQueryApiServer;
 
@@ -29,26 +27,15 @@ import ch.systemsx.cisd.openbis.plugin.query.shared.api.v1.IQueryApiServer;
  */
 public class FacadeFactory
 {
-    private static final int SERVER_TIMEOUT_MIN = 5;
-
-    // Trick for discovering the server Url
-    private static String getServiceUrl(String serverUrl)
-    {
-        OpenBisServiceFactory openBisServiceFactory =
-                new OpenBisServiceFactory(serverUrl, new DefaultLimsServiceStubFactory());
-        openBisServiceFactory.createService();
-        return openBisServiceFactory.getUsedServerUrl() + ResourceNames.QUERY_PLUGIN_SERVER_URL;
-    }
+    private static final ServiceFinder SERVICE_FINDER =
+            new ServiceFinder("openbis", ResourceNames.QUERY_PLUGIN_SERVER_URL);
 
     /**
      * Creates a facade for specified server URL, user Id, and password.
      */
     public static IQueryApiFacade create(String serverURL, String userID, String password)
     {
-        String serviceUrl = getServiceUrl(serverURL);
-        IQueryApiServer service =
-                HttpInvokerUtils.createServiceStub(IQueryApiServer.class, serviceUrl,
-                        SERVER_TIMEOUT_MIN);
+        IQueryApiServer service = createService(serverURL);
         String sessionToken = service.tryToAuthenticateAtQueryServer(userID, password);
         if (sessionToken == null)
         {
@@ -62,9 +49,11 @@ public class FacadeFactory
      */
     public static IQueryApiFacade create(String serverURL, String sessionToken)
     {
-        IQueryApiServer service =
-                HttpInvokerUtils.createServiceStub(IQueryApiServer.class, serverURL
-                        + ResourceNames.QUERY_PLUGIN_SERVER_URL, SERVER_TIMEOUT_MIN);
-        return new QueryApiFacade(service, sessionToken);
+        return new QueryApiFacade(createService(serverURL), sessionToken);
+    }
+    
+    private static IQueryApiServer createService(String serverURL)
+    {
+        return SERVICE_FINDER.createService(IQueryApiServer.class, serverURL);
     }
 }
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/OpenBisServiceFactoryTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/OpenBisServiceFactoryTest.java
deleted file mode 100644
index 99a8afb9c86..00000000000
--- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/OpenBisServiceFactoryTest.java
+++ /dev/null
@@ -1,105 +0,0 @@
-/*
- * Copyright 2010 ETH Zuerich, CISD
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package ch.systemsx.cisd.openbis.generic.shared;
-
-import org.jmock.Expectations;
-import org.jmock.Mockery;
-import org.testng.AssertJUnit;
-import org.testng.annotations.BeforeMethod;
-import org.testng.annotations.Test;
-
-import ch.systemsx.cisd.openbis.generic.shared.IETLLIMSService;
-import ch.systemsx.cisd.openbis.generic.shared.OpenBisServiceFactory;
-import ch.systemsx.cisd.openbis.generic.shared.OpenBisServiceFactory.ILimsServiceStubFactory;
-
-/**
- * @author Chandrasekhar Ramakrishnan
- */
-public class OpenBisServiceFactoryTest extends AssertJUnit
-{
-    private Mockery context;
-
-    private IETLLIMSService openBisService;
-
-    private ILimsServiceStubFactory stubFactory;
-
-    @BeforeMethod
-    public void setUp()
-    {
-        context = new Mockery();
-        openBisService = context.mock(IETLLIMSService.class);
-        stubFactory = context.mock(ILimsServiceStubFactory.class);
-    }
-
-    @Test
-    public void testBasicUse()
-    {
-        context.checking(new Expectations()
-            {
-                {
-                    one(stubFactory).createServiceStub(
-                            "http://localhost:8888/openbis/openbis/rmi-etl");
-                    will(returnValue(openBisService));
-                    one(openBisService).getVersion();
-                }
-            });
-        OpenBisServiceFactory factory =
-                new OpenBisServiceFactory("http://localhost:8888", stubFactory);
-        factory.createService();
-        context.assertIsSatisfied();
-    }
-
-    @Test
-    public void testAlternateLocation()
-    {
-        context.checking(new Expectations()
-            {
-                {
-                    one(stubFactory).createServiceStub(
-                            "http://localhost:8888/openbis/openbis/rmi-etl");
-                    will(returnValue(openBisService));
-                    one(openBisService).getVersion();
-                    will(throwException(new Exception()));
-                    one(stubFactory).createServiceStub("http://localhost:8888/openbis/rmi-etl");
-                    will(returnValue(openBisService));
-                    one(openBisService).getVersion();
-                }
-            });
-        OpenBisServiceFactory factory =
-                new OpenBisServiceFactory("http://localhost:8888", stubFactory);
-        factory.createService();
-        context.assertIsSatisfied();
-    }
-
-    @Test
-    public void testLocationAlreadySpecified()
-    {
-        context.checking(new Expectations()
-            {
-                {
-                    one(stubFactory).createServiceStub(
-                            "http://localhost:8888/openbis/openbis/rmi-etl");
-                    will(returnValue(openBisService));
-                    one(openBisService).getVersion();
-                }
-            });
-        OpenBisServiceFactory factory =
-                new OpenBisServiceFactory("http://localhost:8888/openbis/openbis", stubFactory);
-        factory.createService();
-        context.assertIsSatisfied();
-    }
-}
-- 
GitLab