Пример #1
0
 public boolean containsKey(final Object o) {
   if (o instanceof Class) {
     return mutablePicoContainer.getComponent((Class<?>) o) != null;
   } else {
     return mutablePicoContainer.getComponent(o) != null;
   }
 }
Пример #2
0
 public Object get(final Object o) {
   if (o instanceof Class) {
     return mutablePicoContainer.getComponent((Class<?>) o);
   } else {
     return mutablePicoContainer.getComponent(o);
   }
 }
  @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));
  }
Пример #4
0
  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");
  }
 @SuppressWarnings({"unchecked"})
 public final void testInstancesAreNotSharedBetweenContainers() {
   final MutablePicoContainer picoA = new DefaultPicoContainer();
   final MutablePicoContainer picoB = new DefaultPicoContainer();
   picoA.addAdapter(
       new ThreadLocalizing.ThreadLocalized(
           new ConstructorInjection.ConstructorInjector(List.class, ArrayList.class, null)));
   picoB.addAdapter(
       new ThreadLocalizing.ThreadLocalized(
           new ConstructorInjection.ConstructorInjector(List.class, ArrayList.class, null)));
   final List<String> hello1 = picoA.getComponent(List.class);
   final List hello2 = picoA.getComponent(List.class);
   hello1.add("foo");
   assertEquals(hello1, hello2);
   final List hello3 = picoB.getComponent(List.class);
   assertEquals(0, hello3.size());
 }
  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);
  }
 public <T> T getComponent(
     final Class<T> componentType, final Class<? extends Annotation> binding) {
   if (logger.isDebugEnabled()) {
     logger.debug(
         "Attempting to load component instance with "
             + "type"
             + ": "
             + componentType.getName()
             + " for container "
             + delegate);
   }
   return delegate.getComponent(componentType, binding);
 }
  @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);
  }
 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);
  }
  /** 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 Object getComponent(final Object componentKeyOrType, final Type into) {
    if (logger.isDebugEnabled()) {
      logger.debug(
          "Attempting to load component instance with "
              + (componentKeyOrType instanceof Class ? "type" : "key")
              + ": "
              + (componentKeyOrType instanceof Class
                  ? ((Class) componentKeyOrType).getName()
                  : componentKeyOrType)
              + " for container "
              + delegate);
    }
    Object result = delegate.getComponent(componentKeyOrType, into);
    if (result == null) {
      onKeyOrTypeDoesNotExistInContainer(componentKeyOrType, logger);
    }

    return result;
  }
Пример #14
0
  public Set entrySet() {
    Set<Entry> set = new HashSet<Entry>();
    Collection<ComponentAdapter<?>> adapters = mutablePicoContainer.getComponentAdapters();
    for (ComponentAdapter<?> adapter : adapters) {
      final Object key = adapter.getComponentKey();
      final Object component = mutablePicoContainer.getComponent(key);
      set.add(
          new Entry() {
            public Object getKey() {
              return key;
            }

            public Object getValue() {
              return component;
            }

            public Object setValue(final Object value) {
              throw new UnsupportedOperationException("Cannot set addComponent");
            }
          });
    }
    return Collections.unmodifiableSet(set);
  }
Пример #15
0
  public final void start(ServletContext context) {
    ComponentRegistry componentRegistry = getComponentRegistry();
    registerBundledComponents(componentRegistry);

    this.picoContainer.addComponent(context);

    Scanner scanner = new ReflectionsScanner(context);

    this.picoContainer.addComponent(scanner);

    registerAnnotatedComponents(scanner, componentRegistry);

    getComponentRegistry().init();

    StereotypedComponentRegistrar componentRegistrar =
        picoContainer.getComponent(StereotypedComponentRegistrar.class);
    componentRegistrar.registerFrom(scanner);

    registerCustomComponents(picoContainer, scanner);

    picoContainer.start();

    registerCacheComponents();
  }
Пример #16
0
 @Override
 public <T> T getComponent(Class<T> tClass) {
   return container.getComponent(tClass);
 }
Пример #17
0
 /**
  * Injects dependencies into the annotated fields of the given object. This method should be used
  * for objects that were created by Eclipse, which have a different life cycle than the Saros
  * plug-in.
  */
 @Override
 public synchronized void initComponent(Object toInjectInto) {
   ChildContainer dummyContainer = container.getComponent(ChildContainer.class);
   dummyContainer.reinject(toInjectInto);
   container.removeChildContainer(dummyContainer);
 }