KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > server > J2EEServer


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.server;
24
25 import java.io.*;
26 import java.util.Properties JavaDoc;
27 import java.util.Vector JavaDoc;
28 import java.net.*;
29 import javax.rmi.CORBA.Tie JavaDoc;
30 import javax.rmi.PortableRemoteObject JavaDoc;
31 import org.omg.CORBA.ORB JavaDoc;
32 import java.security.SecureRandom JavaDoc;
33 import com.sun.corba.ee.impl.orbutil.ORBConstants;
34
35 import com.sun.enterprise.*;
36 import com.sun.enterprise.util.*;
37 import com.sun.enterprise.log.*;
38 import com.sun.enterprise.repository.*;
39 import com.sun.enterprise.naming.*;
40 import com.sun.enterprise.distributedtx.*;
41 import com.sun.enterprise.appverification.factory.AppVerification;
42 import com.sun.enterprise.resource.ResourceInstaller;
43 import com.sun.enterprise.iiop.POAProtocolMgr;
44 import com.sun.enterprise.iiop.PEORBConfigurator;
45 import com.sun.enterprise.iiop.ORBMonitoring;
46 import com.sun.ejb.ContainerFactory;
47 import com.sun.ejb.containers.ContainerFactoryImpl;
48 import java.util.logging.*;
49 import com.sun.logging.*;
50
51 import com.sun.appserv.server.ServerLifecycleException;
52
53 import com.sun.enterprise.server.ServerContext;
54 import com.sun.enterprise.server.ServerContextImpl;
55 import com.sun.enterprise.config.ConfigContext;
56 import com.sun.enterprise.config.ConfigBean;
57 import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
58 import com.sun.enterprise.config.ConfigException;
59 import com.sun.enterprise.server.ApplicationServer;
60 import com.sun.enterprise.config.serverbeans.IiopService;
61 import com.sun.enterprise.config.serverbeans.IiopListener;
62 import com.sun.enterprise.config.serverbeans.TransactionService;
63 import com.sun.enterprise.config.serverbeans.Server;
64 import com.sun.enterprise.config.serverbeans.Domain;
65 import com.sun.enterprise.transaction.monitor.JTSMonitorMBean;
66 import com.sun.enterprise.config.serverbeans.JmsService;
67 import com.sun.enterprise.util.diagnostics.Reporter;
68 import com.sun.enterprise.util.ORBManager;
69 import com.sun.enterprise.config.serverbeans.ElementProperty;
70
71
72 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
73
74 import com.sun.enterprise.deployment.ConnectorDescriptor;
75 import com.sun.enterprise.deployment.Descriptor;
76 import com.sun.enterprise.deployment.io.ConnectorDeploymentDescriptorFile;
77 import com.sun.enterprise.deployment.interfaces.SecurityRoleMapperFactoryMgr;
78 import com.sun.enterprise.deployment.interfaces.SecurityRoleMapperFactory;
79 import com.sun.enterprise.connectors.ConnectorRuntime;
80 import com.sun.enterprise.connectors.util.JmsRaUtil;
81
82 import com.sun.enterprise.deployment.backend.OptionalPkgDependency;
83
84 /**
85  * J2EEServer is the main program for the J2EE Reference Implementation Server.
86  * This process hosts all the services and containers in the RI, including
87  * Web (Tomcat), EJB, CORBA, Naming, Security, Deployment and JMS.
88  *
89  * @author various
90  */

