public void testApplicationContainerExists() {
    MutablePicoContainer appContainer = new DefaultPicoContainer();
    appContainer.registerComponentInstance(TestService.class, service);

    requestMock
        .expects(once())
        .method("getAttribute")
        .with(eq(KeyConstants.ACTIONS_CONTAINER))
        .will(returnValue(null));
    requestMock
        .expects(once())
        .method("getAttribute")
        .with(eq(KeyConstants.REQUEST_CONTAINER))
        .will(returnValue(null));
    sessionMock
        .expects(once())
        .method("getAttribute")
        .with(eq(KeyConstants.SESSION_CONTAINER))
        .will(returnValue(null));
    servletContextMock
        .expects(once())
        .method("getAttribute")
        .with(eq(KeyConstants.APPLICATION_CONTAINER))
        .will(returnValue(appContainer));
    requestMock
        .expects(once())
        .method("setAttribute")
        .with(eq(KeyConstants.ACTIONS_CONTAINER), isA(MutablePicoContainer.class));

    TestAction action = (TestAction) actionFactory.getAction(request, mapping1, servlet);
    assertNotNull(action);
    assertSame(service, action.getService());
  }
  public void testActionContainerCreatedOnlyOncePerRequest() {
    MutablePicoContainer requestContainer = new DefaultPicoContainer();
    requestContainer.registerComponentImplementation(TestService.class);
    MutablePicoContainer actionsContainer = new DefaultPicoContainer(requestContainer);

    requestMock
        .expects(once())
        .method("getAttribute")
        .with(eq(KeyConstants.ACTIONS_CONTAINER))
        .will(returnValue(actionsContainer));
    requestMock
        .expects(once())
        .method("getAttribute")
        .with(eq(KeyConstants.ACTIONS_CONTAINER))
        .will(returnValue(actionsContainer));
    requestMock
        .expects(once())
        .method("setAttribute")
        .with(eq(KeyConstants.ACTIONS_CONTAINER), isA(MutablePicoContainer.class));
    requestMock
        .expects(once())
        .method("getAttribute")
        .with(eq(KeyConstants.REQUEST_CONTAINER))
        .will(returnValue(requestContainer));
    requestMock
        .expects(once())
        .method("getAttribute")
        .with(eq(KeyConstants.ACTIONS_CONTAINER))
        .will(returnValue(null));

    actionFactory.getAction(request, mapping1, servlet);
    actionFactory.getAction(request, mapping1, servlet);
    actionFactory.getAction(request, mapping1, servlet);
  }
  public NotificationTestCaseSetup() throws Exception {
    ifrServerSetup =
        new IFRServerSetup(
            TestUtils.testHome() + "/src/test/idl/TypedNotification.idl", null, null);

    ORBSetUp();

    serverORB = ORB.init(new String[0], orbProps);

    POAHelper.narrow(serverORB.resolve_initial_references("RootPOA")).the_POAManager().activate();

    container_ = PicoContainerFactory.createRootContainer((org.jacorb.orb.ORB) serverORB);
    container_.unregisterComponent(Repository.class);
    container_.registerComponent(
        new AbstractComponentAdapter(Repository.class, Repository.class) {

          public Object getComponentInstance(PicoContainer picocontainer)
              throws PicoInitializationException, PicoIntrospectionException {
            try {
              return getRepository();
            } catch (Exception e) {
              throw new RuntimeException(e);
            }
          }

          public void verify(PicoContainer picocontainer) throws PicoIntrospectionException {}
        });

    testUtils_ = new NotificationTestUtils(getServerORB());

    POAHelper.narrow(orb.resolve_initial_references("RootPOA")).the_POAManager().activate();
  }
예제 #4
0
  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));
  }
예제 #5
0
 /** Registers HgMockVcsHelper as the AbstractVcsHelper. */
 protected HgMockVcsHelper registerMockVcsHelper() {
   final String key = "com.intellij.openapi.vcs.AbstractVcsHelper";
   final MutablePicoContainer picoContainer = (MutablePicoContainer) myProject.getPicoContainer();
   picoContainer.unregisterComponent(key);
   picoContainer.registerComponentImplementation(key, HgMockVcsHelper.class);
   return (HgMockVcsHelper) AbstractVcsHelper.getInstance(myProject);
 }
예제 #6
0
 public boolean containsKey(final Object o) {
   if (o instanceof Class) {
     return mutablePicoContainer.getComponent((Class<?>) o) != null;
   } else {
     return mutablePicoContainer.getComponent(o) != null;
   }
 }
