MentaContainer

Compare Revisions

Ignore whitespace Rev 142 → Rev 141

/trunk/src/test/java/org/mentacontainer/impl/MentaContainerTest.java
4,6 → 4,8
 
import org.junit.Test;
import org.mentacontainer.Container;
import org.mentacontainer.Creator;
import org.mentacontainer.Interceptor;
import org.mentacontainer.Scope;
 
public class MentaContainerTest {
/trunk/src/test/java/org/mentacontainer/impl/ClassFactoryTest.java
4,6 → 4,7
 
import org.junit.Test;
import org.mentacontainer.Creator;
import org.mentacontainer.Factory;
 
public class ClassFactoryTest {
/trunk/src/main/java/org/mentacontainer/impl/GenericFactory.java
3,9 → 3,10
import java.lang.reflect.Method;
 
import org.mentacontainer.Creator;
import org.mentacontainer.Interceptor;
import org.mentacontainer.util.FindMethod;
 
public class GenericFactory<E> implements Creator<E> {
public class GenericFactory<E> implements Creator<E>, Interceptor<E> {
private final Object factory;
13,6 → 14,8
private final Class<E> type;
private Interceptor<E> interceptor = null;
public GenericFactory(Object factory, String methodName) {
this.factory = factory;
31,7 → 34,26
}
}
public void setInterceptor(Interceptor<E> interceptor) {
this.interceptor = interceptor;
}
@Override
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 E createInstance() {
try {
/trunk/src/main/java/org/mentacontainer/impl/MentaContainer.java
12,6 → 12,7
import org.mentacontainer.ConfigurableFactory;
import org.mentacontainer.Container;
import org.mentacontainer.Creator;
import org.mentacontainer.Interceptor;
import org.mentacontainer.Pool;
import org.mentacontainer.Scope;
import org.mentacontainer.util.InjectionUtils;
39,7 → 40,7
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);
215,6 → 216,8
target = c.createInstance();
checkInterceptable(c, target);
synchronized(this) {
 
singletonsCache.put(keyString, target);
267,6 → 270,8
target = c.createInstance();
checkInterceptable(c, target);
t.set(target);
}
282,6 → 287,8
 
target = c.createInstance();
checkInterceptable(c, target);
} else {
throw new UnsupportedOperationException("Don't know how to handle scope: " + scope);
331,6 → 338,16
}
}
private final void checkInterceptable(Creator<?> f, Object value) {
if (f instanceof Interceptor) {
Interceptor i = (Interceptor) f;
((Interceptor) f).onCreated(value);
}
}
private static <T> Pool<T> fromFactory(final Creator<T> creator) {
return new Pool<T>() {
354,15 → 371,10
@Override
public <T> Creator<T> ioc(Object key, Creator<T> factory, Scope scope) {
return ioc(key, fromFactory(factory), scope);
}
@Override
public <T> Pool<T> ioc(Object key, Pool<T> factory, Scope scope) {
String keyString = InjectionUtils.getKeyName(key);
factoriesByName.put(keyString, factory);
factoriesByName.put(keyString, fromFactory(factory));
singletonsCache.remove(keyString); // just in case we are overriding a previous singleton bean...
379,18 → 391,12
}
@Override
public <T> Creator<T> ioc(Object key, Creator<T> factory) {
public Creator<?> ioc(Object key, Creator<?> factory) {
return ioc(key, fromFactory(factory));
return ioc(key, factory, Scope.NONE);
}
@Override
public <T> Pool<T> ioc(Object key, Pool<T> pool) {
return ioc(key, pool, Scope.NONE);
}
@Override
public <T> ConfigurableFactory<T> ioc(Object key, Class<T> klass) {
ConfigurableFactory<T> cc = new ClassFactory<T>(this, klass);
/trunk/src/main/java/org/mentacontainer/Container.java
71,19 → 71,9
* @return The factory passed as a parameter. (Fluent API)
* @see Factory
*/
public <T> Creator<T> ioc(Object key, Creator<T> factory);
public Creator<?> ioc(Object key, Creator<?> 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 <T> Pool<T> ioc(Object key, Pool<T> 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.
96,18 → 86,6
public <T> Creator<T> ioc(Object key, Creator<T> 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 <T> Pool<T> ioc(Object key, Pool<T> 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.
*
/trunk/src/main/java/org/mentacontainer/Factory.java
New file
0,0 → 1,24
package org.mentacontainer;
 
/**
* An IoC factory that knows how to create instances.
*
* @author sergio.oliveira.jr@gmail.com
*/
public interface Factory {
/**
* Returns an instance. Creates one if necessary.
*
* @return an instance
*/
public <T> T getInstance();
/**
* 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/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);
}