FileDocCategorySizeDatePackage
ServerMain.javaAPI DocJava SE 5 API10989Fri Aug 26 14:54:18 BST 2005com.sun.corba.se.impl.activation

ServerMain

public class ServerMain extends Object
version
1.8, 99/11/02
author
Ken Cavanaugh
since
JDK1.2

Fields Summary
public static final int
OK
public static final int
MAIN_CLASS_NOT_FOUND
public static final int
NO_MAIN_METHOD
public static final int
APPLICATION_ERROR
public static final int
UNKNOWN_ERROR
public static final int
NO_SERVER_ID
public static final int
REGISTRATION_FAILED
private static final boolean
debug
Constructors Summary
Methods Summary
private java.lang.reflect.MethodgetMainMethod(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.MethodgetNamedMethod(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 intgetServerId()


       
    
	Integer serverId = Integer.getInteger( ORBConstants.SERVER_ID_PROPERTY ) ;

	if (serverId == null)
	    logTerminal( "", NO_SERVER_ID ) ;

	return serverId.intValue() ;
    
private booleanisPublicStaticVoid(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 voidlogError(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 voidlogInformation(java.lang.String msg)
Write information to standard out only.

	writeLogMessage( System.out, "        " + msg ) ;
    
public static voidlogTerminal(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 voidmain(java.lang.String[] args)

	ServerMain server = new ServerMain();
	server.run(args);
    
public static java.lang.StringprintResult(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 voidredirectIOStreams()

	// 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 voidregisterCallback(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 voidrun(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 voidwriteLogMessage(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");