pax_global_header 0000666 0000000 0000000 00000000064 12422476443 0014522 g ustar 00root root 0000000 0000000 52 comment=a5f19524e12199dfc0c15d48851eb07e72673e45
libpicocontainer-java-2.15/ 0000775 0000000 0000000 00000000000 12422476443 0015714 5 ustar 00root root 0000000 0000000 libpicocontainer-java-2.15/META-INF/ 0000775 0000000 0000000 00000000000 12422476443 0017054 5 ustar 00root root 0000000 0000000 libpicocontainer-java-2.15/META-INF/MANIFEST.MF 0000664 0000000 0000000 00000000173 12422476443 0020507 0 ustar 00root root 0000000 0000000 Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: paul
Build-Jdk: 1.7.0_09
libpicocontainer-java-2.15/org/ 0000775 0000000 0000000 00000000000 12422476443 0016503 5 ustar 00root root 0000000 0000000 libpicocontainer-java-2.15/org/picocontainer/ 0000775 0000000 0000000 00000000000 12422476443 0021340 5 ustar 00root root 0000000 0000000 libpicocontainer-java-2.15/org/picocontainer/Behavior.java 0000664 0000000 0000000 00000002000 12422476443 0023732 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by Paul Hammant *
*****************************************************************************/
package org.picocontainer;
/**
* Behaviors modify the components created by a Injector with additional behaviors
*
* @author Paul Hammant
* @author Jörg Schaible
* @author Mauro Talevi
* @see LifecycleStrategy
*/
public interface Behavior extends ComponentAdapter, ComponentLifecycle {
}
libpicocontainer-java-2.15/org/picocontainer/BehaviorFactory.java 0000664 0000000 0000000 00000002274 12422476443 0025277 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer;
import java.util.Properties;
/**
* Extends ComponentFactory to provide factory methods for Behaviors
*
* @author Paul Hammant
* @author Mauro Talevi
*/
public interface BehaviorFactory extends ComponentFactory {
ComponentFactory wrap(ComponentFactory delegate);
ComponentAdapter addComponentAdapter(ComponentMonitor componentMonitor, LifecycleStrategy lifecycleStrategy,
Properties componentProperties, ComponentAdapter adapter);
}
libpicocontainer-java-2.15/org/picocontainer/BindKey.java 0000664 0000000 0000000 00000004014 12422476443 0023527 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Committers. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by Joerg Schaibe *
*****************************************************************************/
package org.picocontainer;
import java.io.Serializable;
import java.lang.annotation.Annotation;
/** @author Paul Hammant */
@SuppressWarnings("serial")
public class BindKey implements Serializable {
private final Class type;
private final Class extends Annotation> annotation;
public BindKey(Class type, Class extends Annotation> annotation) {
this.type = type;
this.annotation = annotation;
}
public Class getType() {
return type;
}
public Class extends Annotation> getAnnotation() {
return annotation;
}
public String toString() {
return type.getName() + ":" + annotation.getName();
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BindKey> bindKey = (BindKey>)o;
if (!annotation.equals(bindKey.annotation)) return false;
if (!type.equals(bindKey.type)) return false;
return true;
}
public int hashCode() {
int result;
result = type.hashCode();
result = 31 * result + annotation.hashCode();
return result;
}
public static BindKey bindKey(Class type, Class extends Annotation> annotation) {
return new BindKey(type, annotation);
}
}
libpicocontainer-java-2.15/org/picocontainer/Characteristics.java 0000664 0000000 0000000 00000021340 12422476443 0025316 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer;
import java.util.Map;
import java.util.Properties;
/**
* Collection of immutable properties, holding behaviour characteristics. See
* The PicoContainer Website for details on the usage
* of Characteristics.
*
* @author Paul Hammant
* @see org.picocontainer.ComponentAdapter
* @see org.picocontainer.Behavior
*/
@SuppressWarnings("serial")
public final class Characteristics {
private static final String _INJECTION = "injection";
private static final String _NONE = "none";
private static final String _CONSTRUCTOR = "constructor";
private static final String _METHOD = "method";
private static final String _SETTER = "setter";
private static final String _CACHE = "cache";
private static final String _SYNCHRONIZING = "synchronizing";
private static final String _LOCKING = "locking";
private static final String _HIDE_IMPL = "hide-impl";
private static final String _PROPERTY_APPLYING = "property-applying";
private static final String _AUTOMATIC = "automatic";
private static final String _USE_NAMES = "use-parameter-names";
private static final String _ENABLE_CIRCULAR = "enable-circular";
private static final String _GUARD = "guard";
private static final String _EMJECTION = "emjection_enabled";
/**
* Since properties use strings, we supply String constants for Boolean conditions.
*/
public static final String FALSE = "false";
/**
* Since properties use strings, we supply String constants for Boolean conditions.
*/
public static final String TRUE = "true";
/**
* Turns on constructor injection.
* @see org.picocontainer.injectors.ConstructorInjection
*/
public static final Properties CDI = immutable(_INJECTION, _CONSTRUCTOR);
/**
* Turns on Setter Injection.
* @see org.picocontainer.injectors.SetterInjection
*/
public static final Properties SDI = immutable(_INJECTION, _SETTER);
/**
* Turns on Method Injection.
*/
public static final Properties METHOD_INJECTION = immutable(_INJECTION, _METHOD);
/**
* Turns off Caching of component instances. (Often referred to in other circles
* as singleton).
* @see org.picocontainer.behaviors.Caching
*/
public static final Properties NO_CACHE = immutable(_CACHE, FALSE);
/**
* Turns on Caching of component instances. (Often referred to in other circles
* as singleton)
* @see org.picocontainer.behaviors.Caching
*/
public static final Properties CACHE = immutable(_CACHE, TRUE);
/**
* Turns on synchronized access to the component instance. (Under JDK 1.5 conditions,
* it will be better to use {@link #LOCK} instead.
* @see org.picocontainer.behaviors.Synchronizing
*/
public static final Properties SYNCHRONIZE = immutable(_SYNCHRONIZING, TRUE);
/**
* Turns off synchronized access to the component instance.
* @see org.picocontainer.behaviors.Synchronizing
*/
public static final Properties NO_SYNCHRONIZE = immutable(_SYNCHRONIZING, FALSE);
/**
* Uses a java.util.concurrent.Lock to provide faster access than synchronized.
* @see org.picocontainer.behaviors.Locking
*/
public static final Properties LOCK = immutable(_LOCKING, TRUE);
/**
* Turns off locking synchronization.
* @see org.picocontainer.behaviors.Locking
*/
public static final Properties NO_LOCK = immutable(_LOCKING, FALSE);
/**
* Synonym for {@link #CACHE CACHE}.
* @see org.picocontainer.behaviors.Caching
*/
public static final Properties SINGLE = CACHE;
/**
* Synonym for {@link #NO_CACHE NO_CACHE}.
* @see org.picocontainer.behaviors.Caching
*/
public static final Properties NO_SINGLE = NO_CACHE;
/**
* Turns on implementation hiding. You may use the JDK Proxy implementation included
* in this version, or the ASM-based implementation hiding method
* included in PicoContainer Gems. However, you cannot use both in a single PicoContainer
* instance.
*/
public static final Properties HIDE_IMPL = immutable(_HIDE_IMPL, TRUE);
/**
* Turns off implementation hiding.
* @see #HIDE_IMPL for more information.
*/
public static final Properties NO_HIDE_IMPL = immutable(_HIDE_IMPL, FALSE);
public static final Properties ENABLE_CIRCULAR = immutable(_ENABLE_CIRCULAR, TRUE);
public static final Properties NONE = immutable(_NONE, "");
/**
* Turns on bean-setting property applications where certain simple properties are set
* after the object is created based.
*/
public static final Properties PROPERTY_APPLYING = immutable(_PROPERTY_APPLYING, TRUE);
/**
* Turns off bean-setting property applications.
* @see org.picocontainer.behaviors.PropertyApplying
*/
public static final Properties NO_PROPERTY_APPLYING = immutable(_PROPERTY_APPLYING, FALSE);
public static final Properties AUTOMATIC = immutable(_AUTOMATIC, TRUE);
public static final Properties USE_NAMES = immutable(_USE_NAMES, TRUE);
public static final Properties EMJECTION_ENABLED = immutable(_EMJECTION, TRUE);
public static final Properties GUARD = immutable(_GUARD, "guard");
public static final Properties GUARD(String with) {
return immutable(_GUARD, with);
};
/**
* Transforms a single name value pair unto a read only {@linkplain java.util.Properties}
* instance.
* Example Usage:
*
* Properties readOnly = immutable("oneKey","oneValue"};
* assert readOnly.getProperty("oneKey") != null);
*
* @param name the property key.
* @param value the property value.
* @return Read Only properties instance.
*/
public static Properties immutable(String name, String value) {
return new ImmutableProperties(name, value);
}
/**
* Read only property set. Once constructed, all methods that modify state will
* throw UnsupportedOperationException.
* @author Paul Hammant.
*/
public static class ImmutableProperties extends Properties {
private boolean sealed = false;
public ImmutableProperties(String name, String value) {
super.setProperty(name, value);
sealed = true;
}
/**
* Read Only Object: will throw UnsupportedOperationException.
*/
@Override
@SuppressWarnings("unused")
public Object remove( Object o) {
throw new UnsupportedOperationException("immutable properties are read only");
}
/**
* Read Only Object: will throw UnsupportedOperationException.
*/
@Override
@SuppressWarnings("unused")
public synchronized Object setProperty(String string, String string1) {
throw new UnsupportedOperationException("immutable properties are read only");
}
/**
* Read Only Object: will throw UnsupportedOperationException.
*/
@Override
public synchronized void clear() {
throw new UnsupportedOperationException("immutable properties are read only");
}
/**
* Once object is constructed, this will throw UnsupportedOperationException because
* this class is a read only wrapper.
*/
@Override
public synchronized Object put(Object key, Object value) {
if (!sealed) {
//setProperty calls put, so until the object is fully constructed, we
//cannot seal it.
return super.put(key, value);
}
throw new UnsupportedOperationException("immutable properties are read only");
}
/**
* Read Only Object: will throw UnsupportedOperationException.
*/
@Override
@SuppressWarnings("unused")
public synchronized void putAll(Map extends Object, ? extends Object> t) {
throw new UnsupportedOperationException("immutable properties are read only");
}
}
}
libpicocontainer-java-2.15/org/picocontainer/ComponentAdapter.java 0000664 0000000 0000000 00000013451 12422476443 0025452 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
*****************************************************************************/
package org.picocontainer;
import java.lang.reflect.Type;
/**
* A component adapter is responsible for providing a specific component
* instance of type <T>. An instance of an implementation of this interface is
* used inside a {@link PicoContainer} for every registered component or
* instance. Each ComponentAdapter
instance has to have a key
* which is unique within that container. The key itself is either a class type
* (normally an interface) or an identifier.
* In a overly simplistic sense, the ComponentAdapter can be thought of us a type of
* an object factory. If you need to modify how your object is constructed, use and appropriate
* ComponentAdapter or roll your own since the API is purposely kept rather simple. See
* http://www.picocontainer.org/adapters.html
* for more information.
*
* @author Jon Tirsén
* @author Paul Hammant
* @author Aslak Hellesøy
*/
public interface ComponentAdapter {
public class NOTHING {
private NOTHING(){
}
};
/**
* Retrieve the key associated with the component.
*
* @return the component's key. Should either be a class type (normally an interface) or an identifier that is
* unique (within the scope of the current PicoContainer).
*/
Object getComponentKey();
/**
* Retrieve the class of the component.
*
* @return the component's implementation class. Should normally be a concrete class (ie, a class that can be
* instantiated).
*/
Class extends T> getComponentImplementation();
/**
* Retrieve the component instance. This method will usually create a new instance each time it is called, but that
* is not required. For example, {@link org.picocontainer.behaviors.Cached} will always return the
* same instance.
*
* @param container the {@link PicoContainer}, that is used to resolve any possible dependencies of the instance.
* @return the component instance.
* @throws PicoCompositionException if the component has dependencies which could not be resolved, or
* instantiation of the component lead to an ambigous situation within the
* container.
* @deprecated since PicoContainer 2.2. Use {@link getComponentInstance(PicoContainer,Type)} with {@link ComponentAdapter.NOTHING.class} as type
* if no type available.
*/
T getComponentInstance(PicoContainer container) throws PicoCompositionException;
/**
* Retrieve the component instance. This method will usually create a new instance each time it is called, but that
* is not required. For example, {@link org.picocontainer.behaviors.Cached} will always return the
* same instance.
*
* @param container the {@link org.picocontainer.PicoContainer}, that is used to resolve any possible dependencies of the instance.
* @param into the class that is about to be injected into. Use ComponentAdapter.NOTHING.class if this is not important to you.
* @return the component instance.
* @throws PicoCompositionException if the component has dependencies which could not be resolved, or
* instantiation of the component lead to an ambiguous situation within the
* container.
*/
T getComponentInstance(PicoContainer container, Type into) throws PicoCompositionException;
/**
* Verify that all dependencies for this adapter can be satisfied. Normally, the adapter should verify this by
* checking that the associated PicoContainer contains all the needed dependencies.
*
* @param container the {@link PicoContainer}, that is used to resolve any possible dependencies of the instance.
* @throws PicoCompositionException if one or more dependencies cannot be resolved.
*/
void verify(PicoContainer container) throws PicoCompositionException;
/**
* Accepts a visitor for this ComponentAdapter. The method is normally called by visiting a {@link PicoContainer}, that
* cascades the visitor also down to all its ComponentAdapter instances.
*
* @param visitor the visitor.
*/
void accept(PicoVisitor visitor);
/**
* Component adapters may be nested in a chain, and this method is used to grab the next ComponentAdapter in the chain.
* @return the next component adapter in line or null if there is no delegate ComponentAdapter.
*/
ComponentAdapter getDelegate();
/**
* Locates a component adapter of type componentAdapterType in the ComponentAdapter chain. Will return null
* if there is no adapter of the given type.
* @param the type of ComponentAdapter being located.
* @param adapterType the class of the adapter type being located. Never null.
* @return the appropriate component adapter of type U . May return null if the component adapter type is not
* returned.
*/
U findAdapterOfType(Class adapterType);
/**
* Get a string key descriptor of the component adapter for use in toString()
* @return the descriptor
*/
String getDescriptor();
}
libpicocontainer-java-2.15/org/picocontainer/ComponentFactory.java 0000664 0000000 0000000 00000007771 12422476443 0025511 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.Parameter;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.ComponentMonitor;
import org.picocontainer.LifecycleStrategy;
import java.util.Properties;
/**
*
* A component factory is responsible for creating
* {@link ComponentAdapter} component adapters. The main use of the component factory is
* inside {@link DefaultPicoContainer#DefaultPicoContainer(ComponentFactory)}, where it can
* be used to customize the default component adapter that is used when none is specified
* explicitly.
*
*
* @author Paul Hammant
* @author Mauro Talevi
* @author Jon Tirsén
*/
public interface ComponentFactory {
/**
* Create a new component adapter based on the specified arguments.
*
* @param componentMonitor the component monitor
* @param lifecycleStrategy te lifecycle strategy
* @param componentProperties the component properties
* @param componentKey the key to be associated with this adapter. This
* value should be returned from a call to
* {@link ComponentAdapter#getComponentKey()} on the created
* adapter.
* @param componentImplementation the implementation class to be associated
* with this adapter. This value should be returned from a call
* to {@link ComponentAdapter#getComponentImplementation()} on
* the created adapter. Should not be null.
* @param parameters additional parameters to use by the component adapter
* in constructing component instances. These may be used, for
* example, to make decisions about the arguments passed into the
* component constructor. These should be considered hints; they
* may be ignored by some implementations. May be null, and may
* be of zero length.
* @return a new component adapter based on the specified arguments. Should
* not return null.
* @throws PicoCompositionException if the creation of the component adapter
* results in a {@link PicoCompositionException}.
* @return The component adapter
*/
ComponentAdapter createComponentAdapter(ComponentMonitor componentMonitor,
LifecycleStrategy lifecycleStrategy,
Properties componentProperties,
Object componentKey,
Class componentImplementation,
Parameter... parameters) throws PicoCompositionException;
/**
* Verification for the ComponentFactory - subject to implementation.
*
* @param container the {@link PicoContainer}, that is used for verification.
* @throws PicoCompositionException if one or more dependencies cannot be resolved.
*/
void verify(PicoContainer container);
/**
* Accepts a visitor for this ComponentFactory. The method is normally called by visiting a {@link PicoContainer}, that
* cascades the visitor also down to all its ComponentFactory instances.
*
* @param visitor the visitor.
*/
void accept(PicoVisitor visitor);
}
libpicocontainer-java-2.15/org/picocontainer/ComponentLifecycle.java 0000664 0000000 0000000 00000002665 12422476443 0025776 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
*****************************************************************************/
package org.picocontainer;
/**
*
*
*/
public interface ComponentLifecycle {
/**
* Invoke the "start" method on the component.
*
* @param container the container to "start" the component
*/
void start(PicoContainer container);
/**
* Invoke the "stop" method on the component.
*
* @param container the container to "stop" the component
*/
void stop(PicoContainer container);
/**
* Invoke the "dispose" method on the component.
*
* @param container the container to "dispose" the component
*/
void dispose(PicoContainer container);
/**
* Test if a component honors a lifecycle.
*
* @return true
if the component has a lifecycle
*/
boolean componentHasLifecycle();
boolean isStarted();
}
libpicocontainer-java-2.15/org/picocontainer/ComponentMonitor.java 0000664 0000000 0000000 00000013257 12422476443 0025525 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by Paul Hammant & Obie Fernandez & Aslak *
*****************************************************************************/
package org.picocontainer;
import java.lang.reflect.Constructor;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
/**
* A component monitor is responsible for monitoring the component instantiation
* and method invocation.
*
* @author Paul Hammant
* @author Obie Fernandez
* @author Aslak Hellesøy
* @author Mauro Talevi
*/
public interface ComponentMonitor {
Object KEEP = new Object();
/**
* Event thrown as the component is being instantiated using the given constructor
*
* @param container
* @param componentAdapter
* @param constructor the Constructor used to instantiate the addComponent @return the constructor to use in instantiation (nearly always the same one as passed in)
*/
Constructor instantiating(PicoContainer container, ComponentAdapter componentAdapter,
Constructor constructor
);
/**
* Event thrown after the component has been instantiated using the given constructor.
* This should be called for both Constructor and Setter DI.
*
* @param container
* @param componentAdapter
* @param constructor the Constructor used to instantiate the addComponent
* @param instantiated the component that was instantiated by PicoContainer
* @param injected the components during instantiation.
* @param duration the duration in milliseconds of the instantiation
*/
void instantiated(PicoContainer container, ComponentAdapter componentAdapter,
Constructor constructor,
Object instantiated,
Object[] injected,
long duration);
/**
* Event thrown if the component instantiation failed using the given constructor
*
* @param container
* @param componentAdapter
* @param constructor the Constructor used to instantiate the addComponent
* @param cause the Exception detailing the cause of the failure
*/
void instantiationFailed(PicoContainer container,
ComponentAdapter componentAdapter,
Constructor constructor,
Exception cause);
/**
* Event thrown as the component method is being invoked on the given instance
*
* @param container
* @param componentAdapter
* @param member
* @param instance the component instance
* @param args
*/
Object invoking(PicoContainer container, ComponentAdapter> componentAdapter, Member member, Object instance, Object[] args);
/**
* Event thrown after the component method has been invoked on the given instance
*
* @param container
* @param componentAdapter
* @param member
* @param instance the component instance
* @param duration
* @param args
* @param retVal
*/
void invoked(PicoContainer container,
ComponentAdapter> componentAdapter,
Member member,
Object instance,
long duration, Object[] args, Object retVal);
/**
* Event thrown if the component method invocation failed on the given instance
*
* @param member
* @param instance the component instance
* @param cause the Exception detailing the cause of the failure
*/
void invocationFailed(Member member, Object instance, Exception cause);
/**
* Event thrown if a lifecycle method invocation - start, stop or dispose -
* failed on the given instance
*
* @param container
* @param componentAdapter
* @param method the lifecycle Method invoked on the component instance
* @param instance the component instance
* @param cause the RuntimeException detailing the cause of the failure
*/
void lifecycleInvocationFailed(MutablePicoContainer container,
ComponentAdapter> componentAdapter, Method method,
Object instance,
RuntimeException cause);
/**
* No Component has been found for the key in question. Implementers of this have a last chance opportunity to
* specify something for the need. This is only relevant to component dependencies, and not to
* container.getComponent() in your user code.
*
* @param container
* @param componentKey
*/
Object noComponentFound(MutablePicoContainer container, Object componentKey);
/**
* A mechanism to monitor or override the Injectors being made for components.
*
* @param injector
* @return an Injector. For most implementations, the same one as was passed in.
*/
Injector newInjector(Injector injector);
/**
* A mechanism to monitor or override the Behaviors being made for components.
*
* @param behavior
* @return an Behavior. For most implementations, the same one as was passed in.
*/
Behavior newBehavior(Behavior behavior);
}
libpicocontainer-java-2.15/org/picocontainer/ComponentMonitorStrategy.java 0000664 0000000 0000000 00000002650 12422476443 0027243 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
*****************************************************************************/
package org.picocontainer;
/**
*
* Interface responsible for changing monitoring strategy.
* It may be implemented by {@link org.picocontainer.PicoContainer containers} and
* single {@link org.picocontainer.ComponentAdapter component adapters}.
* The choice of supporting the monitor strategy is left to the
* implementers of the container and adapters.
*
*
* @author Paul Hammant
* @author Joerg Schaible
* @author Mauro Talevi
*/
public interface ComponentMonitorStrategy {
/**
* Changes the component monitor used
* @param monitor the new ComponentMonitor to use
*/
void changeMonitor(ComponentMonitor monitor);
/**
* Returns the monitor currently used
* @return The ComponentMonitor currently used
*/
ComponentMonitor currentMonitor();
}
libpicocontainer-java-2.15/org/picocontainer/Converters.java 0000664 0000000 0000000 00000002516 12422476443 0024341 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
*****************************************************************************/
package org.picocontainer;
import java.lang.reflect.Type;
/**
* A facade for a collection of converters that provides string-to-type conversions.
*
* @author Paul Hammant
* @author Michael Rimov
*/
public interface Converters {
/**
* Returns true if a converters is available to convert to the given object type
*
* @param type the object Type to convert to
* @return true if the type can be converted to
*/
boolean canConvert(Type type);
/**
* Converts a particular string value into the target type
*
* @param value the String value to convert
* @param type the object Type to convert to
* @return The converted Object instance
*/
Object convert(String value, Type type);
}
libpicocontainer-java-2.15/org/picocontainer/Converting.java 0000664 0000000 0000000 00000000562 12422476443 0024324 0 ustar 00root root 0000000 0000000 package org.picocontainer;
/**
* Interface for containers that can handle string-to-object conversion in object parameters.
* @author Paul Hammant
*/
public interface Converting {
/**
* Retrieve the set of converters for transforming string parameters
* into objects.
* @return converter set instance.
*/
Converters getConverters();
}
libpicocontainer-java-2.15/org/picocontainer/DefaultPicoContainer.java 0000664 0000000 0000000 00000134633 12422476443 0026257 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer;
import org.picocontainer.adapters.AbstractAdapter;
import org.picocontainer.adapters.InstanceAdapter;
import org.picocontainer.behaviors.AbstractBehaviorFactory;
import org.picocontainer.behaviors.AdaptingBehavior;
import org.picocontainer.behaviors.Cached;
import org.picocontainer.behaviors.Caching;
import org.picocontainer.behaviors.HiddenImplementation;
import org.picocontainer.containers.AbstractDelegatingMutablePicoContainer;
import org.picocontainer.containers.AbstractDelegatingPicoContainer;
import org.picocontainer.containers.EmptyPicoContainer;
import org.picocontainer.containers.ImmutablePicoContainer;
import org.picocontainer.converters.BuiltInConverters;
import org.picocontainer.converters.ConvertsNothing;
import org.picocontainer.injectors.AbstractInjector;
import org.picocontainer.injectors.AdaptingInjection;
import org.picocontainer.injectors.FactoryInjector;
import org.picocontainer.lifecycle.DefaultLifecycleState;
import org.picocontainer.lifecycle.LifecycleState;
import org.picocontainer.lifecycle.StartableLifecycleStrategy;
import org.picocontainer.monitors.NullComponentMonitor;
import org.picocontainer.parameters.DefaultConstructorParameter;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.ref.WeakReference;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import static org.picocontainer.parameters.BasicComponentParameter.findInjectorOrInstanceAdapter;
import static org.picocontainer.parameters.BasicComponentParameter.makeFoundAmbiguousStrings;
/**
*
* The Standard {@link PicoContainer}/{@link MutablePicoContainer} implementation.
* Constructing a container c with a parent p container will cause c to look up components
* in p if they cannot be found inside c itself.
*
*
* Using {@link Class} objects as keys to the various registerXXX() methods makes
* a subtle semantic difference:
*
*
* If there are more than one registered components of the same type and one of them are
* registered with a {@link java.lang.Class} key of the corresponding type, this addComponent
* will take precedence over other components during type resolution.
*
*
* Another place where keys that are classes make a subtle difference is in
* {@link HiddenImplementation}.
*
*
* This implementation of {@link MutablePicoContainer} also supports
* {@link ComponentMonitorStrategy}.
*
*
* @author Paul Hammant
* @author Aslak Hellesøy
* @author Jon Tirsén
* @author Thomas Heller
* @author Mauro Talevi
*/
@SuppressWarnings("serial")
public class DefaultPicoContainer implements MutablePicoContainer, Converting, ComponentMonitorStrategy, Serializable {
private String name;
/**
* Component factory instance.
*/
protected final ComponentFactory componentFactory;
/**
* Parent picocontainer
*/
private PicoContainer parent;
/**
* All picocontainer children.
*/
private final Set children = new HashSet();
/**
* Current state of the container.
*/
private LifecycleState lifecycleState = new DefaultLifecycleState();
/**
* Keeps track of child containers started status.
*/
private final Set> childrenStarted = new HashSet>();
/**
* Lifecycle strategy instance.
*/
protected final LifecycleStrategy lifecycleStrategy;
/**
* Properties set at the container level, that will affect subsequent components added.
*/
private final Properties containerProperties = new Properties();
/**
* Component monitor instance. Receives event callbacks.
*/
protected ComponentMonitor componentMonitor;
/**
* Map used for looking up component adapters by their key.
*/
private final Map> componentKeyToAdapterCache = new HashMap >();
private final Set> componentAdapters = new LinkedHashSet>();
protected final List> orderedComponentAdapters = new ArrayList>();
private transient IntoThreadLocal intoThreadLocal;
private Converters converters;
/**
* Creates a new container with a custom ComponentFactory and a parent container.
*
*
* Important note about caching: If you intend the components to be cached, you should pass
* in a factory that creates {@link Cached} instances, such as for example
* {@link Caching}. Caching can delegate to
* other ComponentAdapterFactories.
*
*
* @param componentFactory the factory to use for creation of ComponentAdapters.
* @param parent the parent container (used for component dependency lookups).
*/
public DefaultPicoContainer(final ComponentFactory componentFactory, final PicoContainer parent) {
this(componentFactory, new StartableLifecycleStrategy(new NullComponentMonitor()), parent, new NullComponentMonitor());
}
/**
* Creates a new container with a custom ComponentFactory, LifecycleStrategy for instance registration,
* and a parent container.
*
*
* Important note about caching: If you intend the components to be cached, you should pass
* in a factory that creates {@link Cached} instances, such as for example
* {@link Caching}. Caching can delegate to
* other ComponentAdapterFactories.
*
*
* @param componentFactory the factory to use for creation of ComponentAdapters.
* @param lifecycleStrategy
* the lifecycle strategy chosen for registered
* instance (not implementations!)
* @param parent the parent container (used for component dependency lookups).
*/
public DefaultPicoContainer(final ComponentFactory componentFactory,
final LifecycleStrategy lifecycleStrategy,
final PicoContainer parent) {
this(componentFactory, lifecycleStrategy, parent, new NullComponentMonitor() );
}
public DefaultPicoContainer(final ComponentFactory componentFactory,
final LifecycleStrategy lifecycleStrategy,
final PicoContainer parent, final ComponentMonitor componentMonitor) {
if (componentFactory == null) {
throw new NullPointerException("componentFactory");
}
if (lifecycleStrategy == null) {
throw new NullPointerException("lifecycleStrategy");
}
this.componentFactory = componentFactory;
this.lifecycleStrategy = lifecycleStrategy;
this.parent = parent;
if (parent != null && !(parent instanceof EmptyPicoContainer)) {
this.parent = new ImmutablePicoContainer(parent);
}
this.componentMonitor = componentMonitor;
}
/**
* Creates a new container with the AdaptingInjection using a
* custom ComponentMonitor
*
* @param monitor the ComponentMonitor to use
* @param parent the parent container (used for component dependency lookups).
*/
public DefaultPicoContainer(final ComponentMonitor monitor, final PicoContainer parent) {
this(new AdaptingBehavior(), new StartableLifecycleStrategy(monitor), parent, monitor);
}
/**
* Creates a new container with the AdaptingInjection using a
* custom ComponentMonitor and lifecycle strategy
*
* @param monitor the ComponentMonitor to use
* @param lifecycleStrategy the lifecycle strategy to use.
* @param parent the parent container (used for component dependency lookups).
*/
public DefaultPicoContainer(final ComponentMonitor monitor, final LifecycleStrategy lifecycleStrategy, final PicoContainer parent) {
this(new AdaptingBehavior(), lifecycleStrategy, parent, monitor);
}
/**
* Creates a new container with the AdaptingInjection using a
* custom lifecycle strategy
*
* @param lifecycleStrategy the lifecycle strategy to use.
* @param parent the parent container (used for component dependency lookups).
*/
public DefaultPicoContainer(final LifecycleStrategy lifecycleStrategy, final PicoContainer parent) {
this(new NullComponentMonitor(), lifecycleStrategy, parent);
}
/**
* Creates a new container with a custom ComponentFactory and no parent container.
*
* @param componentFactory the ComponentFactory to use.
*/
public DefaultPicoContainer(final ComponentFactory componentFactory) {
this(componentFactory, null);
}
/**
* Creates a new container with the AdaptingInjection using a
* custom ComponentMonitor
*
* @param monitor the ComponentMonitor to use
*/
public DefaultPicoContainer(final ComponentMonitor monitor) {
this(monitor, new StartableLifecycleStrategy(monitor), null);
}
/**
* Creates a new container with a (caching) {@link AdaptingInjection}
* and a parent container.
*
* @param parent the parent container (used for component dependency lookups).
*/
public DefaultPicoContainer(final PicoContainer parent) {
this(new AdaptingBehavior(), parent);
}
/** Creates a new container with a {@link AdaptingBehavior} and no parent container. */
public DefaultPicoContainer() {
this(new AdaptingBehavior(), null);
}
/** {@inheritDoc} **/
public Collection> getComponentAdapters() {
return Collections.unmodifiableSet(getModifiableComponentAdapterList());
}
/** {@inheritDoc} **/
public final ComponentAdapter> getComponentAdapter(final Object componentKey) {
ComponentAdapter> adapter = getComponentKeyToAdapterCache().get(componentKey);
if (adapter == null && parent != null) {
adapter = getParent().getComponentAdapter(componentKey);
if (adapter != null) {
adapter = new KnowsContainerAdapter(adapter, getParent());
}
}
if (adapter == null) {
Object inst = componentMonitor.noComponentFound(this, componentKey);
if (inst != null) {
adapter = new LateInstance(componentKey, inst);
}
}
return adapter;
}
public static class LateInstance extends AbstractAdapter {
private final Object instance;
private LateInstance(Object componentKey, Object instance) {
super(componentKey, instance.getClass());
this.instance = instance;
}
public Object getComponentInstance() {
return instance;
}
public Object getComponentInstance(PicoContainer container, Type into) throws PicoCompositionException {
return instance;
}
public void verify(PicoContainer container) throws PicoCompositionException {
}
public String getDescriptor() {
return "LateInstance";
}
}
public static class KnowsContainerAdapter implements ComponentAdapter {
private final ComponentAdapter ca;
private final PicoContainer ctr;
public KnowsContainerAdapter(ComponentAdapter ca, PicoContainer ctr) {
this.ca = ca;
this.ctr = ctr;
}
public T getComponentInstance(Type into) throws PicoCompositionException {
return getComponentInstance(ctr, into);
}
public Object getComponentKey() {
return ca.getComponentKey();
}
public Class extends T> getComponentImplementation() {
return ca.getComponentImplementation();
}
public T getComponentInstance(PicoContainer container) throws PicoCompositionException {
return ca.getComponentInstance(container);
}
public T getComponentInstance(PicoContainer container, Type into) throws PicoCompositionException {
return ca.getComponentInstance(container, into);
}
public void verify(PicoContainer container) throws PicoCompositionException {
ca.verify(container);
}
public void accept(PicoVisitor visitor) {
ca.accept(visitor);
}
public ComponentAdapter getDelegate() {
return ca.getDelegate();
}
public U findAdapterOfType(Class adapterType) {
return ca.findAdapterOfType(adapterType);
}
public String getDescriptor() {
return null;
}
}
/** {@inheritDoc} **/
public ComponentAdapter getComponentAdapter(final Class componentType, final NameBinding componentNameBinding) {
return getComponentAdapter(componentType, componentNameBinding, null);
}
/** {@inheritDoc} **/
private ComponentAdapter getComponentAdapter(final Class componentType, final NameBinding componentNameBinding, final Class extends Annotation> binding) {
// See http://jira.codehaus.org/secure/ViewIssue.jspa?key=PICO-115
ComponentAdapter> adapterByKey = getComponentAdapter(componentType);
if (adapterByKey != null) {
return typeComponentAdapter(adapterByKey);
}
List> found = binding == null ? getComponentAdapters(componentType) : getComponentAdapters(componentType, binding);
if (found.size() == 1) {
return found.get(0);
} else if (found.isEmpty()) {
if (parent != null) {
return getParent().getComponentAdapter(componentType, componentNameBinding);
} else {
return null;
}
} else {
if (componentNameBinding != null) {
String parameterName = componentNameBinding.getName();
if (parameterName != null) {
ComponentAdapter> ca = getComponentAdapter(parameterName);
if (ca != null && componentType.isAssignableFrom(ca.getComponentImplementation())) {
return typeComponentAdapter(ca);
}
}
}
String[] foundStrings = makeFoundAmbiguousStrings(found);
throw new AbstractInjector.AmbiguousComponentResolutionException(componentType, foundStrings);
}
}
/** {@inheritDoc} **/
public ComponentAdapter getComponentAdapter(final Class componentType, final Class extends Annotation> binding) {
// 1
return getComponentAdapter(componentType, null, binding);
}
/** {@inheritDoc} **/
public List> getComponentAdapters(final Class componentType) {
return getComponentAdapters(componentType, null);
}
/** {@inheritDoc} **/
public List> getComponentAdapters(final Class componentType, final Class extends Annotation> binding) {
if (componentType == null) {
return Collections.emptyList();
}
List> found = new ArrayList>();
for (ComponentAdapter> componentAdapter : getComponentAdapters()) {
Object k = componentAdapter.getComponentKey();
if (componentType.isAssignableFrom(componentAdapter.getComponentImplementation()) &&
(!(k instanceof BindKey) || (k instanceof BindKey && (((BindKey>)k).getAnnotation() == null || binding == null ||
((BindKey>)k).getAnnotation() == binding)))) {
found.add((ComponentAdapter)typeComponentAdapter(componentAdapter));
}
}
return found;
}
protected MutablePicoContainer addAdapterInternal(ComponentAdapter> componentAdapter) {
Object componentKey = componentAdapter.getComponentKey();
if (getComponentKeyToAdapterCache().containsKey(componentKey)) {
throw new PicoCompositionException("Duplicate Keys not allowed. Duplicate for '" + componentKey + "'");
}
getModifiableComponentAdapterList().add(componentAdapter);
getComponentKeyToAdapterCache().put(componentKey, componentAdapter);
return this;
}
/**
* {@inheritDoc}
* This method can be used to override the ComponentAdapter created by the {@link ComponentFactory}
* passed to the constructor of this container.
*/
public MutablePicoContainer addAdapter(final ComponentAdapter> componentAdapter) {
return addAdapter(componentAdapter, this.containerProperties);
}
/** {@inheritDoc} **/
public MutablePicoContainer addAdapter(final ComponentAdapter> componentAdapter, final Properties properties) {
Properties tmpProperties = (Properties)properties.clone();
AbstractBehaviorFactory.removePropertiesIfPresent(tmpProperties, Characteristics.USE_NAMES);
if (AbstractBehaviorFactory.removePropertiesIfPresent(tmpProperties, Characteristics.NONE) == false && componentFactory instanceof BehaviorFactory) {
MutablePicoContainer container = addAdapterInternal(((BehaviorFactory)componentFactory).addComponentAdapter(
componentMonitor,
lifecycleStrategy,
tmpProperties,
componentAdapter));
throwIfPropertiesLeft(tmpProperties);
return container;
} else {
return addAdapterInternal(componentAdapter);
}
}
/** {@inheritDoc} **/
public ComponentAdapter removeComponent(final Object componentKey) {
lifecycleState.removingComponent();
ComponentAdapter adapter = (ComponentAdapter) getComponentKeyToAdapterCache().remove(componentKey);
getModifiableComponentAdapterList().remove(adapter);
getOrderedComponentAdapters().remove(adapter);
return adapter;
}
/**
* {@inheritDoc}
* The returned ComponentAdapter will be an {@link org.picocontainer.adapters.InstanceAdapter}.
*/
public MutablePicoContainer addComponent(final Object implOrInstance) {
return addComponent(implOrInstance, this.containerProperties);
}
private MutablePicoContainer addComponent(final Object implOrInstance, final Properties props) {
Class> clazz;
if (implOrInstance instanceof String) {
return addComponent(implOrInstance, implOrInstance);
}
if (implOrInstance instanceof Class) {
clazz = (Class>)implOrInstance;
} else {
clazz = implOrInstance.getClass();
}
return addComponent(clazz, implOrInstance, props);
}
public MutablePicoContainer addConfig(final String name, final Object val) {
return addAdapterInternal(new InstanceAdapter(name, val, lifecycleStrategy, componentMonitor));
}
/**
* {@inheritDoc}
* The returned ComponentAdapter will be instantiated by the {@link ComponentFactory}
* passed to the container's constructor.
*/
public MutablePicoContainer addComponent(final Object componentKey,
final Object componentImplementationOrInstance,
final Parameter... parameters) {
return this.addComponent(componentKey, componentImplementationOrInstance, this.containerProperties, parameters);
}
private MutablePicoContainer addComponent(final Object componentKey,
final Object componentImplementationOrInstance,
final Properties properties,
Parameter... parameters) {
if (parameters != null && parameters.length == 0) {
parameters = null; // backwards compatibility! solve this better later - Paul
}
//New replacement for Parameter.ZERO.
if (parameters != null && parameters.length == 1 && DefaultConstructorParameter.INSTANCE.equals(parameters[0])) {
parameters = new Parameter[0];
}
if (componentImplementationOrInstance instanceof Class) {
Properties tmpProperties = (Properties) properties.clone();
ComponentAdapter> adapter = componentFactory.createComponentAdapter(componentMonitor,
lifecycleStrategy,
tmpProperties,
componentKey,
(Class>)componentImplementationOrInstance,
parameters);
AbstractBehaviorFactory.removePropertiesIfPresent(tmpProperties, Characteristics.USE_NAMES);
throwIfPropertiesLeft(tmpProperties);
if (lifecycleState.isStarted()) {
addAdapterIfStartable(adapter);
potentiallyStartAdapter(adapter);
}
return addAdapterInternal(adapter);
} else {
ComponentAdapter> adapter =
new InstanceAdapter(componentKey, componentImplementationOrInstance, lifecycleStrategy, componentMonitor);
if (lifecycleState.isStarted()) {
addAdapterIfStartable(adapter);
potentiallyStartAdapter(adapter);
}
return addAdapter(adapter, properties);
}
}
private void throwIfPropertiesLeft(final Properties tmpProperties) {
if(tmpProperties.size() > 0) {
throw new PicoCompositionException("Unprocessed Characteristics:" + tmpProperties +", please refer to http://picocontainer.org/unprocessed-properties-help.html");
}
}
private synchronized void addOrderedComponentAdapter(final ComponentAdapter> componentAdapter) {
if (!getOrderedComponentAdapters().contains(componentAdapter)) {
getOrderedComponentAdapters().add(componentAdapter);
}
}
public List getComponents() throws PicoException {
return getComponents(Object.class);
}
public List getComponents(final Class componentType) {
if (componentType == null) {
return Collections.emptyList();
}
Map, T> adapterToInstanceMap = new HashMap, T>();
List result = new ArrayList();
synchronized(this) {
for (ComponentAdapter> componentAdapter : getModifiableComponentAdapterList()) {
if (componentType.isAssignableFrom(componentAdapter.getComponentImplementation())) {
ComponentAdapter typedComponentAdapter = typeComponentAdapter(componentAdapter);
T componentInstance = getLocalInstance(typedComponentAdapter);
adapterToInstanceMap.put(typedComponentAdapter, componentInstance);
}
}
for (ComponentAdapter> componentAdapter : getOrderedComponentAdapters()) {
final T componentInstance = adapterToInstanceMap.get(componentAdapter);
if (componentInstance != null) {
// may be null in the case of the "implicit" addAdapter
// representing "this".
result.add(componentInstance);
}
}
}
return result;
}
private T getLocalInstance(final ComponentAdapter typedComponentAdapter) {
T componentInstance = typedComponentAdapter.getComponentInstance(this, ComponentAdapter.NOTHING.class);
// This is to ensure all are added. (Indirect dependencies will be added
// from InstantiatingComponentAdapter).
addOrderedComponentAdapter(typedComponentAdapter);
return componentInstance;
}
@SuppressWarnings({ "unchecked" })
private static ComponentAdapter typeComponentAdapter(final ComponentAdapter> componentAdapter) {
return (ComponentAdapter)componentAdapter;
}
public Object getComponent(final Object componentKeyOrType) {
return getComponent(componentKeyOrType, null);
}
public Object getComponent(final Object componentKeyOrType, Type into) {
synchronized (this) {
if (intoThreadLocal == null) {
intoThreadLocal = new IntoThreadLocal();
}
}
intoThreadLocal.set(into);
try {
return getComponent(componentKeyOrType, (Class extends Annotation>) null);
} finally {
intoThreadLocal.set(null);
}
}
public Object getComponent(final Object componentKeyOrType, final Class extends Annotation> annotation) {
ComponentAdapter> componentAdapter = null;
Object component;
try {
if (annotation != null) {
componentAdapter = getComponentAdapter((Class>)componentKeyOrType, annotation);
component = componentAdapter == null ? null : getInstance(componentAdapter, null);
} else if (componentKeyOrType instanceof Class) {
componentAdapter = getComponentAdapter((Class>)componentKeyOrType, (NameBinding) null);
component = componentAdapter == null ? null : getInstance(componentAdapter, (Class>)componentKeyOrType);
} else {
componentAdapter = getComponentAdapter(componentKeyOrType);
component = componentAdapter == null ? null : getInstance(componentAdapter, null);
}
} catch (AbstractInjector.AmbiguousComponentResolutionException e) {
if (componentAdapter != null) {
e.setComponent(findInjectorOrInstanceAdapter(componentAdapter).toString());
}
throw e;
}
return decorateComponent(component, componentAdapter);
}
/**
* This is invoked when getComponent(..) is called. It allows extendees to decorate a
* component before it is returned to the caller.
* @param component the component that will be returned for getComponent(..)
* @param componentAdapter the component adapter that made that component
* @return the component (the same as that passed in by default)
*/
protected Object decorateComponent(Object component, ComponentAdapter> componentAdapter) {
if (componentAdapter instanceof ComponentLifecycle>
&& lifecycleStrategy.isLazy(componentAdapter) // is Lazy
&& !((ComponentLifecycle>) componentAdapter).isStarted()) {
((ComponentLifecycle>)componentAdapter).start(this);
}
return component;
}
public T getComponent(final Class componentType) {
Object o = getComponent((Object)componentType, null);
return componentType.cast(o);
}
public T getComponent(final Class componentType, final Class extends Annotation> binding) {
Object o = getComponent((Object)componentType, binding);
return componentType.cast(o);
}
private Object getInstance(final ComponentAdapter> componentAdapter, Class componentKey) {
// check whether this is our adapter
// we need to check this to ensure up-down dependencies cannot be followed
final boolean isLocal = getModifiableComponentAdapterList().contains(componentAdapter);
if (isLocal || componentAdapter instanceof LateInstance) {
Object instance;
try {
if (componentAdapter instanceof FactoryInjector) {
instance = ((FactoryInjector) componentAdapter).getComponentInstance(this, getInto());
} else {
instance = componentAdapter.getComponentInstance(this, getInto());
}
} catch (AbstractInjector.CyclicDependencyException e) {
if (parent != null) {
instance = getParent().getComponent(componentAdapter.getComponentKey());
if (instance != null) {
return instance;
}
}
throw e;
}
addOrderedComponentAdapter(componentAdapter);
return instance;
} else if (parent != null) {
Object key = componentKey;
if (key == null) {
key = componentAdapter.getComponentKey();
}
return getParent().getComponent(key);
}
return null;
}
private Type getInto() {
if (intoThreadLocal == null) {
return null;
}
return intoThreadLocal.get();
}
/** {@inheritDoc} **/
public PicoContainer getParent() {
return parent;
}
/** {@inheritDoc} **/
public ComponentAdapter removeComponentByInstance(final T componentInstance) {
for (ComponentAdapter> componentAdapter : getModifiableComponentAdapterList()) {
if (getLocalInstance(componentAdapter).equals(componentInstance)) {
return removeComponent(componentAdapter.getComponentKey());
}
}
return null;
}
/**
* Start the components of this PicoContainer and all its logical child containers.
* The starting of the child container is only attempted if the parent
* container start successfully. The child container for which start is attempted
* is tracked so that upon stop, only those need to be stopped.
* The lifecycle operation is delegated to the component adapter,
* if it is an instance of {@link Behavior lifecycle manager}.
* The actual {@link LifecycleStrategy lifecycle strategy} supported
* depends on the concrete implementation of the adapter.
*
* @see Behavior
* @see LifecycleStrategy
* @see #makeChildContainer()
* @see #addChildContainer(PicoContainer)
* @see #removeChildContainer(PicoContainer)
*/
public synchronized void start() {
lifecycleState.starting();
startAdapters();
childrenStarted.clear();
for (PicoContainer child : children) {
childrenStarted.add(new WeakReference(child));
if (child instanceof Startable) {
((Startable)child).start();
}
}
}
/**
* Stop the components of this PicoContainer and all its logical child containers.
* The stopping of the child containers is only attempted for those that have been
* started, possibly not successfully.
* The lifecycle operation is delegated to the component adapter,
* if it is an instance of {@link Behavior lifecycle manager}.
* The actual {@link LifecycleStrategy lifecycle strategy} supported
* depends on the concrete implementation of the adapter.
*
* @see Behavior
* @see LifecycleStrategy
* @see #makeChildContainer()
* @see #addChildContainer(PicoContainer)
* @see #removeChildContainer(PicoContainer)
*/
public synchronized void stop() {
lifecycleState.stopping();
for (PicoContainer child : children) {
if (childStarted(child)) {
if (child instanceof Startable) {
((Startable)child).stop();
}
}
}
stopAdapters();
lifecycleState.stopped();
}
/**
* Checks the status of the child container to see if it's been started
* to prevent IllegalStateException upon stop
*
* @param child the child PicoContainer
*
* @return A boolean, true
if the container is started
*/
private boolean childStarted(final PicoContainer child) {
for (WeakReference eachChild : childrenStarted) {
PicoContainer ref = eachChild.get();
if (ref == null) {
continue;
}
if (child.equals(ref)) {
return true;
}
}
return false;
}
/**
* Dispose the components of this PicoContainer and all its logical child containers.
* The lifecycle operation is delegated to the component adapter,
* if it is an instance of {@link Behavior lifecycle manager}.
* The actual {@link LifecycleStrategy lifecycle strategy} supported
* depends on the concrete implementation of the adapter.
*
* @see Behavior
* @see LifecycleStrategy
* @see #makeChildContainer()
* @see #addChildContainer(PicoContainer)
* @see #removeChildContainer(PicoContainer)
*/
public synchronized void dispose() {
if (lifecycleState.isStarted()) {
stop();
}
lifecycleState.disposing();
for (PicoContainer child : children) {
if (child instanceof MutablePicoContainer) {
((Disposable)child).dispose();
}
}
disposeAdapters();
lifecycleState.disposed();
}
public synchronized void setLifecycleState(LifecycleState lifecycleState) {
this.lifecycleState = lifecycleState;
}
public MutablePicoContainer makeChildContainer() {
DefaultPicoContainer pc = new DefaultPicoContainer(componentFactory, lifecycleStrategy, this, componentMonitor);
addChildContainer(pc);
return pc;
}
/**
* Checks for identical references in the child container. It doesn't
* traverse an entire hierarchy, namely it simply checks for child containers
* that are equal to the current container.
* @param child
*/
private void checkCircularChildDependencies(PicoContainer child) {
final String MESSAGE = "Cannot have circular dependency between parent %s and child: %s";
if (child == this) {
throw new IllegalArgumentException(String.format(MESSAGE,this,child));
}
//Todo: Circular Import Dependency on AbstractDelegatingPicoContainer
if (child instanceof AbstractDelegatingPicoContainer) {
AbstractDelegatingPicoContainer delegateChild = (AbstractDelegatingPicoContainer) child;
while(delegateChild != null) {
PicoContainer delegateInstance = delegateChild.getDelegate();
if (this == delegateInstance) {
throw new IllegalArgumentException(String.format(MESSAGE,this,child));
}
if (delegateInstance instanceof AbstractDelegatingPicoContainer) {
delegateChild = (AbstractDelegatingPicoContainer) delegateInstance;
} else {
delegateChild = null;
}
}
}
}
public MutablePicoContainer addChildContainer(final PicoContainer child) {
checkCircularChildDependencies(child);
if (children.add(child)) {
// TODO Should only be added if child container has also be started
if (lifecycleState.isStarted()) {
childrenStarted.add(new WeakReference(child));
}
}
return this;
}
public boolean removeChildContainer(final PicoContainer child) {
final boolean result = children.remove(child);
WeakReference foundRef = null;
for (WeakReference eachChild : childrenStarted) {
PicoContainer ref = eachChild.get();
if (ref.equals(child)) {
foundRef = eachChild;
break;
}
}
if (foundRef != null) {
childrenStarted.remove(foundRef);
}
return result;
}
public MutablePicoContainer change(final Properties... properties) {
for (Properties c : properties) {
Enumeration e = (Enumeration) c.propertyNames();
while (e.hasMoreElements()) {
String s = e.nextElement();
containerProperties.setProperty(s,c.getProperty(s));
}
}
return this;
}
public MutablePicoContainer as(final Properties... properties) {
return new AsPropertiesPicoContainer(properties);
}
public void accept(final PicoVisitor visitor) {
//TODO Pico 3 : change accept signatures to allow abort at any point in the traversal.
boolean shouldContinue = visitor.visitContainer(this);
if (!shouldContinue) {
return;
}
componentFactory.accept(visitor); // will cascade through behaviors
final List> componentAdapters = new ArrayList>(getComponentAdapters());
for (ComponentAdapter> componentAdapter : componentAdapters) {
componentAdapter.accept(visitor);
}
final List allChildren = new ArrayList(children);
for (PicoContainer child : allChildren) {
child.accept(visitor);
}
}
/**
* Changes monitor in the ComponentFactory, the component adapters
* and the child containers, if these support a ComponentMonitorStrategy.
* {@inheritDoc}
*/
public void changeMonitor(final ComponentMonitor monitor) {
this.componentMonitor = monitor;
if (lifecycleStrategy instanceof ComponentMonitorStrategy) {
((ComponentMonitorStrategy)lifecycleStrategy).changeMonitor(monitor);
}
for (ComponentAdapter> adapter : getModifiableComponentAdapterList()) {
if (adapter instanceof ComponentMonitorStrategy) {
((ComponentMonitorStrategy)adapter).changeMonitor(monitor);
}
}
for (PicoContainer child : children) {
if (child instanceof ComponentMonitorStrategy) {
((ComponentMonitorStrategy)child).changeMonitor(monitor);
}
}
}
/**
* Returns the first current monitor found in the ComponentFactory, the component adapters
* and the child containers, if these support a ComponentMonitorStrategy.
* {@inheritDoc}
*
* @throws PicoCompositionException if no component monitor is found in container or its children
*/
public ComponentMonitor currentMonitor() {
return componentMonitor;
}
/**
* Loops over all component adapters and invokes
* start(PicoContainer) method on the ones which are LifecycleManagers
*/
private void startAdapters() {
Collection> adapters = getComponentAdapters();
for (ComponentAdapter> adapter : adapters) {
addAdapterIfStartable(adapter);
}
adapters = getOrderedComponentAdapters();
// clone the adapters
List> adaptersClone = new ArrayList>(adapters);
for (final ComponentAdapter> adapter : adaptersClone) {
potentiallyStartAdapter(adapter);
}
}
protected void potentiallyStartAdapter(ComponentAdapter> adapter) {
if (adapter instanceof ComponentLifecycle) {
if (!lifecycleStrategy.isLazy(adapter)) {
((ComponentLifecycle>)adapter).start(this);
}
}
}
private void addAdapterIfStartable(ComponentAdapter> adapter) {
if (adapter instanceof ComponentLifecycle) {
ComponentLifecycle> componentLifecycle = (ComponentLifecycle>)adapter;
if (componentLifecycle.componentHasLifecycle()) {
// create an instance, it will be added to the ordered CA list
instantiateComponentAsIsStartable(adapter);
addOrderedComponentAdapter(adapter);
}
}
}
protected void instantiateComponentAsIsStartable(ComponentAdapter> adapter) {
if (!lifecycleStrategy.isLazy(adapter)) {
adapter.getComponentInstance(DefaultPicoContainer.this, ComponentAdapter.NOTHING.class);
}
}
/**
* Loops over started component adapters (in inverse order) and invokes
* stop(PicoContainer) method on the ones which are LifecycleManagers
*/
private void stopAdapters() {
for (int i = getOrderedComponentAdapters().size() - 1; 0 <= i; i--) {
ComponentAdapter> adapter = getOrderedComponentAdapters().get(i);
if (adapter instanceof ComponentLifecycle) {
ComponentLifecycle> componentLifecycle = (ComponentLifecycle>)adapter;
if (componentLifecycle.componentHasLifecycle() && componentLifecycle.isStarted()) {
componentLifecycle.stop(DefaultPicoContainer.this);
}
}
}
}
/**
* Loops over all component adapters (in inverse order) and invokes
* dispose(PicoContainer) method on the ones which are LifecycleManagers
*/
private void disposeAdapters() {
for (int i = getOrderedComponentAdapters().size() - 1; 0 <= i; i--) {
ComponentAdapter> adapter = getOrderedComponentAdapters().get(i);
if (adapter instanceof ComponentLifecycle) {
ComponentLifecycle> componentLifecycle = (ComponentLifecycle>)adapter;
componentLifecycle.dispose(DefaultPicoContainer.this);
}
}
}
/**
* @return the orderedComponentAdapters
*/
protected List> getOrderedComponentAdapters() {
return orderedComponentAdapters;
}
/**
* @return the componentKeyToAdapterCache
*/
protected Map> getComponentKeyToAdapterCache() {
return componentKeyToAdapterCache;
}
/**
* @return the componentAdapters
*/
protected Set> getModifiableComponentAdapterList() {
return componentAdapters;
}
public synchronized void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return String.format("%s:%d<%s", (name != null ? name : super.toString()), this.componentAdapters.size(), (parent != null && !(parent instanceof EmptyPicoContainer)? parent.toString() : "|"));
}
/**
* If this container has a set of converters, then return it.
* If it does not, and the parent (or their parent ..) does, use that
* If they do not, return a NullObject implementation (ConversNothing)
* @return the converters
*/
public synchronized Converters getConverters() {
if (converters == null) {
if (parent == null || (parent instanceof Converting && ((Converting) parent).getConverters() instanceof ConvertsNothing)) {
converters = new BuiltInConverters();
} else {
return ((Converting) parent).getConverters();
}
}
return converters;
}
@SuppressWarnings("synthetic-access")
private class AsPropertiesPicoContainer extends AbstractDelegatingMutablePicoContainer {
private final Properties properties;
public AsPropertiesPicoContainer(final Properties... props) {
super(DefaultPicoContainer.this);
properties = (Properties) containerProperties.clone();
for (Properties c : props) {
Enumeration> e = c.propertyNames();
while (e.hasMoreElements()) {
String s = (String)e.nextElement();
properties.setProperty(s,c.getProperty(s));
}
}
}
@Override
@SuppressWarnings("unused")
public MutablePicoContainer as( Properties... props) {
throw new PicoCompositionException("Syntax 'as(FOO).as(BAR)' not allowed, do 'as(FOO, BAR)' instead");
}
@Override
public MutablePicoContainer makeChildContainer() {
return getDelegate().makeChildContainer();
}
@Override
public MutablePicoContainer addComponent(final Object componentKey,
final Object componentImplementationOrInstance,
final Parameter... parameters) throws PicoCompositionException {
return DefaultPicoContainer.this.addComponent(componentKey,
componentImplementationOrInstance,
properties,
parameters);
}
@Override
public MutablePicoContainer addComponent(final Object implOrInstance) throws PicoCompositionException {
return DefaultPicoContainer.this.addComponent(implOrInstance, properties);
}
@Override
public MutablePicoContainer addAdapter(final ComponentAdapter> componentAdapter) throws PicoCompositionException {
return DefaultPicoContainer.this.addAdapter(componentAdapter, properties);
}
/**
* {@inheritDoc}
* @see org.picocontainer.MutablePicoContainer#getLifecycleState()
*/
@Override
public LifecycleState getLifecycleState() {
return DefaultPicoContainer.this.getLifecycleState();
}
/**
* {@inheritDoc}
* @see org.picocontainer.MutablePicoContainer#getName()
*/
@Override
public String getName() {
return DefaultPicoContainer.this.getName();
}
}
private static class IntoThreadLocal extends ThreadLocal implements Serializable {
@Override
protected Type initialValue() {
return ComponentAdapter.NOTHING.class;
}
}
/**
* {@inheritDoc}
* @see org.picocontainer.MutablePicoContainer#getLifecycleState()
*/
public synchronized LifecycleState getLifecycleState() {
return lifecycleState;
}
/**
* {@inheritDoc}
* @see org.picocontainer.MutablePicoContainer#getName()
*/
public synchronized String getName() {
return this.name;
}
}
libpicocontainer-java-2.15/org/picocontainer/Disposable.java 0000664 0000000 0000000 00000003126 12422476443 0024272 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (c) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the license.html file. *
* *
* Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
*****************************************************************************/
package org.picocontainer;
/**
* An interface which is implemented by components that need to dispose of resources during the shutdown of that
* component. The {@link Disposable#dispose()} must be called once during shutdown, directly after {@link
* Startable#stop()} (if the component implements the {@link Startable} interface).
* @see org.picocontainer.Startable the Startable interface if you need to start()
and
* stop()
semantics.
* @see org.picocontainer.PicoContainer the main PicoContainer interface (and hence its subinterfaces and
* implementations like {@link DefaultPicoContainer}) implement this interface.
*/
public interface Disposable {
/**
* Dispose this component. The component should deallocate all resources. The contract for this method defines a
* single call at the end of this component's life.
*/
void dispose();
}
libpicocontainer-java-2.15/org/picocontainer/Emjection.java 0000664 0000000 0000000 00000003711 12422476443 0024122 0 ustar 00root root 0000000 0000000 package org.picocontainer;
import org.picocontainer.containers.ImmutablePicoContainer;
import org.picocontainer.containers.TransientPicoContainer;
import org.picocontainer.injectors.ConstructorInjection;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
public class Emjection {
private PicoContainer pico;
public void setPico(ImmutablePicoContainer container) {
if (pico != null) {
throw new PicoCompositionException("Emjection can only be setup once per component");
}
pico = container;
}
public static T neu(Class type, Emjection emjection, Object... args) {
if (emjection.pico == null) {
throw new PicoCompositionException("blah");
}
TransientPicoContainer tpc = new TransientPicoContainer(new ConstructorInjection(), emjection.pico);
for (Object arg : args) {
tpc.addComponent(arg);
}
T inst = tpc.getComponent(type);
if (inst == null) {
tpc.addComponent(type);
inst = tpc.getComponent(type);
}
setPico(inst, tpc);
return inst;
}
private static void setPico(Object inst, PicoContainer container) {
try {
Field field = inst.getClass().getDeclaredField("emjection");
field.setAccessible(true);
Emjection e2 = (Emjection) field.get(inst);
e2.setPico(new ImmutablePicoContainer(container));
} catch (NoSuchFieldException e) {
throw new PicoCompositionException("Components created via emjection have to have a field 'private Emjection emjection'. " + inst.getClass() + " is missing that field");
} catch (IllegalAccessException e) {
throw new PicoCompositionException("unable to access field called emjection on " + inst.getClass());
}
}
public static void setupEmjection(Object inst, PicoContainer container) {
setPico(inst, container);
}
}
libpicocontainer-java-2.15/org/picocontainer/InjectionFactory.java 0000664 0000000 0000000 00000001443 12422476443 0025457 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer;
public interface InjectionFactory extends ComponentFactory {
}
libpicocontainer-java-2.15/org/picocontainer/Injector.java 0000664 0000000 0000000 00000002317 12422476443 0023763 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
*****************************************************************************/
package org.picocontainer;
import java.lang.reflect.Type;
/**
* Implementers are responsible for instantiating and injecting dependancies into
* Constructors, Methods and Fields.
*/
public interface Injector extends ComponentAdapter {
/**
* A preexiting component instance can be injected into after instantiation
*
*
* @param container the container that can provide injectable dependencies
* @param into
* @param instance the instance to
* @return
*/
Object decorateComponentInstance(PicoContainer container, Type into, T instance);
}
libpicocontainer-java-2.15/org/picocontainer/LifecycleStrategy.java 0000664 0000000 0000000 00000004660 12422476443 0025633 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
*****************************************************************************/
package org.picocontainer;
/**
* An interface which specifies the lifecycle strategy on the component instance.
* Lifecycle strategies are used by component adapters to delegate the lifecycle
* operations on the component instances.
*
* @author Paul Hammant
* @author Peter Royal
* @author Jörg Schaible
* @author Mauro Talevi
* @see org.picocontainer.Startable
* @see org.picocontainer.Disposable
*/
public interface LifecycleStrategy {
/**
* Invoke the "start" method on the component instance if this is startable.
* It is up to the implementation of the strategy what "start" and "startable" means.
*
* @param component the instance of the component to start
*/
void start(Object component);
/**
* Invoke the "stop" method on the component instance if this is stoppable.
* It is up to the implementation of the strategy what "stop" and "stoppable" means.
*
* @param component the instance of the component to stop
*/
void stop(Object component);
/**
* Invoke the "dispose" method on the component instance if this is disposable.
* It is up to the implementation of the strategy what "dispose" and "disposable" means.
*
* @param component the instance of the component to dispose
*/
void dispose(Object component);
/**
* Test if a component instance has a lifecycle.
* @param type the component's type
*
* @return true
if the component has a lifecycle
*/
boolean hasLifecycle(Class> type);
/**
* Is a component eager (not lazy) in that it should start when start() or equivalent is called,
* or lazy (it will only start on first getComponent() ).
* The default is the first of those two.
*
* @param adapter
* @return true if lazy, false if not lazy
*/
boolean isLazy(ComponentAdapter> adapter);
}
libpicocontainer-java-2.15/org/picocontainer/MutablePicoContainer.java 0000664 0000000 0000000 00000022476 12422476443 0026265 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (c) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Idea by Rachel Davies, Original code by various *
*****************************************************************************/
package org.picocontainer;
import org.picocontainer.lifecycle.LifecycleState;
import java.util.Properties;
/**
* This is the core interface used for registration of components with a container. It is possible to register
* implementations and instances here
*
* @author Paul Hammant
* @author Aslak Hellesøy
* @author Jon Tirsén
* @see See package description for basic overview how to use PicoContainer.
*/
public interface MutablePicoContainer extends PicoContainer, Startable, Disposable {
/**
* Register a component and creates specific instructions on which constructor to use, along with
* which components and/or constants to provide as constructor arguments. These "directives" are
* provided through an array of Parameter objects. Parameter[0] correspondes to the first constructor
* argument, Parameter[N] corresponds to the N+1th constructor argument.
* Tips for Parameter usage
*
* Partial Autowiring: If you have two constructor args to match and you only wish to specify one of the constructors and
* let PicoContainer wire the other one, you can use as parameters:
* new ComponentParameter() , new ComponentParameter("someService")
* The default constructor for the component parameter indicates auto-wiring should take place for
* that parameter.
*
* Force No-Arg constructor usage: If you wish to force a component to be constructed with
* the no-arg constructor, use a zero length Parameter array. Ex: new Parameter[0]
*
*
* @param componentKey a key that identifies the component. Must be unique within the container. The type
* of the key object has no semantic significance unless explicitly specified in the
* documentation of the implementing container.
* @param componentImplementationOrInstance
* the component's implementation class. This must be a concrete class (ie, a
* class that can be instantiated). Or an intance of the compoent.
* @param parameters the parameters that gives the container hints about what arguments to pass
* to the constructor when it is instantiated. Container implementations may ignore
* one or more of these hints.
*
* @return the same instance of MutablePicoContainer
*
* @throws PicoCompositionException if registration of the component fails.
* @see org.picocontainer.Parameter
* @see org.picocontainer.parameters.ConstantParameter
* @see org.picocontainer.parameters.ComponentParameter
*/
MutablePicoContainer addComponent(Object componentKey,
Object componentImplementationOrInstance,
Parameter... parameters);
/**
* Register an arbitrary object. The class of the object will be used as a key. Calling this method is equivalent to
* calling addComponent(componentImplementation, componentImplementation)
.
*
* @param implOrInstance Component implementation or instance
*
* @return the same instance of MutablePicoContainer
*
* @throws PicoCompositionException if registration fails.
*/
MutablePicoContainer addComponent(Object implOrInstance);
/**
* Register a config item.
*
* @param name the name of the config item
* @param val the value of the config item
*
* @return the same instance of MutablePicoContainer
*
* @throws PicoCompositionException if registration fails.
*/
MutablePicoContainer addConfig(String name, Object val);
/**
* Register a component via a ComponentAdapter. Use this if you need fine grained control over what
* ComponentAdapter to use for a specific component. The adapter will be wrapped in whatever behaviors that the
* the container has been set up with. If you want to bypass that behavior for the adapter you are adding,
* you should use Characteristics.NONE like so pico.as(Characteristics.NONE).addAdapter(...)
*
* @param componentAdapter the adapter
*
* @return the same instance of MutablePicoContainer
*
* @throws PicoCompositionException if registration fails.
*/
MutablePicoContainer addAdapter(ComponentAdapter> componentAdapter);
/**
* Unregister a component by key.
*
* @param componentKey key of the component to unregister.
*
* @return the ComponentAdapter that was associated with this component.
*/
ComponentAdapter removeComponent(Object componentKey);
/**
* Unregister a component by instance.
*
* @param componentInstance the component instance to unregister.
*
* @return the same instance of MutablePicoContainer
*/
ComponentAdapter removeComponentByInstance(T componentInstance);
/**
* Make a child container, using both the same implementation of MutablePicoContainer as the parent
* and identical behaviors as well.
* It will have a reference to this as parent. This will list the resulting MPC as a child.
* Lifecycle events will be cascaded from parent to child
* as a consequence of this.
* Note that for long-lived parent containers, you need to unregister child containers
* made with this call before disposing or you will leak memory. (Experience
* speaking here! )
* Incorrect Example:
*
* MutablePicoContainer parent = new PicoBuilder().withCaching().withLifecycle().build();
* MutablePicoContainer child = parent.makeChildContainer();
* child = null; //Child still retains in memory because parent still holds reference.
*
* Correct Example:
*
* MutablePicoContainer parent = new PicoBuilder().withCaching().withLifecycle().build();
* MutablePicoContainer child = parent.makeChildContainer();
* parent.removeChildContainer(child); //Remove the bi-directional references.
* child = null;
*
* @return the new child container.
*/
MutablePicoContainer makeChildContainer();
/**
* Add a child container. This action will list the the 'child' as exactly that in the parents scope.
* It will not change the child's view of a parent. That is determined by the constructor arguments of the child
* itself. Lifecycle events will be cascaded from parent to child
* as a consequence of calling this method.
*
* @param child the child container
*
* @return the same instance of MutablePicoContainer
*
*/
MutablePicoContainer addChildContainer(PicoContainer child);
/**
* Remove a child container from this container. It will not change the child's view of a parent.
* Lifecycle event will no longer be cascaded from the parent to the child.
*
* @param child the child container
*
* @return true
if the child container has been removed.
*
*/
boolean removeChildContainer(PicoContainer child);
/**
* You can change the characteristic of registration of all subsequent components in this container.
*
* @param properties
* @return the same Pico instance with changed properties
*/
MutablePicoContainer change(Properties... properties);
/**
* You can set for the following operation only the characteristic of registration of a component on the fly.
*
* @param properties
* @return the same Pico instance with temporary properties
*/
MutablePicoContainer as(Properties... properties);
/**
* Name the container instance, to assist debugging or other indexing.
*
* @param name the name to call it.
* @since 2.8
*/
void setName(String name);
/**
* To assist ThreadLocal usage, LifecycleState can be set. No need to use this for normal usages.
* @param lifecycleState the lifecyle state to use.
* @since 2.8
*/
void setLifecycleState(LifecycleState lifecycleState);
/**
* Retrieve the name set (if any).
* @return Retrieve the arbitrary name of the container set by calling {@link #setName(String) setName}.
* @since 2.10.2
*/
String getName();
/**
* Allow querying of the current lifecycle state of a MutablePicoContainer.
* @return the current Lifecycle State.
* @since 2.10.2
*/
LifecycleState getLifecycleState();
}
libpicocontainer-java-2.15/org/picocontainer/NameBinding.java 0000664 0000000 0000000 00000001433 12422476443 0024357 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer;
public interface NameBinding {
String getName();
}
libpicocontainer-java-2.15/org/picocontainer/ObjectReference.java 0000664 0000000 0000000 00000002541 12422476443 0025232 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
*****************************************************************************/
package org.picocontainer;
/**
* A way to refer to objects that are stored in "awkward" places (for example inside a
* HttpSession
or {@link ThreadLocal}).
*
* This interface is typically implemented by someone integrating Pico into an existing container.
*
* @author Joe Walnes
*/
public interface ObjectReference {
/**
* Retrieve an actual reference to the object. Returns null if the reference is not available
* or has not been populated yet.
*
* @return an actual reference to the object.
*/
T get();
/**
* Assign an object to the reference.
*
* @param item the object to assign to the reference. May be null
.
*/
void set(T item);
}
libpicocontainer-java-2.15/org/picocontainer/Parameter.java 0000664 0000000 0000000 00000016323 12422476443 0024130 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (c) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Idea by Rachel Davies, Original code by Jon Tirsen *
*****************************************************************************/
package org.picocontainer;
import org.picocontainer.parameters.ComponentParameter;
import org.picocontainer.parameters.DefaultConstructorParameter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
/**
* This class provides control over the arguments that will be passed to a constructor. It can be used for finer control over
* what arguments are passed to a particular constructor.
*
* @author Jon Tirsén
* @author Aslak Hellesøy
* @author Thomas Heller
* @see MutablePicoContainer#addComponent(Object,Object,Parameter[]) a method on the
* {@link MutablePicoContainer} interface which allows passing in of an array of {@linkplain Parameter Parameters}.
* @see org.picocontainer.parameters.ComponentParameter an implementation of this interface that allows you to specify the key
* used for resolving the parameter.
* @see org.picocontainer.parameters.ConstantParameter an implementation of this interface that allows you to specify a constant
* that will be used for resolving the parameter.
*/
public interface Parameter {
/**
* Zero parameter is used when you wish a component to be instantiated with its default constructor. Ex:
*
*
* MutablePicoContainer mpc = new PicoBuilder().build();
* mpc.addComponent(Map.class, HashMap.class, Parameter.ZERO);
* mpc.addComponent(List.class, ArrayList.class, Parameter.ZERO);
*
*
* By specifying the default constructor in this example code, you allow PicoContainer to recognize
* that HashMap(Collection) should not be used and avoid a CircularDependencyException.
*/
Parameter[] ZERO = new Parameter[] {DefaultConstructorParameter.INSTANCE};
Parameter[] DEFAULT = new Parameter[]{ ComponentParameter.DEFAULT };
/**
* Check if the Parameter can satisfy the expected type using the container.
*
* @param container the container from which dependencies are resolved.
* @param forAdapter the {@link org.picocontainer.ComponentAdapter} that is asking for the instance
* @param injecteeAdapter the adapter to be injected into (null for N/A)
* @param expectedType the required type
* @param expectedNameBinding Expected parameter name
* @param useNames should use parameter names for disambiguation
* @param binding @return true
if the component parameter can be resolved.
* @since 2.8.1
*
*/
Resolver resolve(PicoContainer container, ComponentAdapter> forAdapter,
ComponentAdapter> injecteeAdapter, Type expectedType, NameBinding expectedNameBinding,
boolean useNames, Annotation binding);
/**
* Verify that the Parameter can satisfy the expected type using the container
*
* @param container the container from which dependencies are resolved.
* @param adapter the {@link ComponentAdapter} that is asking for the verification
* @param expectedType the required type
* @param expectedNameBinding Expected parameter name
*
* @param useNames
* @param binding
* @throws PicoCompositionException if parameter and its dependencies cannot be resolved
*/
void verify(PicoContainer container, ComponentAdapter> adapter,
Type expectedType, NameBinding expectedNameBinding,
boolean useNames, Annotation binding);
/**
* Accepts a visitor for this Parameter. The method is normally called by visiting a {@link ComponentAdapter}, that
* cascades the {@linkplain PicoVisitor visitor} also down to all its {@linkplain Parameter Parameters}.
*
* @param visitor the visitor.
*
*/
void accept(PicoVisitor visitor);
@Deprecated
Object resolveInstance(PicoContainer container, ComponentAdapter> forAdapter,
Type expectedType, NameBinding expectedNameBinding,
boolean useNames, Annotation binding);
@Deprecated
boolean isResolvable(PicoContainer container, ComponentAdapter> forAdapter,
Type expectedType, NameBinding expectedNameBinding,
boolean useNames, Annotation binding);
/**
* Resolver is used transitarily during resolving of Parameters.
* isResolvable() and resolveInstance() in series do not cause resolveAdapter() twice
*/
public static interface Resolver {
/**
* @return can the parameter be resolved
*/
public boolean isResolved();
/**
* @return the instance to be used to inject as a parameter
*/
public Object resolveInstance();
/**
* @return the ComponentAdapter for the parameter in question
*/
public ComponentAdapter> getComponentAdapter();
}
/**
* The Parameter cannot (ever) be resolved
*/
public static class NotResolved implements Resolver {
public boolean isResolved() {
return false;
}
public Object resolveInstance() {
return null;
}
public ComponentAdapter> getComponentAdapter() {
return null;
}
}
/**
* Delegate to another reolver
*/
public abstract static class DelegateResolver implements Resolver {
private final Resolver delegate;
public DelegateResolver(Resolver delegate) {
this.delegate = delegate;
}
public boolean isResolved() {
return delegate.isResolved();
}
public Object resolveInstance() {
return delegate.resolveInstance();
}
public ComponentAdapter> getComponentAdapter() {
return delegate.getComponentAdapter();
}
}
/**
* A fixed value wrapped as a Resolver
*/
public static class ValueResolver implements Resolver {
private final boolean resolvable;
private final Object value;
private final ComponentAdapter> adapter;
public ValueResolver(boolean resolvable, Object value, ComponentAdapter> adapter) {
this.resolvable = resolvable;
this.value = value;
this.adapter = adapter;
}
public boolean isResolved() {
return resolvable;
}
public Object resolveInstance() {
return value;
}
public ComponentAdapter> getComponentAdapter() {
return adapter;
}
}
}
libpicocontainer-java-2.15/org/picocontainer/PicoBuilder.java 0000664 0000000 0000000 00000032317 12422476443 0024412 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer;
import org.picocontainer.behaviors.Automating;
import org.picocontainer.behaviors.Locking;
import org.picocontainer.behaviors.PropertyApplying;
import org.picocontainer.behaviors.Synchronizing;
import org.picocontainer.containers.EmptyPicoContainer;
import org.picocontainer.containers.TransientPicoContainer;
import org.picocontainer.injectors.CompositeInjection;
import org.picocontainer.injectors.MethodInjection;
import org.picocontainer.lifecycle.JavaEE5LifecycleStrategy;
import org.picocontainer.lifecycle.NullLifecycleStrategy;
import org.picocontainer.lifecycle.ReflectionLifecycleStrategy;
import org.picocontainer.lifecycle.StartableLifecycleStrategy;
import org.picocontainer.monitors.ConsoleComponentMonitor;
import org.picocontainer.monitors.NullComponentMonitor;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import static org.picocontainer.behaviors.Behaviors.caching;
import static org.picocontainer.behaviors.Behaviors.implementationHiding;
import static org.picocontainer.injectors.Injectors.CDI;
import static org.picocontainer.injectors.Injectors.SDI;
import static org.picocontainer.injectors.Injectors.adaptiveDI;
import static org.picocontainer.injectors.Injectors.annotatedFieldDI;
import static org.picocontainer.injectors.Injectors.annotatedMethodDI;
import static org.picocontainer.injectors.Injectors.namedField;
import static org.picocontainer.injectors.Injectors.namedMethod;
import static org.picocontainer.injectors.Injectors.typedFieldDI;
/**
* Helps assembles the myriad items available to a picocontainer.
* Simple Example:
*
* MutablePicoContainer mpc = new PicoBuilder()
* .withCaching()
* .withLifecycle()
* .build();
*
* @author Paul Hammant
*/
public class PicoBuilder {
private PicoContainer parentContainer;
private Class extends MutablePicoContainer> mpcClass = DefaultPicoContainer.class;
private ComponentMonitor componentMonitor;
private List containerComps = new ArrayList();
private boolean addChildToParent;
private LifecycleStrategy lifecycleStrategy;
private final Stack behaviors = new Stack();
private final List injectors = new ArrayList();
private Class extends ComponentMonitor> componentMonitorClass = NullComponentMonitor.class;
private Class extends LifecycleStrategy> lifecycleStrategyClass = NullLifecycleStrategy.class;
public PicoBuilder(PicoContainer parentContainer, InjectionFactory injectionType) {
this(parentContainer);
addInjector(injectionType);
}
/**
* Constructs a PicoBuilder using the specified PicoContainer as an argument. Note
* that this only creates child -> parent references. You must use parentContainer.addChildContainer()
* to the instance built here if you require child <-> parent references.
* @param parentContainer
*/
public PicoBuilder(PicoContainer parentContainer) {
if (parentContainer != null) {
this.parentContainer = parentContainer;
} else {
this.parentContainer = new EmptyPicoContainer();
}
}
public PicoBuilder(InjectionFactory injectionType) {
this(new EmptyPicoContainer(), injectionType);
}
/**
* Will be used to build a PicoContainer not bound to any parent container.
*/
public PicoBuilder() {
this(new EmptyPicoContainer());
}
public PicoBuilder withLifecycle() {
lifecycleStrategyClass = StartableLifecycleStrategy.class;
lifecycleStrategy = null;
return this;
}
/**
* Constructed PicoContainer will use {@linkplain org.picocontainer.lifecycle.ReflectionLifecycleStrategy ReflectionLifecycle}.
* @return this to allow for method chaining.
*/
public PicoBuilder withReflectionLifecycle() {
lifecycleStrategyClass = ReflectionLifecycleStrategy.class;
lifecycleStrategy = null;
return this;
}
/**
* Allows you to specify your own lifecycle strategy class.
* @param specifiedLifecycleStrategyType lifecycle strategy type.
* @return this to allow for method chaining.
*/
public PicoBuilder withLifecycle(Class extends LifecycleStrategy> specifiedLifecycleStrategyType) {
this.lifecycleStrategyClass = specifiedLifecycleStrategyType;
lifecycleStrategy = null;
return this;
}
/**
* Constructed PicoContainer will use {@linkplain org.picocontainer.lifecycle.JavaEE5LifecycleStrategy JavaEE5LifecycleStrategy}.
* @return this to allow for method chaining.
*/
public PicoBuilder withJavaEE5Lifecycle() {
this.lifecycleStrategyClass = JavaEE5LifecycleStrategy.class;
lifecycleStrategy = null;
return this;
}
/**
* Allows you to fully specify your lifecycle strategy by passing in a built instance
* @param specifiedLifecycleStrategy
* @return this to allow for method chaining.
*/
public PicoBuilder withLifecycle(LifecycleStrategy specifiedLifecycleStrategy) {
this.lifecycleStrategy = specifiedLifecycleStrategy;
lifecycleStrategyClass = null;
return this;
}
public PicoBuilder withConsoleMonitor() {
componentMonitorClass = ConsoleComponentMonitor.class;
return this;
}
public PicoBuilder withMonitor(Class extends ComponentMonitor> cmClass) {
if (cmClass == null) {
throw new NullPointerException("monitor class cannot be null");
}
if (!ComponentMonitor.class.isAssignableFrom(cmClass)) {
throw new ClassCastException(cmClass.getName() + " is not a " + ComponentMonitor.class.getName());
}
componentMonitorClass = cmClass;
componentMonitor = null;
return this;
}
public MutablePicoContainer build() {
DefaultPicoContainer tempContainer = new TransientPicoContainer();
tempContainer.addComponent(PicoContainer.class, parentContainer);
addContainerComponents(tempContainer);
ComponentFactory componentFactory;
if (injectors.size() == 1) {
componentFactory = injectors.get(0);
} else if (injectors.size() == 0) {
componentFactory = adaptiveDI();
} else {
componentFactory = new CompositeInjection(injectors.toArray(new InjectionFactory[injectors.size()]));
}
Stack clonedBehaviors = (Stack< Object >) behaviors.clone();
while (!clonedBehaviors.empty()) {
componentFactory = buildComponentFactory(tempContainer, componentFactory, clonedBehaviors);
}
tempContainer.addComponent(ComponentFactory.class, componentFactory);
buildComponentMonitor(tempContainer);
if (lifecycleStrategy == null) {
tempContainer.addComponent(LifecycleStrategy.class, lifecycleStrategyClass);
} else {
tempContainer.addComponent(LifecycleStrategy.class, lifecycleStrategy);
}
tempContainer.addComponent("mpc", mpcClass);
MutablePicoContainer newContainer = (MutablePicoContainer) tempContainer.getComponent("mpc");
addChildToParent(newContainer);
return newContainer;
}
private void buildComponentMonitor(DefaultPicoContainer tempContainer) {
if (componentMonitorClass == null) {
tempContainer.addComponent(ComponentMonitor.class, componentMonitor);
} else {
tempContainer.addComponent(ComponentMonitor.class, componentMonitorClass);
}
}
private void addChildToParent(MutablePicoContainer newContainer) {
if (addChildToParent) {
if (parentContainer instanceof MutablePicoContainer) {
((MutablePicoContainer)parentContainer).addChildContainer(newContainer);
} else {
throw new PicoCompositionException("If using addChildContainer() the parent must be a MutablePicoContainer");
}
}
}
private void addContainerComponents(DefaultPicoContainer temp) {
for (Object containerComp : containerComps) {
temp.addComponent(containerComp);
}
}
private ComponentFactory buildComponentFactory(DefaultPicoContainer container, final ComponentFactory lastCaf, final Stack clonedBehaviors) {
Object componentFactory = clonedBehaviors.pop();
DefaultPicoContainer tmpContainer = new TransientPicoContainer(container);
tmpContainer.addComponent("componentFactory", componentFactory);
if (lastCaf != null) {
tmpContainer.addComponent(ComponentFactory.class, lastCaf);
}
ComponentFactory newlastCaf = (ComponentFactory) tmpContainer.getComponent("componentFactory");
if (newlastCaf instanceof BehaviorFactory) {
((BehaviorFactory) newlastCaf).wrap(lastCaf);
}
return newlastCaf;
}
public PicoBuilder withHiddenImplementations() {
behaviors.push(implementationHiding());
return this;
}
public PicoBuilder withSetterInjection() {
addInjector(SDI());
return this;
}
public PicoBuilder withAnnotatedMethodInjection(Class extends Annotation> injectionAnnotation) {
addInjector(annotatedMethodDI(injectionAnnotation));
return this;
}
public PicoBuilder withAnnotatedMethodInjection() {
addInjector(annotatedMethodDI());
return this;
}
public PicoBuilder withAnnotatedFieldInjection(Class extends Annotation> injectionAnnotation) {
addInjector(annotatedFieldDI(injectionAnnotation));
return this;
}
public PicoBuilder withAnnotatedFieldInjection() {
addInjector(annotatedFieldDI());
return this;
}
public PicoBuilder withTypedFieldInjection() {
addInjector(typedFieldDI());
return this;
}
public PicoBuilder withConstructorInjection() {
addInjector(CDI());
return this;
}
public PicoBuilder withNamedMethodInjection() {
addInjector(namedMethod());
return this;
}
public PicoBuilder withNamedFieldInjection() {
addInjector(namedField());
return this;
}
public PicoBuilder withCaching() {
behaviors.push(caching());
return this;
}
public PicoBuilder withComponentFactory(ComponentFactory componentFactory) {
if (componentFactory == null) {
throw new NullPointerException("CAF cannot be null");
}
behaviors.push(componentFactory);
return this;
}
public PicoBuilder withSynchronizing() {
behaviors.push(new Synchronizing());
return this;
}
public PicoBuilder withLocking() {
behaviors.push(new Locking());
return this;
}
public PicoBuilder withBehaviors(BehaviorFactory... factories) {
for (BehaviorFactory componentFactory : factories) {
behaviors.push(componentFactory);
}
return this;
}
public PicoBuilder implementedBy(Class extends MutablePicoContainer> containerClass) {
mpcClass = containerClass;
return this;
}
/**
* Allows you to specify your very own component monitor to be used by the created
* picocontainer
* @param specifiedComponentMonitor
* @return this to allow for method chaining.
*/
public PicoBuilder withMonitor(ComponentMonitor specifiedComponentMonitor) {
this.componentMonitor = specifiedComponentMonitor;
componentMonitorClass = null;
return this;
}
public PicoBuilder withComponentFactory(Class extends ComponentFactory> componentFactoryClass) {
behaviors.push(componentFactoryClass);
return this;
}
public PicoBuilder withCustomContainerComponent(Object containerDependency) {
containerComps.add(containerDependency);
return this;
}
public PicoBuilder withPropertyApplier() {
behaviors.push(new PropertyApplying());
return this;
}
public PicoBuilder withAutomatic() {
behaviors.push(new Automating());
return this;
}
public PicoBuilder withMethodInjection() {
addInjector(new MethodInjection());
return this;
}
public PicoBuilder addChildToParent() {
addChildToParent = true;
return this;
}
protected void addInjector(InjectionFactory injectionType) {
injectors.add(injectionType);
}
}
libpicocontainer-java-2.15/org/picocontainer/PicoClassNotFoundException.java 0000664 0000000 0000000 00000001752 12422476443 0027424 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer;
@SuppressWarnings("serial")
public class PicoClassNotFoundException extends PicoException {
public PicoClassNotFoundException(final String className, final ClassNotFoundException cnfe) {
super("Class '" + className + "' not found", cnfe);
}
}
libpicocontainer-java-2.15/org/picocontainer/PicoCompositionException.java 0000664 0000000 0000000 00000003755 12422476443 0027212 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (c) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
*****************************************************************************/
package org.picocontainer;
/**
* Subclass of {@link PicoException} that is thrown when there is:
* - a problem initializing the container
* - a cyclic dependency between components occurs.
* - problem adding a component
* - a request for a component that is ambiguous.
*
*/
@SuppressWarnings("serial")
public class PicoCompositionException extends PicoException {
/**
* Construct a new exception with no cause and the specified detail message. Note modern JVMs may still track the
* exception that caused this one.
*
* @param message the message detailing the exception.
*/
public PicoCompositionException(final String message) {
super(message);
}
/**
* Construct a new exception with the specified cause and no detail message.
*
* @param cause the exception that caused this one.
*/
public PicoCompositionException(final Throwable cause) {
super(cause);
}
/**
* Construct a new exception with the specified cause and the specified detail message.
*
* @param message the message detailing the exception.
* @param cause the exception that caused this one.
*/
public PicoCompositionException(final String message, final Throwable cause) {
super(message, cause);
}
}
libpicocontainer-java-2.15/org/picocontainer/PicoContainer.java 0000664 0000000 0000000 00000016412 12422476443 0024744 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer;
import java.util.Collection;
import java.util.List;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
/**
* This is the core interface for PicoContainer. It is used to retrieve component instances from the container; it only
* has accessor methods (in addition to the {@link #accept(PicoVisitor)} method). In order to register components in a
* PicoContainer, use a {@link MutablePicoContainer}, such as {@link DefaultPicoContainer}.
*
* @author Paul Hammant
* @author Aslak Hellesøy
* @author Jon Tirsén
* @see See package description for basic overview how to use
* PicoContainer.
*/
public interface PicoContainer {
/**
* Retrieve a component instance registered with a specific key or type. If a component cannot be found in this container,
* the parent container (if one exists) will be searched.
*
* @param componentKeyOrType the key or Type that the component was registered with.
* @return an instantiated component, or null
if no component has been registered for the specified
* key.
*/
Object getComponent(Object componentKeyOrType);
Object getComponent(Object componentKeyOrType, Type into);
/**
* Retrieve a component keyed by the component type.
* @param componentType the type of the component
* @return the typed resulting object instance or null if the object does not exist.
*/
T getComponent(Class componentType);
/**
* Retrieve a component keyed by the component type and binding type.
* @param componentType the type of the component
* @param binding the binding type of the component
* @return the typed resulting object instance or null if the object does not exist.
*/
T getComponent(Class componentType, Class extends Annotation> binding);
/**
* Retrieve all the registered component instances in the container, (not including those in the parent container).
* The components are returned in their order of instantiation, which depends on the dependency order between them.
*
* @return all the components.
* @throws PicoException if the instantiation of the component fails
*/
List getComponents();
/**
* Retrieve the parent container of this container.
*
* @return a {@link PicoContainer} instance, or null
if this container does not have a parent.
*/
PicoContainer getParent();
/**
* Find a component adapter associated with the specified key. If a component adapter cannot be found in this
* container, the parent container (if one exists) will be searched.
*
* @param componentKey the key that the component was registered with.
* @return the component adapter associated with this key, or null
if no component has been
* registered for the specified key.
*/
ComponentAdapter> getComponentAdapter(Object componentKey);
/**
* Find a component adapter associated with the specified type and binding name. If a component adapter cannot be found in this
* container, the parent container (if one exists) will be searched.
*
* @param componentType the type of the component.
* @return the component adapter associated with this class, or null
if no component has been
* registered for the specified key.
* @param componentNameBinding the name binding to use
*/
ComponentAdapter getComponentAdapter(Class componentType, NameBinding componentNameBinding);
/**
* Find a component adapter associated with the specified type and binding type. If a component adapter cannot be found in this
* container, the parent container (if one exists) will be searched.
*
* @param componentType the type of the component.
* @return the component adapter associated with this class, or null
if no component has been
* registered for the specified key.
* @param binding the typed binding to use
*/
ComponentAdapter getComponentAdapter(Class componentType, Class extends Annotation> binding);
/**
* Retrieve all the component adapters inside this container. The component adapters from the parent container are
* not returned.
*
* @return a collection containing all the {@link ComponentAdapter}s inside this container. The collection will not
* be modifiable.
* @see #getComponentAdapters(Class) a variant of this method which returns the component adapters inside this
* container that are associated with the specified type.
*/
Collection> getComponentAdapters();
/**
* Retrieve all component adapters inside this container that are associated with the specified type. The addComponent
* adapters from the parent container are not returned.
*
* @param componentType the type of the components.
* @return a collection containing all the {@link ComponentAdapter}s inside this container that are associated with
* the specified type. Changes to this collection will not be reflected in the container itself.
*/
List> getComponentAdapters(Class componentType);
/**
* Retrieve all component adapters inside this container that are associated with the specified type and binding type. The addComponent
* adapters from the parent container are not returned.
*
* @param componentType the type of the components.
* @param binding the typed binding to use
* @return a collection containing all the {@link ComponentAdapter}s inside this container that are associated with
* the specified type. Changes to this collection will not be reflected in the container itself.
*/
List> getComponentAdapters(Class componentType, Class extends Annotation> binding);
/**
* Returns a List of components of a certain componentType. The list is ordered by instantiation order, starting
* with the components instantiated first at the beginning.
*
* @param componentType the searched type.
* @return a List of components.
* @throws PicoException if the instantiation of a component fails
*/
List getComponents(Class componentType);
/**
* Accepts a visitor that should visit the child containers, component adapters and component instances.
*
* @param visitor the visitor
*/
void accept(PicoVisitor visitor);
}
libpicocontainer-java-2.15/org/picocontainer/PicoException.java 0000664 0000000 0000000 00000004402 12422476443 0024754 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer;
/**
* Superclass for all Exceptions in PicoContainer. You can use this if you want to catch all exceptions thrown by
* PicoContainer. Be aware that some parts of the PicoContainer API will also throw {@link NullPointerException} when
* null
values are provided for method arguments, and this is not allowed.
*
* @author Paul Hammant
* @author Aslak Hellesøy
*/
public abstract class PicoException extends RuntimeException {
/**
* Construct a new exception with no cause and no detail message. Note modern JVMs may still track the exception
* that caused this one.
*/
protected PicoException() {
}
/**
* Construct a new exception with no cause and the specified detail message. Note modern JVMs may still track the
* exception that caused this one.
*
* @param message the message detailing the exception.
*/
protected PicoException(final String message) {
super(message);
}
/**
* Construct a new exception with the specified cause and no detail message.
*
* @param cause the exception that caused this one.
*/
protected PicoException(final Throwable cause) {
super(cause);
}
/**
* Construct a new exception with the specified cause and the specified detail message.
*
* @param message the message detailing the exception.
* @param cause the exception that caused this one.
*/
protected PicoException(final String message, final Throwable cause) {
super(message,cause);
}
}
libpicocontainer-java-2.15/org/picocontainer/PicoLifecycleException.java 0000664 0000000 0000000 00000002470 12422476443 0026577 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (c) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
*****************************************************************************/
package org.picocontainer;
import java.lang.reflect.Method;
@SuppressWarnings("serial")
public class PicoLifecycleException extends PicoException {
private final Method method;
private final Object instance;
public PicoLifecycleException(final Method method, final Object instance, final Throwable cause) {
super(cause);
this.method = method;
this.instance = instance;
}
public Method getMethod() {
return method;
}
public Object getInstance() {
return instance;
}
public String getMessage() {
return "PicoLifecycleException: method '" + method + "', instance '"+ instance + ", " + super.getMessage();
}
}
libpicocontainer-java-2.15/org/picocontainer/PicoVerificationException.java 0000664 0000000 0000000 00000004622 12422476443 0027323 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer;
import java.util.ArrayList;
import java.util.List;
/**
* Subclass of {@link PicoException} that is thrown when a {@link PicoContainer} hierarchy
* cannot be verified. A failing verification is caused by ambuigities or missing dependencies
* between the registered components and their parameters. This exception is designed as a
* collector for all Exceptions occurring at the verification of the complete container
* hierarchy. The verification is normally done with the
* {@link org.picocontainer.visitors.VerifyingVisitor}, that will throw this exception.
*/
@SuppressWarnings("serial")
public class PicoVerificationException extends PicoException {
/**
* The exceptions that caused this one.
*/
private final List nestedExceptions = new ArrayList();
/**
* Construct a new exception with a list of exceptions that caused this one.
*
* @param nestedExceptions the exceptions that caused this one.
*/
public PicoVerificationException(final List extends Throwable> nestedExceptions) {
this.nestedExceptions.addAll(nestedExceptions);
}
/**
* Retrieve the list of exceptions that caused this one.
*
* @return the list of exceptions that caused this one.
*/
public List getNestedExceptions() {
return nestedExceptions;
}
/**
* Return a string listing of all the messages associated with the exceptions that caused
* this one.
*
* @return a string listing of all the messages associated with the exceptions that caused
* this one.
*/
public String getMessage() {
return nestedExceptions.toString();
}
}
libpicocontainer-java-2.15/org/picocontainer/PicoVisitor.java 0000664 0000000 0000000 00000005231 12422476443 0024456 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
*****************************************************************************/
package org.picocontainer;
/**
* Interface realizing a visitor pattern for {@link PicoContainer} as described in the GoF.
* The visitor should visit the container, its children, all registered {@link ComponentAdapter}
* instances and all instantiated components.
*
* @author Aslak Hellesøy
* @author Jörg Schaible
*/
public interface PicoVisitor {
/**
* Constant that indicates that the traversal should continue after the
* visit*() method has been called.
*/
boolean CONTINUE_TRAVERSAL = true;
/**
* Constant that indicates that the traversal should abort after the
* visit*() method has been called.
*/
boolean ABORT_TRAVERSAL = false;
/**
* Entry point for the PicoVisitor traversal. The given node is the first object, that is
* asked for acceptance. Only objects of type {@link PicoContainer}, {@link ComponentAdapter},
* or {@link Parameter} are valid.
*
* @param node the start node of the traversal.
* @return a visitor-specific value.
* @throws IllegalArgumentException in case of an argument of invalid type.
*/
Object traverse(Object node);
/**
* Visit a {@link PicoContainer} that has to accept the visitor.
*
* @param pico the visited container.
* @return CONTINUE_TRAVERSAL if the traversal should continue.
* Any visitor callback that returns ABORT_TRAVERSAL indicates
* the desire to abort any further traversal.
*/
boolean visitContainer(PicoContainer pico);
/**
* Visit a {@link ComponentAdapter} that has to accept the visitor.
*
* @param componentAdapter the visited ComponentAdapter.
*/
void visitComponentAdapter(ComponentAdapter> componentAdapter);
/**
* Visit a {@link ComponentAdapter} that has to accept the visitor.
*
* @param componentAdapter the visited ComponentAdapter.
*/
void visitComponentFactory(ComponentFactory componentFactory);
/**
* Visit a {@link Parameter} that has to accept the visitor.
*
* @param parameter the visited Parameter.
*/
void visitParameter(Parameter parameter);
}
libpicocontainer-java-2.15/org/picocontainer/Startable.java 0000664 0000000 0000000 00000003767 12422476443 0024141 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (c) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the license.html file. *
* *
* Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
*****************************************************************************/
package org.picocontainer;
/**
* An interface which is implemented by components that can be started and stopped. The {@link Startable#start()}
* must be called at the begin of the component lifecycle. It can be called again only after a call to
* {@link Startable#stop()}. The {@link Startable#stop()} method must be called at the end of the component lifecycle,
* and can further be called after every {@link Startable#start()}. If a component implements the {@link Disposable}
* interface as well, {@link Startable#stop()} should be called before {@link Disposable#dispose()}.
*
* For more advanced and pluggable lifecycle support, see the functionality offered by picocontainer-gems
* subproject.
* @see org.picocontainer.Disposable the Disposable interface if you need to dispose()
semantics.
* @author Paul Hammant
* @author Aslak Hellesøy
*/
public interface Startable {
/**
* Start this component. Called initially at the begin of the lifecycle. It can be called again after a stop.
*/
void start();
/**
* Stop this component. Called near the end of the lifecycle. It can be called again after a further start. Implement
* {@link Disposable} if you need a single call at the definite end of the lifecycle.
*/
void stop();
}
libpicocontainer-java-2.15/org/picocontainer/adapters/ 0000775 0000000 0000000 00000000000 12422476443 0023143 5 ustar 00root root 0000000 0000000 libpicocontainer-java-2.15/org/picocontainer/adapters/AbstractAdapter.java 0000664 0000000 0000000 00000013237 12422476443 0027060 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer.adapters;
import org.picocontainer.ComponentMonitor;
import org.picocontainer.PicoVisitor;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.ComponentMonitorStrategy;
import org.picocontainer.PicoContainer;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.injectors.ProviderAdapter;
import org.picocontainer.injectors.Provider;
import org.picocontainer.monitors.AbstractComponentMonitor;
import org.picocontainer.monitors.NullComponentMonitor;
import java.io.Serializable;
/**
* Base class for a ComponentAdapter with general functionality.
* This implementation provides basic checks for a healthy implementation of a ComponentAdapter.
* It does not allow to use null
for the component key or the implementation,
* ensures that the implementation is a concrete class and that the key is assignable from the
* implementation if the key represents a type.
*
* @author Paul Hammant
* @author Aslak Hellesøy
* @author Jon Tirsén
*/
public abstract class AbstractAdapter implements ComponentAdapter, ComponentMonitorStrategy, Serializable {
private Object componentKey;
private Class componentImplementation;
private ComponentMonitor componentMonitor;
/**
* Constructs a new ComponentAdapter for the given key and implementation.
* @param componentKey the search key for this implementation
* @param componentImplementation the concrete implementation
*/
public AbstractAdapter(Object componentKey, Class componentImplementation) {
this(componentKey, componentImplementation, new AbstractComponentMonitor());
this.componentMonitor = new NullComponentMonitor();
}
/**
* Constructs a new ComponentAdapter for the given key and implementation.
* @param componentKey the search key for this implementation
* @param componentImplementation the concrete implementation
* @param monitor the component monitor used by this ComponentAdapter
*/
public AbstractAdapter(Object componentKey, Class componentImplementation, ComponentMonitor monitor) {
if (monitor == null) {
throw new NullPointerException("ComponentMonitor==null");
}
this.componentMonitor = monitor;
if (componentImplementation == null) {
throw new NullPointerException("componentImplementation");
}
this.componentKey = componentKey;
this.componentImplementation = componentImplementation;
checkTypeCompatibility();
}
/**
* {@inheritDoc}
* @see org.picocontainer.ComponentAdapter#getComponentKey()
*/
public Object getComponentKey() {
if (componentKey == null) {
throw new NullPointerException("componentKey");
}
return componentKey;
}
/**
* {@inheritDoc}
* @see org.picocontainer.ComponentAdapter#getComponentImplementation()
*/
public Class extends T> getComponentImplementation() {
return componentImplementation;
}
protected void checkTypeCompatibility() {
if (componentKey instanceof Class) {
Class> componentType = (Class) componentKey;
if (Provider.class.isAssignableFrom(componentImplementation)) {
if (!componentType.isAssignableFrom(ProviderAdapter.getProvideMethod(componentImplementation).getReturnType())) {
throw newCCE(componentType);
}
} else {
if (!componentType.isAssignableFrom(componentImplementation)) {
throw newCCE(componentType);
}
}
}
}
private ClassCastException newCCE(Class> componentType) {
return new ClassCastException(componentImplementation.getName() + " is not a " + componentType.getName());
}
public T getComponentInstance(PicoContainer container) throws PicoCompositionException {
return getComponentInstance(container, null);
}
/**
* @return Returns the ComponentAdapter's class name and the component's key.
* @see java.lang.Object#toString()
*/
public String toString() {
return getDescriptor() + getComponentKey();
}
public void accept(PicoVisitor visitor) {
visitor.visitComponentAdapter(this);
}
public void changeMonitor(ComponentMonitor monitor) {
this.componentMonitor = monitor;
}
/**
* Returns the monitor currently used
* @return The ComponentMonitor currently used
*/
public ComponentMonitor currentMonitor(){
return componentMonitor;
}
public final ComponentAdapter getDelegate() {
return null;
}
public final U findAdapterOfType(Class adapterType) {
if (adapterType.isAssignableFrom(this.getClass())) {
return (U) this;
} else if (getDelegate() != null) {
return getDelegate().findAdapterOfType(adapterType);
}
return null;
}
}
libpicocontainer-java-2.15/org/picocontainer/adapters/InstanceAdapter.java 0000664 0000000 0000000 00000011414 12422476443 0027054 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer.adapters;
import org.picocontainer.Behavior;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.ComponentLifecycle;
import org.picocontainer.ComponentMonitor;
import org.picocontainer.LifecycleStrategy;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.PicoContainer;
import org.picocontainer.lifecycle.NullLifecycleStrategy;
import org.picocontainer.monitors.NullComponentMonitor;
import java.lang.reflect.Type;
/**
*
* Component adapter which wraps a component instance.
*
*
* This component adapter supports both a {@link Behavior Behavior} and a
* {@link org.picocontainer.LifecycleStrategy LifecycleStrategy} to control the lifecycle of the component.
* The lifecycle manager methods simply delegate to the lifecycle strategy methods
* on the component instance.
*
*
* @author Aslak Hellesøy
* @author Paul Hammant
* @author Mauro Talevi
*/
@SuppressWarnings("serial")
public final class InstanceAdapter extends AbstractAdapter implements ComponentLifecycle, LifecycleStrategy {
/**
* The actual instance of the component.
*/
private final T componentInstance;
/**
* Lifecycle Strategy for the component adpater.
*/
private final LifecycleStrategy lifecycleStrategy;
private boolean started;
public InstanceAdapter(Object componentKey, T componentInstance, LifecycleStrategy lifecycleStrategy, ComponentMonitor componentMonitor) throws PicoCompositionException {
super(componentKey, getInstanceClass(componentInstance), componentMonitor);
this.componentInstance = componentInstance;
this.lifecycleStrategy = lifecycleStrategy;
}
public InstanceAdapter(Object componentKey, T componentInstance) {
this(componentKey, componentInstance, new NullLifecycleStrategy(), new NullComponentMonitor());
}
public InstanceAdapter(Object componentKey, T componentInstance, LifecycleStrategy lifecycleStrategy) {
this(componentKey, componentInstance, lifecycleStrategy, new NullComponentMonitor());
}
public InstanceAdapter(Object componentKey, T componentInstance, ComponentMonitor componentMonitor) {
this(componentKey, componentInstance, new NullLifecycleStrategy(), componentMonitor);
}
private static Class getInstanceClass(Object componentInstance) {
if (componentInstance == null) {
throw new NullPointerException("componentInstance cannot be null");
}
return componentInstance.getClass();
}
public T getComponentInstance(PicoContainer container, Type into) {
return componentInstance;
}
public void verify(PicoContainer container) {
}
public String getDescriptor() {
return "Instance-";
}
@Override
public String toString() {
Object componentKey = getComponentKey();
if (componentKey instanceof Class) {
componentKey = "of " + ((Class) componentKey).getName();
}
return getDescriptor() + componentKey;
}
public void start(PicoContainer container) {
start(componentInstance);
}
public void stop(PicoContainer container) {
stop(componentInstance);
}
public void dispose(PicoContainer container) {
dispose(componentInstance);
}
public boolean componentHasLifecycle() {
return hasLifecycle(componentInstance.getClass());
}
public boolean isStarted() {
return started;
}
// ~~~~~~~~ LifecycleStrategy ~~~~~~~~
public void start(Object component) {
lifecycleStrategy.start(componentInstance);
started = true;
}
public void stop(Object component) {
lifecycleStrategy.stop(componentInstance);
started = false;
}
public void dispose(Object component) {
lifecycleStrategy.dispose(componentInstance);
}
public boolean hasLifecycle(Class> type) {
return lifecycleStrategy.hasLifecycle(type);
}
public boolean isLazy(ComponentAdapter> adapter) {
return lifecycleStrategy.isLazy(adapter);
}
}
libpicocontainer-java-2.15/org/picocontainer/annotations/ 0000775 0000000 0000000 00000000000 12422476443 0023675 5 ustar 00root root 0000000 0000000 libpicocontainer-java-2.15/org/picocontainer/annotations/Bind.java 0000664 0000000 0000000 00000000516 12422476443 0025416 0 ustar 00root root 0000000 0000000 package org.picocontainer.annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
/** @author Paul Hammant */
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.ANNOTATION_TYPE })
public @interface Bind {
}
libpicocontainer-java-2.15/org/picocontainer/annotations/Cache.java 0000664 0000000 0000000 00000001766 12422476443 0025555 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer.annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
@Retention(RetentionPolicy.RUNTIME)
@Target(value={ ElementType.TYPE })
public @interface Cache {
}
libpicocontainer-java-2.15/org/picocontainer/annotations/Inject.java 0000664 0000000 0000000 00000002067 12422476443 0025761 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer.annotations;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@Target(value={ElementType.METHOD, ElementType.FIELD})
public @interface Inject {
//Marker annotation only, no attributes.
}
libpicocontainer-java-2.15/org/picocontainer/annotations/Nullable.java 0000664 0000000 0000000 00000001773 12422476443 0026306 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer.annotations;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@Target(value={ElementType.PARAMETER})
public @interface Nullable {
} libpicocontainer-java-2.15/org/picocontainer/behaviors/ 0000775 0000000 0000000 00000000000 12422476443 0023322 5 ustar 00root root 0000000 0000000 libpicocontainer-java-2.15/org/picocontainer/behaviors/AbstractBehavior.java 0000664 0000000 0000000 00000015556 12422476443 0027424 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by Jon Tirsen *
*****************************************************************************/
package org.picocontainer.behaviors;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.ComponentMonitor;
import org.picocontainer.ComponentMonitorStrategy;
import org.picocontainer.LifecycleStrategy;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.PicoContainer;
import org.picocontainer.PicoVisitor;
import java.io.Serializable;
import java.lang.reflect.Type;
/**
*
* Component adapter which decorates another adapter.
*
*
* This adapter supports a {@link org.picocontainer.ComponentMonitorStrategy component monitor strategy}
* and will propagate change of monitor to the delegate if the delegate itself
* support the monitor strategy.
*
*
* This adapter also supports a {@link Behavior lifecycle manager} and a
* {@link org.picocontainer.LifecycleStrategy lifecycle strategy} if the delegate does.
*
*
* @author Jon Tirsen
* @author Aslak Hellesoy
* @author Mauro Talevi
*/
public abstract class AbstractBehavior implements org.picocontainer.Behavior, ComponentMonitorStrategy,
LifecycleStrategy, Serializable {
protected final ComponentAdapter delegate;
public AbstractBehavior(ComponentAdapter delegate) {
this.delegate = delegate;
}
public Object getComponentKey() {
return delegate.getComponentKey();
}
public Class extends T> getComponentImplementation() {
return delegate.getComponentImplementation();
}
public T getComponentInstance(PicoContainer container) throws PicoCompositionException {
return getComponentInstance(container, NOTHING.class);
}
public T getComponentInstance(PicoContainer container, Type into) throws PicoCompositionException {
return (T) delegate.getComponentInstance(container, into);
}
public void verify(PicoContainer container) throws PicoCompositionException {
delegate.verify(container);
}
public final ComponentAdapter getDelegate() {
return delegate;
}
@SuppressWarnings("unchecked")
public final U findAdapterOfType(Class adapterType) {
if (adapterType.isAssignableFrom(this.getClass())) {
return (U) this;
} else {
return delegate.findAdapterOfType(adapterType);
}
}
public void accept(PicoVisitor visitor) {
visitor.visitComponentAdapter(this);
delegate.accept(visitor);
}
/**
* Delegates change of monitor if the delegate supports
* a component monitor strategy.
* {@inheritDoc}
*/
public void changeMonitor(ComponentMonitor monitor) {
if (delegate instanceof ComponentMonitorStrategy ) {
((ComponentMonitorStrategy)delegate).changeMonitor(monitor);
}
}
/**
* Returns delegate's current monitor if the delegate supports
* a component monitor strategy.
* {@inheritDoc}
* @throws PicoCompositionException if no component monitor is found in delegate
*/
public ComponentMonitor currentMonitor() {
if (delegate instanceof ComponentMonitorStrategy ) {
return ((ComponentMonitorStrategy)delegate).currentMonitor();
}
throw new PicoCompositionException("No component monitor found in delegate");
}
/**
* Invokes delegate start method if the delegate is a Behavior
* {@inheritDoc}
*/
public void start(PicoContainer container) {
if (delegate instanceof org.picocontainer.Behavior) {
((org.picocontainer.Behavior>)delegate).start(container);
}
}
/**
* Invokes delegate stop method if the delegate is a Behavior
* {@inheritDoc}
*/
public void stop(PicoContainer container) {
if (delegate instanceof org.picocontainer.Behavior) {
((org.picocontainer.Behavior>)delegate).stop(container);
}
}
/**
* Invokes delegate dispose method if the delegate is a Behavior
* {@inheritDoc}
*/
public void dispose(PicoContainer container) {
if (delegate instanceof org.picocontainer.Behavior) {
((org.picocontainer.Behavior>)delegate).dispose(container);
}
}
/**
* Invokes delegate hasLifecycle method if the delegate is a Behavior
* {@inheritDoc}
*/
public boolean componentHasLifecycle() {
if (delegate instanceof org.picocontainer.Behavior) {
return ((org.picocontainer.Behavior>)delegate).componentHasLifecycle();
}
return false;
}
public boolean isStarted() {
if (delegate instanceof org.picocontainer.Behavior) {
return ((org.picocontainer.Behavior>)delegate).isStarted();
}
return false;
}
// ~~~~~~~~ LifecycleStrategy ~~~~~~~~
/**
* Invokes delegate start method if the delegate is a LifecycleStrategy
* {@inheritDoc}
*/
public void start(Object component) {
if (delegate instanceof LifecycleStrategy ) {
((LifecycleStrategy)delegate).start(component);
}
}
/**
* Invokes delegate stop method if the delegate is a LifecycleStrategy
* {@inheritDoc}
*/
public void stop(Object component) {
if (delegate instanceof LifecycleStrategy ) {
((LifecycleStrategy)delegate).stop(component);
}
}
/**
* Invokes delegate dispose method if the delegate is a LifecycleStrategy
* {@inheritDoc}
*/
public void dispose(Object component) {
if (delegate instanceof LifecycleStrategy ) {
((LifecycleStrategy)delegate).dispose(component);
}
}
/**
* Invokes delegate hasLifecycle(Class) method if the delegate is a LifecycleStrategy
* {@inheritDoc}
*/
public boolean hasLifecycle(Class> type) {
return delegate instanceof LifecycleStrategy && ((LifecycleStrategy) delegate).hasLifecycle(type);
}
public boolean isLazy(ComponentAdapter> adapter) {
return delegate instanceof LifecycleStrategy && ((LifecycleStrategy) delegate).isLazy(adapter);
}
public String toString() {
return getDescriptor() + ":" + delegate.toString();
}
}
libpicocontainer-java-2.15/org/picocontainer/behaviors/AbstractBehaviorFactory.java 0000664 0000000 0000000 00000011374 12422476443 0030746 0 ustar 00root root 0000000 0000000 /*******************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD * style
* license a copy of which has been included with this distribution in * the
* LICENSE.txt file. * * Original code by *
******************************************************************************/
package org.picocontainer.behaviors;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.Properties;
import org.picocontainer.BehaviorFactory;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.ComponentFactory;
import org.picocontainer.ComponentMonitor;
import org.picocontainer.LifecycleStrategy;
import org.picocontainer.Parameter;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.PicoContainer;
import org.picocontainer.PicoVisitor;
import org.picocontainer.Characteristics;
import org.picocontainer.InjectionFactory;
import org.picocontainer.injectors.AdaptingInjection;
@SuppressWarnings("serial")
public class AbstractBehaviorFactory implements ComponentFactory, Serializable, BehaviorFactory {
private ComponentFactory delegate;
public ComponentFactory wrap(ComponentFactory delegate) {
this.delegate = delegate;
return this;
}
public ComponentAdapter createComponentAdapter(ComponentMonitor componentMonitor,
LifecycleStrategy lifecycleStrategy, Properties componentProperties, Object componentKey,
Class componentImplementation, Parameter... parameters) throws PicoCompositionException {
if (delegate == null) {
delegate = new AdaptingInjection();
}
ComponentAdapter compAdapter = delegate.createComponentAdapter(componentMonitor, lifecycleStrategy, componentProperties, componentKey,
componentImplementation, parameters);
boolean enableCircular = removePropertiesIfPresent(componentProperties, Characteristics.ENABLE_CIRCULAR);
if (enableCircular && delegate instanceof InjectionFactory) {
return componentMonitor.newBehavior(new HiddenImplementation(compAdapter));
} else {
return compAdapter;
}
}
public void verify(PicoContainer container) {
delegate.verify(container);
}
public void accept(PicoVisitor visitor) {
visitor.visitComponentFactory(this);
if (delegate != null) {
delegate.accept(visitor);
}
}
public ComponentAdapter addComponentAdapter(ComponentMonitor componentMonitor,
LifecycleStrategy lifecycleStrategy, Properties componentProperties, ComponentAdapter adapter) {
if (delegate != null && delegate instanceof BehaviorFactory) {
return ((BehaviorFactory) delegate).addComponentAdapter(componentMonitor, lifecycleStrategy,
componentProperties, adapter);
}
return adapter;
}
public static boolean arePropertiesPresent(Properties current, Properties present, boolean compareValueToo) {
Enumeration> keys = present.keys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
String presentValue = present.getProperty(key);
String currentValue = current.getProperty(key);
if (currentValue == null) {
return false;
}
if (!presentValue.equals(currentValue) && compareValueToo) {
return false;
}
}
return true;
}
public static boolean removePropertiesIfPresent(Properties current, Properties present) {
if (!arePropertiesPresent(current, present, true)) {
return false;
}
Enumeration> keys = present.keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
current.remove(key);
}
return true;
}
public static String getAndRemovePropertiesIfPresentByKey(Properties current, Properties present) {
if (!arePropertiesPresent(current, present, false)) {
return null;
}
Enumeration> keys = present.keys();
String value = null;
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
value = (String) current.remove(key);
}
return value;
}
protected void mergeProperties(Properties into, Properties from) {
Enumeration> e = from.propertyNames();
while (e.hasMoreElements()) {
String s = (String) e.nextElement();
into.setProperty(s, from.getProperty(s));
}
}
}
libpicocontainer-java-2.15/org/picocontainer/behaviors/AdaptingBehavior.java 0000664 0000000 0000000 00000016336 12422476443 0027405 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer.behaviors;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.picocontainer.BehaviorFactory;
import org.picocontainer.Characteristics;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.ComponentFactory;
import org.picocontainer.ComponentMonitor;
import org.picocontainer.LifecycleStrategy;
import org.picocontainer.Parameter;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.PicoContainer;
import org.picocontainer.PicoVisitor;
import org.picocontainer.annotations.Cache;
import org.picocontainer.injectors.AdaptingInjection;
@SuppressWarnings("serial")
public class AdaptingBehavior implements BehaviorFactory, Serializable {
public ComponentAdapter createComponentAdapter(ComponentMonitor componentMonitor,
LifecycleStrategy lifecycleStrategy,
Properties componentProperties,
Object componentKey,
Class componentImplementation,
Parameter... parameters) throws PicoCompositionException {
List list = new ArrayList();
ComponentFactory lastFactory = makeInjectionFactory();
processSynchronizing(componentProperties, list);
processLocking(componentProperties, list);
processPropertyApplying(componentProperties, list);
processAutomatic(componentProperties, list);
processImplementationHiding(componentProperties, list);
processCaching(componentProperties, componentImplementation, list);
processGuarding(componentProperties, componentImplementation, list);
//Instantiate Chain of ComponentFactories
for (ComponentFactory componentFactory : list) {
if (lastFactory != null && componentFactory instanceof BehaviorFactory) {
((BehaviorFactory)componentFactory).wrap(lastFactory);
}
lastFactory = componentFactory;
}
return lastFactory.createComponentAdapter(componentMonitor,
lifecycleStrategy,
componentProperties,
componentKey,
componentImplementation,
parameters);
}
public ComponentAdapter addComponentAdapter(ComponentMonitor componentMonitor,
LifecycleStrategy lifecycleStrategy,
Properties componentProperties,
ComponentAdapter adapter) {
List list = new ArrayList();
processSynchronizing(componentProperties, list);
processImplementationHiding(componentProperties, list);
processCaching(componentProperties, adapter.getComponentImplementation(), list);
processGuarding(componentProperties, adapter.getComponentImplementation(), list);
//Instantiate Chain of ComponentFactories
BehaviorFactory lastFactory = null;
for (BehaviorFactory componentFactory : list) {
if (lastFactory != null) {
componentFactory.wrap(lastFactory);
}
lastFactory = componentFactory;
}
if (lastFactory == null) {
return adapter;
}
return lastFactory.addComponentAdapter(componentMonitor, lifecycleStrategy, componentProperties, adapter);
}
public void verify(PicoContainer container) {
}
public void accept(PicoVisitor visitor) {
visitor.visitComponentFactory(this);
}
protected AdaptingInjection makeInjectionFactory() {
return new AdaptingInjection();
}
protected void processSynchronizing(Properties componentProperties, List list) {
if (AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.SYNCHRONIZE)) {
list.add(new Synchronizing());
}
}
protected void processLocking(Properties componentProperties, List list) {
if (AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.LOCK)) {
list.add(new Locking());
}
}
protected void processCaching(Properties componentProperties,
Class componentImplementation,
List list) {
if (AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.CACHE) ||
componentImplementation.getAnnotation(Cache.class) != null) {
list.add(new Caching());
}
AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.NO_CACHE);
}
protected void processGuarding(Properties componentProperties, Class componentImplementation, List list) {
if (AbstractBehaviorFactory.arePropertiesPresent(componentProperties, Characteristics.GUARD, false)) {
list.add(new Guarding());
}
}
protected void processImplementationHiding(Properties componentProperties,
List list) {
if (AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.HIDE_IMPL)) {
list.add(new ImplementationHiding());
}
AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.NO_HIDE_IMPL);
}
protected void processPropertyApplying(Properties componentProperties,
List list) {
if (AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.PROPERTY_APPLYING)) {
list.add(new PropertyApplying());
}
}
protected void processAutomatic(Properties componentProperties,
List list) {
if (AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.AUTOMATIC)) {
list.add(new Automating());
}
}
public ComponentFactory wrap(ComponentFactory delegate) {
throw new UnsupportedOperationException();
}
}
libpicocontainer-java-2.15/org/picocontainer/behaviors/Automated.java 0000664 0000000 0000000 00000002274 12422476443 0026115 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer.behaviors;
import org.picocontainer.Behavior;
import org.picocontainer.ComponentAdapter;
import java.io.Serializable;
@SuppressWarnings("serial")
public class Automated extends AbstractBehavior implements Behavior, Serializable {
public Automated(ComponentAdapter delegate) {
super(delegate);
}
public boolean hasLifecycle(Class> type) {
return true;
}
public String getDescriptor() {
return "Automated";
}
} libpicocontainer-java-2.15/org/picocontainer/behaviors/Automating.java 0000664 0000000 0000000 00000005527 12422476443 0026306 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer.behaviors;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.ComponentMonitor;
import org.picocontainer.LifecycleStrategy;
import org.picocontainer.Parameter;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.Characteristics;
import java.io.Serializable;
import java.util.Properties;
@SuppressWarnings("serial")
public class Automating extends AbstractBehaviorFactory implements Serializable {
public ComponentAdapter createComponentAdapter(ComponentMonitor componentMonitor,
LifecycleStrategy lifecycleStrategy,
Properties componentProperties,
Object componentKey,
Class componentImplementation,
Parameter... parameters) throws PicoCompositionException {
removePropertiesIfPresent(componentProperties, Characteristics.AUTOMATIC);
return componentMonitor.newBehavior(new Automated(super.createComponentAdapter(componentMonitor,
lifecycleStrategy,
componentProperties,
componentKey,
componentImplementation,
parameters)));
}
public ComponentAdapter addComponentAdapter(ComponentMonitor componentMonitor,
LifecycleStrategy lifecycleStrategy,
Properties componentProperties,
ComponentAdapter adapter) {
removePropertiesIfPresent(componentProperties, Characteristics.AUTOMATIC);
return componentMonitor.newBehavior(new Automated(super.addComponentAdapter(componentMonitor,
lifecycleStrategy,
componentProperties,
adapter)));
}
}
libpicocontainer-java-2.15/org/picocontainer/behaviors/Behavior.java 0000664 0000000 0000000 00000001365 12422476443 0025731 0 ustar 00root root 0000000 0000000 package org.picocontainer.behaviors;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.ObjectReference;
/**
* static collection of factory methods for easier behavior creation
*
* @author Konstantin Pribluda
*/
public class Behavior {
public static final org.picocontainer.Behavior cached(ComponentAdapter delegate) {
return new Cached(delegate);
}
public static final org.picocontainer.Behavior cached(ComponentAdapter delegate, ObjectReference instanceReference) {
return new Cached(delegate,instanceReference);
}
public static final org.picocontainer.Behavior decorated(ComponentAdapter delegate, Decorated.Decorator decorator) {
return new Decorated(delegate,decorator);
}
}
libpicocontainer-java-2.15/org/picocontainer/behaviors/Behaviors.java 0000664 0000000 0000000 00000003133 12422476443 0026107 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer.behaviors;
import org.picocontainer.BehaviorFactory;
/**
* Static collection of factory methods for different BehaviourFactory implementations.
*
* @author Paul Hammant
* @author Mauro Talevi
*/
public class Behaviors {
/**
* Prevents instantiation
*/
private Behaviors(){
// no-op
}
public static BehaviorFactory implementationHiding() {
return new ImplementationHiding();
}
public static BehaviorFactory caching() {
return new Caching();
}
public static BehaviorFactory synchronizing() {
return new Synchronizing();
}
public static BehaviorFactory locking() {
return new Locking();
}
public static BehaviorFactory propertyApplying() {
return new PropertyApplying();
}
public static BehaviorFactory automatic() {
return new Automating();
}
}
libpicocontainer-java-2.15/org/picocontainer/behaviors/Cached.java 0000664 0000000 0000000 00000003675 12422476443 0025347 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (c) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
*****************************************************************************/
package org.picocontainer.behaviors;
import org.picocontainer.Behavior;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.ObjectReference;
import org.picocontainer.references.SimpleReference;
/**
*
* {@link ComponentAdapter} implementation that caches the component instance.
*
*
* This adapter supports components with a lifecycle, as it is a
* {@link Behavior lifecycle manager} which will apply the delegate's
* {@link org.picocontainer.LifecycleStrategy lifecycle strategy} to the cached
* component instance. The lifecycle state is maintained so that the component
* instance behaves in the expected way: it can't be started if already started,
* it can't be started or stopped if disposed, it can't be stopped if not
* started, it can't be disposed if already disposed.
*
*
* @author Mauro Talevi
*/
@SuppressWarnings("serial")
public class Cached extends Stored {
public Cached(ComponentAdapter delegate) {
this(delegate, new SimpleReference>());
}
public Cached(ComponentAdapter delegate, ObjectReference> instanceReference) {
super(delegate, instanceReference);
}
public String getDescriptor() {
return "Cached" + getLifecycleDescriptor();
}
}
libpicocontainer-java-2.15/org/picocontainer/behaviors/Caching.java 0000664 0000000 0000000 00000005746 12422476443 0025535 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (c) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
*****************************************************************************/
package org.picocontainer.behaviors;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.Parameter;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.Characteristics;
import org.picocontainer.ComponentMonitor;
import org.picocontainer.behaviors.AbstractBehaviorFactory;
import org.picocontainer.references.SimpleReference;
import org.picocontainer.LifecycleStrategy;
import java.util.Properties;
/**
* factory class creating cached behaviours
* @author Aslak Hellesøy
* @author rafal@caltha.pl
* @author Konstantin Pribluda
*/
@SuppressWarnings("serial")
public class Caching extends AbstractBehaviorFactory {
public ComponentAdapter createComponentAdapter(
ComponentMonitor componentMonitor,
LifecycleStrategy lifecycleStrategy,
Properties componentProperties, Object componentKey,
Class componentImplementation, Parameter... parameters)
throws PicoCompositionException {
if (removePropertiesIfPresent(componentProperties,
Characteristics.NO_CACHE)) {
return super.createComponentAdapter(componentMonitor,
lifecycleStrategy, componentProperties, componentKey,
componentImplementation, parameters);
}
removePropertiesIfPresent(componentProperties, Characteristics.CACHE);
return componentMonitor.newBehavior(new Cached(super.createComponentAdapter(componentMonitor,
lifecycleStrategy, componentProperties, componentKey,
componentImplementation, parameters),
new SimpleReference>()));
}
public ComponentAdapter addComponentAdapter(
ComponentMonitor componentMonitor,
LifecycleStrategy lifecycleStrategy,
Properties componentProperties, ComponentAdapter adapter) {
if (removePropertiesIfPresent(componentProperties,
Characteristics.NO_CACHE)) {
return super.addComponentAdapter(componentMonitor,
lifecycleStrategy, componentProperties, adapter);
}
removePropertiesIfPresent(componentProperties, Characteristics.CACHE);
ComponentAdapter delegate = super.addComponentAdapter(componentMonitor, lifecycleStrategy, componentProperties, adapter);
return componentMonitor.newBehavior(componentMonitor.newBehavior(new Cached(delegate, new SimpleReference>())));
}
}
libpicocontainer-java-2.15/org/picocontainer/behaviors/Decorated.java 0000664 0000000 0000000 00000003043 12422476443 0026057 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
*****************************************************************************/
package org.picocontainer.behaviors;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.PicoContainer;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.behaviors.AbstractBehavior;
import java.lang.reflect.Type;
@SuppressWarnings("serial")
public class Decorated extends AbstractBehavior {
private final Decorator decorator;
public Decorated(ComponentAdapter delegate, Decorator decorator) {
super(delegate);
this.decorator = decorator;
}
public T getComponentInstance(final PicoContainer container, Type into)
throws PicoCompositionException {
T instance = super.getComponentInstance(container, into);
decorator.decorate(instance);
return instance;
}
public String getDescriptor() {
return "FieldDecorated";
}
interface Decorator {
void decorate(Object instance);
}
}
libpicocontainer-java-2.15/org/picocontainer/behaviors/Decorating.java 0000664 0000000 0000000 00000004547 12422476443 0026256 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) NanoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by Joerg Schaibe *
*****************************************************************************/
package org.picocontainer.behaviors;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.ComponentMonitor;
import org.picocontainer.LifecycleStrategy;
import org.picocontainer.Parameter;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.behaviors.Decorated;
import org.picocontainer.behaviors.AbstractBehaviorFactory;
import java.util.Properties;
/**
* BehaviorFactory for Decorating. This factory will create {@link org.picocontainer.gems.behaviors.Decorated} that will
* allow you to decorate what you like on the component instance that has been created
*
* @author Paul Hammant
*/
public abstract class Decorating extends AbstractBehaviorFactory implements Decorated.Decorator {
public ComponentAdapter createComponentAdapter(ComponentMonitor componentMonitor, LifecycleStrategy lifecycleStrategy,
Properties componentProperties, final Object componentKey,
final Class componentImplementation, final Parameter... parameters) throws PicoCompositionException {
return componentMonitor.newBehavior(new Decorated(super.createComponentAdapter(componentMonitor, lifecycleStrategy,
componentProperties,componentKey, componentImplementation, parameters), this));
}
public ComponentAdapter addComponentAdapter(ComponentMonitor componentMonitor, LifecycleStrategy lifecycleStrategy,
Properties componentProperties, ComponentAdapter adapter) {
return super.addComponentAdapter(componentMonitor, lifecycleStrategy, componentProperties, adapter);
}
} libpicocontainer-java-2.15/org/picocontainer/behaviors/FieldDecorated.java 0000664 0000000 0000000 00000002704 12422476443 0027026 0 ustar 00root root 0000000 0000000 package org.picocontainer.behaviors;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.PicoContainer;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
@SuppressWarnings("serial")
public class FieldDecorated extends AbstractBehavior {
private final Class> fieldClass;
private final Decorator decorator;
public FieldDecorated(ComponentAdapter delegate, Class> fieldClass, Decorator decorator) {
super(delegate);
this.fieldClass = fieldClass;
this.decorator = decorator;
}
public Object getComponentInstance(final PicoContainer container, Type into)
throws PicoCompositionException {
Object instance = super.getComponentInstance(container, into);
Field[] fields = instance.getClass().getDeclaredFields();
for (Field field : fields) {
if (field.getType() == fieldClass) {
Object value = decorator.decorate(instance);
field.setAccessible(true);
try {
field.set(instance, value);
} catch (IllegalAccessException e) {
throw new PicoCompositionException(e);
}
}
}
return instance;
}
public String getDescriptor() {
return "FieldDecorated";
}
public interface Decorator {
Object decorate(Object instance);
}
}
libpicocontainer-java-2.15/org/picocontainer/behaviors/FieldDecorating.java 0000664 0000000 0000000 00000005113 12422476443 0027210 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) NanoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by Joerg Schaibe *
*****************************************************************************/
package org.picocontainer.behaviors;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.ComponentMonitor;
import org.picocontainer.LifecycleStrategy;
import org.picocontainer.Parameter;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.behaviors.AbstractBehaviorFactory;
import java.util.Properties;
/**
* BehaviorFactory for Field Decorating. This factory will create {@link org.picocontainer.gems.behaviors.FieldDecorated} that will
* allow you to decorate fields on the component instance that has been created
*
* @author Paul Hammant
*/
public abstract class FieldDecorating extends AbstractBehaviorFactory implements FieldDecorated.Decorator {
private final Class> fieldClass;
public FieldDecorating(Class> fieldClass) {
this.fieldClass = fieldClass;
}
public ComponentAdapter createComponentAdapter(
ComponentMonitor componentMonitor, LifecycleStrategy lifecycleStrategy, Properties componentProperties, final Object componentKey, final Class componentImplementation, final Parameter... parameters)
throws PicoCompositionException {
return componentMonitor.newBehavior(new FieldDecorated(
super.createComponentAdapter(
componentMonitor, lifecycleStrategy, componentProperties, componentKey, componentImplementation, parameters),
fieldClass, this));
}
public ComponentAdapter addComponentAdapter(ComponentMonitor componentMonitor,
LifecycleStrategy lifecycleStrategy,
Properties componentProperties,
ComponentAdapter adapter) {
return super.addComponentAdapter(componentMonitor,
lifecycleStrategy,
componentProperties,
adapter);
}
} libpicocontainer-java-2.15/org/picocontainer/behaviors/Guarded.java 0000664 0000000 0000000 00000003244 12422476443 0025543 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
*****************************************************************************/
package org.picocontainer.behaviors;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.LifecycleStrategy;
import org.picocontainer.ObjectReference;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.PicoContainer;
import org.picocontainer.ComponentLifecycle;
import java.lang.reflect.Type;
import java.io.Serializable;
/**
* behaviour for allows components to be guarded by another component
*
* @author Paul Hammant
* @param
*/
@SuppressWarnings("serial")
public class Guarded extends AbstractBehavior {
private final String guard;
public Guarded(ComponentAdapter delegate, String guard) {
super(delegate);
this.guard = guard;
}
public T getComponentInstance(PicoContainer container, Type into) throws PicoCompositionException {
container.getComponent(guard);
return super.getComponentInstance(container, into);
}
public String getDescriptor() {
return "Guarded(with " + guard + ")";
}
} libpicocontainer-java-2.15/org/picocontainer/behaviors/Guarding.java 0000664 0000000 0000000 00000005270 12422476443 0025731 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (c) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
*****************************************************************************/
package org.picocontainer.behaviors;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.Parameter;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.Characteristics;
import org.picocontainer.ComponentMonitor;
import org.picocontainer.behaviors.AbstractBehaviorFactory;
import org.picocontainer.LifecycleStrategy;
import java.util.Properties;
/**
* factory class creating guard behaviour
*
* @author Paul Hammant
*/
@SuppressWarnings("serial")
public class Guarding extends AbstractBehaviorFactory {
public ComponentAdapter createComponentAdapter(
ComponentMonitor componentMonitor,
LifecycleStrategy lifecycleStrategy,
Properties componentProperties, Object componentKey,
Class componentImplementation, Parameter... parameters)
throws PicoCompositionException {
String guard = getAndRemovePropertiesIfPresentByKey(componentProperties, Characteristics.GUARD);
ComponentAdapter delegate = super.createComponentAdapter(componentMonitor, lifecycleStrategy,
componentProperties, componentKey, componentImplementation, parameters);
if (guard == null) {
return delegate;
} else {
return componentMonitor.newBehavior(new Guarded(delegate, guard));
}
}
public ComponentAdapter addComponentAdapter(
ComponentMonitor componentMonitor,
LifecycleStrategy lifecycleStrategy,
Properties componentProperties, ComponentAdapter adapter) {
String guard = getAndRemovePropertiesIfPresentByKey(componentProperties, Characteristics.GUARD);
ComponentAdapter delegate = super.addComponentAdapter(componentMonitor, lifecycleStrategy, componentProperties, adapter);
if (guard == null) {
return delegate;
} else {
return componentMonitor.newBehavior(componentMonitor.newBehavior(new Guarded(delegate, guard)));
}
}
} libpicocontainer-java-2.15/org/picocontainer/behaviors/HiddenImplementation.java 0000664 0000000 0000000 00000011457 12422476443 0030276 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer.behaviors;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.ComponentMonitor;
import org.picocontainer.PicoContainer;
import org.picocontainer.PicoCompositionException;
/**
* This component adapter makes it possible to hide the implementation
* of a real subject (behind a proxy) provided the key is an interface.
*
* This class exists here, because a) it has no deps on external jars, b) dynamic proxy is quite easy.
* The user is prompted to look at picocontainer-gems for alternate and bigger implementations.
*
* @author Aslak Hellesøy
* @author Paul Hammant
* @see org.picocontainer.gems.adapters.HotSwappingComponentAdapter for a more feature-rich version of this class.
*/
@SuppressWarnings("serial")
public class HiddenImplementation extends AbstractBehavior {
/**
* Creates an ImplementationHidingComponentAdapter with a delegate
* @param delegate the component adapter to which this adapter delegates
*/
public HiddenImplementation(ComponentAdapter delegate) {
super(delegate);
}
public T getComponentInstance(final PicoContainer container, Type into) throws PicoCompositionException {
ComponentAdapter delegate = getDelegate();
Object componentKey = delegate.getComponentKey();
Class>[] classes;
if (componentKey instanceof Class && ((Class>) delegate.getComponentKey()).isInterface()) {
classes = new Class[]{(Class>) delegate.getComponentKey()};
} else if (componentKey instanceof Class[]) {
classes = (Class[]) componentKey;
} else {
return delegate.getComponentInstance(container, into);
}
verifyInterfacesOnly(classes);
return createProxy(classes, container, delegate.getComponentImplementation().getClassLoader());
}
public String getDescriptor() {
return "Hidden";
}
@SuppressWarnings("unchecked")
protected T createProxy(Class[] interfaces, final PicoContainer container, final ClassLoader classLoader) {
final PicoContainer container1 = container;
return (T) Proxy.newProxyInstance(classLoader, interfaces, new InvocationHandler() {
private final PicoContainer container = container1;
private volatile Object instance;
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
if (instance == null) {
synchronized (HiddenImplementation.this) {
if (instance == null) {
instance = getDelegate().getComponentInstance(container, NOTHING.class);
}
}
}
return invokeMethod(instance, method, args, container);
}
});
}
protected Object invokeMethod(Object componentInstance, Method method, Object[] args, PicoContainer container) throws Throwable {
ComponentMonitor componentMonitor = currentMonitor();
try {
componentMonitor.invoking(container, this, method, componentInstance, args);
long startTime = System.currentTimeMillis();
Object rv = method.invoke(componentInstance, args);
componentMonitor.invoked(container, this,
method, componentInstance, System.currentTimeMillis() - startTime, args, rv);
return rv;
} catch (final InvocationTargetException ite) {
componentMonitor.invocationFailed(method, componentInstance, ite);
throw ite.getTargetException();
}
}
private void verifyInterfacesOnly(Class>[] classes) {
for (Class> clazz : classes) {
if (!clazz.isInterface()) {
throw new PicoCompositionException(
"Class keys must be interfaces. " + clazz + " is not an interface.");
}
}
}
}
libpicocontainer-java-2.15/org/picocontainer/behaviors/ImplementationHiding.java 0000664 0000000 0000000 00000006220 12422476443 0030275 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer.behaviors;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.Parameter;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.ComponentMonitor;
import org.picocontainer.LifecycleStrategy;
import org.picocontainer.Characteristics;
import org.picocontainer.behaviors.AbstractBehaviorFactory;
import java.util.Properties;
/**
* @author Aslak Hellesøy
* @see org.picocontainer.gems.adapters.HotSwappingComponentFactory for a more feature-rich version of the class
*/
@SuppressWarnings("serial")
public class ImplementationHiding extends AbstractBehaviorFactory {
public ComponentAdapter createComponentAdapter(ComponentMonitor componentMonitor, LifecycleStrategy lifecycleStrategy, Properties componentProperties, Object componentKey, Class componentImplementation, Parameter... parameters) throws PicoCompositionException {
removePropertiesIfPresent(componentProperties, Characteristics.ENABLE_CIRCULAR);
ComponentAdapter componentAdapter = super.createComponentAdapter(componentMonitor, lifecycleStrategy,
componentProperties, componentKey, componentImplementation, parameters);
if (removePropertiesIfPresent(componentProperties, Characteristics.NO_HIDE_IMPL)) {
return componentAdapter;
}
removePropertiesIfPresent(componentProperties, Characteristics.HIDE_IMPL);
return componentMonitor.newBehavior(new HiddenImplementation(componentAdapter));
}
public ComponentAdapter addComponentAdapter(ComponentMonitor componentMonitor,
LifecycleStrategy lifecycleStrategy,
Properties componentProperties,
ComponentAdapter adapter) {
if (removePropertiesIfPresent(componentProperties, Characteristics.NO_HIDE_IMPL)) {
return adapter;
}
removePropertiesIfPresent(componentProperties, Characteristics.HIDE_IMPL);
return componentMonitor.newBehavior(new HiddenImplementation(super.addComponentAdapter(componentMonitor,
lifecycleStrategy,
componentProperties,
adapter)));
}
}
libpicocontainer-java-2.15/org/picocontainer/behaviors/Intercepted.java 0000664 0000000 0000000 00000012477 12422476443 0026446 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (c) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
*****************************************************************************/
package org.picocontainer.behaviors;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.PicoContainer;
import java.util.Map;
import java.util.HashMap;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.io.Serializable;
/** @author Paul Hammant */
@SuppressWarnings("serial")
public class Intercepted extends HiddenImplementation {
private final Map pres = new HashMap();
private final Map posts = new HashMap();
private Controller controller = new ControllerWrapper(new InterceptorThreadLocal());
public Intercepted(ComponentAdapter delegate) {
super(delegate);
}
public void addPreInvocation(Class type, Object interceptor) {
pres.put(type, interceptor);
}
public void addPostInvocation(Class type, Object interceptor) {
posts.put(type, interceptor);
}
@Override
protected Object invokeMethod(Object componentInstance, Method method, Object[] args, PicoContainer container) throws Throwable {
try {
controller.clear();
controller.instance(componentInstance);
Object pre = pres.get(method.getDeclaringClass());
if (pre != null) {
Object rv = method.invoke(pre, args);
if (controller.isVetoed()) {
return rv;
}
}
Object result = method.invoke(componentInstance, args);
controller.setOriginalRetVal(result);
Object post = posts.get(method.getDeclaringClass());
if (post != null) {
Object rv = method.invoke(post, args);
if (controller.isOverridden()) {
return rv;
}
}
return result;
} catch (final InvocationTargetException ite) {
throw ite.getTargetException();
}
}
public Controller getController() {
return controller;
}
public static class InterceptorThreadLocal extends ThreadLocal implements Serializable {
protected Object initialValue() {
return new ControllerImpl();
}
}
public interface Controller {
void veto();
void clear();
boolean isVetoed();
void setOriginalRetVal(Object retVal);
boolean isOverridden();
void instance(Object instance);
Object getInstance();
Object getOriginalRetVal();
void override();
}
public static class ControllerImpl implements Controller {
private boolean vetoed;
private Object retVal;
private boolean overridden;
private Object instance;
public void veto() {
vetoed = true;
}
public void clear() {
vetoed = false;
overridden = false;
retVal = null;
instance = null;
}
public boolean isVetoed() {
return vetoed;
}
public void setOriginalRetVal(Object retVal) {
this.retVal = retVal;
}
public Object getOriginalRetVal() {
return retVal;
}
public boolean isOverridden() {
return overridden;
}
public void instance(Object instance) {
this.instance = instance;
}
public Object getInstance() {
return instance;
}
public void override() {
overridden = true;
}
}
public class ControllerWrapper implements Controller {
private final ThreadLocal threadLocal;
public ControllerWrapper(ThreadLocal threadLocal) {
this.threadLocal = threadLocal;
}
public void veto() {
threadLocal.get().veto();
}
public void clear() {
threadLocal.get().clear();
}
public boolean isVetoed() {
return threadLocal.get().isVetoed();
}
public void setOriginalRetVal(Object retVal) {
threadLocal.get().setOriginalRetVal(retVal);
}
public Object getOriginalRetVal() {
return threadLocal.get().getOriginalRetVal();
}
public boolean isOverridden() {
return threadLocal.get().isOverridden();
}
public void instance(Object instance) {
threadLocal.get().instance(instance);
}
public Object getInstance() {
return threadLocal.get().getInstance();
}
public void override() {
threadLocal.get().override();
}
}
public String getDescriptor() {
return "Intercepted";
}
}
libpicocontainer-java-2.15/org/picocontainer/behaviors/Intercepting.java 0000664 0000000 0000000 00000003614 12422476443 0026624 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (c) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
*****************************************************************************/
package org.picocontainer.behaviors;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.ComponentMonitor;
import org.picocontainer.LifecycleStrategy;
import org.picocontainer.Parameter;
import org.picocontainer.PicoCompositionException;
import java.util.Properties;
/** @author Paul Hammant */
@SuppressWarnings("serial")
public class Intercepting extends AbstractBehaviorFactory {
public ComponentAdapter createComponentAdapter(ComponentMonitor componentMonitor,
LifecycleStrategy lifecycleStrategy,
Properties componentProperties,
Object componentKey,
Class componentImplementation,
Parameter... parameters) throws PicoCompositionException {
return componentMonitor.newBehavior(new Intercepted(super.createComponentAdapter(componentMonitor,
lifecycleStrategy, componentProperties, componentKey,
componentImplementation, parameters)));
}
}
libpicocontainer-java-2.15/org/picocontainer/behaviors/Locked.java 0000664 0000000 0000000 00000003203 12422476443 0025364 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer.behaviors;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.PicoContainer;
import org.picocontainer.PicoCompositionException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.lang.reflect.Type;
/**
* @author Paul Hammant
*/
@SuppressWarnings("serial")
public class Locked extends AbstractBehavior {
/**
* Reentrant lock.
*/
private Lock lock = new ReentrantLock();
public Locked(ComponentAdapter delegate) {
super(delegate);
}
public T getComponentInstance(PicoContainer container, Type into) throws PicoCompositionException {
T retVal = null;
lock.lock();
try {
retVal = super.getComponentInstance(container, into);
}
finally {
lock.unlock();
}
return retVal;
}
public String getDescriptor() {
return "Locked";
}
} libpicocontainer-java-2.15/org/picocontainer/behaviors/Locking.java 0000664 0000000 0000000 00000007162 12422476443 0025561 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer.behaviors;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.Parameter;
import org.picocontainer.ComponentMonitor;
import org.picocontainer.LifecycleStrategy;
import org.picocontainer.Characteristics;
import org.picocontainer.behaviors.AbstractBehaviorFactory;
import java.util.Properties;
/**
* This behavior factory provides java.util.concurrent locks. It is recommended to be used instead
* of {@link org.picocontainer.behaviors.Synchronizing} since it results in better performance.
* @author Aslak Hellesøy
* @author Paul Hammant.
*/
@SuppressWarnings("serial")
public class Locking extends AbstractBehaviorFactory {
/** {@inheritDoc} **/
public ComponentAdapter createComponentAdapter(ComponentMonitor componentMonitor,
LifecycleStrategy lifecycleStrategy,
Properties componentProperties,
Object componentKey,
Class componentImplementation,
Parameter... parameters) {
if (removePropertiesIfPresent(componentProperties, Characteristics.NO_LOCK)) {
return super.createComponentAdapter(
componentMonitor,
lifecycleStrategy,
componentProperties,
componentKey,
componentImplementation,
parameters);
}
removePropertiesIfPresent(componentProperties, Characteristics.LOCK);
return componentMonitor.newBehavior(new Locked(super.createComponentAdapter(
componentMonitor,
lifecycleStrategy,
componentProperties,
componentKey,
componentImplementation,
parameters)));
}
/** {@inheritDoc} **/
public ComponentAdapter addComponentAdapter(ComponentMonitor componentMonitor,
LifecycleStrategy lifecycleStrategy,
Properties componentProperties,
ComponentAdapter adapter) {
if (removePropertiesIfPresent(componentProperties, Characteristics.NO_LOCK)) {
return super.addComponentAdapter(componentMonitor,
lifecycleStrategy,
componentProperties,
adapter);
}
removePropertiesIfPresent(componentProperties, Characteristics.LOCK);
return componentMonitor.newBehavior(new Locked(super.addComponentAdapter(componentMonitor,
lifecycleStrategy,
componentProperties,
adapter)));
}
} libpicocontainer-java-2.15/org/picocontainer/behaviors/OptInCaching.java 0000664 0000000 0000000 00000010315 12422476443 0026473 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (c) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
*****************************************************************************/
package org.picocontainer.behaviors;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.Parameter;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.Characteristics;
import org.picocontainer.ComponentMonitor;
import org.picocontainer.behaviors.Cached;
import org.picocontainer.behaviors.AbstractBehaviorFactory;
import org.picocontainer.LifecycleStrategy;
import java.util.Properties;
/**
* Behavior that turns off Caching behavior by default.
* Example:
*
* import org.picocontainer.*;
* import static org.picocontainer.Characteristics.*;
*
* MutablePicoContainer mpc = new PicoBuilder().withBehaviors(new OptInCaching()).build();
* mpc.addComponent(Map.class, HashMap.class) //Multiple Instances, no Caching.
* mpc.as(CACHE).addComponent(Set.class, HashSet.class) //Single Cached Instance.
*
* @author Aslak Hellesøy
* @author rafal@caltha.pl
*/
@SuppressWarnings("serial")
public class OptInCaching extends AbstractBehaviorFactory {
public ComponentAdapter createComponentAdapter(ComponentMonitor componentMonitor, LifecycleStrategy lifecycleStrategy, Properties componentProperties, Object componentKey,
Class componentImplementation, Parameter... parameters)
throws PicoCompositionException {
if (AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.CACHE)) {
return componentMonitor.newBehavior(new Cached(super.createComponentAdapter(componentMonitor,
lifecycleStrategy,
componentProperties,
componentKey,
componentImplementation,
parameters)));
}
AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.NO_CACHE);
return super.createComponentAdapter(componentMonitor, lifecycleStrategy,
componentProperties, componentKey, componentImplementation, parameters);
}
public ComponentAdapter addComponentAdapter(ComponentMonitor componentMonitor,
LifecycleStrategy lifecycleStrategy,
Properties componentProperties,
ComponentAdapter adapter) {
if (AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.CACHE)) {
return componentMonitor.newBehavior(new Cached(super.addComponentAdapter(componentMonitor,
lifecycleStrategy,
componentProperties,
adapter)));
}
AbstractBehaviorFactory.removePropertiesIfPresent(componentProperties, Characteristics.NO_CACHE);
return super.addComponentAdapter(componentMonitor,
lifecycleStrategy,
componentProperties,
adapter);
}
} libpicocontainer-java-2.15/org/picocontainer/behaviors/PropertyApplicator.java 0000664 0000000 0000000 00000031060 12422476443 0030030 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer.behaviors;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.io.File;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Set;
import java.util.HashMap;
import java.security.AccessController;
import java.security.PrivilegedAction;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.ComponentMonitor;
import org.picocontainer.PicoContainer;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.PicoClassNotFoundException;
import org.picocontainer.injectors.SetterInjector;
import org.picocontainer.behaviors.AbstractBehavior;
import org.picocontainer.behaviors.Cached;
/**
* Decorating component adapter that can be used to set additional properties
* on a component in a bean style. These properties must be managed manually
* by the user of the API, and will not be managed by PicoContainer. This class
* is therefore not the same as {@link SetterInjector},
* which is a true Setter Injection adapter.
*
* This adapter is mostly handy for setting various primitive properties via setters;
* it is also able to set javabean properties by discovering an appropriate
* {@link PropertyEditor} and using its setAsText
method.
*
*
* Note that this class doesn't cache instances. If you want caching,
* use a {@link Cached} around this one.
*
*
* @author Aslak Hellesøy
* @author Mauro Talevi
*/
@SuppressWarnings("serial")
public class PropertyApplicator extends AbstractBehavior {
private Map properties;
private transient Map setters = null;
/**
* Construct a PropertyApplicator.
*
* @param delegate the wrapped {@link ComponentAdapter}
* @throws PicoCompositionException {@inheritDoc}
*/
public PropertyApplicator(ComponentAdapter delegate) throws PicoCompositionException {
super(delegate);
}
/**
* Get a component instance and set given property values.
*
* @return the component instance with any properties of the properties map set.
* @throws PicoCompositionException {@inheritDoc}
* @throws PicoCompositionException {@inheritDoc}
* @throws org.picocontainer.PicoCompositionException
* {@inheritDoc}
* @see #setProperties(Map)
*/
public T getComponentInstance(PicoContainer container, Type into) throws PicoCompositionException {
final T componentInstance = super.getComponentInstance(container, into);
if (setters == null) {
setters = getSetters(getComponentImplementation());
}
if (properties != null) {
ComponentMonitor componentMonitor = currentMonitor();
Set propertyNames = properties.keySet();
for (String propertyName : propertyNames) {
final Object propertyValue = properties.get(propertyName);
Method setter = setters.get(propertyName);
Object valueToInvoke = this.getSetterParameter(propertyName, propertyValue, componentInstance, container);
try {
componentMonitor.invoking(container, PropertyApplicator.this, setter, componentInstance, new Object[] {valueToInvoke});
long startTime = System.currentTimeMillis();
setter.invoke(componentInstance, valueToInvoke);
componentMonitor.invoked(container,
PropertyApplicator.this,
setter, componentInstance, System.currentTimeMillis() - startTime, new Object[] {valueToInvoke}, null);
} catch (final Exception e) {
componentMonitor.invocationFailed(setter, componentInstance, e);
throw new PicoCompositionException("Failed to set property " + propertyName + " to " + propertyValue + ": " + e.getMessage(), e);
}
}
}
return componentInstance;
}
public String getDescriptor() {
return "PropertyApplied";
}
private Map getSetters(Class> clazz) {
Map result = new HashMap();
Method[] methods = getMethods(clazz);
for (Method method : methods) {
if (isSetter(method)) {
result.put(getPropertyName(method), method);
}
}
return result;
}
private Method[] getMethods(final Class> clazz) {
return (Method[]) AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return clazz.getMethods();
}
});
}
private String getPropertyName(Method method) {
final String name = method.getName();
String result = name.substring(3);
if(result.length() > 1 && !Character.isUpperCase(result.charAt(1))) {
result = "" + Character.toLowerCase(result.charAt(0)) + result.substring(1);
} else if(result.length() == 1) {
result = result.toLowerCase();
}
return result;
}
private boolean isSetter(Method method) {
final String name = method.getName();
return name.length() > 3 &&
name.startsWith("set") &&
method.getParameterTypes().length == 1;
}
private Object convertType(PicoContainer container, Method setter, String propertyValue) {
if (propertyValue == null) {
return null;
}
Class> type = setter.getParameterTypes()[0];
String typeName = type.getName();
Object result = convert(typeName, propertyValue, Thread.currentThread().getContextClassLoader());
if (result == null) {
// check if the propertyValue is a key of a component in the container
// if so, the typeName of the component and the setters parameter typeName
// have to be compatible
// TODO: null check only because of test-case, otherwise null is impossible
if (container != null) {
Object component = container.getComponent(propertyValue);
if (component != null && type.isAssignableFrom(component.getClass())) {
return component;
}
}
}
return result;
}
/**
* Converts a String value of a named type to an object.
* Works with primitive wrappers, String, File, URL types, or any type that has
* an appropriate {@link PropertyEditor}.
*
* @param typeName name of the type
* @param value its value
* @param classLoader used to load a class if typeName is "class" or "java.lang.Class" (ignored otherwise)
* @return instantiated object or null if the type was unknown/unsupported
*/
public static Object convert(String typeName, String value, ClassLoader classLoader) {
if (typeName.equals(Boolean.class.getName()) || typeName.equals(boolean.class.getName())) {
return Boolean.valueOf(value);
} else if (typeName.equals(Byte.class.getName()) || typeName.equals(byte.class.getName())) {
return Byte.valueOf(value);
} else if (typeName.equals(Short.class.getName()) || typeName.equals(short.class.getName())) {
return Short.valueOf(value);
} else if (typeName.equals(Integer.class.getName()) || typeName.equals(int.class.getName())) {
return Integer.valueOf(value);
} else if (typeName.equals(Long.class.getName()) || typeName.equals(long.class.getName())) {
return Long.valueOf(value);
} else if (typeName.equals(Float.class.getName()) || typeName.equals(float.class.getName())) {
return Float.valueOf(value);
} else if (typeName.equals(Double.class.getName()) || typeName.equals(double.class.getName())) {
return Double.valueOf(value);
} else if (typeName.equals(Character.class.getName()) || typeName.equals(char.class.getName())) {
return value.toCharArray()[0];
} else if (typeName.equals(String.class.getName()) || typeName.equals("string")) {
return value;
} else if (typeName.equals(File.class.getName()) || typeName.equals("file")) {
return new File(value);
} else if (typeName.equals(URL.class.getName()) || typeName.equals("url")) {
try {
return new URL(value);
} catch (MalformedURLException e) {
throw new PicoCompositionException(e);
}
} else if (typeName.equals(Class.class.getName()) || typeName.equals("class")) {
return loadClass(classLoader, value);
} else {
final Class> clazz = loadClass(classLoader, typeName);
final PropertyEditor editor = PropertyEditorManager.findEditor(clazz);
if (editor != null) {
editor.setAsText(value);
return editor.getValue();
}
}
return null;
}
private static Class> loadClass(ClassLoader classLoader, String typeName) {
try {
return classLoader.loadClass(typeName);
} catch (ClassNotFoundException e) {
throw new PicoClassNotFoundException(typeName, e);
}
}
/**
* Sets the bean property values that should be set upon creation.
*
* @param properties bean properties
*/
public void setProperties(Map properties) {
this.properties = properties;
}
/**
* Converts and validates the given property value to an appropriate object
* for calling the bean's setter.
* @param propertyName String the property name on the component that
* we will be setting the value to.
* @param propertyValue Object the property value that we've been given. It
* may need conversion to be formed into the value we need for the
* component instance setter.
* @param componentInstance the component that we're looking to provide
* the setter to.
* @return Object: the final converted object that can
* be used in the setter.
* @param container
*/
private Object getSetterParameter(final String propertyName, final Object propertyValue,
final Object componentInstance, PicoContainer container) {
if (propertyValue == null) {
return null;
}
Method setter = setters.get(propertyName);
//We can assume that there is only one object (as per typical setters)
//because the Setter introspector does that job for us earlier.
Class> setterParameter = setter.getParameterTypes()[0];
Object convertedValue;
Class extends Object> givenParameterClass = propertyValue.getClass();
//
//If property value is a string or a true primative then convert it to whatever
//we need. (String will convert to string).
//
convertedValue = convertType(container, setter, propertyValue.toString());
//Otherwise, check the parameter type to make sure we can
//assign it properly.
if (convertedValue == null) {
if (setterParameter.isAssignableFrom(givenParameterClass)) {
convertedValue = propertyValue;
} else {
throw new ClassCastException("Setter: " + setter.getName() + " for addComponent: "
+ componentInstance.toString() + " can only take objects of: " + setterParameter.getName()
+ " instead got: " + givenParameterClass.getName());
}
}
return convertedValue;
}
public void setProperty(String name, String value) {
if (properties == null) {
properties = new HashMap();
}
properties.put(name, value);
}
}
libpicocontainer-java-2.15/org/picocontainer/behaviors/PropertyApplying.java 0000664 0000000 0000000 00000005011 12422476443 0027512 0 ustar 00root root 0000000 0000000 /*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package org.picocontainer.behaviors;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.Parameter;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.ComponentMonitor;
import org.picocontainer.LifecycleStrategy;
import org.picocontainer.Characteristics;
import org.picocontainer.behaviors.AbstractBehaviorFactory;
import org.picocontainer.behaviors.PropertyApplicator;
import java.util.Properties;
/**
* A {@link org.picocontainer.ComponentFactory} that creates
* {@link PropertyApplicator} instances.
*
* @author Aslak Hellesøy
*/
@SuppressWarnings("serial")
public final class PropertyApplying extends AbstractBehaviorFactory {
public ComponentAdapter