/** Shut down all the service components in the reverse order that they were added. */ void shutdownServices() { // reverse the list of services ArrayList<Object> list = new ArrayList<Object>(); for (Object service : serviceComponents) { list.add(service); } Collections.reverse(list); for (Object service : list) { ((Service) service).shutdown(); } }
/** * 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); }
/** * Notifies all included Services that the system is now ready. * * @throws Exception if there is any failure during notification */ void notifyReady() throws Exception { for (Object service : serviceComponents) ((Service) service).ready(); }