KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > server > ss > ASLazyKernel


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.ss;
24
25 import java.io.IOException JavaDoc;
26 import java.net.ServerSocket JavaDoc;
27 import java.net.Socket JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30 import com.sun.appserv.server.ServerLifecycle;
31 import com.sun.enterprise.admin.server.core.AdminChannelLifecycle;
32 import com.sun.enterprise.config.ConfigContext;
33 import com.sun.enterprise.config.ConfigBean;
34 import com.sun.enterprise.config.serverbeans.AdminService;
35 import com.sun.enterprise.config.serverbeans.Config;
36 import com.sun.enterprise.config.serverbeans.HttpListener;
37 import com.sun.enterprise.config.serverbeans.HttpService;
38 import com.sun.enterprise.config.serverbeans.IiopListener;
39 import com.sun.enterprise.config.serverbeans.IiopService;
40 import com.sun.enterprise.config.serverbeans.JmsHost;
41 import com.sun.enterprise.config.serverbeans.JmsService;
42 import com.sun.enterprise.config.serverbeans.JmxConnector;
43 import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
44 import com.sun.enterprise.config.serverbeans.ServerTags;
45 import com.sun.enterprise.jms.JmsProviderLifecycle;
46 import com.sun.enterprise.server.ServerContext;
47 import com.sun.enterprise.server.ss.provider.ASClientSocketFactory;
48 import com.sun.enterprise.server.ss.provider.ASServerSocketFactory;
49 import com.sun.enterprise.server.ss.provider.PortConflictException;
50 import com.sun.logging.LogDomains;
51
52 /**
53  * This is a helper class to start an ASSocketService instance on
54  * various ports.
55  * We also setup socket factories.
56  */

