/** {@inheritDoc} */ public void close() { m_hasBeenClosed = true; if (m_ac.isActive()) { m_ac.publishEvent(new SakaiComponentEvent(this, SakaiComponentEvent.Type.STOPPING)); } m_ac.close(); }
/** {@inheritDoc} */ public Object get(String ifaceName) { Object component = null; try { component = m_ac.getBean(ifaceName); } catch (NoSuchBeanDefinitionException e) { // This is an expected outcome, we don't usually want logs if (M_log.isDebugEnabled()) { M_log.debug("get(" + ifaceName + "): " + e, e); } } catch (Exception e) { M_log.error("get(" + ifaceName + "): ", e); } return component; }
/** {@inheritDoc} */ public <T> T get(Class<T> iface) { T component = null; try { component = m_ac.getBean(iface.getName(), iface); } catch (NoSuchBeanDefinitionException e) { // This is an expected outcome, we don't usually want logs if (M_log.isDebugEnabled()) { M_log.debug("get(" + iface.getName() + "): " + e, e); } } catch (Exception e) { M_log.error("get(" + iface.getName() + "): ", e); } return component; }
/** {@inheritDoc} */ public Set getRegisteredInterfaces() { Set rv = new HashSet(); // get the registered ones String[] names = m_ac.getBeanDefinitionNames(); for (int i = 0; i < names.length; i++) { rv.add(names[i]); } // add the loaded ones for (Iterator iLoaded = m_loadedComponents.iterator(); iLoaded.hasNext(); ) { String loaded = (String) iLoaded.next(); rv.add(loaded); } return rv; }
/** {@inheritDoc} */ public void loadComponent(String ifaceName, Object component) { // Spring doesn't list these in getBeanDefinitionNames, so we keep track m_loadedComponents.add(ifaceName); m_ac.getBeanFactory().registerSingleton(ifaceName, component); }
/** {@inheritDoc} */ public boolean contains(String ifaceName) { boolean found = m_ac.containsBeanDefinition(ifaceName); return found; }
/** {@inheritDoc} */ public boolean contains(Class iface) { boolean found = m_ac.containsBeanDefinition(iface.getName()); return found; }
/** * Initialize the component manager. * * @param lateRefresh If <code>true</code> then don't refresh the application context but leave it * up to the caller, this is useful when running tests as it means you can change the * application context before everything gets setup. In production systems it should be <code> * false</code>. */ public void init(boolean lateRefresh) { if (m_ac != null) return; // Make sure a "sakai.home" system property is set. ensureSakaiHome(); checkSecurityPath(); m_ac = new SakaiApplicationContext(); m_ac.setInitialSingletonNames(CONFIGURATION_COMPONENTS); List<String> configLocationList = new ArrayList<String>(); configLocationList.add(DEFAULT_CONFIGURATION_FILE); String localConfigLocation = System.getProperty("sakai.home") + CONFIGURATION_FILE_NAME; File configFile = new File(localConfigLocation); if (configFile.exists()) { configLocationList.add("file:" + localConfigLocation); } m_ac.setConfigLocations(configLocationList.toArray(new String[0])); // load component packages loadComponents(); // if configured (with the system property CLOSE_ON_SHUTDOWN set), // create a shutdown task to close when the JVM closes // (otherwise we will close in removeChildAc() when the last child is gone) if (System.getProperty(CLOSE_ON_SHUTDOWN) != null) { Runtime.getRuntime() .addShutdownHook( new Thread() { public void run() { close(); } }); } // skip during tests if (!lateRefresh) { try { // get the singletons loaded m_ac.refresh(); m_ac.publishEvent(new SakaiComponentEvent(this, SakaiComponentEvent.Type.STARTED)); } catch (Exception e) { if (Boolean.valueOf(System.getProperty(SHUTDOWN_ON_ERROR, "false"))) { M_log.fatal(e.getMessage(), e); M_log.fatal("Shutting down JVM"); System.exit(1); } else { M_log.error(e.getMessage(), e); } } // dump the configuration values out try { final ServerConfigurationService scs = (ServerConfigurationService) this.get(ServerConfigurationService.class); if (scs != null) { ConfigData cd = scs.getConfigData(); M_log.info( "Configuration loaded " + cd.getTotalConfigItems() + " values, " + cd.getRegisteredConfigItems() + " registered"); if (scs.getBoolean("config.dump.to.log", false)) { // output the config logs now and then output then again in 120 seconds M_log.info("Configuration values:\n" + cd.toString()); Timer timer = new Timer(true); timer.schedule( new TimerTask() { @Override public void run() { M_log.info( "Configuration values: (delay 1):\n" + scs.getConfigData().toString()); } }, 120 * 1000); timer.schedule( new TimerTask() { @Override public void run() { M_log.info( "Configuration values: (delay 2):\n" + scs.getConfigData().toString()); } }, 300 * 1000); } } else { // probably testing so just say we cannot dump the config M_log.warn( "Configuration: Unable to get and dump out the registered server config values because no ServerConfigurationService is available - this is OK if this is part of a test, this is very bad otherwise"); } } catch (Exception e) { M_log.error( "Configuration: Unable to get and dump out the registered server config values (config.dump.to.log): " + e, e); } } }