MentaContainer

Compare Revisions

Ignore whitespace Rev 145 → Rev 146

/trunk/src/test/java/org/mentacontainer/impl/GenericFactoryTest.java
3,8 → 3,10
import junit.framework.Assert;
 
import org.junit.Test;
import org.mentacontainer.Factory;
import org.mentacontainer.Container;
import org.mentacontainer.Factory;
import org.mentacontainer.impl.GenericFactory;
import org.mentacontainer.impl.MentaContainer;
 
public class GenericFactoryTest {
25,7 → 27,7
Container c = new MentaContainer();
// giveMeSomething => method that will be called to return object
Factory<String> generic = new GenericFactory<String>(factory, "giveMeSomething");
Factory generic = new GenericFactory(factory, "giveMeSomething");
c.ioc("myFactory", generic);
/trunk/src/test/java/org/mentacontainer/impl/MentaContainerTest.java
3,6 → 3,8
import junit.framework.Assert;
 
import org.junit.Test;
import org.mentacontainer.Interceptor;
import org.mentacontainer.Factory;
import org.mentacontainer.Container;
import org.mentacontainer.Scope;
 
434,4 → 436,100
Assert.assertNotNull(conn.getMyDep());
}
private static class SomeObject {
private boolean destroyed = false;
private boolean created = false;
public void destroyed() { this.destroyed = true; }
public boolean isDestroyed() { return destroyed; }
public void created() { this.created = true; }
public boolean isCreated() { return created; }
}
private static class SomeFactory implements Factory, Interceptor<SomeObject> {
@Override
public <T> T getInstance() {
return (T) new SomeObject();
}
@Override
public Class<? extends Object> getType() {
return SomeObject.class;
}
@Override
public void onCleared(SomeObject obj) {
obj.destroyed();
}
@Override
public void onCreated(SomeObject obj) {
obj.created();
}
}
@Test
public void testInterceptor() {
Container c = new MentaContainer();
c.ioc("o", new SomeFactory(), Scope.SINGLETON);
SomeObject o = c.get("o");
Assert.assertTrue(o.isCreated());
c.clear(Scope.SINGLETON);
Assert.assertEquals(true, o.isDestroyed());
c.ioc("o", new SomeFactory(), Scope.THREAD);
o = c.get("o");
Assert.assertTrue(o.isCreated());
c.clear(Scope.SINGLETON);
Assert.assertEquals(false, o.isDestroyed());
c.clear(Scope.THREAD);
Assert.assertEquals(true, o.isDestroyed());
c.ioc("o", new SomeFactory(), Scope.NONE);
o = c.get("o");
Assert.assertTrue(o.isCreated());
o = c.clear("o");
Assert.assertNull(o);
c.ioc("o", new SomeFactory(), Scope.THREAD);
o = c.clear("o");
Assert.assertNull(o);
o = c.get("o");
Assert.assertTrue(o.isCreated());
o = c.clear("o");
Assert.assertEquals(true, o.isDestroyed());
}
}
/trunk/src/test/java/org/mentacontainer/impl/WrapperFactoryTest.java
3,8 → 3,8
import junit.framework.Assert;
 
import org.junit.Test;
import org.mentacontainer.Factory;
import org.mentacontainer.Container;
import org.mentacontainer.Factory;
 
