Refactored EvAServer + RMIServer(EvA) to use a logger. Syntax clean up and variable naming according to Java conventions.

This commit is contained in:
Fabian Becker 2012-04-20 10:28:38 +00:00
parent df39ab0152
commit e8b560a478
3 changed files with 79 additions and 107 deletions

View File

@ -10,6 +10,8 @@ import eva2.EvAInfo;
import java.io.*; import java.io.*;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
/** /**
* *
@ -24,16 +26,15 @@ public class EvAServer {
public static String m_UserName; public static String m_UserName;
public static int m_NumberOfVM = 0; public static int m_NumberOfVM = 0;
private RMIServerEvA m_RMIServer; private RMIServerEvA m_RMIServer;
private static final Logger logger = Logger.getLogger(EvAInfo.defaultLogger);
/** /**
* Constructor of EvAServer. Calls RMIConnection(). * Constructor of EvAServer. Calls RMIConnection().
*/ */
public EvAServer(boolean insideClient, boolean Restart) { public EvAServer(boolean insideClient, boolean Restart) {
System.out.println("Number of CPUs :" + Runtime.getRuntime().availableProcessors()); logger.log(Level.INFO, "Number of CPUs :" + Runtime.getRuntime().availableProcessors());
System.out.println("*******************************************************************************"); logger.log(Level.INFO, "This is EvA Server Version: " + EvAInfo.getVersion());
System.out.println("This is EvA Server Version: " + EvAInfo.getVersion()); logger.log(Level.INFO, "Java Version: " + System.getProperty("java.version"));
//System.out.println ("Java Version: " + System.getProperty("java.version") );
System.out.println("*******************************************************************************");
m_UserName = System.getProperty("user.name"); m_UserName = System.getProperty("user.name");
// RMIConnection(); // RMIConnection();
// m_ComAdapter = new EvAComAdapter(); // m_ComAdapter = new EvAComAdapter();

View File

@ -13,6 +13,7 @@ package eva2.server;
*==========================================================================*/ *==========================================================================*/
import eva2.tools.jproxy.RMIProxyLocal; import eva2.tools.jproxy.RMIProxyLocal;
import eva2.tools.jproxy.RMIServer; import eva2.tools.jproxy.RMIServer;
import java.util.logging.Level;
/** /**
* *
@ -20,22 +21,22 @@ import eva2.tools.jproxy.RMIServer;
public class RMIServerEvA extends RMIServer { public class RMIServerEvA extends RMIServer {
public static RMIServerEvA getInstance() { public static RMIServerEvA getInstance() {
if (m_instance==null) { if (instance==null) {
m_instance = new RMIServerEvA(); instance = new RMIServerEvA();
} }
return (RMIServerEvA)m_instance; return (RMIServerEvA)instance;
} }
protected void createMainRemoteObject(String mainAdapterName) { protected void createMainRemoteObject(String mainAdapterName) {
try { try {
m_MainRemoteObject = new EvAMainAdapterImpl(); mainRemoteObject = new EvAMainAdapterImpl();
m_MainRemoteObject = mainRemoteObject =
(EvAMainAdapter) RMIProxyLocal.newInstance( (EvAMainAdapter) RMIProxyLocal.newInstance(
m_MainRemoteObject, mainRemoteObject,
mainAdapterName + "_" + m_NumberOfVM); mainAdapterName + "_" + numberOfVM);
m_MainRemoteObject.setRemoteThis(m_MainRemoteObject); mainRemoteObject.setRemoteThis(mainRemoteObject);
} catch (Exception e) { } catch (Exception ex) {
e.printStackTrace(); logger.log(Level.WARNING, "Could not create main remote object!", ex);
} }
} }
} }

View File

@ -9,60 +9,61 @@ package eva2.tools.jproxy;
* $Author: ulmerh $ * $Author: ulmerh $
*/ */
import eva2.EvAInfo;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.rmi.Naming; import java.rmi.Naming;
import java.rmi.RemoteException; import java.rmi.RemoteException;
import java.rmi.registry.Registry; import java.rmi.registry.Registry;
import java.util.logging.Level;
import java.util.logging.Logger;
/** /**
* *
*/ */
public class RMIServer { public class RMIServer {
/* Version string of the server application. */ /* Version string of the server application. */
public static boolean TRACE = false; protected static RMIServer instance;
// public static Registry m_Registry;
protected static RMIServer m_instance;
/* Name of host on which the server is running. */ /* Name of host on which the server is running. */
private String m_MyHostName = "undefined"; private String myHostName = "undefined";
/* IP of host on which the server is running. */ /* IP of host on which the server is running. */
private String m_MyHostIP = "undefined"; private String myHostIP = "undefined";
/* MainAdapterImp object. This is need for the first /* MainAdapterImp object. This is need for the first
connection between the server and the client program. */ connection between the server and the client program. */
public MainAdapter m_MainRemoteObject; protected MainAdapter mainRemoteObject;
/* String describing the properties of the enviroment. */ /* String describing the properties of the enviroment. */
// private ComAdapter m_ComAdapter; // private ComAdapter m_ComAdapter;
public static String m_UserName; protected static String userName;
public static int m_NumberOfVM = 0; protected static int numberOfVM = 0;
Registry m_Registry = null; private Registry myRegistry = null;
protected static final Logger logger = Logger.getLogger(EvAInfo.defaultLogger);
/** /**
* *
*/ */
public static RMIServer getInstance() { public static RMIServer getInstance() {
if (m_instance == null) { if (instance == null) {
m_instance = new RMIServer(); instance = new RMIServer();
} }
return m_instance; return instance;
} }
/** /**
* Constructor of EvAServer. * Constructor of EvAServer.
* Calls RMIConnection(). * Calls RMIConnection().
*/ */
protected RMIServer() { protected RMIServer() {
m_UserName = System.getProperty("user.name"); userName = System.getProperty("user.name");
//System.out.println(EVAHELP.getSystemPropertyString());
initConnection(); initConnection();
// m_ComAdapter = ComAdapter.getInstance();
} }
/** /**
* Main method of this class. * Main method of this class.
* Is the starting point of the server application. * Is the starting point of the server application.
*/ */
static public void main(String[] args) { public static void main(String[] args) {
System.out.println("Start RMIServer !"); logger.log(Level.INFO, "Start RMIServer !");
RMIServer Application = RMIServer.getInstance(); RMIServer application = RMIServer.getInstance();
} }
@ -72,52 +73,40 @@ public class RMIServer {
* @param * @param
*/ */
private void initConnection() { private void initConnection() {
String MainAdapterName = m_UserName + MainAdapterImpl.MAIN_ADAPTER_NAME; String mainAdapterName = userName + MainAdapterImpl.MAIN_ADAPTER_NAME;
System.setProperty("java.security.policy", "server.policy"); System.setProperty("java.security.policy", "server.policy");
launchRMIRegistry(); launchRMIRegistry();
try { try {
m_MyHostIP = InetAddress.getLocalHost().getHostAddress(); myHostIP = InetAddress.getLocalHost().getHostAddress();
m_MyHostName = InetAddress.getLocalHost().getHostName(); myHostName = InetAddress.getLocalHost().getHostName();
} catch (Exception e) { } catch (Exception e) {
System.out.println("ERROR getting HostName (RMIServer.initConnection)" + e.getMessage()); logger.log(Level.SEVERE, "Error getting HostName " + e.getMessage(), e);
} }
System.out.println("Start of EvA RMI-Server on host " + m_MyHostName + " = " + m_MyHostIP); logger.log(Level.INFO, "Start of EvA RMI-Server on host " + myHostName + " = " + myHostIP);
// Object test = null;
try {
try {
String[] list =
Naming.list("rmi://localhost:" + MainAdapterImpl.PORT);
// System.out.println("-->list");
// for (int i = 0; i < list.length; i++)
// System.out.println("-->RMIName" + list[i]);
m_NumberOfVM = getNumberOfVM(list);
} catch (RemoteException e) {
System.err.println("no RMI registry available yet...");
if (TRACE)
System.out.println(
"RemoteException OK IAM the first server for this rmiregistry: "
+ e.getMessage());
}
} catch (MalformedURLException ex) {
System.out.println(
"MalformedURLException: Error while looking up "
+ ex.getMessage());
}
createMainRemoteObject(MainAdapterName);
System.out.println("End of RMI-Server Initialisation"); try {
System.out.println(" --> OK on Host: " + m_MyHostName + " = " + m_MyHostIP + ", adapter name is " + MainAdapterName); String[] list = Naming.list("rmi://localhost:" + MainAdapterImpl.PORT);
System.out.println("Waiting for a client .............."); numberOfVM = getNumberOfVM(list);
} catch (RemoteException e) {
logger.log(Level.WARNING, "No RMI registry available yet");
} catch (MalformedURLException ex) {
logger.log(Level.SEVERE, "MalformedURLException: Error while looking up " + ex.getMessage(), ex);
}
createMainRemoteObject(mainAdapterName);
logger.log(Level.INFO, "End of RMI-Server Initialisation");
logger.log(Level.INFO, "Host: " + myHostName + " = " + myHostIP + ", adapter name is " + mainAdapterName);
logger.log(Level.INFO, "Waiting for a client.");
} }
protected void createMainRemoteObject(String mainAdapterName) { protected void createMainRemoteObject(String mainAdapterName) {
try { try {
m_MainRemoteObject = new MainAdapterImpl(); mainRemoteObject = new MainAdapterImpl();
m_MainRemoteObject = mainRemoteObject =
(MainAdapter) RMIProxyLocal.newInstance(m_MainRemoteObject,mainAdapterName + "_" + m_NumberOfVM); (MainAdapter) RMIProxyLocal.newInstance(mainRemoteObject,mainAdapterName + "_" + numberOfVM);
m_MainRemoteObject.setRemoteThis(m_MainRemoteObject); mainRemoteObject.setRemoteThis(mainRemoteObject);
} catch (Exception e) { } catch (Exception ex) {
e.printStackTrace(); logger.log(Level.WARNING, "Could not create main remote object!", ex);
} }
} }
@ -125,46 +114,29 @@ public class RMIServer {
* *
*/ */
public MainAdapter getMainRemoteObject() { public MainAdapter getMainRemoteObject() {
return m_MainRemoteObject; return mainRemoteObject;
} }
/** /**
* Install RMIregistry on default port !! * Install RMIregistry on default port !!
*/ */
private void launchRMIRegistry() { private void launchRMIRegistry() {
if (TRACE)
System.out.println(
"LaunchRMIRegistry on Server on PORT " + MainAdapterImpl.PORT);
try { try {
m_Registry = myRegistry = java.rmi.registry.LocateRegistry.createRegistry(MainAdapterImpl.PORT);
java.rmi.registry.LocateRegistry.createRegistry( } catch (Throwable e) {
MainAdapterImpl.PORT); myRegistry = null;
} catch (Throwable e) { }
if (TRACE) if (myRegistry == null) {
System.out.println("Registry not created !!" + e.getMessage()); logger.log(Level.INFO, "Try to get registry with getRegistry on port " + MainAdapterImpl.PORT);
m_Registry = null; try {
} myRegistry = java.rmi.registry.LocateRegistry.getRegistry(MainAdapterImpl.PORT);
if (m_Registry == null) { } catch (RemoteException e) {
System.out.println( myRegistry = null;
"Try to get registry with getRegistry on PORT " }
+ MainAdapterImpl.PORT); }
try { if (myRegistry == null) {
m_Registry = logger.log(Level.WARNING, "Got no RMIREGISTRY");
java.rmi.registry.LocateRegistry.getRegistry( }
MainAdapterImpl.PORT);
if (TRACE)
System.out.println(
"m_Registry.REGISTRY_PORT=" + m_Registry.REGISTRY_PORT);
} catch (Throwable e) {
if (TRACE)
System.out.println(
"registry notcreated !!" + e.getMessage());
m_Registry = null;
}
}
if (m_Registry == null) {
System.err.println("--> got no RMIREGISTRY");
} else if (TRACE) System.out.println("--> got RMIREGISTRY");
} }
/** /**
@ -176,8 +148,6 @@ public class RMIServer {
if (list[i].indexOf(MainAdapterImpl.MAIN_ADAPTER_NAME) != -1) if (list[i].indexOf(MainAdapterImpl.MAIN_ADAPTER_NAME) != -1)
ret++; ret++;
} }
if (TRACE)
System.out.println(" getNumberOfVM() NumberOfVM =" + ret);
return ret; return ret;
} }
} }