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());
   }
 }
  /**
   * 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());
  }
 public void testAddComponentUsesImplementationHidingBehavior() {
   DefaultPicoContainer pico =
       new DefaultPicoContainer(new ImplementationHiding().wrap(new ConstructorInjection()));
   pico.addComponent("foo", String.class);
   ComponentAdapter foo = pico.getComponentAdapter("foo");
   assertEquals(HiddenImplementation.class, foo.getClass());
   assertEquals(ConstructorInjector.class, ((AbstractBehavior) foo).getDelegate().getClass());
 }
 public void testAddComponentUsesImplementationHidingBehaviorWithRedundantHideImplProperty() {
   DefaultPicoContainer pico =
       new DefaultPicoContainer(new ImplementationHiding().wrap(new ConstructorInjection()));
   pico.change(Characteristics.HIDE_IMPL).addComponent("foo", String.class);
   ComponentAdapter foo = pico.getComponentAdapter("foo");
   assertEquals(HiddenImplementation.class, foo.getClass());
   assertEquals(ConstructorInjector.class, ((AbstractBehavior) foo).getDelegate().getClass());
 }
 public void
     testAddComponentNoesNotUseImplementationHidingBehaviorWhenNoCachePropertyIsSpecified() {
   DefaultPicoContainer pico =
       new DefaultPicoContainer(new ImplementationHiding().wrap(new ConstructorInjection()));
   pico.change(Characteristics.NO_HIDE_IMPL).addComponent("foo", String.class);
   ComponentAdapter foo = pico.getComponentAdapter("foo");
   assertEquals(ConstructorInjector.class, foo.getClass());
 }
 /** 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);
 }
Exemple #7
0
 public Set keySet() {
   Set<Object> set = new HashSet<Object>();
   Collection<ComponentAdapter<?>> adapters = mutablePicoContainer.getComponentAdapters();
   for (final ComponentAdapter<?> adapter : adapters) {
     set.add(adapter.getComponentKey());
   }
   return Collections.unmodifiableSet(set);
 }
 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);
 }
 public void testAddAdapterUsesImplementationHidingBehavior() {
   DefaultPicoContainer pico =
       new DefaultPicoContainer(new ImplementationHiding().wrap(new ConstructorInjection()));
   pico.addAdapter(
       new InstanceAdapter("foo", "bar", new NullLifecycleStrategy(), new NullComponentMonitor()));
   ComponentAdapter foo = pico.getComponentAdapter("foo");
   assertEquals(HiddenImplementation.class, foo.getClass());
   assertEquals(InstanceAdapter.class, ((AbstractBehavior) foo).getDelegate().getClass());
 }
 public void testAddAdapterNoesNotUseImplementationHidingBehaviorWhenNoCachePropertyIsSpecified() {
   DefaultPicoContainer pico =
       new DefaultPicoContainer(new ImplementationHiding().wrap(new ConstructorInjection()));
   pico.change(Characteristics.NO_HIDE_IMPL)
       .addAdapter(
           new InstanceAdapter(
               "foo", "bar", new NullLifecycleStrategy(), new NullComponentMonitor()));
   ComponentAdapter foo = pico.getComponentAdapter("foo");
   assertEquals(InstanceAdapter.class, foo.getClass());
 }
 @Override
 protected List<Class<?>> stepsTypes() {
   List<Class<?>> types = new ArrayList<Class<?>>();
   for (ComponentAdapter<?> adapter : parent.getComponentAdapters()) {
     if (hasAnnotatedMethods(adapter.getComponentImplementation())) {
       types.add(adapter.getComponentImplementation());
     }
   }
   return types;
 }
Exemple #12
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;
   }
 }
 /**
  * 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);
   }
 }
 /**
  * Construct an AssimilatingBehavior. The <code>type</code> may not implement the type of the
  * component instance. If the component instance <b>does</b> implement the interface, no proxy is
  * used though.
  *
  * @param type The class type used as key.
  * @param delegate The delegated {@link ComponentAdapter}.
  * @param proxyFactory The {@link ProxyFactory} to use.
  * @throws PicoCompositionException Thrown if the <code>type</code> is not compatible and cannot
  *     be proxied.
  */
 public AssimilatingBehavior(
     final Class type, final ComponentAdapter delegate, final ProxyFactory proxyFactory)
     throws PicoCompositionException {
   super(delegate);
   this.type = type;
   this.proxyFactory = proxyFactory;
   final Class delegationType = delegate.getComponentImplementation();
   this.isCompatible = type.isAssignableFrom(delegationType);
   if (!isCompatible) {
     if (!proxyFactory.canProxy(type)) {
       throw new PicoCompositionException("Cannot create proxy for type " + type.getName());
     }
     final Method[] methods = type.getMethods();
     for (final Method method : methods) {
       try {
         delegationType.getMethod(method.getName(), method.getParameterTypes());
       } catch (final NoSuchMethodException e) {
         throw new PicoCompositionException(
             "Cannot create proxy for type "
                 + type.getName()
                 + ", because of incompatible method "
                 + method.toString());
       }
     }
   }
 }
  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;
    }
  }
Exemple #16
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);
  }
 /**
  * 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;
 }
 @Override
 public Class<?> apply(ComponentAdapter<?> input) {
   return input.getComponentImplementation();
 }