From aa610f202ffdc6fe7e465d0c3882a0669fd34553 Mon Sep 17 00:00:00 2001
From: Arthur Coolen <coolen@astron.nl>
Date: Tue, 30 May 2006 12:51:30 +0000
Subject: [PATCH] BugID:604 Added RMI interface (java part)

---
 .../java/cep/jparmfacade/jParmFacade.java     |  15 +--
 .../cep/jparmfacade/jParmFacadeAdapter.java   |  90 +++++++++++++
 .../cep/jparmfacade/jParmFacadeInterface.java |  54 ++++++++
 .../cep/jparmfacade/jParmFacadeServer.java    | 121 ++++++++++++++++++
 4 files changed, 271 insertions(+), 9 deletions(-)
 create mode 100644 JAVA/CEP/jParmFacade/src/nl/astron/lofar/java/cep/jparmfacade/jParmFacadeAdapter.java
 create mode 100644 JAVA/CEP/jParmFacade/src/nl/astron/lofar/java/cep/jparmfacade/jParmFacadeInterface.java
 create mode 100644 JAVA/CEP/jParmFacade/src/nl/astron/lofar/java/cep/jparmfacade/jParmFacadeServer.java

diff --git a/JAVA/CEP/jParmFacade/src/nl/astron/lofar/java/cep/jparmfacade/jParmFacade.java b/JAVA/CEP/jParmFacade/src/nl/astron/lofar/java/cep/jparmfacade/jParmFacade.java
index 38f64ff2ce6..10042b433c7 100644
--- a/JAVA/CEP/jParmFacade/src/nl/astron/lofar/java/cep/jparmfacade/jParmFacade.java
+++ b/JAVA/CEP/jParmFacade/src/nl/astron/lofar/java/cep/jparmfacade/jParmFacade.java
@@ -15,19 +15,16 @@ import java.util.Vector;
  * @author coolen
  */
 public class jParmFacade {
+    private String itsParmFacadeDB="";
     
     /** Creates a new instance of jParmFacade */
-    public jParmFacade(String tableName) {
-        try {
-            initParmFacade(tableName);
-        } catch (Exception ex) {
-            System.out.println("Error during init: ");
-            ex.printStackTrace();
-        }
+    public jParmFacade() {
+        itsParmFacadeDB="";
+    }
+    public void setParmFacadeDB(String tableName) {
+        itsParmFacadeDB=tableName;
     }
     
-    // Make a connection to the given ParmTable.
-    private native void initParmFacade (String tableName) throws Exception;
     
     // Get the domain range (as startx,endx,starty,endy) of the given
     // parameters in the table.
diff --git a/JAVA/CEP/jParmFacade/src/nl/astron/lofar/java/cep/jparmfacade/jParmFacadeAdapter.java b/JAVA/CEP/jParmFacade/src/nl/astron/lofar/java/cep/jparmfacade/jParmFacadeAdapter.java
new file mode 100644
index 00000000000..563c9dfea9c
--- /dev/null
+++ b/JAVA/CEP/jParmFacade/src/nl/astron/lofar/java/cep/jparmfacade/jParmFacadeAdapter.java
@@ -0,0 +1,90 @@
+//#  jParmFacadeAdapter.java: The RMI adapter of the OTDB database.
+//#
+//#  Copyright (C) 2002-2007
+//#  ASTRON (Netherlands Foundation for Research in Astronomy)
+//#  P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, seg@astron.nl
+//#
+//#  This program is free software; you can redistribute it and/or modify
+//#  it under the terms of the GNU General Public License as published by
+//#  the Free Software Foundation; either version 2 of the License, or
+//#  (at your option) any later version.
+//#
+//#  This program is distributed in the hope that it will be useful,
+//#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//#  GNU General Public License for more details.
+//#
+//#  You should have received a copy of the GNU General Public License
+//#  along with this program; if not, write to the Free Software
+//#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+//#
+  
+package nl.astron.lofar.java.cep.jparmfacade;
+
+import java.util.Vector;
+import java.rmi.server.UnicastRemoteObject;
+import java.rmi.RemoteException;
+import java.util.HashMap;
+
+public class jParmFacadeAdapter extends UnicastRemoteObject implements jParmFacadeInterface
+{
+   // Constructor
+   public jParmFacadeAdapter (jParmFacade adaptee) throws RemoteException
+     {
+	this.adaptee = adaptee;
+     }
+   
+    public void setParmFacadeDB(String tableName) throws RemoteException {
+        adaptee.setParmFacadeDB(tableName);
+    }
+    
+// Get the domain range (as startx,endx,starty,endy) of the given
+    // parameters in the table.
+    // This is the minimum start value and maximum end value for all parameters.
+    // An empty name pattern is the same as * (all parm names).
+    public Vector<Double> getRange(String parmNamePattern) throws RemoteException {
+        Vector<Double> aV=null;
+        try {
+            aV=adaptee.getRange(parmNamePattern);
+        } catch (Exception ex) {
+            RemoteException anEx=new RemoteException("JNI getRange error");
+            anEx.initCause(ex);
+            throw anEx;            
+        }
+        return aV;
+    }
+    
+    // Get parameter names in the table matching the pattern.
+    // An empty name pattern is the same as * (all parm names).
+    public Vector<String> getNames(String parmNamePattern) throws RemoteException {
+        Vector<String> aV=null;
+        try {        
+            aV= adaptee.getNames(parmNamePattern);
+        } catch (Exception ex) {
+            RemoteException anEx=new RemoteException("JNI getNames error");
+            anEx.initCause(ex);
+            throw anEx;            
+        }
+        return aV;            
+    }
+    
+
+    // Get the parameter values for the given parameters and domain.
+    // The domain is given by the start and end values, while the grid is
+    // given by nx and ny.
+    public HashMap<String,Vector<Double>> getValues(String parmNamePattern,
+            double startx, double endx, int nx,
+            double starty, double endy, int ny) throws RemoteException {
+        HashMap<String,Vector<Double>> aM=null;
+        try {            
+            aM=adaptee.getValues(parmNamePattern,startx,endx,nx,starty,endy,ny);
+        } catch (Exception ex) {
+            RemoteException anEx=new RemoteException("JNI getValues error");
+            anEx.initCause(ex);
+            throw anEx;            
+        }
+        return aM;            
+    }
+
+   protected jParmFacade adaptee;   
+}
diff --git a/JAVA/CEP/jParmFacade/src/nl/astron/lofar/java/cep/jparmfacade/jParmFacadeInterface.java b/JAVA/CEP/jParmFacade/src/nl/astron/lofar/java/cep/jparmfacade/jParmFacadeInterface.java
new file mode 100644
index 00000000000..5ba7948c306
--- /dev/null
+++ b/JAVA/CEP/jParmFacade/src/nl/astron/lofar/java/cep/jparmfacade/jParmFacadeInterface.java
@@ -0,0 +1,54 @@
+//#  jParmFacadeInterface.java: The RMI interface to the Parameter database.
+//#
+//#  Copyright (C) 2002-2007
+//#  ASTRON (Netherlands Foundation for Research in Astronomy)
+//#  P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, seg@astron.nl
+//#
+//#  This program is free software; you can redistribute it and/or modify
+//#  it under the terms of the GNU General Public License as published by
+//#  the Free Software Foundation; either version 2 of the License, or
+//#  (at your option) any later version.
+//#
+//#  This program is distributed in the hope that it will be useful,
+//#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//#  GNU General Public License for more details.
+//#
+//#  You should have received a copy of the GNU General Public License
+//#  along with this program; if not, write to the Free Software
+//#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+//#
+  
+package nl.astron.lofar.java.cep.jparmfacade;
+
+
+import java.rmi.Remote;
+import java.rmi.RemoteException;
+import java.util.HashMap;
+import java.util.Vector;
+
+public interface jParmFacadeInterface extends Remote 
+{
+    // Constants
+    public static final String SERVICENAME = "jParmFacade";
+    
+    public void setParmFacadeDB(String tableName) throws RemoteException;
+
+    // Get the domain range (as startx,endx,starty,endy) of the given
+    // parameters in the table.
+    // This is the minimum start value and maximum end value for all parameters.
+    // An empty name pattern is the same as * (all parm names).
+    public Vector<Double> getRange(String parmNamePattern) throws RemoteException;
+    
+    // Get parameter names in the table matching the pattern.
+    // An empty name pattern is the same as * (all parm names).
+    public Vector<String> getNames(String parmNamePattern) throws RemoteException;
+    
+
+    // Get the parameter values for the given parameters and domain.
+    // The domain is given by the start and end values, while the grid is
+    // given by nx and ny.
+    public HashMap<String,Vector<Double>> getValues(String parmNamePattern,
+            double startx, double endx, int nx,
+            double starty, double endy, int ny) throws RemoteException;
+}
diff --git a/JAVA/CEP/jParmFacade/src/nl/astron/lofar/java/cep/jparmfacade/jParmFacadeServer.java b/JAVA/CEP/jParmFacade/src/nl/astron/lofar/java/cep/jparmfacade/jParmFacadeServer.java
new file mode 100644
index 00000000000..83289e75105
--- /dev/null
+++ b/JAVA/CEP/jParmFacade/src/nl/astron/lofar/java/cep/jparmfacade/jParmFacadeServer.java
@@ -0,0 +1,121 @@
+//#  jParmFacadeServer.java: The RMI server of the OTDB database.
+//#
+//#  Copyright (C) 2002-2007
+//#  ASTRON (Netherlands Foundation for Research in Astronomy)
+//#  P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, seg@astron.nl
+//#
+//#  This program is free software; you can redistribute it and/or modify
+//#  it under the terms of the GNU General Public License as published by
+//#  the Free Software Foundation; either version 2 of the License, or
+//#  (at your option) any later version.
+//#
+//#  This program is distributed in the hope that it will be useful,
+//#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//#  GNU General Public License for more details.
+//#
+//#  You should have received a copy of the GNU General Public License
+//#  along with this program; if not, write to the Free Software
+//#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+//#
+
+package nl.astron.lofar.java.cep.jparmfacade;
+import java.rmi.registry.*;
+import java.rmi.server.RMISocketFactory;
+import nl.astron.lofar.sas.otb.jotdb2.jInitCPPLogger;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
+
+
+
+public class jParmFacadeServer {
+    private static jParmFacade jParmFacadeAdaptee;
+    private static jParmFacadeAdapter jParmFacadeAdapter;
+    
+    static
+    {
+        System.loadLibrary("jparmfacade");
+    }
+    
+    static Logger logger = Logger.getLogger(jParmFacadeServer.class);
+    
+    public static void main(String[] args)  {
+        try {
+            String logConfig = "jParmFacade.log_prop";
+            
+            PropertyConfigurator.configure(logConfig);
+            jInitCPPLogger aCPPLogger=new jInitCPPLogger(logConfig);
+            logger.info("jOTDBServer started. LogPropFile: "+ logConfig);
+            
+//	     if (System.getSecurityManager () == null)
+//	       {
+//		  System.out.println ("No security mananger is running, will start one now...");
+//		  System.setSecurityManager (new RMISecurityManager ());
+//	       }
+            
+            if (args.length < 4) {
+                System.out.println("Usage: java -jar jParmFacadeServer.jar <hostname> <rmiportnumber-OPTIONAL> <rmi objects portnumber for firewall/tunneling purposes-OPTIONAL>");
+                System.exit(0);
+            }
+            
+            
+            String aHostName = new String(args[0]);
+            
+            logger.info("Running on: "+aHostName);
+            System.setProperty("java.rmi.server.hostname", aHostName);
+            
+            
+            Registry localRegistry = null;
+            int objectPort = 0;
+            
+            if (args.length == 1){
+                logger.info("jParmFacadeServer creating a local RMI registry on port "+Registry.REGISTRY_PORT+" ...");
+                localRegistry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
+            }else if (args.length > 1) {
+                Integer rmiPort = new Integer(args[4]);
+                logger.info("jParmFacadeServer creating a local RMI registry on port "+rmiPort+" ...");
+                localRegistry = LocateRegistry.createRegistry(rmiPort.intValue());
+                if (args.length ==3){
+                    Integer rmiObjectsPort = new Integer(args[5]);
+                    logger.info("jParmFacadeServer setting up RMI server objects on port "+rmiObjectsPort+" ...");
+                    RMISocketFactory socketFactory = RMISocketFactory.getDefaultSocketFactory();
+                    socketFactory.createServerSocket(rmiObjectsPort);
+                    objectPort = rmiObjectsPort;
+                }
+            }
+            
+            logger.info("jParmFacadeServer creating local object and remote adapter...");
+            
+            // Export jParmFacade
+            jParmFacadeAdaptee = new jParmFacade();
+            jParmFacadeAdapter = new jParmFacadeAdapter(jParmFacadeAdaptee);
+            //A custom port was specified, export the object using the port specified
+            if(objectPort!=0){
+                jParmFacadeAdapter.unexportObject(jParmFacadeAdapter,true);
+                jParmFacadeAdapter.exportObject(jParmFacadeAdapter,objectPort);
+            }
+            logger.info("jParmFacadeServer publishing service " + jParmFacadeInterface.SERVICENAME + " in local registry...");
+            
+            localRegistry.rebind(jParmFacadeInterface.SERVICENAME, jParmFacadeAdapter);
+            
+            logger.info("Published jParmFacadeInterface as service " + jParmFacadeInterface.SERVICENAME + ". Ready...");
+            
+            String statusmessage = "jParmFacadeserver is ready for incoming calls";
+            if (args.length > 1) {
+                Integer rmiPort = new Integer(args[1]);
+                statusmessage += " on rmi registry port "+rmiPort;
+                if (args.length ==3){
+                    Integer rmiObjectsPort = new Integer(args[2]);
+                    statusmessage += " and rmi server object port "+rmiObjectsPort +". Please tunnel/forward both ports for your client to work";
+                }
+            }
+            statusmessage+="...";
+            
+            logger.info(statusmessage);
+        }
+        
+        catch (Exception e) {
+            logger.fatal("jParmFacadeServer failed: " + e);
+        }
+    }
+}
-- 
GitLab