MentaContainer

Compare Revisions

Ignore whitespace Rev 57 → Rev 56

/trunk/src/main/java/org/mentacontainer/impl/MentaContainer.java
10,7 → 10,6
import org.mentacontainer.ConfigurableComponent;
import org.mentacontainer.Container;
import org.mentacontainer.Dependency;
import org.mentacontainer.Scope;
import org.mentacontainer.util.InjectionUtils;
import org.mentacontainer.util.InjectionUtils.Provider;
 
23,28 → 22,11
 
private Map<String, Component> beans = new HashMap<String, Component>();
private Map<String, Scope> scopes = new HashMap<String, Scope>();
private Set<String> singletons = new HashSet<String>();
private Map<String, Object> singletonsCache = new HashMap<String, Object>();
private Map<String, ThreadLocal<Object>> threadLocalsCache = new HashMap<String, ThreadLocal<Object>>();
 
private Set<Dependency> dependencies = new HashSet<Dependency>();
public void clear(Scope scope) {
if (scope == Scope.SINGLETON) {
singletonsCache.clear();
} else if (scope == Scope.THREAD) {
for(ThreadLocal<Object> t : threadLocalsCache.values()) {
t.set(null);
}
}
}
 
public <T> T get(String key) {
 
52,19 → 34,19
 
Component c = beans.get(key);
Scope scope = scopes.get(key);
boolean isSingleton = singletons.contains(key);
 
Object target = null;
 
try {
 
if (scope == Scope.SINGLETON) {
if (isSingleton) {
 
if (singletonsCache.containsKey(key)) {
 
target = singletonsCache.get(key);
 
return (T) target; // no need to wire again...
return (T) target;
 
} else {
 
72,50 → 54,11
 
singletonsCache.put(key, target);
}
} else if (scope == Scope.THREAD) {
if (threadLocalsCache.containsKey(key)) {
ThreadLocal<Object> t = threadLocalsCache.get(key);
target = t.get();
if (target == null) { // different thread...
target = c.getInstance();
t.set(target);
// don't return... let it be wired...
} else {
return (T) target; // no need to wire again...
}
} else {
ThreadLocal<Object> t = new ThreadLocal<Object>();
target = c.getInstance();
t.set(target);
threadLocalsCache.put(key, t);
// let it be wired...
}
} else if (scope == Scope.NONE) {
 
} else {
 
target = c.getInstance();
 
} else {
throw new UnsupportedOperationException("Don't know how to handle scope: " + scope);
}
 
if (target != null) {
170,22 → 113,27
}
}
 
public Component ioc(String key, Component component, Scope scope) {
public Component ioc(String key, Component component, boolean isSingleton) {
beans.put(key, component);
singletonsCache.remove(key); // just in case we are overriding a previous singleton bean...
threadLocalsCache.remove(key); // just in case we are overriding a previous thread local...
if (isSingleton) {
singletons.add(key);
} else {
scopes.put(key, scope);
singletons.remove(key); // just in case...
}
 
return component;
}
public Component ioc(String key, Component component) {
return ioc(key, component, Scope.NONE);
return ioc(key, component, false);
}
public ConfigurableComponent ioc(String key, Class<? extends Object> klass) {
197,11 → 145,11
return cc;
}
public ConfigurableComponent ioc(String key, Class<? extends Object> klass, Scope scope) {
public ConfigurableComponent ioc(String key, Class<? extends Object> klass, boolean singleton) {
ConfigurableComponent cc = new MentaComponent(klass);
ioc(key, cc, scope);
ioc(key, cc, singleton);
return cc;
}