1 23 package com.sun.enterprise.server.ss; 24 25 import java.io.IOException ; 26 import java.net.ServerSocket ; 27 import java.net.Socket ; 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 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 57 public class ASLazyKernel { 58 private static final String AS_SELECTOR_PROVIDER = 59 "com.sun.enterprise.server.ss.provider.ASSelectorProvider"; 60 private static final String SELECTOR_PROVIDER_PROP = 61 "java.nio.channels.spi.SelectorProvider"; 62 private static final String QUICK_STARTUP = 63 "com.sun.enterprise.server.ss.ASQuickStartup"; 64 private static final String DEFAULT_SELECTOR_PROVIDER = 65 "sun.nio.ch.DefaultSelectorProvider"; 66 67 private static boolean defaultSelectorProviderCreatable = false; 68 private static Logger 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 []{String.valueOf(pex.getConflictedPort())}); 90 exitServer(context, pex.getConflictedPort()); 91 } catch ( Exception 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 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 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 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 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 JmsService jmsService_ = conf.getJmsService(); 146 String 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 { 174 startAdminService( context ); 175 } 176 177 private void initializeAdminService (ServerContext context) throws Exception { 178 createAdminChannelLifecycle(); 179 adminlc.onInitialization(context); 180 } 181 182 private void startAdminService(ServerContext context) throws Exception { 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 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 t) { 211 logger.log(Level.SEVERE, "socketservice.stop_lc_ex", t); 212 } 213 try { 214 lifecycle.onTermination(); 215 } catch (Throwable 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 { 222 lifecycle.onInitialization(sc); 223 lifecycle.onStartup(sc); 224 lifecycle.onReady(sc); 225 } 226 227 public static boolean isQuickStartupEnabled() { 228 String 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 msg ) { 237 if (logger.isLoggable(Level.FINE)) { 238 logger.fine( msg ); 239 } 240 } 241 242 254 private boolean initializeASSocketService() throws IOException { 255 if ( isQuickStartupEnabled()) { 256 System.setProperty(SELECTOR_PROVIDER_PROP, 258 AS_SELECTOR_PROVIDER); 259 260 ServerSocket.setSocketFactory( new ASServerSocketFactory() ); 264 Socket.setSocketImplFactory( new ASClientSocketFactory() ); 265 266 return true; 267 } 268 return false; 269 } 270 271 276 private static boolean isDefaultSelectorProviderCreatable() { 277 if ( defaultSelectorProviderCreatable ) { 278 return true; 279 } 280 try { 281 Class clazz = Class.forName(DEFAULT_SELECTOR_PROVIDER); 282 283 java.lang.reflect.Method createMeth = clazz.getMethod("create", 284 new Class [] {}); 285 createMeth.invoke( null, new Object [] {} ); 286 } catch( Exception e ) { 287 logFine( "Exception in isDefaultSelectorProviderCreatable : " + e ); 288 return false; 289 } 290 291 defaultSelectorProviderCreatable = true; 294 return true; 295 } 296 297 } 298 | Popular Tags |