/** * Private helper that creates the services and their associated managers, taking care to call out * the standard services first, because we need to get the ordering constant and make sure that * they're all present. */ private void fetchServices(ComponentRegistryImpl services, HashSet<Object> managerSet) throws Exception { // before we start, figure out if we're running with only a sub-set // of services, in which case there should be no external services String finalService = appProperties.getProperty(StandardProperties.FINAL_SERVICE); StandardService finalStandardService = null; String externalServices = appProperties.getProperty(StandardProperties.SERVICES); String externalManagers = appProperties.getProperty(StandardProperties.MANAGERS); if (finalService != null) { if ((externalServices != null) || (externalManagers != null)) throw new IllegalArgumentException( "Cannot specify external " + "services and a final " + "service"); // validate the final service try { finalStandardService = Enum.valueOf(StandardService.class, finalService); } catch (IllegalArgumentException iae) { if (logger.isLoggable(Level.SEVERE)) logger.logThrow(Level.SEVERE, iae, "Invalid final " + "service name: {0}", finalService); throw iae; } // make sure we're not running with an application if (!appProperties .getProperty(StandardProperties.APP_LISTENER) .equals(StandardProperties.APP_LISTENER_NONE)) throw new IllegalArgumentException( "Cannot specify an app " + "listener and a final " + "service"); } else { finalStandardService = StandardService.LAST_SERVICE; } // load the data service String dataServiceClass = appProperties.getProperty(StandardProperties.DATA_SERVICE, DEFAULT_DATA_SERVICE); String dataManagerClass = appProperties.getProperty(StandardProperties.DATA_MANAGER, DEFAULT_DATA_MANAGER); services.addComponent(setupService(dataServiceClass, dataManagerClass, managerSet)); // load the watch-dog service, which has no associated manager if (StandardService.WatchdogService.ordinal() > finalStandardService.ordinal()) return; String watchdogServiceClass = appProperties.getProperty(StandardProperties.WATCHDOG_SERVICE, DEFAULT_WATCHDOG_SERVICE); Service watchdogService = createService(Class.forName(watchdogServiceClass)); services.addComponent(watchdogService); if (watchdogService instanceof ProfileProducer) { if (profileRegistrar != null) ((ProfileProducer) watchdogService).setProfileRegistrar(profileRegistrar); } // load the node mapping service, which has no associated manager if (StandardService.NodeMappingService.ordinal() > finalStandardService.ordinal()) return; String nodemapServiceClass = appProperties.getProperty( StandardProperties.NODE_MAPPING_SERVICE, DEFAULT_NODE_MAPPING_SERVICE); Service nodemapService = createService(Class.forName(nodemapServiceClass)); services.addComponent(nodemapService); if (nodemapService instanceof ProfileProducer) { if (profileRegistrar != null) ((ProfileProducer) nodemapService).setProfileRegistrar(profileRegistrar); } // load the task service if (StandardService.TaskService.ordinal() > finalStandardService.ordinal()) return; String taskServiceClass = appProperties.getProperty(StandardProperties.TASK_SERVICE, DEFAULT_TASK_SERVICE); String taskManagerClass = appProperties.getProperty(StandardProperties.TASK_MANAGER, DEFAULT_TASK_MANAGER); services.addComponent(setupService(taskServiceClass, taskManagerClass, managerSet)); // load the client session service, which has no associated manager if (StandardService.ClientSessionService.ordinal() > finalStandardService.ordinal()) return; String clientSessionServiceClass = appProperties.getProperty( StandardProperties.CLIENT_SESSION_SERVICE, DEFAULT_CLIENT_SESSION_SERVICE); Service clientSessionService = createService(Class.forName(clientSessionServiceClass)); services.addComponent(clientSessionService); if (clientSessionService instanceof ProfileProducer) { if (profileRegistrar != null) ((ProfileProducer) clientSessionService).setProfileRegistrar(profileRegistrar); } // load the channel service if (StandardService.ChannelService.ordinal() > finalStandardService.ordinal()) return; String channelServiceClass = appProperties.getProperty(StandardProperties.CHANNEL_SERVICE, DEFAULT_CHANNEL_SERVICE); String channelManagerClass = appProperties.getProperty(StandardProperties.CHANNEL_MANAGER, DEFAULT_CHANNEL_MANAGER); services.addComponent(setupService(channelServiceClass, channelManagerClass, managerSet)); // finally, load any external services and their associated managers if ((externalServices != null) && (externalManagers != null)) { String[] serviceClassNames = externalServices.split(":", -1); String[] managerClassNames = externalManagers.split(":", -1); if (serviceClassNames.length != managerClassNames.length) { if (logger.isLoggable(Level.SEVERE)) logger.log( Level.SEVERE, "External service count " + "({0}) does not match manager count ({1}).", serviceClassNames.length, managerClassNames.length); throw new IllegalArgumentException("Mis-matched service " + "and manager count"); } for (int i = 0; i < serviceClassNames.length; i++) { if (!managerClassNames[i].equals("")) { services.addComponent( setupService(serviceClassNames[i], managerClassNames[i], managerSet)); } else { Class<?> serviceClass = Class.forName(serviceClassNames[i]); Service service = createService(serviceClass); services.addComponent(service); if ((profileRegistrar != null) && (service instanceof ProfileProducer)) ((ProfileProducer) service).setProfileRegistrar(profileRegistrar); } } } }
/** * Creates each of the <code>Service</code>s in order, in preparation for starting up an * application. At completion, this schedules an <code>AppStartupRunner</code> to finish * application startup. */ public void run() { if (logger.isLoggable(Level.CONFIG)) logger.log(Level.CONFIG, "{0}: starting services", appName); // create an empty context and register with the scheduler ComponentRegistryImpl services = new ComponentRegistryImpl(); ComponentRegistryImpl managers = new ComponentRegistryImpl(); AppKernelAppContext ctx = new AppKernelAppContext(appName, services, managers); MasterTaskScheduler scheduler = systemRegistry.getComponent(MasterTaskScheduler.class); try { scheduler.registerApplication(ctx, appProperties); } catch (Exception e) { if (logger.isLoggable(Level.SEVERE)) logger.logThrow(Level.SEVERE, e, "{0}: failed app scheduler " + "setup", appName); return; } // create the application's identity, and set as the current owner IdentityImpl id = new IdentityImpl("app:" + appName); TaskOwnerImpl owner = new TaskOwnerImpl(id, ctx); ThreadState.setCurrentOwner(owner); // get the managers and services that we're using HashSet<Object> managerSet = new HashSet<Object>(); try { fetchServices(services, managerSet); } catch (Exception e) { if (logger.isLoggable(Level.SEVERE)) logger.logThrow(Level.SEVERE, e, "{0}: failed to create " + "services", appName); ctx.shutdownServices(); return; } // register any profiling managers and fill in the manager registry for (Object manager : managerSet) { if (profileRegistrar != null) { if (manager instanceof ProfileProducer) ((ProfileProducer) manager).setProfileRegistrar(profileRegistrar); } managers.addComponent(manager); } // with the managers created, setup the final context and owner ctx = new AppKernelAppContext(appName, services, managers); owner = new TaskOwnerImpl(id, ctx); ThreadState.setCurrentOwner(owner); // notify all of the services that the application state is ready try { for (Object s : services) ((Service) s).ready(); } catch (Exception e) { logger.logThrow( Level.SEVERE, e, "{0}: failed when notifying services that application is " + "ready", appName); // shutdown all of the services ctx.shutdownServices(); return; } // At this point the services are now created, so the final step // is to try booting the application by running a special // KernelRunnable in an unbounded transaction. Note that if we're // running as a "server" then we don't actually start an app if (!appProperties .getProperty(StandardProperties.APP_LISTENER) .equals(StandardProperties.APP_LISTENER_NONE)) { AppStartupRunner startupRunner = new AppStartupRunner(ctx, appProperties); UnboundedTransactionRunner unboundedTransactionRunner = new UnboundedTransactionRunner(startupRunner); try { if (logger.isLoggable(Level.CONFIG)) logger.log(Level.CONFIG, "{0}: starting application", appName); // run the startup task, notifying the kernel on success scheduler.runTask(unboundedTransactionRunner, owner, true); kernel.contextReady(owner, true); } catch (Exception e) { if (logger.isLoggable(Level.CONFIG)) logger.logThrow(Level.CONFIG, e, "{0}: failed to " + "start application", appName); return; } } else { // we're running without an application, so we're finished kernel.contextReady(owner, false); } if (logger.isLoggable(Level.CONFIG)) logger.log(Level.CONFIG, "{0}: finished service config runner", appName); }