List of usage examples for org.springframework.context.support GenericApplicationContext getBeansOfType
@Override public <T> Map<String, T> getBeansOfType(@Nullable Class<T> type) throws BeansException
From source file:org.apache.ignite.yardstick.IgniteNode.java
/** * @param springCfgPath Spring configuration file path. * @return Grid configuration.// w w w.j a va2 s . c o m * @throws Exception If failed. */ protected static IgniteConfiguration loadConfiguration(String springCfgPath) throws Exception { URL url; try { url = new URL(springCfgPath); } catch (MalformedURLException e) { url = IgniteUtils.resolveIgniteUrl(springCfgPath); if (url == null) throw new IgniteCheckedException("Spring XML configuration path is invalid: " + springCfgPath + ". Note that this path should be either absolute or a relative local file system path, " + "relative to META-INF in classpath or valid URL to IGNITE_HOME.", e); } GenericApplicationContext springCtx; try { springCtx = new GenericApplicationContext(); new XmlBeanDefinitionReader(springCtx).loadBeanDefinitions(new UrlResource(url)); springCtx.refresh(); } catch (BeansException e) { throw new Exception("Failed to instantiate Spring XML application context [springUrl=" + url + ", err=" + e.getMessage() + ']', e); } Map<String, IgniteConfiguration> cfgMap; try { cfgMap = springCtx.getBeansOfType(IgniteConfiguration.class); } catch (BeansException e) { throw new Exception("Failed to instantiate bean [type=" + IgniteConfiguration.class + ", err=" + e.getMessage() + ']', e); } if (cfgMap == null || cfgMap.isEmpty()) throw new Exception("Failed to find ignite configuration in: " + url); return cfgMap.values().iterator().next(); }
From source file:org.yardstickframework.gridgain.GridGainNode.java
/** * @param springCfgPath Spring configuration file path. * @return Grid configuration.//from w w w .j av a2 s . co m * @throws Exception If failed. */ private static GridConfiguration loadConfiguration(String springCfgPath) throws Exception { URL url; try { url = new URL(springCfgPath); } catch (MalformedURLException e) { url = GridUtils.resolveGridGainUrl(springCfgPath); if (url == null) throw new GridException("Spring XML configuration path is invalid: " + springCfgPath + ". Note that this path should be either absolute or a relative local file system path, " + "relative to META-INF in classpath or valid URL to GRIDGAIN_HOME.", e); } GenericApplicationContext springCtx; try { springCtx = new GenericApplicationContext(); new XmlBeanDefinitionReader(springCtx).loadBeanDefinitions(new UrlResource(url)); springCtx.refresh(); } catch (BeansException e) { throw new Exception("Failed to instantiate Spring XML application context [springUrl=" + url + ", err=" + e.getMessage() + ']', e); } Map<String, GridConfiguration> cfgMap; try { cfgMap = springCtx.getBeansOfType(GridConfiguration.class); } catch (BeansException e) { throw new Exception( "Failed to instantiate bean [type=" + GridConfiguration.class + ", err=" + e.getMessage() + ']', e); } if (cfgMap == null || cfgMap.isEmpty()) throw new Exception("Failed to find grid configuration in: " + url); return cfgMap.values().iterator().next(); }
From source file:atunit.spring.SpringContainer.java
protected void fillInMissingFieldBeans(Class<?> testClass, GenericApplicationContext ctx) throws Exception { for (Field field : testClass.getDeclaredFields()) { Bean beanAnno = field.getAnnotation(Bean.class); if (beanAnno == null) continue; String name = beanAnno.value(); if (!name.equals("") && !ctx.containsBean(name)) { ctx.registerBeanDefinition(name, defineAutowireBean(field.getType())); } else if (ctx.getBeansOfType(field.getType()).isEmpty()) { BeanDefinitionReaderUtils.registerWithGeneratedName(defineAutowireBean(field.getType()), ctx); }/*ww w .j a v a2 s.c o m*/ } }
From source file:org.gridgain.grid.GridFactory.java
/** * Starts all grids specified within given Spring XML configuration file URL. If grid with given name * is already started, then exception is thrown. In this case all instances that may * have been started so far will be stopped too. * <p>//from w ww .j a va 2s.c o m * Usually Spring XML configuration file will contain only one Grid definition. Note that * Grid configuration bean(s) is retrieved form configuration file by type, so the name of * the Grid configuration bean is ignored. * * @param springCfgUrl Spring XML configuration file URL. This cannot be {@code null}. * @param ctx Optional Spring application context. * @return Started grid. If Spring configuration contains multiple grid instances, * then the 1st found instance is returned. * @throws GridException If grid could not be started or configuration * read. This exception will be thrown also if grid with given name has already * been started or Spring XML configuration file is invalid. */ // Warning is due to Spring. public static Grid start(URL springCfgUrl, @Nullable ApplicationContext ctx) throws GridException { A.notNull(springCfgUrl, "springCfgUrl"); boolean isLog4jUsed = GridFactory.class.getClassLoader() .getResource("org/apache/log4j/Appender.class") != null; Object rootLog = null; Object nullApp = null; if (isLog4jUsed) try { // Add no-op logger to remove no-appender warning. Class logCls = Class.forName("org.apache.log4j.Logger"); rootLog = logCls.getMethod("getRootLogger").invoke(logCls); nullApp = Class.forName("org.apache.log4j.varia.NullAppender").newInstance(); Class appCls = Class.forName("org.apache.log4j.Appender"); rootLog.getClass().getMethod("addAppender", appCls).invoke(rootLog, nullApp); } catch (Exception e) { throw new GridException("Failed to add no-op logger for Log4j.", e); } GenericApplicationContext springCtx; try { springCtx = new GenericApplicationContext(); new XmlBeanDefinitionReader(springCtx).loadBeanDefinitions(new UrlResource(springCfgUrl)); springCtx.refresh(); } catch (BeansException e) { throw new GridException("Failed to instantiate Spring XML application context [springUrl=" + springCfgUrl + ", err=" + e.getMessage() + ']', e); } Map<String, GridConfiguration> cfgMap; try { cfgMap = springCtx.getBeansOfType(GridConfiguration.class); } catch (BeansException e) { throw new GridException( "Failed to instantiate bean [type=" + GridConfiguration.class + ", err=" + e.getMessage() + ']', e); } if (cfgMap == null) throw new GridException("Failed to find a single grid factory configuration in: " + springCfgUrl); if (isLog4jUsed) { try { // Remove previously added no-op logger. Class appenderCls = Class.forName("org.apache.log4j.Appender"); rootLog.getClass().getMethod("removeAppender", appenderCls).invoke(rootLog, nullApp); } catch (Exception e) { throw new GridException("Failed to remove previously added no-op logger for Log4j.", e); } } if (cfgMap.isEmpty()) throw new GridException("Can't find grid factory configuration in: " + springCfgUrl); List<GridNamedInstance> grids = new ArrayList<GridNamedInstance>(cfgMap.size()); try { for (GridConfiguration cfg : cfgMap.values()) { assert cfg != null; // Use either user defined context or our one. GridNamedInstance grid = start0(cfg, ctx == null ? springCtx : ctx); // Add it if it was not stopped during startup. if (grid != null) grids.add(grid); } } catch (GridException e) { // Stop all instances started so far. for (GridNamedInstance grid : grids) { try { grid.stop(true, false); } catch (Exception e1) { U.error(grid.log, "Error when stopping grid: " + grid, e1); } } throw e; } // Return the first grid started. GridNamedInstance res = !grids.isEmpty() ? grids.get(0) : null; return res != null ? res.grid() : null; }
From source file:org.gridgain.grid.loaders.glassfish.GridGlassfishLoader.java
/** * Starts all grids with given properties. * * @param props Startup properties.// ww w . ja v a2 s . c o m * @throws ServerLifecycleException Thrown in case of startup fails. */ @SuppressWarnings({ "unchecked" }) private void start(Properties props) throws ServerLifecycleException { GridLogger log = new GridJclLogger(LogFactory.getLog("GridGain")); if (props != null) cfgFile = props.getProperty(cfgFilePathParam); if (cfgFile == null) throw new ServerLifecycleException("Failed to read property: " + cfgFilePathParam); ctxClsLdr = Thread.currentThread().getContextClassLoader(); // Set thread context classloader because Spring use it for loading classes. Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); URL cfgUrl = U.resolveGridGainUrl(cfgFile); if (cfgUrl == null) throw new ServerLifecycleException("Failed to find Spring configuration file (path provided should be " + "either absolute, relative to GRIDGAIN_HOME, or relative to META-INF folder): " + cfgFile); GenericApplicationContext springCtx; try { springCtx = new GenericApplicationContext(); XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(springCtx); xmlReader.loadBeanDefinitions(new UrlResource(cfgUrl)); springCtx.refresh(); } catch (BeansException e) { throw new ServerLifecycleException( "Failed to instantiate Spring XML application context: " + e.getMessage(), e); } Map cfgMap; try { // Note: Spring is not generics-friendly. cfgMap = springCtx.getBeansOfType(GridConfiguration.class); } catch (BeansException e) { throw new ServerLifecycleException( "Failed to instantiate bean [type=" + GridConfiguration.class + ", err=" + e.getMessage() + ']', e); } if (cfgMap == null) throw new ServerLifecycleException("Failed to find a single grid factory configuration in: " + cfgUrl); if (cfgMap.isEmpty()) throw new ServerLifecycleException("Can't find grid factory configuration in: " + cfgUrl); try { for (GridConfiguration cfg : (Collection<GridConfiguration>) cfgMap.values()) { assert cfg != null; GridConfigurationAdapter adapter = new GridConfigurationAdapter(cfg); // Set Glassfish logger. if (cfg.getGridLogger() == null) adapter.setGridLogger(log); Grid grid = G.start(adapter, springCtx); // Test if grid is not null - started properly. if (grid != null) gridNames.add(grid.name()); } } catch (GridException e) { // Stop started grids only. for (String name : gridNames) G.stop(name, true); throw new ServerLifecycleException("Failed to start GridGain.", e); } }
From source file:org.gridgain.grid.loaders.websphere.GridWebsphereLoader.java
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override//w w w . j av a 2 s .com public void initialize(Properties properties) throws Exception { GridLogger log = new GridJclLogger(LogFactory.getLog("GridGain")); cfgFile = properties.getProperty(cfgFilePathParam); if (cfgFile == null) throw new IllegalArgumentException("Failed to read property: " + cfgFilePathParam); String workMgrName = properties.getProperty(workMgrParam); URL cfgUrl = U.resolveGridGainUrl(cfgFile); if (cfgUrl == null) throw new IllegalArgumentException("Failed to find Spring configuration file (path provided should be " + "either absolute, relative to GRIDGAIN_HOME, or relative to META-INF folder): " + cfgFile); GenericApplicationContext springCtx; try { springCtx = new GenericApplicationContext(); XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(springCtx); xmlReader.loadBeanDefinitions(new UrlResource(cfgUrl)); springCtx.refresh(); } catch (BeansException e) { throw new IllegalArgumentException( "Failed to instantiate Spring XML application context: " + e.getMessage(), e); } Map cfgMap; try { // Note: Spring is not generics-friendly. cfgMap = springCtx.getBeansOfType(GridConfiguration.class); } catch (BeansException e) { throw new IllegalArgumentException( "Failed to instantiate bean [type=" + GridConfiguration.class + ", err=" + e.getMessage() + ']', e); } if (cfgMap == null) throw new IllegalArgumentException("Failed to find a single grid factory configuration in: " + cfgUrl); if (cfgMap.isEmpty()) throw new IllegalArgumentException("Can't find grid factory configuration in: " + cfgUrl); try { ExecutorService execSvc = null; MBeanServer mbeanSrvr = null; for (GridConfiguration cfg : (Collection<GridConfiguration>) cfgMap.values()) { assert cfg != null; GridConfigurationAdapter adapter = new GridConfigurationAdapter(cfg); // Set WebSphere logger. if (cfg.getGridLogger() == null) adapter.setGridLogger(log); if (cfg.getExecutorService() == null) { if (execSvc == null) { if (workMgrName != null) execSvc = new GridThreadWorkManagerExecutor(workMgrName); else { // Obtain/create singleton. J2EEServiceManager j2eeMgr = J2EEServiceManager.getSelf(); // Start it if was not started before. j2eeMgr.start(); // Create new configuration. CommonJWorkManagerConfiguration workMgrCfg = j2eeMgr .createCommonJWorkManagerConfiguration(); workMgrCfg.setName("GridGain"); workMgrCfg.setJNDIName("wm/gridgain"); // Set worker. execSvc = new GridThreadWorkManagerExecutor(j2eeMgr.getCommonJWorkManager(workMgrCfg)); } } adapter.setExecutorService(execSvc); } if (cfg.getMBeanServer() == null) { if (mbeanSrvr == null) mbeanSrvr = AdminServiceFactory.getMBeanFactory().getMBeanServer(); adapter.setMBeanServer(mbeanSrvr); } Grid grid = G.start(adapter, springCtx); // Test if grid is not null - started properly. if (grid != null) gridNames.add(grid.name()); } } catch (GridException e) { // Stop started grids only. for (String name : gridNames) G.stop(name, true); throw new IllegalArgumentException("Failed to start GridGain.", e); } }
From source file:uk.co.modularaudio.util.springhibernate.SpringHibernateContextHelper.java
@Override public void postRefreshDoThings(final GenericApplicationContext appContext, final BeanInstantiationListAsPostProcessor beanInstatiationList) throws DatastoreException { final Map<String, ComponentWithHibernatePersistence> components = appContext .getBeansOfType(ComponentWithHibernatePersistence.class); final List<HibernatePersistedBeanDefinition> hpbdList = new ArrayList<HibernatePersistedBeanDefinition>(); for (final Iterator<ComponentWithHibernatePersistence> iter = components.values().iterator(); iter .hasNext();) {/* w w w .j av a2 s . c om*/ final ComponentWithHibernatePersistence component = iter.next(); final List<HibernatePersistedBeanDefinition> hbmDefinitions = component .listHibernatePersistedBeanDefinitions(); hpbdList.addAll(hbmDefinitions); } // Now make sure the hibernate session service is configured try { final HibernateSessionFactory hssi = appContext.getBean(HibernateSessionFactory.class); hssi.configureHibernate(hpbdList); } catch (final Exception e) { final String msg = "Exception caught initialising hibernate persisted components: " + e.toString(); log.error(msg, e); throw new DatastoreException(msg, e); } }