91
92 public final class J2EEServer
93 {
94     // SecureRandom number used for HTTPS and IIOP/SSL.
95
// This number is accessed by iiop/IIOPSSLSocketFactory
96
// & web/security/SSLSocketFactory classes.
97
private static final Logger _logger =
98     LogDomains.getLogger(LogDomains.CORE_LOGGER);
99     public static final SecureRandom JavaDoc secureRandom = new SecureRandom JavaDoc();
100     static {
101         secureRandom.setSeed(System.currentTimeMillis());
102     }
103     public static final String JavaDoc J2EE_HOME="com.sun.enterprise.home";
104
105     public static final String JavaDoc J2EE_APPNAME =
106     "com.sun.enterprise.appname";
107     public static final String JavaDoc JTS_SERVER_ID =
108         "com.sun.jts.persistentServerId";
109     public static final String JavaDoc J2EE_SERVER_ID_PROP =
110     "com.sun.enterprise.J2EEServerId";
111
112     // private static final boolean debug = true;
113
private static final boolean debug =
114     com.sun.enterprise.util.logging.Debug.enabled;
115
116     private static LocalStringManagerImpl localStrings =
117     new LocalStringManagerImpl(J2EEServer.class);
118
119     private static java.io.PrintStream JavaDoc ostream = System.out;
120     private static java.io.PrintStream JavaDoc estream = System.err;
121
122     private static final int MAX_INITIAL_CONTEXT_RETRIES = 15;
123
124     /** Property names */
125     private static final String JavaDoc DefaultDbDir= "orb.db";
126     private static final String JavaDoc LISTEN_PROP =
127     "com.sun.CORBA.connection.ORBListenSocket";
128
129     private static final String JavaDoc OUTPUT_LOG= "system.out";
130     private static final String JavaDoc ERROR_LOG= "system.err";
131
132
133     // Used for POAProtocolMgr to create poaids.db
134
// Used for activation repository servers.db
135
private File repositoryDir;
136
137     //IASRI 4661409 private Properties defaultProperties;
138
private int orbInitialPort;
139     private Configuration conf = null;
140     private ORB JavaDoc orb;
141     private ProtocolManager protocolMgr;
142
143     private ServerContext serverContext;
144   
145     /**
146      * Default constructor.
147      */

148     public J2EEServer()
149     {
150     }
151
152     private void run(String JavaDoc[] args, boolean verbose, boolean startJMS)
153     {
154         ConnectorRuntime.getRuntime().initialize(ConnectorRuntime.SERVER);
155         try {
156             if ( (serverContext != null) &&
157                  (serverContext instanceof ServerContextImpl) ) {
158
159                 ServerContextImpl ctxImp = (ServerContextImpl)serverContext;
160                 String JavaDoc seedFile = ctxImp.getServerConfigPath() +
161                     File.separator + "secure.seed";
162                 File secureSeedFile = new File(seedFile);
163
164                 // read the secure random from the file
165
long seed = readSecureSeed(secureSeedFile);
166                 secureRandom.setSeed(seed);
167                 // generate a new one for the next startup
168
seed = secureRandom.nextLong();
169                 writeSecureSeed(secureSeedFile, seed);
170                 secureSeedFile = null;
171
172             } else {
173                 _logger.log(Level.FINE,"Unable to initialize secure seed.");
174             }
175
176             // Initialize System properties
177
Properties JavaDoc props = System.getProperties();
178
179         // set server-id and server-port so that persistent EJBs
180
// in J2EEServer will be reachable after crashes.
181
props.setProperty(J2EE_APPNAME, "j2ee");
182
183
184         // Moved it here since the ORBManager uses this
185
Switch theSwitch = Switch.getSwitch();
186             theSwitch.setContainerType(Switch.EJBWEB_CONTAINER);
187
188
189             Switch.getSwitch().setProviderManager(ProviderManager.getProviderManager());
190
191
192         // initialize InvocationManager (needed by NamingManager, TM)
193
InvocationManager invMgr = new InvocationManagerImpl();
194         theSwitch.setInvocationManager(invMgr);
195
196         // initialize NamingManager
197
NamingManager nm = new NamingManagerImpl();
198         theSwitch.setNamingManager(nm);
199
200             // initialize InjectionManager
201
InjectionManager injectionMgr = new InjectionManagerImpl();
202             theSwitch.setInjectionManager(injectionMgr);
203
204         J2EETransactionManager tm =
205         J2EETransactionManagerImpl.createTransactionManager();
206         theSwitch.setTransactionManager(tm);
207
208
209         // Install JDBC data sources and Connector connection factories
210
ResourceInstaller installer = theSwitch.getResourceInstaller();
211
212             // Load System Resource Adapters.
213
ConnectorRuntime connRuntime = ConnectorRuntime.getRuntime();
214
215             // Upgrade jms resource adapter, if necessary.
216
JmsRaUtil raUtil = new JmsRaUtil();
217             raUtil.upgradeIfNecessary();
218
219             // instantiate and register the server-side RoleMapperFactory
220
initRoleMapperFactory();
221             
222             //Deploy jms ra
223
/*
224             JmsService jmsService = ServerBeansFactory.getConfigBean(
225                             serverContext.getConfigContext()).getJmsService();
226             if (jmsService.getType().equals("LOCAL")) {
227                 _logger.log(Level.INFO, "mqra.start", jmsService.getType());
228                 String mqraModuleName = ConnectorRuntime.DEFAULT_JMS_ADAPTER;
229                 String mqraModuleLoc = installer.getSystemModuleLocation(mqraModuleName);
230                 connRuntime.createActiveResourceAdapter(mqraModuleLoc,mqraModuleName,false);
231             }
232 */

233             // register managed objects
234
ManagementObjectManager mgmtObjectMgr =
235         theSwitch.getManagementObjectManager();
236  
237             //mgmtObjectMgr.registerJVM();
238
//mgmtObjectMgr.registerJ2EEDomain("Domain");
239
//mgmtObjectMgr.registerJavaMailResource("JavaMail");
240
//mgmtObjectMgr.registerJNDIResource("JNDI-" + nm.hashCode());
241
//mgmtObjectMgr.registerRMI_IIOPResource("ORB-" + orb.hashCode());
242
//mgmtObjectMgr.registerJTAResource("JTA-" + tm.hashCode());
243

244         //installer.installJdbcDataSources();
245
ResourcesUtil resourcesUtil =
246         ResourcesUtil.getInstance(serverContext);
247
248             installer.installPersistenceManagerResources();
249             installer.installCustomResources();
250             installer.installExternalJndiResources();
251             installer.installMailResources();
252
253         // Now happens thru resource adapter.
254
//installer.installJMSResources();
255

256         // Create EJB ContainerFactory
257
ContainerFactory cf = new ContainerFactoryImpl();
258         theSwitch.setContainerFactory(cf);
259
260         //satisfy the dependencies between optional packages
261
try {
262                 _logger.fine("satisfy.optionalpkg.dependency");
263             OptionalPkgDependency.satisfyOptionalPackageDependencies();
264         } catch (Exception JavaDoc e) {
265         _logger.log(Level.WARNING,
266                 "optionalpkg.error", e);
267         }
268
269
270         //register the monitoring bean for JTS
271
com.sun.enterprise.admin.monitor.MonitoringHelper.registerTxnMonitoringMBean(new JTSMonitorMBean());
272    
273         // Install UserTransaction object for use by standalone clients
274
nm.publishObject("UserTransaction", new UserTransactionImpl(),true);
275
276             //initialize monitoring for all connector related stats
277
ConnectorRuntime.getRuntime().initializeConnectorMonitoring();
278
279             String JavaDoc startupComplete =
280         localStrings.getLocalString("j2ee.started",
281                         "J2EE server startup complete.");
282         _logger.log(Level.FINE,startupComplete);
283         }
284     catch (Exception JavaDoc ex) {
285         if(_logger.isLoggable(Level.SEVERE))
286                 _logger.log(Level.SEVERE,"enterprise.j2eeservice_running_exception",ex.toString());
287         if ( debug )
288                 _logger.log(Level.FINEST,"Exception running j2ee services",ex);
289         Log.err.flush();
290         throw new RuntimeException JavaDoc(ex.getMessage(), ex);
291         }
292     }
293
294     /** register the RoleMapperFactory that should be used on the server side
295      */

296     private void initRoleMapperFactory() throws Exception JavaDoc
297     {
298         Object JavaDoc o = null;
299         Class JavaDoc c=null;
300         // this should never fail.
301
try {
302             c = Class.forName("com.sun.enterprise.security.acl.RoleMapperFactory");
303             if (c!=null) {
304                 o = c.newInstance();
305                 if (o!=null && o instanceof SecurityRoleMapperFactory) {
306                     SecurityRoleMapperFactoryMgr.registerFactory((SecurityRoleMapperFactory) o);
307                 }
308             }
309             if (o==null) {
310                 _logger.log(Level.SEVERE,localStrings.getLocalString("j2ee.norolemapper",
311                                      "Cannot instantiate the SecurityRoleMapperFactory"));
312             }
313         } catch(Exception JavaDoc cnfe) {
314             _logger.log(Level.SEVERE,
315             localStrings.getLocalString("j2ee.norolemapper", "Cannot instantiate the SecurityRoleMapperFactory"),
316             cnfe);
317             throw cnfe;
318         }
319     }
320     
321     /** read the secure random number from the file.
322      * If the seed is not present, the default expensive SecureRandom seed
323      * generation algorithm is invoked to return a new seed number
324      * @param File the file to be read - here secure.seed file.
325      */

326     private long readSecureSeed(File fname){
327     byte[] seed;
328     try {
329             BufferedReader fis = new BufferedReader(new FileReader(fname));
330             try{
331                 String JavaDoc line = fis.readLine();
332                 fis.close();
333                 // returning a long value.
334
Long JavaDoc lseed = new Long JavaDoc(line);
335                 return lseed.longValue();
336             } catch (IOException e){
337         if (fis != null)
338             fis.close();
339         }
340     } catch (Throwable JavaDoc e){ // IASRI 4666401 if all fails just create new
341
}
342         // BEGIN IASRI 4703002
343
// In order to work around JVM bug 4709460 avoid internal seeding.
344
// (Call setSeed again (see static block) to attempt to add some
345
// minimal randomness; setSeed calls are cumulative)
346

347         secureRandom.setSeed(System.currentTimeMillis());
348         long newSeed = secureRandom.nextLong();
349         return newSeed;
350     }
351     
352     /** write the new secure seed to the secure.seed file to speed up
353      * startup the next time the server is started.
354      * @param File secure.seed file
355      * @param long seed the value of the 8 byte seed.
356      */

357     private void writeSecureSeed(File fname, long seed){
358     try{
359         FileOutputStream fos = new FileOutputStream(fname);
360         String JavaDoc sseed = Long.toString(seed);
361         fos.write(sseed.getBytes());
362         fos.close();
363     }catch(IOException e){
364         String JavaDoc errmsg =
365         localStrings.getLocalString("j2ee.startupslow",
366                         "Cannot write the seed file for fast startup. The next startup will be slow.");
367
368         _logger.log(Level.WARNING,"enterprise.j2ee_startupslow");
369     }
370
371     }
372
373     /**
374      * Main for the J2EE server.
375      */

376     public static void main(ServerContext serverContext) throws ServerLifecycleException
377     {
378         String JavaDoc[] args = serverContext.getCmdLineArgs();
379     if (args == null) args = new String JavaDoc[0];
380
381
382     Utility.checkJVMVersion();
383     
384     // parse args
385
boolean verbose = false;
386     boolean startJMS = true;
387     
388     if (System.getProperty("j2ee.appverification.home") != null) {
389         AppVerification.setInstrument(true);
390     }
391     _logger.log(Level.FINE, "S1AS AVK Instrumentation "
392             + (AppVerification.doInstrument() ? "enabled" : "disabled"));
393
394     try {
395
396         // setting ORB class and ORB singleton to SE values
397
//setting RMI-IIOP delegates to EE values
398
ORBManager.setORBSystemProperties();
399
400         // start the server
401
J2EEServer j2ee = new J2EEServer();
402         j2ee.setServerContext(serverContext);
403         j2ee.run(args, verbose, startJMS);
404     } catch(Exception JavaDoc e) {
405         _logger.log(Level.SEVERE,"enterprise.run_exception",e);
406         if(e.getMessage() != null) {
407         if(_logger.isLoggable(Level.SEVERE))
408             _logger.log(Level.SEVERE,"enterprise.j2ee_server_error",e.getMessage());
409         }
410         _logger.log(Level.SEVERE,"enterprise.j2ee_server_error1");
411         System.err.flush();
412         throw new ServerLifecycleException(e.getMessage(), e);
413     }
414     }
415   
416     private void setServerContext(ServerContext serverContext) {
417         this.serverContext = serverContext;
418     }
419
420
421     /**
422      * Shutdown the J2EE server.
423      */

424     public static void shutdown() {
425         PEMain.shutdownStarted = true;
426     try {
427         if(_logger.isLoggable(Level.FINE))
428             _logger.log(Level.FINE,localStrings.getLocalString("j2ee.shutdown","Shutting down the J2EE server..."));
429         
430         if (AppVerification.doInstrument()) {
431             AppVerification.getInstrumentLogger().writeResults();
432         }
433         
434         Log.flushAll();
435     } catch(Throwable JavaDoc t) {
436         if(_logger.isLoggable(Level.SEVERE))
437             _logger.log(Level.SEVERE,"enterprise.shutdown_exception",t.toString());
438         if(_logger.isLoggable(Level.FINE))
439             _logger.log(Level.FINE,localStrings.getLocalString("j2ee.cannot.shutdown","Unable to shutdown the J2EE server..."));
440     }
441     if (PEMain.shutdownThreadInvoked == false)
442         System.exit(0);
443
444     }
445 }
446
447 // End of file.
448

449
450
451
452
Popular Tags