MentaContainer

Compare Revisions

Ignore whitespace Rev 140 → Rev 141

/trunk/src/main/java/org/mentacontainer/impl/MentaContainer.java
11,8 → 11,9
 
import org.mentacontainer.ConfigurableFactory;
import org.mentacontainer.Container;
import org.mentacontainer.Factory;
import org.mentacontainer.Creator;
import org.mentacontainer.Interceptor;
import org.mentacontainer.Pool;
import org.mentacontainer.Scope;
import org.mentacontainer.util.InjectionUtils;
import org.mentacontainer.util.InjectionUtils.Provider;
24,7 → 25,7
*/
public class MentaContainer implements Container {
 
private Map<String, Factory> factoriesByName = new Hashtable<String, Factory>();
private Map<String, Pool<?>> factoriesByName = new Hashtable<String, Pool<?>>();
private Map<String, Scope> scopes = new Hashtable<String, Scope>();
43,7 → 44,7
String k = InjectionUtils.getKeyName(key);
Factory factory = factoriesByName.get(k);
Creator<?> factory = factoriesByName.get(k);
if (factory == null) return null;
61,16 → 62,11
for(String key : singletonsCache.keySet()) {
Factory factory = factoriesByName.get(key);
Pool<?> pool = factoriesByName.get(key);
if (factory instanceof Interceptor) {
Interceptor c = (Interceptor) factory;
Object value = singletonsCache.get(key);
Object value = singletonsCache.get(key);
listToClear.add(new ClearableHolder(c, value));
}
listToClear.add(new ClearableHolder(pool, value));
}
singletonsCache.clear();
88,18 → 84,13
for(String key : threadLocalsCache.keySet()) {
Factory factory = factoriesByName.get(key);
Pool<?> factory = factoriesByName.get(key);
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));
}
ThreadLocal<Object> t = threadLocalsCache.get(key);
Object value = t.get();
if (value != null) listToClear.add(new ClearableHolder(factory, value));
}
for(ThreadLocal<Object> t : threadLocalsCache.values()) {
135,18 → 126,13
if (value != null) {
Factory factory = factoriesByName.get(keyString);
Pool<?> factory = factoriesByName.get(keyString);
if (factory instanceof Interceptor) {
Interceptor c = (Interceptor) factory;
cp = new ClearableHolder(c, value);
}
cp = new ClearableHolder(factory, value);
}
}
if (cp != null) cp.c.onCleared(cp.value);
if (cp != null) cp.clear();
return (T) value;
166,14 → 152,9
if (o != null) {
Factory factory = factoriesByName.get(keyString);
Pool<?> factory = factoriesByName.get(keyString);
if (factory instanceof Interceptor) {
Interceptor c = (Interceptor) factory;
cp = new ClearableHolder(c, o);
}
cp = new ClearableHolder(factory, o);
t.remove();
182,7 → 163,7
}
}
if (cp != null) cp.c.onCleared(cp.value);
if (cp != null) cp.clear();
return (T) retVal;
203,7 → 184,7
 
if (!factoriesByName.containsKey(keyString)) return null;
 
Factory c = factoriesByName.get(keyString);
Creator<?> c = factoriesByName.get(keyString);
Scope scope = scopes.get(keyString);
233,7 → 214,7
// getInstance needs to be in a non-synchronized block
target = c.getInstance();
target = c.createInstance();
checkInterceptable(c, target);
287,7 → 268,7
// getInstance needs to be in a non-synchronized block
target = c.getInstance();
target = c.createInstance();
checkInterceptable(c, target);
304,7 → 285,7
} else if (scope == Scope.NONE) {
 
target = c.getInstance();
target = c.createInstance();
checkInterceptable(c, target);
357,7 → 338,7
}
}
private final void checkInterceptable(Factory f, Object value) {
private final void checkInterceptable(Creator<?> f, Object value) {
if (f instanceof Interceptor) {
367,12 → 348,33
}
}
private static <T> Pool<T> fromFactory(final Creator<T> creator) {
return new Pool<T>() {
 
@Override
public T createInstance() {
return creator.createInstance();
}
 
@Override
public Class<T> getType() {
return creator.getType();
}
 
@Override
public void releaseInstance(T e) {
// do nothing!
}
};
}
@Override
public Factory ioc(Object key, Factory factory, Scope scope) {
public <T> Creator<T> ioc(Object key, Creator<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...
389,15 → 391,15
}
@Override
public Factory ioc(Object key, Factory factory) {
public Creator<?> ioc(Object key, Creator<?> factory) {
return ioc(key, factory, Scope.NONE);
}
@Override
public ConfigurableFactory ioc(Object key, Class<? extends Object> klass) {
public <T> ConfigurableFactory<T> ioc(Object key, Class<T> klass) {
ConfigurableFactory cc = new ClassFactory(this, klass);
ConfigurableFactory<T> cc = new ClassFactory<T>(this, klass);
ioc(key, cc);
405,9 → 407,9
}
@Override
public ConfigurableFactory ioc(Object key, Class<? extends Object> klass, Scope scope) {
public <T> ConfigurableFactory<T> ioc(Object key, Class<T> klass, Scope scope) {
ConfigurableFactory cc = new ClassFactory(this, klass);
ConfigurableFactory<T> cc = new ClassFactory<T>(this, klass);
ioc(key, cc, scope);
467,11 → 469,11
}
@Override
public <T> T construct(Class<? extends Object> klass) {
public <T> T construct(Class<T> klass) {
ClassFactory f = new ClassFactory(this, klass, forConstructMethod);
ClassFactory<T> f = new ClassFactory<T>(this, klass, forConstructMethod);
return (T) f.getInstance();
return f.createInstance();
}
 
@Override
536,16 → 538,16
private static class ClearableHolder {
 
private Interceptor c;
private Pool<Object> c;
private Object value;
public ClearableHolder(Interceptor c, Object value) {
this.c = c;
public ClearableHolder(Pool<?> c, Object value) {
this.c = (Pool<Object>) c;
this.value = value;
}
public void clear() {
c.onCleared(value);
c.releaseInstance(value);
}
}