FileDocCategorySizeDatePackage
J2EEServer.javaAPI DocGlassfish v2 API17339Fri May 04 22:35:40 BST 2007com.sun.enterprise.server

J2EEServer

public final class J2EEServer extends Object
J2EEServer is the main program for the J2EE Reference Implementation Server. This process hosts all the services and containers in the RI, including Web (Tomcat), EJB, CORBA, Naming, Security, Deployment and JMS.
author
various

Fields Summary
private static final Logger
_logger
public static final SecureRandom
secureRandom
public static final String
J2EE_HOME
public static final String
J2EE_APPNAME
public static final String
JTS_SERVER_ID
public static final String
J2EE_SERVER_ID_PROP
private static final boolean
debug
private static LocalStringManagerImpl
localStrings
private static PrintStream
ostream
private static PrintStream
estream
private static final int
MAX_INITIAL_CONTEXT_RETRIES
private static final String
DefaultDbDir
Property names
private static final String
LISTEN_PROP
private static final String
OUTPUT_LOG
private static final String
ERROR_LOG
private File
repositoryDir
private int
orbInitialPort
private Configuration
conf
private org.omg.CORBA.ORB
orb
private ProtocolManager
protocolMgr
private com.sun.enterprise.server.ServerContext
serverContext
Constructors Summary
public J2EEServer()
Default constructor.

  
           
     
    
    
Methods Summary
private voidinitRoleMapperFactory()
register the RoleMapperFactory that should be used on the server side

    
        Object o = null;
        Class c=null;
        // this should never fail.
        try {
            c = Class.forName("com.sun.enterprise.security.acl.RoleMapperFactory");
            if (c!=null) {
                o = c.newInstance();  
                if (o!=null && o instanceof SecurityRoleMapperFactory) {
                    SecurityRoleMapperFactoryMgr.registerFactory((SecurityRoleMapperFactory) o);
                }
            }
            if (o==null) {
                _logger.log(Level.SEVERE,localStrings.getLocalString("j2ee.norolemapper", 
								     "Cannot instantiate the SecurityRoleMapperFactory"));
            }
        } catch(Exception cnfe) {
            _logger.log(Level.SEVERE,
			localStrings.getLocalString("j2ee.norolemapper", "Cannot instantiate the SecurityRoleMapperFactory"), 
			cnfe);
            throw  cnfe;
        } 
    
public static voidmain(com.sun.enterprise.server.ServerContext serverContext)
Main for the J2EE server.

        String[] args = serverContext.getCmdLineArgs();
	if (args == null) args = new String[0];


	Utility.checkJVMVersion();
	
	// parse args
	boolean verbose = false;
	boolean startJMS = true;
	
	if (System.getProperty("j2ee.appverification.home") != null) {
	    AppVerification.setInstrument(true);
	}
	_logger.log(Level.FINE, "S1AS AVK Instrumentation "
		    + (AppVerification.doInstrument() ? "enabled" : "disabled"));

	try {

	    // setting ORB class and ORB singleton to SE values
	    //setting RMI-IIOP delegates to EE values
  	    ORBManager.setORBSystemProperties();

	    // start the server
	    J2EEServer j2ee = new J2EEServer();
	    j2ee.setServerContext(serverContext);
	    j2ee.run(args, verbose, startJMS);
	} catch(Exception e) {
	    _logger.log(Level.SEVERE,"enterprise.run_exception",e);
	    if(e.getMessage() != null) {		  
		if(_logger.isLoggable(Level.SEVERE))
		    _logger.log(Level.SEVERE,"enterprise.j2ee_server_error",e.getMessage());
	    }
	    _logger.log(Level.SEVERE,"enterprise.j2ee_server_error1");
	    System.err.flush();
	    throw new ServerLifecycleException(e.getMessage(), e);
	}
    
private longreadSecureSeed(java.io.File fname)
read the secure random number from the file. If the seed is not present, the default expensive SecureRandom seed generation algorithm is invoked to return a new seed number

