PicoBasedContainer provideRequestContainer(RequestInfo request) { HttpSession session = request.getRequest().getSession(); MutablePicoContainer sessionScope = (MutablePicoContainer) session.getAttribute(CONTAINER_SESSION_KEY); if (sessionScope == null) { sessionScope = createSessionContainer(session); } if (logger.isDebugEnabled()) { logger.debug("Request components are " + requestScoped); } MutablePicoContainer requestContainer = new DefaultPicoContainer( new Caching(), new JavaEE5LifecycleStrategy(new NullComponentMonitor()), sessionScope); for (Map.Entry<Class<?>, Class<?>> entry : requestScoped.entrySet()) { requestContainer.addComponent(entry.getKey(), entry.getValue()); } for (Map.Entry<Class<?>, Class<?>> entry : prototypeScoped.entrySet()) { requestContainer.as(Characteristics.NO_CACHE).addComponent(entry.getKey(), entry.getValue()); } requestContainer .addComponent(request) .addComponent(request.getRequest()) .addComponent(request.getResponse()); registerComponentFactories(requestContainer, componentFactoryRegistry.getRequestMap()); return new PicoBasedContainer(requestContainer, this.appContainer.getComponent(Router.class)); }
public void testMBeanInfoIsDeterminedIfKeyIsString() { final ComponentAdapter componentAdapter = pico.addComponent("JUnit", Person.class).getComponentAdapter("JUnit"); pico.addComponent("JUnitMBeanInfo", Person.createMBeanInfo()); final MBeanInfo info = mBeanProvider.provide(pico, componentAdapter); assertNotNull(info); assertEquals(Person.createMBeanInfo().getDescription(), info.getDescription()); }
public void testMBeanInfoIsDeterminedIfKeyIsManagementInterface() { final ComponentAdapter componentAdapter = pico.addComponent(PersonMBean.class, Person.class) .getComponentAdapter(PersonMBean.class, null); pico.addComponent(PersonMBean.class.getName() + "Info", Person.createMBeanInfo()); final MBeanInfo info = mBeanProvider.provide(pico, componentAdapter); assertNotNull(info); assertEquals(Person.createMBeanInfo().getDescription(), info.getDescription()); }
private void startSystemWithXmlSerializer() { MutablePicoContainer container = new PicoBuilder().withLifecycle().withCaching().build(); container.addComponent(directory); container.addComponent(JavaPersistenceManager.class); container.addComponent(XmlPersistenceManager.class); container.addComponent(PersistenceConverter.class); container.start(); persistence = container.getComponent(DestinationPersistenceManager.class); }
@Test public void canInstantiateExplicitCollectionWithComponentParameter() { MutablePicoContainer pico = new DefaultPicoContainer(new Caching()); List<String> searchPath = new ArrayList<String>(); searchPath.add("a"); searchPath.add("b"); pico.addComponent("searchPath", searchPath); pico.addComponent(Searcher.class, Searcher.class, new ComponentParameter("searchPath")); assertNotNull(pico.getComponent(Searcher.class)); assertNotNull(pico.getComponent(Searcher.class).getSearchPath()); }
private void startSystemWithXmlSerializerWillFailAfterWritingTheFirstEvent() { MutablePicoContainer container = new PicoBuilder().withLifecycle().withCaching().build(); container.addComponent(directory); container.addComponent(JavaPersistenceManager.class); container.addComponent(XmlSerializerWillFailAfterWritingTheFirstEvent.class); container.addComponent(PersistenceConverter.class); try { container.start(); fail("Must throw exception"); } catch (Exception e) { assertEquals("Premeditate error writing event", e.getCause().getMessage()); } persistence = container.getComponent(DestinationPersistenceManager.class); }
@Test public void testCreateWithParentContainer() { MutablePicoContainer parent = new DefaultPicoContainer(); parent.addComponent("key", "value"); AspectablePicoContainerFactory containerFactory = new DynaopAspectablePicoContainerFactory(); PicoContainer child = containerFactory.createContainer(parent); assertEquals("value", child.getComponent("key")); }
/** TODO Revisit this for Pico 3. */ @Ignore @Test public void canInstantiateAutowiredCollectionThatAreDefinedImplicitly() { MutablePicoContainer pico = new DefaultPicoContainer(new Caching()); List<String> searchPath = new ArrayList<String>(); searchPath.add("a"); searchPath.add("b"); List<Integer> conflictingList = new ArrayList<Integer>(); conflictingList.add(1); conflictingList.add(2); pico.addComponent("conflict", conflictingList); pico.addComponent("searchPath", searchPath).addComponent(Searcher.class); assertNotNull(pico.getComponent(Searcher.class)); assertNotNull(pico.getComponent(Searcher.class).getSearchPath()); }
public void testComponentRegistrationMismatch() throws PicoCompositionException { MutablePicoContainer pico = new DefaultPicoContainer(); try { pico.addComponent(List.class, SimpleTouchable.class); } catch (ClassCastException e) { // not worded in message assertTrue(e.getMessage().indexOf(List.class.getName()) > 0); assertTrue(e.getMessage().indexOf(SimpleTouchable.class.getName()) == 0); } }
private PicoContainer createPicoContainer() { MutablePicoContainer container = new DefaultPicoContainer(new Caching().wrap(new ConstructorInjection())); container.addComponent(TradingService.class); container.addComponent(TraderSteps.class); container.addComponent(BeforeAfterSteps.class); container.addComponent(AndSteps.class); container.addComponent(CalendarSteps.class); container.addComponent(PriorityMatchingSteps.class); container.addComponent(SandpitSteps.class); container.addComponent(SearchSteps.class); return container; }
/** * {@inheritDoc} * * @param implOrInstance * @return * @see org.picocontainer.MutablePicoContainer#addComponent(java.lang.Object) */ public MutablePicoContainer addComponent(final Object implOrInstance) { if (logger.isDebugEnabled()) { logger.debug( "Registering component impl or instance " + implOrInstance + "(class: " + ((implOrInstance != null) ? implOrInstance.getClass().getName() : " null ")); } return delegate.addComponent(implOrInstance); }
public void FAILING_testBuildContainerWithParentAttributesPropagatesComponentFactory() { DefaultClassLoadingPicoContainer parent = new DefaultClassLoadingPicoContainer(new SetterInjection()); Reader script = new StringReader("container(:parent => $parent)\n"); MutablePicoContainer pico = (MutablePicoContainer) buildContainer(script, parent, ASSEMBLY_SCOPE); // Should be able to get instance that was registered in the parent container ComponentAdapter<?> componentAdapter = pico.addComponent(String.class).getComponentAdapter(String.class, (NameBinding) null); assertTrue( "ComponentAdapter should be originally defined by parent", componentAdapter instanceof SetterInjector); }
private MutablePicoContainer createSessionContainer(HttpSession session) { MutablePicoContainer sessionContainer = new DefaultPicoContainer( new Caching(), new JavaEE5LifecycleStrategy(new NullComponentMonitor()), this.appContainer); sessionContainer.addComponent(HttpSession.class, session); session.setAttribute(CONTAINER_SESSION_KEY, sessionContainer); if (logger.isDebugEnabled()) { logger.debug("Session components are " + sessionScoped); } for (Map.Entry<Class<?>, Class<?>> entry : sessionScoped.entrySet()) { sessionContainer.addComponent(entry.getKey(), entry.getValue()); } registerComponentFactories(sessionContainer, componentFactoryRegistry.getSessionMap()); sessionContainer.start(); return sessionContainer; }
/** * Fills the container. * * @param container the underlying container * @throws ContainerException if initialisation fails, or the container has already been * initialised */ @Override protected void fillContainer(MutablePicoContainer container) { addComponent(Properties.class); addComponent(DefaultVariables.class); addComponent(CompilerContainer.class, this); addComponent(CliAnalyzer.class); addComponent(CmdlinePackagerListener.class); addComponent(Compiler.class); addComponent(ResourceFinder.class); addComponent(CompilerConfig.class); addComponent(ConditionContainer.class, ConditionContainer.class); addComponent(AssertionHelper.class); addComponent(PropertyManager.class); addComponent(VariableSubstitutor.class, VariableSubstitutorImpl.class); addComponent(CompilerHelper.class); container.addComponent( RulesEngine.class, RulesEngineImpl.class, new ComponentParameter(ConditionContainer.class), new ComponentParameter(Platform.class)); addComponent(MergeManager.class, MergeManagerImpl.class); container.addComponent( ObjectFactory.class, DefaultObjectFactory.class, new ComponentParameter(CompilerContainer.class)); container.addComponent(PlatformModelMatcher.class); addComponent(Platforms.class); new ResolverContainerFiller().fillContainer(this); container .addAdapter(new ProviderAdapter(new XmlCompilerHelperProvider())) .addAdapter(new ProviderAdapter(new JarOutputStreamProvider())) .addAdapter(new ProviderAdapter(new CompressedOutputStreamProvider())) .addAdapter(new ProviderAdapter(new PackCompressorProvider())) .addAdapter(new ProviderAdapter(new PlatformProvider())); }
@Test public void canInstantiateAutowiredCollectionThatAreDefinedExplicitlyAnotherWay() { MutablePicoContainer pico = new DefaultPicoContainer(new Caching()); List<String> searchPath = new StringArrayList(); searchPath.add("a"); searchPath.add("b"); pico.addComponent(searchPath).addComponent(Searcher.class); assertNotNull(pico.getComponent(Searcher.class)); List<String> list = pico.getComponent(Searcher.class).getSearchPath(); assertNotNull(list); assertEquals("a", list.get(0)); assertEquals("b", list.get(1)); }
private void init() { log.info("creating Saros runtime context..."); /* * All singletons which exist for the whole plug-in life-cycle are * managed by PicoContainer for us. */ PicoBuilder picoBuilder = new PicoBuilder( new CompositeInjection(new ConstructorInjection(), new AnnotatedFieldInjection())) .withCaching() .withLifecycle(); /* * If given, the dotMonitor is used to capture an architecture diagram * of the application */ if (dotMonitor != null) picoBuilder = picoBuilder.withMonitor(dotMonitor); // Initialize our dependency injection container container = picoBuilder.build(); // Add Adapter which creates ChildContainers container .as(Characteristics.NO_CACHE) .addAdapter(new ProviderAdapter(new ChildContainerProvider(this.container))); for (ISarosContextFactory factory : factories) { factory.createComponents(container); } container.addComponent(ISarosContext.class, this); /* * Create a reinjector to allow platform specific stuff to reinject * itself into the context. */ reinjector = new Reinjector(container); initAccountStore(container.getComponent(XMPPAccountStore.class)); installPacketExtensionProviders(); XMPPUtils.setDefaultConnectionService(container.getComponent(XMPPConnectionService.class)); log.info("successfully created Saros runtime context"); }
public void testNonSetterMethodInjectionWithAlternativeAnnotation() { MutablePicoContainer pico = new DefaultPicoContainer(); pico.addAdapter( new AnnotatedMethodInjector( AnotherAnnotatedBurp.class, AnotherAnnotatedBurp.class, Parameter.DEFAULT, new NullComponentMonitor(), new NullLifecycleStrategy(), AlternativeInject.class, false)); pico.addComponent(Wind.class, new Wind()); AnotherAnnotatedBurp burp = pico.getComponent(AnotherAnnotatedBurp.class); assertNotNull(burp); assertNotNull(burp.wind); }
public void testSetterMethodInjectionToContrastWithThatBelow() { MutablePicoContainer pico = new DefaultPicoContainer(); pico.addAdapter( new SetterInjector( SetterBurp.class, SetterBurp.class, Parameter.DEFAULT, new NullComponentMonitor(), new NullLifecycleStrategy(), "set", false)); pico.addComponent(Wind.class, new Wind()); SetterBurp burp = pico.getComponent(SetterBurp.class); assertNotNull(burp); assertNotNull(burp.wind); }
public void testMBeanInfoIsDeterminedIfKeyIsType() { final PersonMBean person = new OtherPerson(); final Mock mockComponentAdapter = mock(ComponentAdapter.class); mockComponentAdapter.stubs().method("getComponentKey").will(returnValue(Person.class)); mockComponentAdapter .stubs() .method("getComponentImplementation") .will(returnValue(person.getClass())); pico.addAdapter((ComponentAdapter) mockComponentAdapter.proxy()); pico.addComponent(Person.class.getName() + "MBeanInfo", Person.createMBeanInfo()); final MBeanInfo info = mBeanProvider.provide(pico, (ComponentAdapter) mockComponentAdapter.proxy()); assertNotNull(info); assertEquals(Person.createMBeanInfo().getDescription(), info.getDescription()); }
/** * {@inheritDoc} * * @param componentKey * @param componentImplementationOrInstance * @param parameters * @return */ public MutablePicoContainer addComponent( final Object componentKey, final Object componentImplementationOrInstance, final Parameter... parameters) { if (logger.isDebugEnabled()) { logger.debug( "Registering component " + (componentImplementationOrInstance instanceof Class ? "implementation" : "instance") + " with key " + componentKey + " and implementation " + (componentImplementationOrInstance instanceof Class ? ((Class) componentImplementationOrInstance).getCanonicalName() : componentKey.getClass()) + " using parameters " + parameters); } return delegate.addComponent(componentKey, componentImplementationOrInstance, parameters); }
/** * Adds the object to Saros' container, and injects dependencies into the annotated fields of the * given object. It should only be used for objects that were created by Eclipse, which have the * same life cycle as the Saros plug-in, e.g. the popup menu actions. */ public synchronized void reinject(Object toInjectInto) { try { // Remove the component if an instance of it was already registered Class<?> clazz = toInjectInto.getClass(); ComponentAdapter<?> removed = container.removeComponent(clazz); if (removed != null) { log.warn(clazz.getName() + " added more than once!", new StackTrace()); } // Add the given instance to the container container.addComponent(clazz, toInjectInto); /* * Ask PicoContainer to inject into the component via fields * annotated with @Inject */ reinjector.reinject(clazz, new AnnotatedFieldInjection()); } catch (PicoCompositionException e) { log.error("Internal error in reinjection:", e); } }
public void addToPico(MutablePicoContainer pico) { pico.addComponent(hasName() ? getName() : Double.class, d); }
public Object put(final Object o, final Object o1) { Object object = remove(o); mutablePicoContainer.addComponent(o, o1); return object; }
public void addComponent(Object o, Object o1, Parameter... parameters) { container.addComponent(o, o1, parameters); }