From b727a045bf8e2774347eef1e889adc249d3a7d3b Mon Sep 17 00:00:00 2001
From: cramakri <cramakri>
Date: Mon, 10 May 2010 12:16:39 +0000
Subject: [PATCH] LMS-1527 Moved RpcServiceNameServer to common.

SVN: 15867
---
 .../systemsx/cisd/common/api/IRpcService.java |  45 ++++++
 .../common/api/IRpcServiceNameServer.java     |  29 ++++
 .../common/api/RpcServiceInterfaceDTO.java    |  94 ++++++++++++
 .../api/RpcServiceInterfaceVersionDTO.java    | 138 ++++++++++++++++++
 .../api/server/RpcServiceNameServer.java      |  71 +++++++++
 5 files changed, 377 insertions(+)
 create mode 100644 common/source/java/ch/systemsx/cisd/common/api/IRpcService.java
 create mode 100644 common/source/java/ch/systemsx/cisd/common/api/IRpcServiceNameServer.java
 create mode 100644 common/source/java/ch/systemsx/cisd/common/api/RpcServiceInterfaceDTO.java
 create mode 100644 common/source/java/ch/systemsx/cisd/common/api/RpcServiceInterfaceVersionDTO.java
 create mode 100644 common/source/java/ch/systemsx/cisd/common/api/server/RpcServiceNameServer.java

