List of usage examples for org.springframework.context ApplicationContext getBeansOfType
<T> Map<String, T> getBeansOfType(@Nullable Class<T> type) throws BeansException;
From source file:org.openmrs.api.context.ServiceContext.java
/** * Private method which returns all components registered in a Spring applicationContext of a * given type This method recurses through each parent ApplicationContext * * @param context - The applicationContext to check * @param type - The type of component to retrieve * @return all components registered in a Spring applicationContext of a given type *///from www. java 2 s . c o m @SuppressWarnings("unchecked") private <T> Map<String, T> getRegisteredComponents(ApplicationContext context, Class<T> type) { Map<String, T> components = new HashMap<String, T>(); Map registeredComponents = context.getBeansOfType(type); if (log.isTraceEnabled()) { log.trace("getRegisteredComponents(" + context + ", " + type + ") = " + registeredComponents); } if (registeredComponents != null) { components.putAll(registeredComponents); } if (context.getParent() != null) { components.putAll(getRegisteredComponents(context.getParent(), type)); } return components; }
From source file:org.sipfoundry.sipxconfig.registrar.RegistrarConfigurationTest.java
@Test public void testConfigWithNoPlugins() throws Exception { RegistrarConfiguration config = new RegistrarConfiguration(); ApplicationContext context = EasyMock.createMock(ApplicationContext.class); context.getBeansOfType(RegistrarConfigurationPlugin.class); EasyMock.expectLastCall().andReturn(null).anyTimes(); EasyMock.replay(context);// ww w . ja v a 2s. c o m config.setApplicationContext(context); StringWriter actual = new StringWriter(); RegistrarSettings settings = new RegistrarSettings(); settings.setModelFilesContext(TestHelper.getModelFilesContext()); //test allow unbound config value replication settings.setSettingTypedValue("registrar-config/SIP_REDIRECT.140-FALLBACK.ALLOW_UNBOUND", true); Domain domain = new Domain("example.org"); domain.setSipRealm("grapefruit"); Address imApi = new Address(ImManager.XMLRPC_ADDRESS, "im.example.org", 100); Address presenceApi = new Address(PresenceServer.HTTP_ADDRESS, "presence.example.org", 101); Address park = new Address(ParkOrbitContext.SIP_TCP_PORT, "park.example.org", 102); Address proxy = new Address(ProxyManager.TCP_ADDRESS, "proxy.example.org", 103); Location location = TestHelper.createDefaultLocation(); config.write(actual, settings, domain, location, proxy, imApi, presenceApi, park, new FeatureManagerImpl()); String expected = IOUtils.toString(getClass().getResourceAsStream("expected-registrar-config")); assertEquals(expected, actual.toString()); }
From source file:org.sipfoundry.sipxconfig.registrar.RegistrarConfigurationTest.java
@Test public void testConfigWithPlugins() throws Exception { RegistrarConfiguration config = new RegistrarConfiguration(); ApplicationContext context = EasyMock.createMock(ApplicationContext.class); context.getBeansOfType(RegistrarConfigurationPlugin.class); RegistrarConfigurationPlugin regPlugin = new RegistrarConfigurationPlugin(); Map<String, String> plugins = new HashMap<String, String>(); plugins.put("SIP_REGISTRAR_HOOK_LIBRARY.TEST_PLUGIN", "/usr/lib/testPlugin.so"); plugins.put("SIP_REGISTRAR_HOOK_LIBRARY.TEST1_PLUGIN", "/usr/lib/test1Plugin.so"); regPlugin.setRegistrarPlugins(plugins); Map<String, RegistrarConfigurationPlugin> beans = new HashMap<String, RegistrarConfigurationPlugin>(); beans.put("test", regPlugin); EasyMock.expectLastCall().andReturn(beans).anyTimes(); EasyMock.replay(context);/*from w w w . j av a2 s.c om*/ config.setApplicationContext(context); StringWriter actual = new StringWriter(); RegistrarSettings settings = new RegistrarSettings(); settings.setModelFilesContext(TestHelper.getModelFilesContext()); Domain domain = new Domain("example.org"); domain.setSipRealm("grapefruit"); Address imApi = new Address(ImManager.XMLRPC_ADDRESS, "im.example.org", 100); Address presenceApi = new Address(PresenceServer.HTTP_ADDRESS, "presence.example.org", 101); Address park = new Address(ParkOrbitContext.SIP_TCP_PORT, "park.example.org", 102); Address proxy = new Address(ProxyManager.TCP_ADDRESS, "proxy.example.org", 103); Location location = TestHelper.createDefaultLocation(); config.write(actual, settings, domain, location, proxy, imApi, presenceApi, park, new FeatureManagerImpl()); String expected = IOUtils .toString(getClass().getResourceAsStream("expected-registrar-config-with-plugins")); assertEquals(expected, actual.toString()); }
From source file:org.sipfoundry.sipxconfig.registrar.RegistrarConfigurationTest.java
@Test public void testConfigWithPluginsFeatureNotEnabled() throws Exception { RegistrarConfiguration config = new RegistrarConfiguration(); ApplicationContext context = EasyMock.createMock(ApplicationContext.class); context.getBeansOfType(RegistrarConfigurationPlugin.class); RegistrarConfigurationPlugin regPlugin = new RegistrarConfigurationPlugin(); Map<String, String> plugins = new HashMap<String, String>(); plugins.put("SIP_REGISTRAR_HOOK_LIBRARY.TEST_PLUGIN", "/usr/lib/testPlugin.so"); plugins.put("SIP_REGISTRAR_HOOK_LIBRARY.TEST1_PLUGIN", "/usr/lib/test1Plugin.so"); regPlugin.setRegistrarPlugins(plugins); regPlugin.setFeatureId(FEATURE_ID);/*from w ww.j a v a 2 s . c o m*/ Map<String, RegistrarConfigurationPlugin> beans = new HashMap<String, RegistrarConfigurationPlugin>(); beans.put("test", regPlugin); EasyMock.expectLastCall().andReturn(beans).anyTimes(); EasyMock.replay(context); FeatureManager fm = EasyMock.createMock(FeatureManager.class); fm.isFeatureEnabled(new LocationFeature(FEATURE_ID)); EasyMock.expectLastCall().andReturn(false); fm.isFeatureEnabled(new GlobalFeature(FEATURE_ID)); EasyMock.expectLastCall().andReturn(false); EasyMock.replay(fm); RegistrarSettings settings = new RegistrarSettings(); settings.setModelFilesContext(TestHelper.getModelFilesContext()); //test allow unbound config value replication settings.setSettingTypedValue("registrar-config/SIP_REDIRECT.140-FALLBACK.ALLOW_UNBOUND", true); config.setApplicationContext(context); StringWriter actual = new StringWriter(); Domain domain = new Domain("example.org"); domain.setSipRealm("grapefruit"); Address imApi = new Address(ImManager.XMLRPC_ADDRESS, "im.example.org", 100); Address presenceApi = new Address(PresenceServer.HTTP_ADDRESS, "presence.example.org", 101); Address park = new Address(ParkOrbitContext.SIP_TCP_PORT, "park.example.org", 102); Address proxy = new Address(ProxyManager.TCP_ADDRESS, "proxy.example.org", 103); Location location = TestHelper.createDefaultLocation(); config.write(actual, settings, domain, location, proxy, imApi, presenceApi, park, fm); String expected = IOUtils.toString(getClass().getResourceAsStream("expected-registrar-config")); assertEquals(expected, actual.toString()); }
From source file:org.sipfoundry.sipxconfig.registrar.RegistrarConfigurationTest.java
@Test public void testConfigWithPluginsFeatureEnabled() throws Exception { RegistrarConfiguration config = new RegistrarConfiguration(); ApplicationContext context = EasyMock.createMock(ApplicationContext.class); context.getBeansOfType(RegistrarConfigurationPlugin.class); RegistrarConfigurationPlugin regPlugin = new RegistrarConfigurationPlugin(); Map<String, String> plugins = new HashMap<String, String>(); plugins.put("SIP_REGISTRAR_HOOK_LIBRARY.TEST_PLUGIN", "/usr/lib/testPlugin.so"); plugins.put("SIP_REGISTRAR_HOOK_LIBRARY.TEST1_PLUGIN", "/usr/lib/test1Plugin.so"); regPlugin.setRegistrarPlugins(plugins); regPlugin.setFeatureId(FEATURE_ID);//from ww w . ja v a 2s . c o m Map<String, RegistrarConfigurationPlugin> beans = new HashMap<String, RegistrarConfigurationPlugin>(); beans.put("test", regPlugin); EasyMock.expectLastCall().andReturn(beans).anyTimes(); EasyMock.replay(context); FeatureManager fm = EasyMock.createMock(FeatureManager.class); fm.isFeatureEnabled(new LocationFeature(FEATURE_ID)); EasyMock.expectLastCall().andReturn(true); fm.isFeatureEnabled(new GlobalFeature(FEATURE_ID)); EasyMock.expectLastCall().andReturn(false); EasyMock.replay(fm); config.setApplicationContext(context); StringWriter actual = new StringWriter(); RegistrarSettings settings = new RegistrarSettings(); settings.setModelFilesContext(TestHelper.getModelFilesContext()); Domain domain = new Domain("example.org"); domain.setSipRealm("grapefruit"); Address imApi = new Address(ImManager.XMLRPC_ADDRESS, "im.example.org", 100); Address presenceApi = new Address(PresenceServer.HTTP_ADDRESS, "presence.example.org", 101); Address park = new Address(ParkOrbitContext.SIP_TCP_PORT, "park.example.org", 102); Address proxy = new Address(ProxyManager.TCP_ADDRESS, "proxy.example.org", 103); Location location = TestHelper.createDefaultLocation(); config.write(actual, settings, domain, location, proxy, imApi, presenceApi, park, fm); String expected = IOUtils .toString(getClass().getResourceAsStream("expected-registrar-config-with-plugins")); assertEquals(expected, actual.toString()); }
From source file:org.slc.sli.api.security.context.ContextValidator.java
@Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { validators = new ArrayList<IContextValidator>(); validators.addAll(applicationContext.getBeansOfType(IContextValidator.class).values()); }
From source file:org.springframework.amqp.rabbit.core.RabbitAdminTests.java
@Test public void testTemporaryLogs() throws Exception { SingleConnectionFactory connectionFactory = new SingleConnectionFactory(); connectionFactory.setHost("localhost"); RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory); try {//from w w w .j a v a 2s .c o m ApplicationContext ctx = mock(ApplicationContext.class); Map<String, Queue> queues = new HashMap<String, Queue>(); queues.put("nonDurQ", new Queue("testq.nonDur", false, false, false)); queues.put("adQ", new Queue("testq.ad", true, false, true)); queues.put("exclQ", new Queue("testq.excl", true, true, false)); queues.put("allQ", new Queue("testq.all", false, true, true)); when(ctx.getBeansOfType(Queue.class)).thenReturn(queues); Map<String, Exchange> exchanges = new HashMap<String, Exchange>(); exchanges.put("nonDurEx", new DirectExchange("testex.nonDur", false, false)); exchanges.put("adEx", new DirectExchange("testex.ad", true, true)); exchanges.put("allEx", new DirectExchange("testex.all", false, true)); when(ctx.getBeansOfType(Exchange.class)).thenReturn(exchanges); rabbitAdmin.setApplicationContext(ctx); rabbitAdmin.afterPropertiesSet(); Log logger = spy(TestUtils.getPropertyValue(rabbitAdmin, "logger", Log.class)); doReturn(true).when(logger).isInfoEnabled(); doAnswer(new DoesNothing()).when(logger).info(anyString()); new DirectFieldAccessor(rabbitAdmin).setPropertyValue("logger", logger); connectionFactory.createConnection().close(); // force declarations ArgumentCaptor<String> log = ArgumentCaptor.forClass(String.class); verify(logger, times(7)).info(log.capture()); List<String> logs = log.getAllValues(); Collections.sort(logs); assertThat(logs.get(0), Matchers.containsString("(testex.ad) durable:true, auto-delete:true")); assertThat(logs.get(1), Matchers.containsString("(testex.all) durable:false, auto-delete:true")); assertThat(logs.get(2), Matchers.containsString("(testex.nonDur) durable:false, auto-delete:false")); assertThat(logs.get(3), Matchers.containsString("(testq.ad) durable:true, auto-delete:true, exclusive:false")); assertThat(logs.get(4), Matchers.containsString("(testq.all) durable:false, auto-delete:true, exclusive:true")); assertThat(logs.get(5), Matchers.containsString("(testq.excl) durable:true, auto-delete:false, exclusive:true")); assertThat(logs.get(6), Matchers.containsString("(testq.nonDur) durable:false, auto-delete:false, exclusive:false")); } finally { cleanQueuesAndExchanges(rabbitAdmin); connectionFactory.destroy(); } }
From source file:org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.java
/** * Use {@link RabbitAdmin#initialize()} to redeclare everything if necessary. * Since auto deletion of a queue can cause upstream elements * (bindings, exchanges) to be deleted too, everything needs to be redeclared if * a queue is missing./*from w w w . j av a 2 s . c om*/ * Declaration is idempotent so, aside from some network chatter, there is no issue, * and we only will do it if we detect our queue is gone. * <p> * In general it makes sense only for the 'auto-delete' or 'expired' queues, * but with the server TTL policy we don't have ability to determine 'expiration' * option for the queue. * <p> * Starting with version 1.6, if * {@link #setMismatchedQueuesFatal(boolean) mismatchedQueuesFatal} is true, * the declarations are always attempted during restart so the listener will * fail with a fatal error if mismatches occur. */ protected synchronized void redeclareElementsIfNecessary() { RabbitAdmin rabbitAdmin = getRabbitAdmin(); if (rabbitAdmin == null || !isAutoDeclare()) { return; } try { ApplicationContext applicationContext = this.getApplicationContext(); if (applicationContext != null) { Set<String> queueNames = this.getQueueNamesAsSet(); Map<String, Queue> queueBeans = applicationContext.getBeansOfType(Queue.class); for (Entry<String, Queue> entry : queueBeans.entrySet()) { Queue queue = entry.getValue(); if (isMismatchedQueuesFatal() || (queueNames.contains(queue.getName()) && rabbitAdmin.getQueueProperties(queue.getName()) == null)) { if (logger.isDebugEnabled()) { logger.debug("Redeclaring context exchanges, queues, bindings."); } rabbitAdmin.initialize(); return; } } } } catch (Exception e) { if (RabbitUtils.isMismatchedQueueArgs(e)) { throw new FatalListenerStartupException("Mismatched queues", e); } logger.error("Failed to check/redeclare auto-delete queue(s).", e); } }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.java
/** * Use {@link RabbitAdmin#initialize()} to redeclare everything if necessary. * Since auto deletion of a queue can cause upstream elements * (bindings, exchanges) to be deleted too, everything needs to be redeclared if * a queue is missing./*from www .j ava 2s. c om*/ * Declaration is idempotent so, aside from some network chatter, there is no issue, * and we only will do it if we detect our queue is gone. * <p> * In general it makes sense only for the 'auto-delete' or 'expired' queues, * but with the server TTL policy we don't have ability to determine 'expiration' * option for the queue. * <p> * Starting with version 1.6, if * {@link #setMismatchedQueuesFatal(boolean) mismatchedQueuesFatal} is true, * the declarations are always attempted during restart so the listener will * fail with a fatal error if mismatches occur. */ private synchronized void redeclareElementsIfNecessary() { if (this.rabbitAdmin == null) { return; } try { ApplicationContext applicationContext = this.getApplicationContext(); if (applicationContext != null) { Set<String> queueNames = this.getQueueNamesAsSet(); Map<String, Queue> queueBeans = applicationContext.getBeansOfType(Queue.class); for (Entry<String, Queue> entry : queueBeans.entrySet()) { Queue queue = entry.getValue(); if (this.mismatchedQueuesFatal || (queueNames.contains(queue.getName()) && this.rabbitAdmin.getQueueProperties(queue.getName()) == null)) { if (logger.isDebugEnabled()) { logger.debug("Redeclaring context exchanges, queues, bindings."); } this.rabbitAdmin.initialize(); return; } } } } catch (Exception e) { if (RabbitUtils.isMismatchedQueueArgs(e)) { throw new FatalListenerStartupException("Mismatched queues", e); } logger.error("Failed to check/redeclare auto-delete queue(s).", e); } }
From source file:org.springframework.batch.core.configuration.support.DefaultJobLoader.java
/** * Returns all the {@link Step} instances defined by the specified {@link StepLocator}. * <br>//from w w w.j a v a 2 s. c o m * The specified <tt>jobApplicationContext</tt> is used to collect additional steps that * are not exposed by the step locator * * @param stepLocator the given step locator * @param jobApplicationContext the application context of the job * @return all the {@link Step} defined by the given step locator and context * @see StepLocator */ private Collection<Step> getSteps(final StepLocator stepLocator, final ApplicationContext jobApplicationContext) { final Collection<String> stepNames = stepLocator.getStepNames(); final Collection<Step> result = new ArrayList<Step>(); for (String stepName : stepNames) { result.add(stepLocator.getStep(stepName)); } // Because some steps are referenced by name, we need to look in the context to see if there // are more Step instances defined. Right now they are registered as being available in the // context of the job but we have no idea if they are linked to that Job or not. final Map<String, Step> allSteps = jobApplicationContext.getBeansOfType(Step.class); for (Map.Entry<String, Step> entry : allSteps.entrySet()) { if (!stepNames.contains(entry.getKey())) { result.add(entry.getValue()); } } return result; }