MentaContainer

Compare Revisions

Ignore whitespace Rev 145 → Rev 146

/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);
}
}