param
File the file to be read - here secure.seed file.

	byte[] seed;
	try {
            BufferedReader fis = new BufferedReader(new FileReader(fname));
            try{
                String line = fis.readLine();
                fis.close();
                // returning a long value.
                Long lseed = new Long(line);
                return lseed.longValue();
            } catch (IOException e){
		if (fis != null)
		    fis.close();
	    }
	} catch (Throwable e){  // IASRI 4666401 if all fails just create new
	}
        // BEGIN IASRI 4703002
        // In order to work around JVM bug 4709460 avoid internal seeding.
        // (Call setSeed again (see static block) to attempt to add some
        // minimal randomness; setSeed calls are cumulative)
        
        secureRandom.setSeed(System.currentTimeMillis());
        long newSeed = secureRandom.nextLong();
        return newSeed;
    
private voidrun(java.lang.String[] args, boolean verbose, boolean startJMS)

        ConnectorRuntime.getRuntime().initialize(ConnectorRuntime.SERVER);
        try {
            if ( (serverContext != null) &&
                 (serverContext instanceof ServerContextImpl) ) {

                ServerContextImpl ctxImp = (ServerContextImpl)serverContext;
                String seedFile = ctxImp.getServerConfigPath() +
                    File.separator + "secure.seed";
                File secureSeedFile = new File(seedFile);

                // read the secure random from the file
                long seed = readSecureSeed(secureSeedFile);
                secureRandom.setSeed(seed);
                // generate a new one for the next startup
                seed = secureRandom.nextLong();
                writeSecureSeed(secureSeedFile, seed);
                secureSeedFile = null;

            } else {
                _logger.log(Level.FINE,"Unable to initialize secure seed.");
            }

       	    // Initialize System properties
	    Properties props = System.getProperties();

	    // set server-id and server-port so that persistent EJBs
	    // in J2EEServer will be reachable after crashes.
	    props.setProperty(J2EE_APPNAME, "j2ee");


	    // Moved it here since the ORBManager uses this
	    Switch theSwitch = Switch.getSwitch();
            theSwitch.setContainerType(Switch.EJBWEB_CONTAINER);


            Switch.getSwitch().setProviderManager(ProviderManager.getProviderManager());

            //set server context for ResourcesUtil
            ResourcesUtil.setServerContext(serverContext);

	    // initialize InvocationManager (needed by NamingManager, TM)
	    InvocationManager invMgr = new InvocationManagerImpl();
	    theSwitch.setInvocationManager(invMgr);

	    // initialize NamingManager
	    NamingManager nm = new NamingManagerImpl();
	    theSwitch.setNamingManager(nm);

            // initialize InjectionManager
            InjectionManager injectionMgr = new InjectionManagerImpl();
            theSwitch.setInjectionManager(injectionMgr);

	    J2EETransactionManager tm =
		J2EETransactionManagerImpl.createTransactionManager();
	    theSwitch.setTransactionManager(tm);


	    // Install JDBC data sources and Connector connection factories
            ResourceInstaller installer = theSwitch.getResourceInstaller();

            // Load System Resource Adapters.
	    ConnectorRuntime connRuntime = ConnectorRuntime.getRuntime();


            // instantiate and register the server-side RoleMapperFactory
            initRoleMapperFactory();
            
            //Deploy jms ra
/*
            JmsService jmsService = ServerBeansFactory.getConfigBean(
                            serverContext.getConfigContext()).getJmsService();
            if (jmsService.getType().equals("LOCAL")) {
                _logger.log(Level.INFO, "mqra.start", jmsService.getType());
                String mqraModuleName = ConnectorRuntime.DEFAULT_JMS_ADAPTER;
                String mqraModuleLoc = installer.getSystemModuleLocation(mqraModuleName);
                connRuntime.createActiveResourceAdapter(mqraModuleLoc,mqraModuleName,false);
            }
*/
            // register managed objects
            ManagementObjectManager mgmtObjectMgr =
		theSwitch.getManagementObjectManager();
 
            //mgmtObjectMgr.registerJVM();
            //mgmtObjectMgr.registerJ2EEDomain("Domain");
            //mgmtObjectMgr.registerJavaMailResource("JavaMail");
            //mgmtObjectMgr.registerJNDIResource("JNDI-" + nm.hashCode());
	    //mgmtObjectMgr.registerRMI_IIOPResource("ORB-" + orb.hashCode());
            //mgmtObjectMgr.registerJTAResource("JTA-" + tm.hashCode());

	    //initialize monitoring for all connector related stats
            ConnectorRuntime.getRuntime().initializeConnectorMonitoring();

            //installer.installJdbcDataSources();
            
            installer.installPersistenceManagerResources();
            installer.installCustomResources();
            installer.installExternalJndiResources();
            installer.installMailResources();

	    // Now happens thru resource adapter.
            //installer.installJMSResources();

	    // Create EJB ContainerFactory
	    ContainerFactory cf = new ContainerFactoryImpl();
	    theSwitch.setContainerFactory(cf);

	    //satisfy the dependencies between optional packages
	    try { 
                _logger.fine("satisfy.optionalpkg.dependency");
	        OptionalPkgDependency.satisfyOptionalPackageDependencies();                
	    } catch (Exception e) {
		_logger.log(Level.WARNING, 
			    "optionalpkg.error", e);	
	    }


	    //register the monitoring bean for JTS
	    com.sun.enterprise.admin.monitor.MonitoringHelper.registerTxnMonitoringMBean(new JTSMonitorMBean());
   
	    // Install UserTransaction object for use by standalone clients
	    nm.publishObject("UserTransaction", new UserTransactionImpl(),true);

            String startupComplete =
		localStrings.getLocalString("j2ee.started",
					    "J2EE server startup complete.");
	    _logger.log(Level.FINE,startupComplete);
        }
	catch (Exception ex) {
	    if(_logger.isLoggable(Level.SEVERE))
                _logger.log(Level.SEVERE,"enterprise.j2eeservice_running_exception",ex.toString());
	    if ( debug )
                _logger.log(Level.FINEST,"Exception running j2ee services",ex);
	    Log.err.flush();
	    throw new RuntimeException(ex.getMessage(), ex);
        }
    