57 public class ASLazyKernel {
58     private static final String JavaDoc AS_SELECTOR_PROVIDER =
59         "com.sun.enterprise.server.ss.provider.ASSelectorProvider";
60     private static final String JavaDoc SELECTOR_PROVIDER_PROP =
61         "java.nio.channels.spi.SelectorProvider";
62     private static final String JavaDoc QUICK_STARTUP =
63     "com.sun.enterprise.server.ss.ASQuickStartup";
64     private static final String JavaDoc DEFAULT_SELECTOR_PROVIDER =
65     "sun.nio.ch.DefaultSelectorProvider";
66
67     private static boolean defaultSelectorProviderCreatable = false;
68     private static Logger JavaDoc logger = LogDomains.getLogger(LogDomains.CORE_LOGGER);
69
70     private Config conf = null;
71     private JmsProviderLifecycle jmslc = null;
72     private AdminChannelLifecycle adminlc = null;
73
74
75     public boolean startASSocketServices(ServerContext context) {
76     try {
77         if ( initializeASSocketService() ) {
78         logFine("STARTING ASSocketService");
79                 ASSocketService.initialize();
80                 initializeAdminService(context);
81         setupSocketListeners(context);
82         setupInitialServices(context);
83         logger.log( Level.INFO, "socketservice.init_done");
84         return true;
85         }
86         logFine("Not using ASSocketService. Proceeding with normal startup");
87     } catch ( PortConflictException pex ) {
88             logger.log(Level.SEVERE, "socketservice.port_conflict",
89                        new Object JavaDoc[]{String.valueOf(pex.getConflictedPort())});
90             exitServer(context, pex.getConflictedPort());
91     } catch ( Exception JavaDoc ex ) {
92
93             logger.log(Level.SEVERE, "socketservice.unknown_ex", ex);
94             exitServer(context,0);
95
96         }
97     return false;
98     }
99
100     private void exitServer(ServerContext context, int port) {
101         logFine("Stopped MQ");
102                                    
103         abortAdminService(context, port);
104         logFine("Aborted admin service");
105                                               
106         // Abort the server.
107
System.exit(1);
108     }
109
110     private void setupSocketListeners(ServerContext scontext)
111     throws PortConflictException, com.sun.enterprise.config.ConfigException {
112
113     ConfigContext ctxt = scontext.getConfigContext();
114     conf = ServerBeansFactory.getConfigBean( ctxt );
115     
116     // Start HTTP listener ports
117
HttpService httpService = conf.getHttpService();
118     HttpListener[] httpListeners = httpService.getHttpListener();
119     for ( int i=0; i<httpListeners.length; i++ ) {
120             startService(httpListeners[i]);
121         logFine("Started ASSocketService for HTTP(S) ");
122     }
123
124     // Start IIOP listener ports
125
IiopService iiopService = conf.getIiopService();
126     IiopListener[] iiopListeners = iiopService.getIiopListener();
127     for ( int i=0; i<iiopListeners.length; i++ ) {
128         if ( !iiopListeners[i].isEnabled() )
129         continue;
130             startService(iiopListeners[i]);
131         logFine("Started ASSocketService for IIOP(S)");
132     }
133
134     // Start JMX Connector ports
135
AdminService adminService = conf.getAdminService();
136     JmxConnector[] jmxConnectors = adminService.getJmxConnector();
137     for ( int i=0; i<jmxConnectors.length; i++ ) {
138         if ( !jmxConnectors[i].isEnabled() )
139         continue;
140             startService(jmxConnectors[i]);
141         logFine("Started ASSocketService for JMX Connector ");
142     }
143
144         // Start MQ ports
145
JmsService jmsService_ = conf.getJmsService();
146         String JavaDoc defaultJmsHost = jmsService_.getDefaultJmsHost();
147         JmsHost jmsHost_ = null;
148
149         if (defaultJmsHost==null || defaultJmsHost.equals("")) {
150             jmsHost_ = ServerBeansFactory.getJmsHostBean(ctxt);
151         } else {
152             jmsHost_ = jmsService_.getJmsHostByName(defaultJmsHost);
153         }
154         if (jmsService_.getType().equalsIgnoreCase("embedded") &&
155             jmsHost_ != null && jmsHost_.isEnabled()) {
156             ASSocketServiceConfig ssConfig =
157             new ASSocketServiceConfig(jmsHost_);
158             ssConfig.setPortTag(ServerTags.PORT);
159         (new ASSocketService(ssConfig)).start();
160         logFine("Started ASSocketService for JMS ");
161         }
162     }
163
164     private void startService(ConfigBean bean) throws PortConflictException {
165         ASSocketServiceConfig ssConfig =
166         new ASSocketServiceConfig(bean);
167         ssConfig.setAddressTag(ServerTags.ADDRESS);
168         ssConfig.setPortTag(ServerTags.PORT);
169     (new ASSocketService(ssConfig)).start();
170     }
171
172     private void setupInitialServices( ServerContext context )
173                                                         throws Exception JavaDoc {
174     startAdminService( context );
175     }
176
177     private void initializeAdminService (ServerContext context) throws Exception JavaDoc{
178         createAdminChannelLifecycle();
179         adminlc.onInitialization(context);
180     }
181
182     private void startAdminService(ServerContext context) throws Exception JavaDoc{
183         logFine( "About to start AdminService ");
184         createAdminChannelLifecycle();
185         adminlc.onStartup(context);
186         adminlc.onReady(context);
187     }
188
189     private void abortAdminService(ServerContext sc, int port) {
190         try {
191             createAdminChannelLifecycle();
192             adminlc.onAbort(port);
193             ASSocketService.waitForClientNotification();
194             adminlc.onShutdown();
195             adminlc.onTermination();
196         } catch (Exception JavaDoc e) {
197             e.printStackTrace();
198         }
199     }
200
201     private void createAdminChannelLifecycle() {
202         if (adminlc == null) {
203             adminlc = new AdminChannelLifecycle();
204         }
205     }
206
207     private void stopLifecycle(ServerLifecycle lifecycle) {
208         try {
209             lifecycle.onShutdown();
210         } catch (Throwable JavaDoc t) {
211             logger.log(Level.SEVERE, "socketservice.stop_lc_ex", t);
212         }
213         try {
214             lifecycle.onTermination();
215         } catch (Throwable JavaDoc t) {
216             logger.log(Level.SEVERE, "socketservice.stop_lc_ex", t);
217         }
218     }
219
220     private void startLifecycle(ServerLifecycle lifecycle, ServerContext sc)
221                                     throws Exception JavaDoc {
222         lifecycle.onInitialization(sc);
223         lifecycle.onStartup(sc);
224         lifecycle.onReady(sc);
225     }
226     
227     public static boolean isQuickStartupEnabled() {
228     // enabled by default if system property absent
229
String JavaDoc quickStartValue = System.getProperty(QUICK_STARTUP);
230     if ( quickStartValue == null || quickStartValue.equals("true") ) {
231         return isDefaultSelectorProviderCreatable();
232     }
233     return false;
234     }
235     
236     private static void logFine( String JavaDoc msg ) {
237         if (logger.isLoggable(Level.FINE)) {
238         logger.fine( msg );
239     }
240     }
241     
242     /**
243      * This method does the following: <br>
244      * <ul>
245      * <li>Checks if the class sun.nio.ch.DefaultSelectorProvider is present
246      * and its "create()" method is callable</li>
247      * <li>Sets the system property that defines our impl of a SelectorProvider</li>
248      * <li>Creates and sets the SocketImplFactories for ServerSocket and Socket</li>
249      *</ul>
250      *
251      * If a problem occurs in any of the above steps, we simply assume that we
252      * should use the default startup behavior and proceed
253      */

254     private boolean initializeASSocketService() throws IOException JavaDoc {
255     if ( isQuickStartupEnabled()) {
256         // Set the NIO SelectorProvider impl property
257
System.setProperty(SELECTOR_PROVIDER_PROP,
258                    AS_SELECTOR_PROVIDER);
259
260         // Set the SocketImplFactories for ServerSocket and Socket
261
// Note: if either of these factories was already set (almost
262
// impossible), an exception will be thrown and server will abort.
263
ServerSocket.setSocketFactory( new ASServerSocketFactory() );
264         Socket.setSocketImplFactory( new ASClientSocketFactory() );
265
266         return true;
267     }
268         return false;
269     }
270
271     /**
272      * The whole quick startup for the appserver hinges on whether we can
273      * invoke the create method of DefaultSelectorProvider
274      * So check this
275      */

276     private static boolean isDefaultSelectorProviderCreatable() {
277     if ( defaultSelectorProviderCreatable ) {
278          return true;
279     }
280         try {
281         Class JavaDoc clazz = Class.forName(DEFAULT_SELECTOR_PROVIDER);
282             
283         java.lang.reflect.Method JavaDoc createMeth = clazz.getMethod("create",
284                                 new Class JavaDoc[] {});
285         createMeth.invoke( null, new Object JavaDoc[] {} );
286     } catch( Exception JavaDoc e ) {
287         logFine( "Exception in isDefaultSelectorProviderCreatable : " + e );
288         return false;
289     }
290        
291     //Since we could invoke "create" on a DefaultSelectorProvider object
292
//successfully, everything is ok
293
defaultSelectorProviderCreatable = true;
294     return true;
295     }
296
297 }
298
Popular Tags