예제 #7
0
 public Object get(final Object o) {
   if (o instanceof Class) {
     return mutablePicoContainer.getComponent((Class<?>) o);
   } else {
     return mutablePicoContainer.getComponent(o);
   }
 }
 public void unregisterExtension(String pluginName, Element extensionElement) {
   String epName = extractEPName(extensionElement);
   if (!myExtensionElement2extension.containsKey(extensionElement)) {
     XMLOutputter xmlOutputter = new XMLOutputter();
     Format format =
         Format.getCompactFormat().setIndent("  ").setTextMode(Format.TextMode.NORMALIZE);
     xmlOutputter.setFormat(format);
     StringWriter stringWriter = new StringWriter();
     try {
       xmlOutputter.output(extensionElement, stringWriter);
     } catch (IOException e) {
       throw new RuntimeException(e);
     }
     myLogger.warn(stringWriter.toString());
     throw new IllegalArgumentException(
         "Trying to unregister extension element that was never registered");
   }
   ExtensionComponentAdapter adapter = myExtensionElement2extension.remove(extensionElement);
   if (adapter == null) return;
   if (getExtensionPoint(epName).unregisterComponentAdapter(adapter)) {
     MutablePicoContainer pluginContainer = internalGetPluginContainer();
     pluginContainer.unregisterComponent(adapter.getComponentKey());
     if (pluginContainer.getComponentAdapters().isEmpty()) {
       disposePluginContainer(pluginName);
     }
   }
 }
  public void testGetActionWhenActionsContainerAlreadyExists() {
    MutablePicoContainer requestContainer = new DefaultPicoContainer();
    requestContainer.registerComponentInstance(TestService.class, service);
    MutablePicoContainer actionsContainer = new DefaultPicoContainer(requestContainer);

    requestMock
        .stubs()
        .method("getAttribute")
        .with(eq(KeyConstants.ACTIONS_CONTAINER))
        .will(returnValue(actionsContainer));

    StrutsTestAction action1 =
        (StrutsTestAction) actionFactory.getAction(request, mapping1, servlet);
    StrutsTestAction action2 =
        (StrutsTestAction) actionFactory.getAction(request, mapping2, servlet);
    TestAction action3 = (TestAction) actionFactory.getAction(request, mapping1, servlet);
    TestAction action4 = (TestAction) actionFactory.getAction(request, mapping2, servlet);

    assertNotNull(action1);
    assertNotNull(action2);
    assertNotSame(action1, action2);
    assertSame(action1, action3);
    assertSame(action2, action4);

    assertSame(action1, actionsContainer.getComponentInstance("/myPath1"));
    assertSame(action2, actionsContainer.getComponentInstance("/myPath2"));

    assertSame(service, action1.getService());
    assertSame(service, action2.getService());

    assertNotNull(action1.getServlet());
    assertNotNull(action2.getServlet());
    assertSame(servlet, action1.getServlet());
    assertSame(servlet, action2.getServlet());
  }
 @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"));
 }
  protected AbstractEventChannel newEventChannel() {
    final MutablePicoContainer _container = newContainerForChannel();

    _container.registerComponentImplementation(
        AbstractEventChannel.class, TypedEventChannelImpl.class);

    return (AbstractEventChannel) _container.getComponentInstance(AbstractEventChannel.class);
  }
예제 #12
0
 @SuppressWarnings("unchecked")
 public static <T> T overrideService(
     @NotNull Project project, Class<? super T> serviceInterface, Class<T> serviceImplementation) {
   String key = serviceInterface.getName();
   MutablePicoContainer picoContainer = (MutablePicoContainer) project.getPicoContainer();
   picoContainer.unregisterComponent(key);
   picoContainer.registerComponentImplementation(key, serviceImplementation);
   return (T) ServiceManager.getService(project, serviceInterface);
 }
  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());
  }