private voidsetServerContext(com.sun.enterprise.server.ServerContext serverContext)

        this.serverContext = serverContext;
    
public static voidshutdown()
Shutdown the J2EE server.

        PEMain.shutdownStarted = true;
	try {
	    if(_logger.isLoggable(Level.FINE))
	        _logger.log(Level.FINE,localStrings.getLocalString("j2ee.shutdown","Shutting down the J2EE server..."));
	    
	    if (AppVerification.doInstrument()) {
	        AppVerification.getInstrumentLogger().writeResults();
	    }
	    
	    Log.flushAll();
	} catch(Throwable t) {
	    if(_logger.isLoggable(Level.SEVERE))
	        _logger.log(Level.SEVERE,"enterprise.shutdown_exception",t.toString());
	    if(_logger.isLoggable(Level.FINE))
	        _logger.log(Level.FINE,localStrings.getLocalString("j2ee.cannot.shutdown","Unable to shutdown the J2EE server..."));
	}
	if (PEMain.shutdownThreadInvoked == false)
	    System.exit(0);

    
private voidwriteSecureSeed(java.io.File fname, long seed)
write the new secure seed to the secure.seed file to speed up startup the next time the server is started.

param
File secure.seed file
param
long seed the value of the 8 byte seed.

	try{
	    FileOutputStream fos = new FileOutputStream(fname);
	    String sseed = Long.toString(seed);
	    fos.write(sseed.getBytes());
	    fos.close();
	}catch(IOException e){
	    String errmsg =
		localStrings.getLocalString("j2ee.startupslow",
					    "Cannot write the seed file for fast startup. The next startup will be slow.");

	    _logger.log(Level.WARNING,"enterprise.j2ee_startupslow");
	}