Methods Summary |
---|
private java.lang.reflect.Method | getMainMethod(java.lang.Class serverClass)
Class argTypes[] = new Class[] { String[].class } ;
Method method = null ;
try {
method = serverClass.getDeclaredMethod( "main", argTypes ) ;
} catch (Exception exc) {
logTerminal( exc.getMessage(), NO_MAIN_METHOD ) ;
}
if (!isPublicStaticVoid( method ))
logTerminal( "", NO_MAIN_METHOD ) ;
return method ;
|
private java.lang.reflect.Method | getNamedMethod(java.lang.Class serverClass, java.lang.String methodName)
Class argTypes[] = new Class[] { org.omg.CORBA.ORB.class } ;
Method method = null ;
try {
method = serverClass.getDeclaredMethod( methodName, argTypes ) ;
} catch (Exception exc) {
return null ;
}
if (!isPublicStaticVoid( method ))
return null ;
return method ;
|
private int | getServerId()
Integer serverId = Integer.getInteger( ORBConstants.SERVER_ID_PROPERTY ) ;
if (serverId == null)
logTerminal( "", NO_SERVER_ID ) ;
return serverId.intValue() ;
|
private boolean | isPublicStaticVoid(java.lang.reflect.Method method)
// check modifiers: public static
int modifiers = method.getModifiers ();
if (!Modifier.isPublic (modifiers) || !Modifier.isStatic (modifiers)) {
logError( method.getName() + " is not public static" ) ;
return false ;
}
// check return type and exceptions
if (method.getExceptionTypes ().length != 0) {
logError( method.getName() + " declares exceptions" ) ;
return false ;
}
if (!method.getReturnType().equals (Void.TYPE)) {
logError( method.getName() + " does not have a void return type" ) ;
return false ;
}
return true ;
|
public static void | logError(java.lang.String msg)Write error message to standard out and standard err.
writeLogMessage( System.out, "ERROR: " + msg ) ;
writeLogMessage( System.err, "ERROR: " + msg ) ;
|
public static void | logInformation(java.lang.String msg)Write information to standard out only.
writeLogMessage( System.out, " " + msg ) ;
|
public static void | logTerminal(java.lang.String msg, int code)Write final message to log(s) and then terminate by calling
System.exit( code ). If code == OK, write a normal termination
message to standard out, otherwise write an abnormal termination
message to standard out and standard error.
if (code == 0) {
writeLogMessage( System.out, " " + msg ) ;
} else {
writeLogMessage( System.out, "FATAL: " +
printResult( code ) + ": " + msg ) ;
writeLogMessage( System.err, "FATAL: " +
printResult( code ) + ": " + msg ) ;
}
System.exit( code ) ;
|
public static void | main(java.lang.String[] args)
ServerMain server = new ServerMain();
server.run(args);
|
public static java.lang.String | printResult(int result)
switch (result) {
case OK : return "Server terminated normally" ;
case MAIN_CLASS_NOT_FOUND : return "main class not found" ;
case NO_MAIN_METHOD : return "no main method" ;
case APPLICATION_ERROR : return "application error" ;
case NO_SERVER_ID : return "server ID not defined" ;
case REGISTRATION_FAILED: return "server registration failed" ;
default : return "unknown error" ;
}
|
private void | redirectIOStreams()
// redirect out and err streams
try {
String logDirName =
System.getProperty( ORBConstants.DB_DIR_PROPERTY ) +
System.getProperty("file.separator") +
ORBConstants.SERVER_LOG_DIR +
System.getProperty("file.separator");
File logDir = new File(logDirName);
String server = System.getProperty(
ORBConstants.SERVER_ID_PROPERTY ) ;
FileOutputStream foutStream =
new FileOutputStream(logDirName + server+".out", true);
FileOutputStream ferrStream =
new FileOutputStream(logDirName + server+".err", true);
PrintStream pSout = new PrintStream(foutStream, true);
PrintStream pSerr = new PrintStream(ferrStream, true);
System.setOut(pSout);
System.setErr(pSerr);
logInformation( "Server started" ) ;
} catch (Exception ex) {}
|
private void | registerCallback(java.lang.Class serverClass)
Method installMethod = getNamedMethod( serverClass, "install" ) ;
Method uninstallMethod = getNamedMethod( serverClass, "uninstall" ) ;
Method shutdownMethod = getNamedMethod( serverClass, "shutdown" ) ;
Properties props = new Properties() ;
props.put( "org.omg.CORBA.ORBClass",
"com.sun.corba.se.impl.orb.ORBImpl" ) ;
// NOTE: Very important to pass this property, otherwise the
// Persistent Server registration will be unsucessfull.
props.put( ORBConstants.ACTIVATED_PROPERTY, "false" );
String args[] = null ;
ORB orb = ORB.init( args, props ) ;
ServerCallback serverObj = new ServerCallback( orb,
installMethod, uninstallMethod, shutdownMethod ) ;
int serverId = getServerId() ;
try {
Activator activator = ActivatorHelper.narrow(
orb.resolve_initial_references( ORBConstants.SERVER_ACTIVATOR_NAME ));
activator.active(serverId, serverObj);
} catch (Exception ex) {
logTerminal( "exception " + ex.getMessage(),
REGISTRATION_FAILED ) ;
}
|
private void | run(java.lang.String[] args)
try {
redirectIOStreams() ;
String serverClassName = System.getProperty(
ORBConstants.SERVER_NAME_PROPERTY ) ;
// determine the class loader to be used for loading the class
// since ServerMain is going to be in JDK and we need to have this
// class to load application classes, this is required here.
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null)
cl = ClassLoader.getSystemClassLoader();
// determine the main class
Class serverClass = null;
try {
// determine the main class, try loading with current class loader
serverClass = Class.forName( serverClassName ) ;
} catch (ClassNotFoundException ex) {
// eat the exception and try to load using SystemClassLoader
serverClass = Class.forName( serverClassName, true, cl);
}
if (debug)
System.out.println("class " + serverClassName + " found");
// get the main method
Method mainMethod = getMainMethod( serverClass ) ;
// This piece of code is required, to verify the server definition
// without launching it.
// verify the server
boolean serverVerifyFlag = Boolean.getBoolean(
ORBConstants.SERVER_DEF_VERIFY_PROPERTY) ;
if (serverVerifyFlag) {
if (mainMethod == null)
logTerminal("", NO_MAIN_METHOD);
else {
if (debug)
System.out.println("Valid Server");
logTerminal("", OK);
}
}
registerCallback( serverClass ) ;
// build args to the main and call it
Object params [] = new Object [1];
params[0] = args;
mainMethod.invoke(null, params);
} catch (ClassNotFoundException e) {
logTerminal("ClassNotFound exception: " + e.getMessage(),
MAIN_CLASS_NOT_FOUND);
} catch (Exception e) {
logTerminal("Exception: " + e.getMessage(),
APPLICATION_ERROR);
}
|
private static void | writeLogMessage(java.io.PrintStream pstream, java.lang.String msg)Write a time-stamped message to the indicated PrintStream.
Date date = new Date();
pstream.print( "[" + date.toString() + "] " + msg + "\n");
|