From 4cfccfb0b6f95bd191b909b27557e70e4e241f5f Mon Sep 17 00:00:00 2001
From: cramakri <cramakri>
Date: Wed, 12 May 2010 09:04:07 +0000
Subject: [PATCH] LMS-1527 Moved OpenBisServcieFactory, made the name-server
 easier to access.

SVN: 15932
---
 .../dss/api/v1/client/impl/DssComponent.java  |   7 +-
 .../v1/client/impl/OpenBisServiceFactory.java | 155 ------------------
 .../dss/generic/server/DataStoreServer.java   |  38 ++---
 .../server/EncapsulatedOpenBISService.java    |  18 +-
 .../dss/generic/shared/ServiceProvider.java   |   5 +-
 .../source/java/dssApplicationContext.xml     |   9 +-
 .../impl/OpenBisServiceFactoryTest.java       | 104 ------------
 7 files changed, 41 insertions(+), 295 deletions(-)
 delete mode 100644 datastore_server/source/java/ch/systemsx/cisd/openbis/dss/api/v1/client/impl/OpenBisServiceFactory.java
 delete mode 100644 datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/api/v1/client/impl/OpenBisServiceFactoryTest.java

diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/api/v1/client/impl/DssComponent.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/api/v1/client/impl/DssComponent.java
index 174a346e7a6..9f6d8a0afe8 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/api/v1/client/impl/DssComponent.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/api/v1/client/impl/DssComponent.java
@@ -17,7 +17,7 @@
 package ch.systemsx.cisd.openbis.dss.api.v1.client.impl;
 
 import java.io.InputStream;
-import java.util.List;
+import java.util.Collection;
 
 import org.springframework.remoting.RemoteAccessException;
 import org.springframework.remoting.RemoteConnectFailureException;
@@ -31,10 +31,11 @@ import ch.systemsx.cisd.common.exceptions.InvalidSessionException;
 import ch.systemsx.cisd.common.spring.HttpInvokerUtils;
 import ch.systemsx.cisd.openbis.dss.api.v1.client.IDataSetDss;
 import ch.systemsx.cisd.openbis.dss.api.v1.client.IDssComponent;
-import ch.systemsx.cisd.openbis.dss.api.v1.client.impl.OpenBisServiceFactory.ILimsServiceStubFactory;
 import ch.systemsx.cisd.openbis.dss.api.v1.shared.FileInfoDssDTO;
 import ch.systemsx.cisd.openbis.dss.api.v1.shared.IDssServiceRpcGeneric;
 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;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataStore;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.ExternalData;
 import ch.systemsx.cisd.openbis.generic.shared.dto.SessionContextDTO;
