/** * Returns an instance which uses the the specified selector, as the name of the * definition file(s). In the case of a name with a Spring "classpath*:" prefix, * or with no prefix, which is treated the same, the current thread's context class * loader's {@code getResources} method will be called with this value to get * all resources having that name. These resources will then be combined to form a * definition. In the case where the name uses a Spring "classpath:" prefix, or * a standard URL prefix, then only one resource file will be loaded as the * definition. * @param selector the location of the resource(s) which will be read and * combined to form the definition for the BeanFactoryLocator instance. * Any such files must form a valid ApplicationContext definition. * @return the corresponding BeanFactoryLocator instance * @throws BeansException in case of factory loading failure */ public static BeanFactoryLocator getInstance(String selector) throws BeansException { String resourceLocation = selector; if (resourceLocation == null) { resourceLocation = DEFAULT_RESOURCE_LOCATION; } // For backwards compatibility, we prepend "classpath*:" to the selector name if there // is no other prefix (i.e. "classpath*:", "classpath:", or some URL prefix). if (!ResourcePatternUtils.isUrl(resourceLocation)) { resourceLocation = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + resourceLocation; } synchronized (instances) { if (logger.isTraceEnabled()) { logger.trace("ContextSingletonBeanFactoryLocator.getInstance(): instances.hashCode=" + instances.hashCode() + ", instances=" + instances); } BeanFactoryLocator bfl = instances.get(resourceLocation); if (bfl == null) { bfl = new ContextSingletonBeanFactoryLocator(resourceLocation); instances.put(resourceLocation, bfl); } return bfl; } }
/** * This test can run multiple times, but due to static keyed lookup of the locators, * 2nd and subsequent calls will actually get back same locator instance. This is not * really an issue, since the contained bean factories will still be loaded and released. */ @Override @Test public void testGetInstance() { // Try with and without 'classpath*:' prefix, and with 'classpath:' prefix. BeanFactoryLocator facLoc = ContextSingletonBeanFactoryLocator.getInstance( ClassUtils.addResourcePathToPackagePath(CLASS, CONTEXT)); getInstanceTest1(facLoc); facLoc = ContextSingletonBeanFactoryLocator.getInstance( "classpath*:" + ClassUtils.addResourcePathToPackagePath(CLASS, CONTEXT)); getInstanceTest2(facLoc); // This will actually get another locator instance, as the key is the resource name. facLoc = ContextSingletonBeanFactoryLocator.getInstance( "classpath:" + ClassUtils.addResourcePathToPackagePath(CLASS, CONTEXT)); getInstanceTest3(facLoc); }
/** * Returns an instance which uses the specified selector, as the name of the * definition file(s). In the case of a name with a Spring "classpath*:" prefix, * or with no prefix, which is treated the same, the current thread's context class * loader's {@code getResources} method will be called with this value to get * all resources having that name. These resources will then be combined to form a * definition. In the case where the name uses a Spring "classpath:" prefix, or * a standard URL prefix, then only one resource file will be loaded as the * definition. * @param selector the location of the resource(s) which will be read and * combined to form the definition for the BeanFactoryLocator instance. * Any such files must form a valid ApplicationContext definition. * @return the corresponding BeanFactoryLocator instance * @throws BeansException in case of factory loading failure */ public static BeanFactoryLocator getInstance(String selector) throws BeansException { String resourceLocation = selector; if (resourceLocation == null) { resourceLocation = DEFAULT_RESOURCE_LOCATION; } // For backwards compatibility, we prepend "classpath*:" to the selector name if there // is no other prefix (i.e. "classpath*:", "classpath:", or some URL prefix). if (!ResourcePatternUtils.isUrl(resourceLocation)) { resourceLocation = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + resourceLocation; } synchronized (instances) { if (logger.isTraceEnabled()) { logger.trace("ContextSingletonBeanFactoryLocator.getInstance(): instances.hashCode=" + instances.hashCode() + ", instances=" + instances); } BeanFactoryLocator bfl = instances.get(resourceLocation); if (bfl == null) { bfl = new ContextSingletonBeanFactoryLocator(resourceLocation); instances.put(resourceLocation, bfl); } return bfl; } }
/** * Gets the applicationContext. * * @return the applicationContext. */ protected BeanFactory getApplicationContext() { try { if (applicationContext == null) { synchronized (LOCK) { if (applicationContext == null) { BeanFactoryLocator bfl = SingletonBeanFactoryLocator.getInstance(getBeanFactorySelector()); BeanFactoryReference bf = bfl.useBeanFactory(getApplicationContextKey()); BeanFactory tempApplicationContext = bf.getFactory(); if (tempApplicationContext instanceof ConfigurableApplicationContext) { ((ConfigurableApplicationContext) tempApplicationContext).registerShutdownHook(); } applicationContext = tempApplicationContext; } } } return applicationContext; } catch (RuntimeException ex) { LOG.error("{} context could not be instantiated.", getApplicationContextKey(), ex); throw ex; } }
public ComponentLoaderImpl () throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException { checkInit(); BeanFactoryLocator factoryLocator = ContextSingletonBeanFactoryLocator .getInstance(String.format("classpath:%s", CoreGlobalProperty.BEAN_REF_CONTEXT_CONF)); BeanFactoryReference ref = factoryLocator.useBeanFactory("parentContext"); ioc = ref.getFactory(); }
private BeanFactory getBeanFactory(ServletContextEvent event) { String beanFactorySelector = getBeanFactorySelector(event); String applicationContextKey = getApplicationContextKey(event); BeanFactoryLocator bfl = SingletonBeanFactoryLocator .getInstance(beanFactorySelector); BeanFactoryReference bf = bfl.useBeanFactory(applicationContextKey); BeanFactory beanFactory = bf.getFactory(); if (beanFactory instanceof ConfigurableApplicationContext) { ((ConfigurableApplicationContext) beanFactory).registerShutdownHook(); } return beanFactory; }
/** * Construct */ protected JBPMSpringActionHandler() { // The following implementation is derived from Spring Modules v0.4 BeanFactoryLocator factoryLocator = new JbpmFactoryLocator(); BeanFactoryReference factory = factoryLocator.useBeanFactory(null); initialiseHandler(factory.getFactory()); }
/** * Gets the JBPM Engine instance * * @return JBPM Engine */ private JBPMEngine getJBPMEngine() { if (jbpmEngine == null) { BeanFactoryLocator factoryLocator = new JbpmFactoryLocator(); BeanFactoryReference factory = factoryLocator.useBeanFactory(null); if (jbpmEngineName == null) jbpmEngineName = "jbpm_engine"; jbpmEngine = (JBPMEngine) factory.getFactory().getBean(jbpmEngineName); if (jbpmEngine == null) { throw new WorkflowException( "Failed to retrieve JBPMEngine component"); } } return jbpmEngine; }
/** * Constructor */ public AlfrescoJobExecutor() { BeanFactoryLocator factoryLocator = new JbpmFactoryLocator(); BeanFactoryReference factory = factoryLocator.useBeanFactory(null); transactionService = (TransactionService)factory.getFactory().getBean(ServiceRegistry.TRANSACTION_SERVICE.getLocalName()); jobLockService = (JobLockService)factory.getFactory().getBean(ServiceRegistry.JOB_LOCK_SERVICE.getLocalName()); jbpmConfiguration = (JbpmConfiguration)factory.getFactory().getBean("jbpm_configuration"); }
/** * Construct */ protected JBPMSpringAssignmentHandler() { // The following implementation is derived from Spring Modules v0.4 BeanFactoryLocator factoryLocator = new JbpmFactoryLocator(); BeanFactoryReference factory = factoryLocator.useBeanFactory(null); initialiseHandler(factory.getFactory()); }
/** * This method obtains a global ThreadLocal class independent of the classloader (JVM-scope singleton). * The easiest solution is to use System.getProperties().get/put, but this solution violate * Properties contract and have other drawbacks. * <p>Current solution relies on the idea behind * {@link org.springframework.beans.factory.access.SingletonBeanFactoryLocator}. See also bug #4648 * * @return Global ThreadLocal (JVM-scope singleton). */ @SuppressWarnings("unchecked") private static ThreadLocal<BeanFactory> getGlobalThreadLocal() { BeanFactoryLocator locator = SingletonBeanFactoryLocator.getInstance(BEAN_FACTORY_XML_PATH); BeanFactoryReference ref = locator.useBeanFactory(FACTORY_BEAN_NAME); StaticApplicationContext ctx = (StaticApplicationContext) ref.getFactory(); if (!ctx.containsBean(THREAD_LOCAL_BEAN_NAME)) { ctx.registerSingleton(THREAD_LOCAL_BEAN_NAME, ThreadLocal.class); } return (ThreadLocal) ctx.getBean(THREAD_LOCAL_BEAN_NAME); }
@Test public void getInstance() { BeanFactoryLocator bf = DefaultLocatorFactory.getInstance(); BeanFactoryLocator bf2 = DefaultLocatorFactory.getInstance(); assertTrue(bf.equals(bf2)); }
@Test public void getInstanceString() { BeanFactoryLocator bf = DefaultLocatorFactory.getInstance("my-bean-refs.xml"); BeanFactoryLocator bf2 = DefaultLocatorFactory.getInstance("my-bean-refs.xml"); assertTrue(bf.equals(bf2)); }
/** * Template method with default implementation (which may be overridden by a * subclass), to load or obtain an ApplicationContext instance which will be * used as the parent context of the root WebApplicationContext. If the * return value from the method is null, no parent context is set. * <p>The main reason to load a parent context here is to allow multiple root * web application contexts to all be children of a shared EAR context, or * alternately to also share the same parent context that is visible to * EJBs. For pure web applications, there is usually no need to worry about * having a parent context to the root web application context. * <p>The default implementation uses * {@link org.springframework.context.access.ContextSingletonBeanFactoryLocator}, * configured via {@link #LOCATOR_FACTORY_SELECTOR_PARAM} and * {@link #LOCATOR_FACTORY_KEY_PARAM}, to load a parent context * which will be shared by all other users of ContextsingletonBeanFactoryLocator * which also use the same configuration parameters. * @param servletContext current servlet context * @return the parent application context, or {@code null} if none * @see org.springframework.context.access.ContextSingletonBeanFactoryLocator */ protected ApplicationContext loadParentContext(ServletContext servletContext) { ApplicationContext parentContext = null; String locatorFactorySelector = servletContext.getInitParameter(LOCATOR_FACTORY_SELECTOR_PARAM); String parentContextKey = servletContext.getInitParameter(LOCATOR_FACTORY_KEY_PARAM); if (parentContextKey != null) { // locatorFactorySelector may be null, indicating the default "classpath*:beanRefContext.xml" BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator.getInstance(locatorFactorySelector); Log logger = LogFactory.getLog(ContextLoader.class); if (logger.isDebugEnabled()) { logger.debug("Getting parent context definition: using parent context key of '" + parentContextKey + "' with BeanFactoryLocator"); } this.parentContextRef = locator.useBeanFactory(parentContextKey); parentContext = (ApplicationContext) this.parentContextRef.getFactory(); } return parentContext; }
private ApplicationContext getApplicationContext() { BeanFactoryLocator bfl = SingletonBeanFactoryLocator .getInstance(beanFactorySelector); BeanFactoryReference bf = bfl.useBeanFactory(applicationContextKey); return (ApplicationContext) bf.getFactory(); }
private BeanFactoryReference getBeanFactoryReference() { BeanFactoryLocator bfl = SingletonBeanFactoryLocator.getInstance(beanFactorySelector); BeanFactoryReference bf = bfl.useBeanFactory(applicationContextKey); return bf; }
private static BeanFactory getBeanFactory() { BeanFactoryLocator bfl = SingletonBeanFactoryLocator.getInstance(CONTEXT_PATH); bfl.useBeanFactory("org.sakuli.app.root"); return getBeanFactoryReference().getFactory(); }
private static BeanFactoryReference getBeanFactoryReference() { BeanFactoryLocator bfl = SingletonBeanFactoryLocator.getInstance(CONTEXT_PATH); return bfl.useBeanFactory("org.sakuli.app.root"); }
public void testGetInstance() { BeanFactoryLocator bf = DefaultLocatorFactory.getInstance(); BeanFactoryLocator bf2 = DefaultLocatorFactory.getInstance(); assertTrue(bf.equals(bf2)); }
public void testGetInstanceString() { BeanFactoryLocator bf = DefaultLocatorFactory.getInstance("my-bean-refs.xml"); BeanFactoryLocator bf2 = DefaultLocatorFactory.getInstance("my-bean-refs.xml"); assertTrue(bf.equals(bf2)); }
/** * <p>onStart</p> */ protected void onStart() { //TODO REMOVE EXAMPLE IMPORTER CODE // ThreadCategory.setPrefix(ImporterService.NAME); // m_status = Fiber.STARTING; // ThreadCategory.getInstance().debug("SPRING: thread.classLoader="+Thread.currentThread().getContextClassLoader()); // BeanFactoryLocator bfl = DefaultLocatorFactory.getInstance(); // BeanFactoryReference bf = bfl.useBeanFactory("daoContext"); // ApplicationContext daoContext = (ApplicationContext) bf.getFactory(); // m_context = new ClassPathXmlApplicationContext(new String[] { "/org/opennms/netmgt/importer/importer-context.xml" }, daoContext); // ThreadCategory.getInstance().debug("SPRING: context.classLoader="+m_context.getClassLoader()); // m_status = Fiber.RUNNING; ThreadCategory.getInstance().debug("SPRING: thread.classLoader="+Thread.currentThread().getContextClassLoader()); // finds the already instantiated OpenNMS daoContext BeanFactoryLocator bfl = DefaultLocatorFactory.getInstance(); BeanFactoryReference bf = bfl.useBeanFactory("daoContext"); ApplicationContext daoContext = (ApplicationContext) bf.getFactory(); // this chooses if we expect AlarmMonitor to run in seperate j2ee container ( Jboss ) or in local // OpenNMS spring container String qosdj2ee=System.getProperty("qosd.usej2ee"); ThreadCategory.getInstance().info("QoSD System Property qosd.usej2ee=" + qosdj2ee ); if ("true".equals(qosdj2ee)){ ThreadCategory.getInstance().debug("QoSD using /org/openoss/opennms/spring/qosd/qosd-j2ee-context.xml"); m_context = new ClassPathXmlApplicationContext(new String[] { "/org/openoss/opennms/spring/qosd/qosd-j2ee-context.xml" },daoContext); } else { ThreadCategory.getInstance().debug("QoSD using /org/openoss/opennms/spring/qosd/qosd-spring-context.xml"); m_context = new ClassPathXmlApplicationContext(new String[] { "/org/openoss/opennms/spring/qosd/qosd-spring-context.xml" },daoContext); } ThreadCategory.getInstance().debug("SPRING: context.classLoader="+m_context.getClassLoader()); getQoSD().init(); getQoSD().start(); // TODO remove original code // ThreadCategory.setPrefix(QoSD.NAME); // m_status = Fiber.STARTING; // ThreadCategory.getInstance().debug("SPRING: thread.classLoader="+Thread.currentThread().getContextClassLoader()); // // this chooses if we expect AlarmMonitor to run in seperate j2ee container ( Jboss ) or in local // // OpenNMS spring container // String qosdj2ee=System.getProperty("qosd.usej2ee"); // ThreadCategory.getInstance().info("QoSD System Property qosd.usej2ee=" + qosdj2ee ); // if ("true".equals(qosdj2ee)){ // ThreadCategory.getInstance().debug("QoSD using /org/openoss/opennms/spring/qosd/qosd-j2ee-context.xml"); // m_context = new ClassPathXmlApplicationContext(new String[] { "/org/openoss/opennms/spring/qosd/qosd-j2ee-context.xml" }); // } // else { // ThreadCategory.getInstance().debug("QoSD using /org/openoss/opennms/spring/qosd/qosd-spring-context.xml"); // m_context = new ClassPathXmlApplicationContext(new String[] { "/org/openoss/opennms/spring/qosd/qosd-spring-context.xml" }); // } // ThreadCategory.getInstance().debug("SPRING: context.classLoader="+m_context.getClassLoader()); // getQoSD().init(); // getQoSD().start(); // m_status = Fiber.RUNNING; }
/** * <p>onStart</p> */ protected void onStart() { // TODO REMOVE EXAMPLE IMPORTER CODE // ThreadCategory.setPrefix(ImporterService.NAME); // m_status = Fiber.STARTING; // ThreadCategory.getInstance().debug("SPRING: thread.classLoader="+Thread.currentThread().getContextClassLoader()); // BeanFactoryLocator bfl = DefaultLocatorFactory.getInstance(); // BeanFactoryReference bf = bfl.useBeanFactory("daoContext"); // ApplicationContext daoContext = (ApplicationContext) bf.getFactory(); // m_context = new ClassPathXmlApplicationContext(new String[] { "/org/opennms/netmgt/importer/importer-context.xml" }, daoContext); // ThreadCategory.getInstance().debug("SPRING: context.classLoader="+m_context.getClassLoader()); // m_status = Fiber.RUNNING; ThreadCategory.getInstance().debug("SPRING: thread.classLoader="+Thread.currentThread().getContextClassLoader()); // finds the already instantiated OpenNMS daoContext BeanFactoryLocator bfl = DefaultLocatorFactory.getInstance(); BeanFactoryReference bf = bfl.useBeanFactory("daoContext"); ApplicationContext daoContext = (ApplicationContext) bf.getFactory(); ThreadCategory.getInstance().debug("QoSDrx using /org/openoss/opennms/spring/qosdrx/qosdrx-spring-context.xml"); m_context = new ClassPathXmlApplicationContext(new String[] { "/org/openoss/opennms/spring/qosdrx/qosdrx-spring-context.xml" }, daoContext); ThreadCategory.getInstance().debug("SPRING: context.classLoader="+m_context.getClassLoader()); getQoSDrx().init(); getQoSDrx().start(); //TODO remove old code // ThreadCategory.setPrefix(QoSDrx.NAME); // m_status = Fiber.STARTING; // ThreadCategory.getInstance().debug("SPRING: thread.classLoader="+Thread.currentThread().getContextClassLoader()); // // ThreadCategory.getInstance().debug("QoSDrx using /org/openoss/opennms/spring/qosdrx/qosdrx-spring-context.xml"); // m_context = new ClassPathXmlApplicationContext(new String[] { "/org/openoss/opennms/spring/qosdrx/qosdrx-spring-context.xml" }); // ThreadCategory.getInstance().debug("SPRING: context.classLoader="+m_context.getClassLoader()); // // getQoSDrx().init(); // getQoSDrx().start(); // // // m_status = Fiber.RUNNING; }
/** * Initialization. * * Retrieves the Spring context for the correlator. */ public void init() { final BeanFactoryLocator bfl = DefaultLocatorFactory.getInstance(); final BeanFactoryReference bf = bfl.useBeanFactory("correlatorContext"); m_correlator = (org.opennms.netmgt.correlation.Correlator) bf.getFactory().getBean("correlator"); }
/** * <p>init</p> */ public void init() { final BeanFactoryLocator bfl = DefaultLocatorFactory.getInstance(); final BeanFactoryReference bf = bfl.useBeanFactory("correlatorContext"); m_correlator = (org.opennms.netmgt.correlation.Correlator) bf.getFactory().getBean("correlator"); }
/** * Template method with default implementation (which may be overridden by a * subclass), to load or obtain an ApplicationContext instance which will be * used as the parent context of the root WebApplicationContext. If the * return value from the method is null, no parent context is set. * <p>The main reason to load a parent context here is to allow multiple root * web application contexts to all be children of a shared EAR context, or * alternately to also share the same parent context that is visible to * EJBs. For pure web applications, there is usually no need to worry about * having a parent context to the root web application context. * <p>The default implementation uses * {@link org.springframework.context.access.ContextSingletonBeanFactoryLocator}, * configured via {@link #LOCATOR_FACTORY_SELECTOR_PARAM} and * {@link #LOCATOR_FACTORY_KEY_PARAM}, to load a parent context * which will be shared by all other users of ContextsingletonBeanFactoryLocator * which also use the same configuration parameters. * @param servletContext current servlet context * @return the parent application context, or <code>null</code> if none * @throws BeansException if the context couldn't be initialized * @see org.springframework.context.access.ContextSingletonBeanFactoryLocator */ protected ApplicationContext loadParentContext(ServletContext servletContext) throws BeansException { ApplicationContext parentContext = null; String locatorFactorySelector = servletContext.getInitParameter(LOCATOR_FACTORY_SELECTOR_PARAM); String parentContextKey = servletContext.getInitParameter(LOCATOR_FACTORY_KEY_PARAM); if (parentContextKey != null) { // locatorFactorySelector may be null, indicating the default "classpath*:beanRefContext.xml" BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator.getInstance(locatorFactorySelector); if (logger.isDebugEnabled()) { logger.debug("Getting parent context definition: using parent context key of '" + parentContextKey + "' with BeanFactoryLocator"); } this.parentContextRef = locator.useBeanFactory(parentContextKey); parentContext = (ApplicationContext) this.parentContextRef.getFactory(); } return parentContext; }
/** * Returns an instance which uses the default "classpath*:beanRefContext.xml", as * the name of the definition file(s). All resources returned by the current * thread's context class loader's {@code getResources} method with this * name will be combined to create a definition, which is just a BeanFactory. * @return the corresponding BeanFactoryLocator instance * @throws BeansException in case of factory loading failure */ public static BeanFactoryLocator getInstance() throws BeansException { return getInstance(null); }
/** * Return an instance object implementing BeanFactoryLocator. This will normally * be a singleton instance of the specific ContextSingletonBeanFactoryLocator class, * using the default resource selector. */ public static BeanFactoryLocator getInstance() throws FatalBeanException { return ContextSingletonBeanFactoryLocator.getInstance(); }
/** * Return an instance object implementing BeanFactoryLocator. This will normally * be a singleton instance of the specific ContextSingletonBeanFactoryLocator class, * using the specified resource selector. * @param selector a selector variable which provides a hint to the factory as to * which instance to return. */ public static BeanFactoryLocator getInstance(String selector) throws FatalBeanException { return ContextSingletonBeanFactoryLocator.getInstance(selector); }
/** * Determine the BeanFactoryLocator to obtain the BeanFactoryReference from. * <p>The default implementation exposes Spring's default * {@link ContextSingletonBeanFactoryLocator}. * @param target the target bean to autowire * @return the BeanFactoryLocator to use (never {@code null}) * @see org.springframework.context.access.ContextSingletonBeanFactoryLocator#getInstance() */ protected BeanFactoryLocator getBeanFactoryLocator(Object target) { return ContextSingletonBeanFactoryLocator.getInstance(); }