diff --git a/common/source/java/ch/systemsx/cisd/common/api/IRpcService.java b/common/source/java/ch/systemsx/cisd/common/api/IRpcService.java
new file mode 100644
index 00000000000..4b834358c39
--- /dev/null
+++ b/common/source/java/ch/systemsx/cisd/common/api/IRpcService.java
@@ -0,0 +1,45 @@
+/*
+ * 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.common.api;
+
+/**
+ * The most generic interface for RPC invocations into openBIS.
+ * <p>
+ * This interface defines a minimal interface presented by RPC services. It lets clients determine
+ * which version of the interface the server supports. To do anything interesting, clients need to
+ * get a reference to a specific interface using the {@link IRpcServiceNameServer}.
+ * 
+ * @author Chandrasekhar Ramakrishnan
+ */
+public interface IRpcService
+{
+    //
+    // Protocol versioning
+    //
+    /**
+     * Returns the major version of the server side interface. Different major versions are
+     * incompatible with one another.
+     */
+    public int getMajorVersion();
+
+    /**
+     * Returns the minor version of this server side interface. Different minor versions, within the
+     * same major version, are compatible with one another.
+     */
+    public int getMinorVersion();
+
+}
diff --git a/common/source/java/ch/systemsx/cisd/common/api/IRpcServiceNameServer.java b/common/source/java/ch/systemsx/cisd/common/api/IRpcServiceNameServer.java
new file mode 100644
index 00000000000..37dbe28ea0e
--- /dev/null
+++ b/common/source/java/ch/systemsx/cisd/common/api/IRpcServiceNameServer.java
@@ -0,0 +1,29 @@
+/*
+ * 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.common.api;
+
+import java.util.List;
+
+/**
+ * An Interface for finding out about the IRpcService interfaces supported by a server.
+ * 
+ * @author Chandrasekhar Ramakrishnan
+ */
+public interface IRpcServiceNameServer extends IRpcService
+{
+    List<RpcServiceInterfaceDTO> getSupportedInterfaces();
+}
diff --git a/common/source/java/ch/systemsx/cisd/common/api/RpcServiceInterfaceDTO.java b/common/source/java/ch/systemsx/cisd/common/api/RpcServiceInterfaceDTO.java
new file mode 100644
index 00000000000..0d7d39ba23d
--- /dev/null
+++ b/common/source/java/ch/systemsx/cisd/common/api/RpcServiceInterfaceDTO.java
@@ -0,0 +1,94 @@
+/*
+ * 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.common.api;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+
+/**
+ * Describes an RPC interface supported by the server.
+ * 
+ * @author Chandrasekhar Ramakrishnan
+ */
+public class RpcServiceInterfaceDTO implements Serializable
+{
+    private static final long serialVersionUID = 1L;
+
+    private String interfaceName;
+
+    private final ArrayList<RpcServiceInterfaceVersionDTO> versions =
+            new ArrayList<RpcServiceInterfaceVersionDTO>();
+
+    public RpcServiceInterfaceDTO()
+    {
+        interfaceName = "Unknown";
+    }
+
+    /**
+     * The name of this interface used for identification.
+     */
+    public String getInterfaceName()
+    {
+        return interfaceName;
+    }
+
+    public void setInterfaceName(String interfaceName)
+    {
+        this.interfaceName = interfaceName;
+    }
+
+    public ArrayList<RpcServiceInterfaceVersionDTO> getVersions()
+    {
+        return versions;
+    }
+
+    public void addVersion(RpcServiceInterfaceVersionDTO ifaceVersion)
+    {
+        versions.add(ifaceVersion);
+    }
+
+    @Override
+    public boolean equals(Object obj)
+    {
+        if (false == obj instanceof RpcServiceInterfaceDTO)
+            return false;
+
+        RpcServiceInterfaceDTO other = (RpcServiceInterfaceDTO) obj;
+        return getInterfaceName().equals(other.getInterfaceName())
+                && getVersions().equals(other.getVersions());
+    }
+
+    @Override
+    public int hashCode()
+    {
+        int hash = getInterfaceName().hashCode();
+        hash = hash * 31 + getVersions().hashCode();
+        return hash;
+    }
+
+    @Override
+    public String toString()
+    {
+        StringBuilder sb = new StringBuilder();
+        sb.append("RpcServiceInterfaceDTO[");
+        sb.append(getInterfaceName());
+        sb.append(" ");
+        sb.append(getVersions().toString());
+        sb.append("]");
+        return sb.toString();
+    }
+}
diff --git a/common/source/java/ch/systemsx/cisd/common/api/RpcServiceInterfaceVersionDTO.java b/common/source/java/ch/systemsx/cisd/common/api/RpcServiceInterfaceVersionDTO.java
new file mode 100644
index 00000000000..3139d622062
--- /dev/null
+++ b/common/source/java/ch/systemsx/cisd/common/api/RpcServiceInterfaceVersionDTO.java
@@ -0,0 +1,138 @@
+/*
+ * 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.common.api;
+
+import java.io.Serializable;
+
+/**
+ * Describes an RPC interface supported by the server.
+ * 
+ * @author Chandrasekhar Ramakrishnan
+ */
+public class RpcServiceInterfaceVersionDTO implements Serializable
+{
+    private static final long serialVersionUID = 1L;
+
+    private String interfaceName;
+
+    private String interfaceUrlSuffix;
+
+    private int interfaceMajorVersion;
+
+    private int interfaceMinorVersion;
+
+    public RpcServiceInterfaceVersionDTO()
+    {
+        interfaceName = "Unknown";
+        interfaceUrlSuffix = "unknown";
+        interfaceMajorVersion = 0;
+        interfaceMinorVersion = 0;
+    }
+
+    /**
+     * The name of this interface used for identification.
+     */
+    public String getInterfaceName()
+    {
+        return interfaceName;
+    }
+
+    public void setInterfaceName(String interfaceName)
+    {
+        this.interfaceName = interfaceName;
+    }
+
+    /**
+     * The suffix added to the server's URL to produce the URL for this interface. Used by a service
+     * factory to create a proxy to the service.
+     */
+    public String getInterfaceUrlSuffix()
+    {
+        return interfaceUrlSuffix;
+    }
+
+    public void setInterfaceUrlSuffix(String interfaceUrlSuffix)
+    {
+        this.interfaceUrlSuffix = interfaceUrlSuffix;
+    }
+
+    /**
+     * The major version of the interface. E.g., an interface with version 2.11 has major version 2.
+     */
+    public int getInterfaceMajorVersion()
+    {
+        return interfaceMajorVersion;
+    }
+
+    public void setInterfaceMajorVersion(int interfaceMajorVersion)
+    {
+        this.interfaceMajorVersion = interfaceMajorVersion;
+    }
+
+    /**
+     * The major version of the interface. E.g., an interface with version 2.11 has minor version
+     * 11.
+     */
+    public int getInterfaceMinorVersion()
+    {
+        return interfaceMinorVersion;
+    }
+
+    public void setInterfaceMinorVersion(int interfaceMinorVersion)
+    {
+        this.interfaceMinorVersion = interfaceMinorVersion;
+    }
+
+    @Override
+    public boolean equals(Object obj)
+    {
+        if (false == obj instanceof RpcServiceInterfaceVersionDTO)
+            return false;
+
+        RpcServiceInterfaceVersionDTO other = (RpcServiceInterfaceVersionDTO) obj;
+        return getInterfaceName().equals(other.getInterfaceName())
+                && getInterfaceUrlSuffix().equals(other.getInterfaceUrlSuffix())
+                && getInterfaceMajorVersion() == other.getInterfaceMajorVersion()
+                && getInterfaceMinorVersion() == other.getInterfaceMinorVersion();
+    }
+
+    @Override
+    public int hashCode()
+    {
+        int hash = getInterfaceName().hashCode();
+        hash = hash * 31 + getInterfaceUrlSuffix().hashCode();
+        hash = hash * 31 + getInterfaceMajorVersion();
+        hash = hash * 31 + getInterfaceMinorVersion();
+        return hash;
+    }
+
+    @Override
+    public String toString()
+    {
+        StringBuilder sb = new StringBuilder();
+        sb.append("RpcServiceInterfaceVersionDTO[");
+        sb.append(getInterfaceName());
+        sb.append(",");
+        sb.append(getInterfaceUrlSuffix());
+        sb.append(",v.");
+        sb.append(getInterfaceMajorVersion());
+        sb.append(".");
+        sb.append(getInterfaceMinorVersion());
+        sb.append("]");
+        return sb.toString();
+    }
+}
diff --git a/common/source/java/ch/systemsx/cisd/common/api/server/RpcServiceNameServer.java b/common/source/java/ch/systemsx/cisd/common/api/server/RpcServiceNameServer.java
new file mode 100644
index 00000000000..be424f4bd7a
--- /dev/null
+++ b/common/source/java/ch/systemsx/cisd/common/api/server/RpcServiceNameServer.java
@@ -0,0 +1,71 @@
+/*
+ * 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.common.api.server;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import ch.systemsx.cisd.common.api.IRpcServiceNameServer;
+import ch.systemsx.cisd.common.api.RpcServiceInterfaceDTO;
+import ch.systemsx.cisd.common.logging.LogCategory;
+import ch.systemsx.cisd.common.logging.LogFactory;
+
+/**
+ * Implementation of the {@link IRpcServiceNameServer} interface which registry for accessing the
+ * RPC services supported by a server.
+ * 
+ * @author Chandrasekhar Ramakrishnan
+ */
+public class RpcServiceNameServer implements IRpcServiceNameServer
+{
+    private final ArrayList<RpcServiceInterfaceDTO> supportedInterfaces;
+
+    static private final Logger operationLog =
+            LogFactory.getLogger(LogCategory.OPERATION, RpcServiceNameServer.class);
+
+    RpcServiceNameServer()
+    {
+        supportedInterfaces = new ArrayList<RpcServiceInterfaceDTO>();
+    }
+
+    public List<RpcServiceInterfaceDTO> getSupportedInterfaces()
+    {
+        return supportedInterfaces;
+    }
+
+    public int getMajorVersion()
+    {
+        return 1;
+    }
+
+    public int getMinorVersion()
+    {
+        return 0;
+    }
+
+    /**
+     * Add an interface to the registry.
+     */
+    public void addSupportedInterface(RpcServiceInterfaceDTO iface)
+    {
+        supportedInterfaces.add(iface);
+        operationLog.info("Registered RPC Interface " + iface.toString());
+    }
+
+}
-- 
GitLab