@@ -394,7 +395,7 @@ class AuthenticatedState extends AbstractDssComponentState
     private IDssServiceRpcGeneric basicGetDssServiceForUrl(String serverURL)
     {
         IDssServiceRpcGeneric dssService = null;
-        List<RpcServiceInterfaceDTO> ifaces =
+        Collection<RpcServiceInterfaceDTO> ifaces =
                 dssServiceFactory.getSupportedInterfaces(serverURL, false);
 
         for (RpcServiceInterfaceDTO iface : ifaces)
diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/api/v1/client/impl/OpenBisServiceFactory.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/api/v1/client/impl/OpenBisServiceFactory.java
deleted file mode 100644
index 6896419bc28..00000000000
--- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/api/v1/client/impl/OpenBisServiceFactory.java
+++ /dev/null
@@ -1,155 +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.dss.api.v1.client.impl;
-
-import ch.systemsx.cisd.openbis.generic.shared.IETLLIMSService;
-
-/**
- * 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.
- * 
- * @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 ILimsServiceStubFactory 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
-     * automatically append likely locations of the openBIS service to the url.
-     * <p>
-     * Examples:
-     * <ul>
-     * <li>OpenBisServiceFactory("http://localhost:8888/", stubFactory)</li>
-     * <li>OpenBisServiceFactory("https://openbis.ethz.ch:8443/", 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.initialServerUrl = serverUrl;
-        this.stubFactory = stubFactory;
-    }
-
-    /**
-     * Create a IETLLIMSService by trying several possible locations for the service until one that
-     * works is found. If the service cannot be found, a proxy to the constructor-provided serverUrl
-     * will be returned.
-     */
-    public IETLLIMSService createService()
-    {
-        IETLLIMSService service;
-        // Try the url that ends in openbis/openbis
-        service =
-                stubFactory.createServiceStub(computeOpenbisOpenbisServerUrl(initialServerUrl)
-                        + "/rmi-etl");
-        if (canConnectToService(service))
-        {
-            return service;
-        }
-
-        // Try the url that ends in just one openbis
-        service =
-                stubFactory.createServiceStub(computeOpenbisServerUrl(initialServerUrl)
-                        + "/rmi-etl");
-        if (canConnectToService(service))
-        {
-            return service;
-        }
-
-        // Try the url as provided
-        service = stubFactory.createServiceStub(initialServerUrl + "/rmi-etl");
-        return service;
-    }
-
-    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/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreServer.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreServer.java
index 734b4afd553..aef7532fca0 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreServer.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreServer.java
@@ -36,6 +36,7 @@ import org.mortbay.jetty.security.SslSocketConnector;
 import org.mortbay.jetty.servlet.Context;
 import org.mortbay.jetty.servlet.ServletHolder;
 import org.springframework.context.i18n.LocaleContextHolder;
+import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
 import org.springframework.util.StringUtils;
 import org.springframework.web.HttpRequestHandler;
 import org.springframework.web.HttpRequestMethodNotSupportedException;
@@ -43,7 +44,7 @@ import org.springframework.web.context.WebApplicationContext;
 
 import com.marathon.util.spring.StreamSupportingHttpInvokerServiceExporter;
 
-import ch.systemsx.cisd.common.api.RpcServiceInterfaceDTO;
+import ch.systemsx.cisd.common.api.IRpcServiceNameServer;
 import ch.systemsx.cisd.common.api.RpcServiceInterfaceVersionDTO;
 import ch.systemsx.cisd.common.api.server.RpcServiceNameServer;
 import ch.systemsx.cisd.common.exceptions.ConfigurationFailureException;
@@ -53,7 +54,6 @@ import ch.systemsx.cisd.common.logging.LogFactory;
 import ch.systemsx.cisd.common.logging.LogInitializer;
 import ch.systemsx.cisd.common.utilities.ExtendedProperties;
 import ch.systemsx.cisd.openbis.dss.api.v1.server.DssServiceRpcGeneric;
-import ch.systemsx.cisd.openbis.dss.api.v1.shared.IDssServiceRpcGeneric;
 import ch.systemsx.cisd.openbis.dss.generic.server.ConfigParameters.PluginServlet;
 import ch.systemsx.cisd.openbis.dss.generic.shared.IEncapsulatedOpenBISService;
 import ch.systemsx.cisd.openbis.dss.generic.shared.ServiceProvider;
@@ -67,11 +67,6 @@ import ch.systemsx.cisd.openbis.generic.shared.IServer;
  */
 public class DataStoreServer
 {
-
-    /** Part of the URL of the DSS RPC service. */
-    public static final String DATA_STORE_SERVER_RPC_SERVICE_NAME =
-            DATA_STORE_SERVER_WEB_APPLICATION_NAME + "/rpc";
-
     private static final class DataStoreServlet extends HttpServlet
     {
         private static final long serialVersionUID = 1L;
@@ -218,34 +213,31 @@ public class DataStoreServer
         service.setStoreDirectory(applicationContext.getConfigParameters().getStorePath());
 
         // Export the spring bean to the world by wrapping it in an HttpInvokerServlet
-        String rpcV1Path = "/" + DATA_STORE_SERVER_RPC_SERVICE_NAME + "/v1";
+        String rpcV1Suffix = "/rmi-dss-api-v1";
+        String rpcV1Path = "/" + DATA_STORE_SERVER_WEB_APPLICATION_NAME + rpcV1Suffix;
         context.addServlet(new ServletHolder(new HttpInvokerServlet(v1ServiceExporter, rpcV1Path)),
                 rpcV1Path);
 
         // Inform the name server about the services I export
         // N.b. In the future, this could be done using spring instead of programmatically
-        StreamSupportingHttpInvokerServiceExporter nameServiceExporter =
-                ServiceProvider.getDssServiceRpcNameServer();
+        HttpInvokerServiceExporter nameServiceExporter =
+                ServiceProvider.getRpcNameServiceExporter();
         RpcServiceNameServer rpcNameServer =
                 (RpcServiceNameServer) nameServiceExporter.getService();
 
-        RpcServiceInterfaceDTO dssInterface =
-                new RpcServiceInterfaceDTO(IDssServiceRpcGeneric.DSS_SERVICE_NAME);
-
         RpcServiceInterfaceVersionDTO v1Interface =
-                new RpcServiceInterfaceVersionDTO(DssServiceRpcGeneric.DSS_SERVICE_NAME, "/rpc/v1",
-                        1, 0);
-        dssInterface.addVersion(v1Interface);
-        rpcNameServer.addSupportedInterface(dssInterface);
-
-        String nameServerPath = "/" + DATA_STORE_SERVER_RPC_SERVICE_NAME;
-        RpcServiceInterfaceDTO nameServerInterface = new RpcServiceInterfaceDTO("NameServer");
+                new RpcServiceInterfaceVersionDTO(DssServiceRpcGeneric.DSS_SERVICE_NAME,
+                        rpcV1Suffix, 1, 0);
+        rpcNameServer.addSupportedInterfaceVersion(v1Interface);
 
+        String nameServerPath =
+                "/" + DATA_STORE_SERVER_WEB_APPLICATION_NAME
+                        + IRpcServiceNameServer.PREFFERED_URL_SUFFIX;
         RpcServiceInterfaceVersionDTO nameServerVersion =
-                new RpcServiceInterfaceVersionDTO("NameServer", "/rpc", 1, 0);
-        nameServerInterface.addVersion(nameServerVersion);
+                new RpcServiceInterfaceVersionDTO(IRpcServiceNameServer.PREFFERED_SERVICE_NAME,
+                        IRpcServiceNameServer.PREFFERED_URL_SUFFIX, 1, 0);
 
-        rpcNameServer.addSupportedInterface(nameServerInterface);
+        rpcNameServer.addSupportedInterfaceVersion(nameServerVersion);
 
         context.addServlet(new ServletHolder(new HttpInvokerServlet(nameServiceExporter,
                 nameServerPath)), nameServerPath);
diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/EncapsulatedOpenBISService.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/EncapsulatedOpenBISService.java
index a8b4ac192cd..fc1bf7d950b 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/EncapsulatedOpenBISService.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/EncapsulatedOpenBISService.java
@@ -31,6 +31,8 @@ import ch.systemsx.cisd.openbis.dss.generic.server.plugins.tasks.PluginTaskProvi
 import ch.systemsx.cisd.openbis.dss.generic.shared.IEncapsulatedOpenBISService;
 import ch.systemsx.cisd.openbis.dss.generic.shared.dto.DataSetInformation;
 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;
 import ch.systemsx.cisd.openbis.generic.shared.basic.TechId;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.ArchiverDataSetCriteria;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataSetArchivingStatus;
@@ -103,11 +105,23 @@ public final class EncapsulatedOpenBISService implements IEncapsulatedOpenBISSer
 
     private final boolean archiverConfigured;
 
+    private static IETLLIMSService createOpenBisService(String openBISURL)
+    {
+        ILimsServiceStubFactory stubFactory = new ILimsServiceStubFactory()
+            {
+                public IETLLIMSService createServiceStub(String serverUrl)
+                {
+                    return HttpInvokerUtils.createServiceStub(IETLLIMSService.class, serverUrl, 5);
+                }
+
+            };
+        return new OpenBisServiceFactory(openBISURL, stubFactory).createService();
+    }
+
     public EncapsulatedOpenBISService(SessionTokenManager sessionTokenManager, String serverURL,
             PluginTaskProviders pluginTaskParameters)
     {
-        this(sessionTokenManager, HttpInvokerUtils.createServiceStub(IETLLIMSService.class,
-                serverURL + "/rmi-etl", 5), pluginTaskParameters);
+        this(sessionTokenManager, createOpenBisService(serverURL), pluginTaskParameters);
     }
 
     public EncapsulatedOpenBISService(SessionTokenManager sessionTokenManager,
diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/ServiceProvider.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/ServiceProvider.java
index a8b01c55f40..0ae4351741b 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/ServiceProvider.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/ServiceProvider.java
@@ -22,7 +22,6 @@ import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
 
 import com.marathon.util.spring.StreamSupportingHttpInvokerServiceExporter;
 
-
 /**
  * Provider of remote service onto openBIS.
  * 
@@ -47,9 +46,9 @@ public class ServiceProvider
         return ((HttpInvokerServiceExporter) APPLICATION_CONTEXT.getBean("data-store-server"));
     }
 
-    public static StreamSupportingHttpInvokerServiceExporter getDssServiceRpcNameServer()
+    public static HttpInvokerServiceExporter getRpcNameServiceExporter()
     {
-        return ((StreamSupportingHttpInvokerServiceExporter) APPLICATION_CONTEXT
+        return ((HttpInvokerServiceExporter) APPLICATION_CONTEXT
                 .getBean("data-store-rpc-name-server"));
     }
 
diff --git a/datastore_server/source/java/dssApplicationContext.xml b/datastore_server/source/java/dssApplicationContext.xml
index 09e22a58476..98701462dd0 100644
--- a/datastore_server/source/java/dssApplicationContext.xml
+++ b/datastore_server/source/java/dssApplicationContext.xml
@@ -67,12 +67,11 @@
         <property name="serviceInterface" value="ch.systemsx.cisd.openbis.dss.api.v1.shared.IDssServiceRpcGeneric" />
     </bean>
     
+    <bean id="rpc-name-server" class="ch.systemsx.cisd.common.api.server.RpcServiceNameServer" />
+    
     <bean id="data-store-rpc-name-server"
-        class="com.marathon.util.spring.StreamSupportingHttpInvokerServiceExporter">
-        <property name="service">
-            <bean class="ch.systemsx.cisd.common.api.server.RpcServiceNameServer">
-            </bean>
-        </property>
+        class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
+        <property name="service" ref="rpc-name-server" />
         <property name="serviceInterface" value="ch.systemsx.cisd.common.api.IRpcServiceNameServer" />
     </bean>
     
diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/api/v1/client/impl/OpenBisServiceFactoryTest.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/api/v1/client/impl/OpenBisServiceFactoryTest.java
deleted file mode 100644
index 387e68e968e..00000000000
--- a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/api/v1/client/impl/OpenBisServiceFactoryTest.java
+++ /dev/null
@@ -1,104 +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.dss.api.v1.client.impl;
-
-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.dss.api.v1.client.impl.OpenBisServiceFactory.ILimsServiceStubFactory;
-import ch.systemsx.cisd.openbis.generic.shared.IETLLIMSService;
-
-/**
- * @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