public class WrapperFactoryTest {
13,7 → 13,7
String s = new String("saoj");
Factory<String> ic = new SingletonFactory<String>(s);
Factory ic = new WrapperFactory(s);
Container c = new MentaContainer();
/trunk/src/test/java/org/mentacontainer/impl/ClassFactoryTest.java
31,9 → 31,9
MentaContainer container = new MentaContainer();
Factory<User> c1 = new ClassFactory<User>(container, User.class).addInitValue("saoj");
Factory c1 = new ClassFactory(container, User.class).addInitValue("saoj");
Factory<User> c2 = new ClassFactory<User>(container, User.class).addPropertyValue("username", "soliveira");
Factory c2 = new ClassFactory(container, User.class).addPropertyValue("username", "soliveira");
User u1 = c1.getInstance();
66,7 → 66,7
c.ioc("myString", String.class).addInitValue("hello");
Factory<TestObj1> c1 = new ClassFactory<TestObj1>(c, TestObj1.class).addConstructorDependency("myString").addInitValue(20).addInitValue(30000L)
Factory c1 = new ClassFactory(c, TestObj1.class).addConstructorDependency("myString").addInitValue(20).addInitValue(30000L)
.addInitValue(new User("saoj"));
TestObj1 o = c1.getInstance();
95,7 → 95,7
c.ioc("myString", String.class).addInitValue("hello");
Factory<TestObj1_1> c1 = new ClassFactory<TestObj1_1>(c, TestObj1_1.class).addPropertyDependency("s", "myString").addPropertyValue("i", 30);
Factory c1 = new ClassFactory(c, TestObj1_1.class).addPropertyDependency("s", "myString").addPropertyValue("i", 30);
TestObj1_1 o = c1.getInstance();
119,7 → 119,7
MentaContainer container = new MentaContainer();
Factory<TestObj2> f = new ClassFactory<TestObj2>(container, TestObj2.class).addPropertyValue("integer", 20);
Factory f = new ClassFactory(container, TestObj2.class).addPropertyValue("integer", 20);
TestObj2 o = f.getInstance();
131,7 → 131,7
MentaContainer container = new MentaContainer();
Factory<TestObj2> f = new ClassFactory<TestObj2>(container, TestObj2.class).addPropertyValue("integer", new Integer(20));
Factory f = new ClassFactory(container, TestObj2.class).addPropertyValue("integer", new Integer(20));
TestObj2 o = f.getInstance();
154,7 → 154,7
MentaContainer container = new MentaContainer();
Factory<TestObj2_1> f = new ClassFactory<TestObj2_1>(container, TestObj2_1.class).addPropertyValue("integer", 20);
Factory f = new ClassFactory(container, TestObj2_1.class).addPropertyValue("integer", 20);
TestObj2_1 o = f.getInstance();
166,7 → 166,7
MentaContainer container = new MentaContainer();
Factory<TestObj2_1>f = new ClassFactory<TestObj2_1>(container, TestObj2_1.class).addPropertyValue("integer", new Integer(20));
Factory f = new ClassFactory(container, TestObj2_1.class).addPropertyValue("integer", new Integer(20));
TestObj2_1 o = f.getInstance();
188,7 → 188,7
MentaContainer container = new MentaContainer();
Factory<TestObj3> f = new ClassFactory<TestObj3>(container, TestObj3.class).addInitValue(20);
Factory f = new ClassFactory(container, TestObj3.class).addInitValue(20);
TestObj3 o = f.getInstance();
209,7 → 209,7
MentaContainer container = new MentaContainer();
Factory<TestObj3_1> f = new ClassFactory<TestObj3_1>(container, TestObj3_1.class).addInitValue(20);
Factory f = new ClassFactory(container, TestObj3_1.class).addInitValue(20);
TestObj3_1 o = f.getInstance();
232,7 → 232,7
MentaContainer container = new MentaContainer();
Factory<TestObj3_2> f = new ClassFactory<TestObj3_2>(container, TestObj3_2.class).addInitValue(20).addInitPrimitive(30L);
Factory f = new ClassFactory(container, TestObj3_2.class).addInitValue(20).addInitPrimitive(30L);
TestObj3_2 o = f.getInstance();
260,7 → 260,7
MentaContainer container = new MentaContainer();
Factory<TestObj4> f = new ClassFactory<TestObj4>(container, TestObj4.class).addInitPrimitive(20).addInitValue("hello").addInitValue(20L).addInitPrimitive(20L);
Factory f = new ClassFactory(container, TestObj4.class).addInitPrimitive(20).addInitValue("hello").addInitValue(20L).addInitPrimitive(20L);
TestObj4 o = f.getInstance();
280,7 → 280,7
MentaContainer container = new MentaContainer();
Factory<TestObj22> f = new ClassFactory<TestObj22>(container, TestObj22.class);
Factory f = new ClassFactory(container, TestObj22.class);
TestObj22 o = f.getInstance();
/trunk/src/main/java/org/mentacontainer/Pool.java
File deleted
\ No newline at end of file
/trunk/src/main/java/org/mentacontainer/impl/GenericPool.java
File deleted
\ No newline at end of file
/trunk/src/main/java/org/mentacontainer/impl/SingletonFactory.java
File deleted
\ No newline at end of file
/trunk/src/main/java/org/mentacontainer/impl/GenericFactory.java
3,27 → 3,30
import java.lang.reflect.Method;
 
import org.mentacontainer.Factory;
import org.mentacontainer.Interceptor;
import org.mentacontainer.util.FindMethod;
 
public class GenericFactory<E> implements Factory<E> {
public class GenericFactory<E> implements Factory, Interceptor<E> {
private final Object factory;
private final Method getMethod;
private final Method method;
private final Class<E> type;
private final Class<? extends Object> type;
public GenericFactory(Object factory, String getMethodName) {
private Interceptor<E> interceptor = null;
public GenericFactory(Object factory, String methodName) {
this.factory = factory;
try {
this.getMethod = FindMethod.getMethod(factory.getClass(), getMethodName, new Class[] { });
this.method = FindMethod.getMethod(factory.getClass(), methodName, new Class[] { });
this.getMethod.setAccessible(true);
this.method.setAccessible(true);
this.type = (Class<E>) getMethod.getReturnType();
this.type = method.getReturnType();
} catch(Exception e) {
31,22 → 34,41
}
}
public void setInterceptor(Interceptor<E> interceptor) {
this.interceptor = interceptor;
}
@Override
public E getInstance() {
public void onCreated(E createdObject) {
if (interceptor != null) {
interceptor.onCreated(createdObject);
}
}
@Override
public void onCleared(E clearedObject) {
if (interceptor != null) {
interceptor.onCleared(clearedObject);
}
}
@Override
public <T> T getInstance() {
try {
return (E) getMethod.invoke(factory, (Object[]) null);
return (T) method.invoke(factory, (Object[]) null);
} catch(Exception e) {
throw new RuntimeException("Cannot invoke method: " + getMethod, e);
throw new RuntimeException("Cannot invoke method: " + method, e);
}
}
@Override
public Class<E> getType() {
public Class<? extends Object> getType() {
return type;
}
}
/trunk/src/main/java/org/mentacontainer/impl/MentaContainer.java
12,7 → 12,7
import org.mentacontainer.ConfigurableFactory;
import org.mentacontainer.Container;
import org.mentacontainer.Factory;
import org.mentacontainer.Pool;
import org.mentacontainer.Interceptor;
import org.mentacontainer.Scope;
import org.mentacontainer.util.InjectionUtils;
import org.mentacontainer.util.InjectionUtils.Provider;
24,7 → 24,7
*/
public class MentaContainer implements Container {
 
private Map<String, Pool<?>> poolsByName = new Hashtable<String, Pool<?>>();
private Map<String, Factory> factoriesByName = new Hashtable<String, Factory>();
private Map<String, Scope> scopes = new Hashtable<String, Scope>();
39,11 → 39,11
private Set<ConstructorDependency> forConstructMethod = Collections.synchronizedSet(new HashSet<ConstructorDependency>());
@Override
public Class<?> getType(Object key) {
public Class<? extends Object> getType(Object key) {
String k = InjectionUtils.getKeyName(key);
Pool<?> factory = poolsByName.get(k);
Factory factory = factoriesByName.get(k);
if (factory == null) return null;
61,11 → 61,16
for(String key : singletonsCache.keySet()) {
Pool<?> pool = poolsByName.get(key);
Factory factory = factoriesByName.get(key);
Object value = singletonsCache.get(key);
if (factory instanceof Interceptor) {
Interceptor c = (Interceptor) factory;
listToClear.add(new ClearableHolder(pool, value));
Object value = singletonsCache.get(key);
listToClear.add(new ClearableHolder(c, value));
}
}
singletonsCache.clear();
83,13 → 88,18
for(String key : threadLocalsCache.keySet()) {
Pool<?> factory = poolsByName.get(key);
Factory factory = factoriesByName.get(key);
ThreadLocal<Object> t = threadLocalsCache.get(key);
Object value = t.get();
if (value != null) listToClear.add(new ClearableHolder(factory, value));
if (factory instanceof Interceptor) {
Interceptor c = (Interceptor) factory;
ThreadLocal<Object> t = threadLocalsCache.get(key);
Object value = t.get();
if (value != null) listToClear.add(new ClearableHolder(c, value));
}
}
for(ThreadLocal<Object> t : threadLocalsCache.values()) {
109,7 → 119,7
String keyString = InjectionUtils.getKeyName(key);
if (!poolsByName.containsKey(keyString)) return null;
if (!factoriesByName.containsKey(keyString)) return null;
Scope scope = scopes.get(keyString);
125,13 → 135,18
if (value != null) {
Pool<?> factory = poolsByName.get(keyString);
Factory factory = factoriesByName.get(keyString);
cp = new ClearableHolder(factory, value);
if (factory instanceof Interceptor) {
Interceptor c = (Interceptor) factory;
cp = new ClearableHolder(c, value);
}
}
}
if (cp != null) cp.clear();
if (cp != null) cp.c.onCleared(cp.value);
return (T) value;
151,9 → 166,14
if (o != null) {
Pool<?> factory = poolsByName.get(keyString);
Factory factory = factoriesByName.get(keyString);
cp = new ClearableHolder(factory, o);
if (factory instanceof Interceptor) {
Interceptor c = (Interceptor) factory;
cp = new ClearableHolder(c, o);
}
t.remove();
162,7 → 182,7
}
}
if (cp != null) cp.clear();
if (cp != null) cp.c.onCleared(cp.value);
return (T) retVal;
181,9 → 201,9
String keyString = InjectionUtils.getKeyName(key);
 
if (!poolsByName.containsKey(keyString)) return null;
if (!factoriesByName.containsKey(keyString)) return null;
 
Pool<?> c = poolsByName.get(keyString);
Factory c = factoriesByName.get(keyString);
Scope scope = scopes.get(keyString);
215,6 → 235,8
target = c.getInstance();
checkInterceptable(c, target);
synchronized(this) {
 
singletonsCache.put(keyString, target);
267,6 → 289,8
target = c.getInstance();
checkInterceptable(c, target);
t.set(target);
}
282,6 → 306,8
 
target = c.getInstance();
checkInterceptable(c, target);
} else {
throw new UnsupportedOperationException("Don't know how to handle scope: " + scope);
331,39 → 357,22
}
}
private static <T> Pool<T> fromFactory(final Factory<T> creator) {
private final void checkInterceptable(Factory f, Object value) {
return new Pool<T>() {
 
@Override
public T getInstance() {
return creator.getInstance();
}
 
@Override
public Class<T> getType() {
return creator.getType();
}
 
@Override
public void releaseInstance(T e) {
// do nothing!
}
};
if (f instanceof Interceptor) {
Interceptor i = (Interceptor) f;
((Interceptor) f).onCreated(value);
}
}
@Override
public void ioc(Object key, Factory<?> factory, Scope scope) {
public Factory ioc(Object key, Factory factory, Scope scope) {
ioc(key, fromFactory(factory), scope);
}
@Override
public void ioc(Object key, Pool<?> factory, Scope scope) {
String keyString = InjectionUtils.getKeyName(key);
poolsByName.put(keyString, factory);
factoriesByName.put(keyString, factory);
singletonsCache.remove(keyString); // just in case we are overriding a previous singleton bean...
375,24 → 384,20
scopes.put(keyString, scope);
forConstructMethod.add(new ConstructorDependency(keyString, factory.getType()));
}
@Override
public void ioc(Object key, Factory<?> factory) {
ioc(key, fromFactory(factory));
return factory;
}
@Override
public void ioc(Object key, Pool<?> pool) {
public Factory ioc(Object key, Factory factory) {
ioc(key, pool, Scope.NONE);
return ioc(key, factory, Scope.NONE);
}
@Override
public <T> ConfigurableFactory<T> ioc(Object key, Class<T> klass) {
public ConfigurableFactory ioc(Object key, Class<? extends Object> klass) {
ConfigurableFactory<T> cc = new ClassFactory<T>(this, klass);
ConfigurableFactory cc = new ClassFactory(this, klass);
ioc(key, cc);
400,9 → 405,9
}
@Override
public <T> ConfigurableFactory<T> ioc(Object key, Class<T> klass, Scope scope) {
public ConfigurableFactory ioc(Object key, Class<? extends Object> klass, Scope scope) {
ConfigurableFactory<T> cc = new ClassFactory<T>(this, klass);
ConfigurableFactory cc = new ClassFactory(this, klass);
ioc(key, cc, scope);
462,11 → 467,11
}
@Override
public <T> T construct(Class<T> klass) {
public <T> T construct(Class<? extends Object> klass) {
ClassFactory<T> f = new ClassFactory<T>(this, klass, forConstructMethod);
ClassFactory f = new ClassFactory(this, klass, forConstructMethod);
return f.getInstance();
return (T) f.getInstance();
}
 
@Override
503,7 → 508,7
String key = InjectionUtils.getKeyName(obj);
if (!poolsByName.containsKey(key)) return false;
if (!factoriesByName.containsKey(key)) return false;
Scope scope = scopes.get(key);
531,16 → 536,16
private static class ClearableHolder {
 
private Pool<Object> c;
private Interceptor c;
private Object value;
public ClearableHolder(Pool<?> c, Object value) {
this.c = (Pool<Object>) c;
public ClearableHolder(Interceptor c, Object value) {
this.c = c;
this.value = value;
}
public void clear() {
c.releaseInstance(value);
c.onCleared(value);
}
}
/trunk/src/main/java/org/mentacontainer/impl/WrapperFactory.java
New file
0,0 → 1,30
package org.mentacontainer.impl;
 
import org.mentacontainer.Factory;
 
public class WrapperFactory implements Factory {
private final Object instance;
private final Class<? extends Object> type;
public WrapperFactory(Object instance) {
 
this.instance = instance;
this.type = instance.getClass();
}
@Override
public <T> T getInstance() {
return (T) instance;
}
@Override
public Class<? extends Object> getType() {
return type;
}
}
/trunk/src/main/java/org/mentacontainer/impl/ClassFactory.java
20,11 → 20,11
*
* @author sergio.oliveira.jr@gmail.com
*/
class ClassFactory<E> implements ConfigurableFactory<E> {
class ClassFactory implements ConfigurableFactory {
private final MentaContainer container;
private final Class<E> klass;
private final Class<? extends Object> klass;
private Map<String, Object> props = null;
40,12 → 40,12
private final Set<ConstructorDependency> constructorDependencies;
public ClassFactory(MentaContainer container, Class<E> klass) {
public ClassFactory(MentaContainer container, Class<? extends Object> klass) {
 
this(container, klass, null);
}
ClassFactory(MentaContainer container, Class<E> klass, Set<ConstructorDependency> constructorDependencies) {
ClassFactory(MentaContainer container, Class<? extends Object> klass, Set<ConstructorDependency> constructorDependencies) {
this.container = container;
57,7 → 57,7
 
@Override
public ConfigurableFactory<E> addPropertyValue(String name, Object value) {
public ConfigurableFactory addPropertyValue(String name, Object value) {
if (props == null) {
72,7 → 72,7
}
@Override
public ConfigurableFactory<E> useZeroArgumentConstructor() {
public ConfigurableFactory useZeroArgumentConstructor() {
this.useZeroArgumentsConstructor = true;
80,7 → 80,7
}
@Override
public ConfigurableFactory<E> addPropertyDependency(String property, Object key) {
public ConfigurableFactory addPropertyDependency(String property, Object key) {
String k = InjectionUtils.getKeyName(key);
88,20 → 88,20
}
@Override
public ConfigurableFactory<E> addPropertyDependency(String property) {
public ConfigurableFactory addPropertyDependency(String property) {
return addPropertyDependency(property, property);
}
@Override
public ConfigurableFactory<E> addConstructorDependency(Object key) {
public ConfigurableFactory addConstructorDependency(Object key) {
String k = InjectionUtils.getKeyName(key);
return addInitValue(new DependencyKey(k), container.getType(k));
}
private ConfigurableFactory<E> addInitValue(Object value, Class<? extends Object> type) {
private ConfigurableFactory addInitValue(Object value, Class<? extends Object> type) {
if (initValues == null) {
118,13 → 118,13
}
@Override
public ConfigurableFactory<E> addInitValue(Object value) {
public ConfigurableFactory addInitValue(Object value) {
return addInitValue(value, value.getClass());
}
@Override
public ConfigurableFactory<E> addInitPrimitive(Object value) {
public ConfigurableFactory addInitPrimitive(Object value) {
Class<? extends Object> primitive = getPrimitiveFrom(value);
290,7 → 290,7
}
@Override
public E getInstance() {
public <T> T getInstance() {
Object obj = null;
373,7 → 373,7
}
}
return (E) obj;
return (T) obj;
}
private static boolean betterIsAssignableFrom(Class<? extends Object> klass1, Class<? extends Object> klass2) {
499,7 → 499,7
}
@Override
public Class<E> getType() {
public Class<? extends Object> getType() {
return klass;
}
}
/trunk/src/main/java/org/mentacontainer/Container.java
38,7 → 38,7
* @param key The factory
* @return The type returned by this factory
*/
public Class<?> getType(Object key);
public Class<? extends Object> getType(Object key);
/**
* Configure a bean to be returned with the given implementation when {@link #get(String)} is called.
50,7 → 50,7
* @return The factory created as a ConfigurableFactory. (Fluent API)
* @see Scope
*/
public <T> ConfigurableFactory<T> ioc(Object key, Class<T> klass, Scope scope);
public ConfigurableFactory ioc(Object key, Class<? extends Object> klass, Scope scope);
/**
* Same as {@link #ioc(String, Class, Scope)} except that it assumes
61,7 → 61,7
* @return The factory created as a ConfigurableFactory. (Fluent API)
* @see Scope
*/
public <T> ConfigurableFactory<T> ioc(Object key, Class<T> klass);
public ConfigurableFactory ioc(Object key, Class<?extends Object> klass);
/**
* Set up a factory for the given key. The scope assumed is NONE.
71,19 → 71,9
* @return The factory passed as a parameter. (Fluent API)
* @see Factory
*/
public void ioc(Object key, Factory<?> factory);
public Factory ioc(Object key, Factory factory);
/**
* Set up a pool for the given key. The scope assumed is NONE.
*
* @param key The key representing the bean to return. The name of the bean in the container.
* @param pool The pool for the IoC.
* @return The factory passed as a parameter. (Fluent API)
* @see Pool
*/
public void ioc(Object key, Pool<?> pool);
/**
* Set up a factory for the given key in the given scope.
*
* @param key The key representing the bean to return. The name of the bean in the container.
93,21 → 83,9
* @see Factory
* @see Scope
*/
public void ioc(Object key, Factory<?> factory, Scope scope);
public Factory ioc(Object key, Factory factory, Scope scope);
/**
* Set up a pool for the given key in the given scope.
*
* @param key The key representing the bean to return. The name of the bean in the container.
* @param pool The pool for the IoC.
* @param scope The scope used by the factory.
* @return The factory passed as a parameter (Fluent API).
* @see Pool
* @see Scope
*/
public void ioc(Object key, Pool<?> pool, Scope scope);
/**
* Configure a bean dependency to be auto-wired by the container.
* It wires by constructor and by setter. By constructor is uses the type of sourceFromContainer. By setter it assumes the property is also named sourceFromContainer.
*
140,7 → 118,7
* @param klass The class that should be instantiated.
* @return An instantiated bean.
*/
public <T> T construct(Class<T> klass);
public <T> T construct(Class<? extends Object> klass);
/**
* Check whether the container currently has a value for this key. For example,
/trunk/src/main/java/org/mentacontainer/Factory.java
1,9 → 1,24
package org.mentacontainer;
 
public interface Factory<E> {
/**
* An IoC factory that knows how to create instances.
*
* @author sergio.oliveira.jr@gmail.com
*/
public interface Factory {
public E getInstance();
/**
* Returns an instance. Creates one if necessary.
*
* @return an instance
*/
public <T> T getInstance();
public Class<E> getType();
}
/**
* Return the type of objects that this factory disposes.
*
* @return the type of objects returned by this factory.
*/
public Class<? extends Object> getType();
}
/trunk/src/main/java/org/mentacontainer/ConfigurableFactory.java
12,7 → 12,7
* @author sergio.oliveira.jr@gmail.com
* @see Interceptor
*/
public interface ConfigurableFactory<E> extends Factory<E> {
public interface ConfigurableFactory extends Factory {
/**
* Add a constructor parameter to be used when the bean is instantiated. It can be called more than once to
21,7 → 21,7
* @param value A parameter value to be used by a constructor.
* @return The factory itself. (Fluent API)
*/
public ConfigurableFactory<E> addInitValue(Object value);
public ConfigurableFactory addInitValue(Object value);
/**
* Add a constructor parameter that is a primitive to be used when the bean is instantiated. It can be called more than once to
35,7 → 35,7
* @param value A parameter value to be used by a constructor. Must be a primitive that will be autoboxed.
* @return The factory itself. (Fluent API)
*/
public ConfigurableFactory<E> addInitPrimitive(Object value);
public ConfigurableFactory addInitPrimitive(Object value);
/**
45,7 → 45,7
* @param key The key used to get an instance from the container
* @return The factory itself. (Fluent API)
*/
public ConfigurableFactory<E> addConstructorDependency(Object key);
public ConfigurableFactory addConstructorDependency(Object key);
/**
* In case you want to force the use of a zero argument constructor and avoid any ambiguity when choosing the constructor to use.
55,7 → 55,7
*
* @return The factory itself. (Fluent API)
*/
public ConfigurableFactory<E> useZeroArgumentConstructor();
public ConfigurableFactory useZeroArgumentConstructor();
/**
* Add a property to be injected through a setter when the factory instantiates an object.
64,7 → 64,7
* @param value The property value.
* @return The factory itself. (Fluent API)
*/
public ConfigurableFactory<E> addPropertyValue(String name, Object value);
public ConfigurableFactory addPropertyValue(String name, Object value);
/**
* Add a setter property that is a dependency, in other words, its value will be obtained from the container.
74,7 → 74,7
* @param property The dependency name which is equal to the property name.
* @return The factory itself. (Fluent API)
*/
public ConfigurableFactory<E> addPropertyDependency(String property);
public ConfigurableFactory addPropertyDependency(String property);
/**
* Add a setter property that is a dependency, in other words, its value will be obtained from the container.
85,5 → 85,5
* @param key The dependency name, in other words, the key used to get a bean from the container.
* @return The factory itself. (Fluent API)
*/
public ConfigurableFactory<E> addPropertyDependency(String property, Object key);
public ConfigurableFactory addPropertyDependency(String property, Object key);
}
/trunk/src/main/java/org/mentacontainer/util/InjectionUtils.java
267,8 → 267,7
 
try {
@SuppressWarnings("rawtypes")
Class k = (Class) targetType; // not sure how to avoid this raw type!
Class k = (Class) targetType; // not sure how to avoid this raw type!
 
newValue = Enum.valueOf(k, value);
 
/trunk/src/main/java/org/mentacontainer/Interceptor.java
New file
0,0 → 1,36
package org.mentacontainer;
 
/**
* Some factories can also implement this interface to perform some cleanup
* when the instance is created or cleared. For example, a connection pool will want
* to know when the connection instance is cleared so it can return it to
* the pool.
*
* It makes more sense to use this interface for factories that will be placed
* in the THREAD scope, but you can also use it with other scopes as well.
*
* This is particular useful for the THREAD scope for dealing with thread pools, so
* when the thread is returned to the thread pool you will want to clear the THREAD
* scope. That's pretty much how web containers work: one thread per request coming from
* a thread pool.
*
* @author sergio.oliveira.jr@gmail.com
*
* @param <E>
*/
public interface Interceptor<E> {
/**
* This method will be called right before the getInstance() method return a new instance created by the factory.
*
* @param createdObject The object that was just created.
*/
public void onCreated(E createdObject);
/**
* This method will be called right before the object is cleared from the scope.
*
* @param clearedObject The object being cleared.
*/
public void onCleared(E clearedObject);
}