예제 #14
0
  /**
   * Invoked by {@link #initialise} to fill the container.
   *
   * @param container the underlying container
   * @throws ContainerException if initialisation fails
   * @throws PicoException for any PicoContainer error
   */
  @Override
  protected void fillContainer(MutablePicoContainer container) {
    super.fillContainer(container);
    addComponent(InstallDataConfiguratorWithRules.class);
    addComponent(Log.class, Mockito.mock(Log.class));

    container.addAdapter(new ProviderAdapter(new GUIInstallDataMockProvider()));
    container.addAdapter(new ProviderAdapter(new IconsProvider()));
  }
  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());
  }
  protected void setUp() throws Exception {
    super.setUp();

    decimal = createAtribute(GML.NAMESPACE, "decimal", XS.STRING, ".");
    ts = createAtribute(GML.NAMESPACE, "ts", XS.STRING, null);
    cs = createAtribute(GML.NAMESPACE, "cs", XS.STRING, null);
    coordinates = createElement(GML.NAMESPACE, "myCoordinates", GML.COORDTYPE, null);
    container = new DefaultPicoContainer();
    container.registerComponentInstance(CoordinateArraySequenceFactory.instance());
    container.registerComponentImplementation(GMLCoordinatesTypeBinding.class);
  }
  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);
    }
  }
예제 #18
0
 @SuppressWarnings({"rawtypes", "unchecked"})
 public Publisher publish(Object key) {
   if (key == null) {
     throw new NullPointerException("key");
   }
   final ComponentAdapter ca = child.getComponentAdapter(key);
   if (ca == null) {
     throw new IllegalArgumentException(key + " does not appear to exist in container " + child);
   }
   parent.addAdapter(new Publishing<Object>(child, ca));
   return this;
 }
  @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());
  }
예제 #20
0
 public <T> T provideForRequest(RequestInfo request, Execution<T> execution) {
   PicoBasedContainer container = null;
   try {
     container = getComponentRegistry().provideRequestContainer(request);
     container.getContainer().start();
     return execution.insideRequest(container);
   } finally {
     if (container != null) {
       MutablePicoContainer picoContainer = container.getContainer();
       picoContainer.stop();
       picoContainer.dispose();
     }
   }
 }
  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);
  }
  @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));
  }
예제 #23
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");
  }
  /**
   * {@inheritDoc}
   *
   * @param componentKey
   * @return
   * @see org.picocontainer.MutablePicoContainer#removeComponent(java.lang.Object)
   */
  public ComponentAdapter removeComponent(final Object componentKey) {
    if (logger.isDebugEnabled()) {
      logger.debug("Unregistering component " + componentKey + " from container " + delegate);
    }

    return delegate.removeComponent(componentKey);
  }
  /**
   * {@inheritDoc}
   *
   * @see org.picocontainer.Startable#start()
   */
  public void start() {
    if (logger.isInfoEnabled()) {
      logger.info("Starting Container " + delegate);
    }

    delegate.start();
  }
  public MutablePicoContainer addConfig(final String name, final Object val) {
    if (logger.isDebugEnabled()) {
      logger.debug("Registering config: " + name);
    }

    return delegate.addConfig(name, val);
  }
  /**
   * {@inheritDoc}
   *
   * @param componentAdapter
   * @return
   * @see org.picocontainer.MutablePicoContainer#addAdapter(org.picocontainer.ComponentAdapter)
   */
  public MutablePicoContainer addAdapter(final ComponentAdapter componentAdapter) {
    if (logger.isDebugEnabled()) {
      logger.debug("Registering component adapter " + componentAdapter);
    }

    return delegate.addAdapter(componentAdapter);
  }
  /**
   * {@inheritDoc}
   *
   * @return
   * @see org.picocontainer.PicoContainer#getParent()
   */
  public PicoContainer getParent() {
    if (logger.isDebugEnabled()) {
      logger.debug("Retrieving the parent for container " + delegate);
    }

    return delegate.getParent();
  }
  @Test
  public void testContainerCanBeBuiltWithComponentImplementation() {
    X.reset();
    Reader script =
        new StringReader(
            "A = org.picocontainer.script.testmodel.A\n"
                + "container {\n"
                + "    component(A)\n"
                + "}");

    MutablePicoContainer pico = (MutablePicoContainer) buildContainer(script, null, ASSEMBLY_SCOPE);
    // LifecyleContainerBuilder starts the container
    pico.dispose();

    assertEquals("Should match the expression", "<AA>!A", X.componentRecorder);
  }
  @Test
  public void testInstantiateBasicComponentInDeeperTree() {
    X.reset();
    Reader script =
        new StringReader(
            "A = org.picocontainer.script.testmodel.A\n"
                + "container {\n"
                + "  container {\n"
                + "    component(A)\n"
                + "  }\n"
                + "}");

    MutablePicoContainer pico = (MutablePicoContainer) buildContainer(script, null, ASSEMBLY_SCOPE);
    pico.dispose();
    assertEquals("Should match the expression", "<AA>!A", X.componentRecorder);
  }