public void testExceptionHandling() {
   final ComponentAdapter componentAdapter =
       new ThreadLocalizing.ThreadLocalized(
           new ConstructorInjection.ConstructorInjector(
               TargetInvocationExceptionTester.class, ThrowingComponent.class, null));
   final TargetInvocationExceptionTester tester =
       (TargetInvocationExceptionTester)
           componentAdapter.getComponentInstance(null, ComponentAdapter.NOTHING.class);
   try {
     tester.throwsCheckedException();
     fail("ClassNotFoundException expected");
   } catch (final ClassNotFoundException e) {
     assertEquals("junit", e.getMessage());
   }
   try {
     tester.throwsRuntimeException();
     fail("RuntimeException expected");
   } catch (final RuntimeException e) {
     assertEquals("junit", e.getMessage());
   }
   try {
     tester.throwsError();
     fail("Error expected");
   } catch (final Error e) {
     assertEquals("junit", e.getMessage());
   }
 }
 /**
  * in case component adapter is JNDIExposed, poke it gently and it will create component and
  * register it to JNDI if not already done.
  */
 @Override
 public void visitComponentAdapter(ComponentAdapter componentAdapter) {
   super.visitComponentAdapter(componentAdapter);
   if (componentAdapter instanceof JNDIExposed) {
     componentAdapter.getComponentInstance(container);
   }
 }
  /**
   * Test usage from multiple threads.
   *
   * @throws InterruptedException if interrupted
   */
  public final void testInstancesUsedFromMultipleThreads() throws InterruptedException {
    final Set<Touchable> set = Collections.synchronizedSet(new HashSet<Touchable>());
    final List<Touchable> list = Collections.synchronizedList(new ArrayList<Touchable>());
    final ComponentAdapter componentAdapter =
        new ThreadLocalizing.ThreadLocalized(
            new ConstructorInjection.ConstructorInjector(
                Touchable.class, SimpleTouchable.class, null));
    final Touchable touchable =
        (Touchable) componentAdapter.getComponentInstance(null, ComponentAdapter.NOTHING.class);

    final Thread[] threads = {
      new Thread(new Runner(touchable, list, set), "junit-1"),
      new Thread(new Runner(touchable, list, set), "junit-2"),
      new Thread(new Runner(touchable, list, set), "junit-3"),
    };
    for (int i = threads.length; i-- > 0; ) {
      threads[i].start();
    }
    Thread.sleep(300);
    for (int i = threads.length; i-- > 0; ) {
      synchronized (threads[i]) {
        threads[i].notifyAll();
      }
    }
    Thread.sleep(300);
    for (int i = threads.length; i-- > 0; ) {
      threads[i].interrupt();
    }
    Thread.sleep(300);
    assertEquals(6, list.size());
    assertEquals(3, set.size());
  }
 /** Test ComponentAdapter using simple keys. */
 public final void testSimpleKeys() {
   final ComponentAdapter componentAdapter =
       new ThreadLocalizing.ThreadLocalized<ArrayList>(
           new ConstructorInjection.ConstructorInjector("List", ArrayList.class, null));
   final List hello =
       (List) componentAdapter.getComponentInstance(null, ComponentAdapter.NOTHING.class);
   assertNotNull(hello);
 }
 public void testThreadLocalInstancesEqual() throws Exception {
   final ComponentAdapter componentAdapter =
       new ThreadLocalizing.ThreadLocalized(
           new ConstructorInjection.ConstructorInjector(
               Touchable.class, SimpleTouchable.class, null));
   final Touchable touchable =
       (Touchable) componentAdapter.getComponentInstance(null, ComponentAdapter.NOTHING.class);
   assertEquals(touchable, touchable);
 }
Пример #6
0
 public Object remove(final Object o) {
   ComponentAdapter adapter = mutablePicoContainer.removeComponent(o);
   if (adapter != null) {
     // if previously an instance was registered, return it, otherwise return the type
     return adapter instanceof InstanceAdapter
         ? adapter.getComponentInstance(mutablePicoContainer, ComponentAdapter.NOTHING.class)
         : adapter.getComponentImplementation();
   } else {
     return null;
   }
 }
  public JMXRegistrationInfo provide(
      PicoContainer picoContainer, ComponentAdapter componentAdapter) {
    final Object _componentInstance = componentAdapter.getComponentInstance(picoContainer);

    try {
      final JMXManageable _manageable = (JMXManageable) _componentInstance;

      Exception _exception = null;

      try {
        // try to load XML XMBean Descriptor.
        String _clazzName = _manageable.getClass().getName().replace('.', '/');

        URL _url = _manageable.getClass().getResource("/" + _clazzName + ".xml");

        if (_url == null) {
          return fallback_.provide(picoContainer, componentAdapter);
        }

        final ObjectName _objectName =
            ObjectName.getInstance(domain_ + ":" + _manageable.getJMXObjectName());
        final JMXManageableXMBean _xmbean = new JMXManageableXMBean(_manageable, _url);

        return new JMXRegistrationInfo(_objectName, _xmbean);
      } catch (MalformedObjectNameException e) {
        _exception = e;
      } catch (NotCompliantMBeanException e) {
        _exception = e;
      } catch (NullPointerException e) {
        _exception = e;
      } catch (MBeanException e) {
        _exception = e;
      }

      throw new JMXRegistrationException(
          "Cannot create MBean for component '" + componentAdapter.getComponentKey() + "'",
          _exception);

    } catch (ClassCastException e) {
      return null;
    }
  }
 /**
  * Retrieve the component instance. The implementation will automatically register it in the
  * {@link MBeanServer}, if a provider can return a {@link javax.management.DynamicMBean} for it.
  *
  * <p>Note, that you will have to wrap this {@link ComponentAdapter} with a {@link
  * CachingComponentAdapter} to avoid the registration of the same component again.
  *
  * @throws PicoInitializationException Thrown by the delegate or if the registering of the {@link
  *     javax.management.DynamicMBean} in the {@link MBeanServer } fails.
  * @see
  *     org.picocontainer.defaults.DecoratingComponentAdapter#getComponentInstance(org.picocontainer.PicoContainer)
  */
 public Object getComponentInstance(final PicoContainer container)
     throws PicoInitializationException, PicoIntrospectionException {
   final ComponentAdapter componentAdapter = new CachingComponentAdapter(getDelegate());
   final Object componentInstance = componentAdapter.getComponentInstance(container);
   for (int i = 0; i < providers.length; ++i) {
     final JMXRegistrationInfo info = providers[i].provide(container, componentAdapter);
     if (info != null) {
       Exception exception = null;
       try {
         mBeanServer.registerMBean(info.getMBean(), info.getObjectName());
       } catch (final InstanceAlreadyExistsException e) {
         exception = e;
       } catch (final MBeanRegistrationException e) {
         exception = e;
       } catch (final NotCompliantMBeanException e) {
         exception = e;
       }
       if (exception != null) {
         throw new PicoInitializationException("Registering MBean failed", exception);
       }
     }
   }
   return componentInstance;
 }