MentaContainer

Compare Revisions

Regard whitespace Rev 56 → Rev 57

/trunk/src/main/java/org/mentacontainer/impl/MentaContainer.java
10,6 → 10,7
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;
 
22,31 → 23,48
 
private Map<String, Component> beans = new HashMap<String, Component>();
private Set<String> singletons = new HashSet<String>();
private Map<String, Scope> scopes = new HashMap<String, Scope>();
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) {
 
if (!beans.containsKey(key)) return null;
 
Component c = beans.get(key);
boolean isSingleton = singletons.contains(key);
Scope scope = scopes.get(key);
 
Object target = null;
 
try {
 
if (isSingleton) {
if (scope == Scope.SINGLETON) {
 
if (singletonsCache.containsKey(key)) {
 
target = singletonsCache.get(key);
 
return (T) target;
return (T) target; // no need to wire again...
 
} else {
 
55,12 → 73,51
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) {
 
target = c.getInstance();
 
} else {
throw new UnsupportedOperationException("Don't know how to handle scope: " + scope);
}
 
if (target != null) {
 
for (Dependency d : dependencies) {
113,27 → 170,22
}
}
 
public Component ioc(String key, Component component, boolean isSingleton) {
public Component ioc(String key, Component component, Scope scope) {
beans.put(key, component);
singletonsCache.remove(key); // just in case we are overriding a previous singleton bean...
if (isSingleton) {
threadLocalsCache.remove(key); // just in case we are overriding a previous thread local...
singletons.add(key);
scopes.put(key, scope);
} else {
singletons.remove(key); // just in case...
}
 
return component;
}
public Component ioc(String key, Component component) {
return ioc(key, component, false);
return ioc(key, component, Scope.NONE);
}
public ConfigurableComponent ioc(String key, Class<? extends Object> klass) {
145,11 → 197,11
return cc;
}
public ConfigurableComponent ioc(String key, Class<? extends Object> klass, boolean singleton) {
public ConfigurableComponent ioc(String key, Class<? extends Object> klass, Scope scope) {
ConfigurableComponent cc = new MentaComponent(klass);
ioc(key, cc, singleton);
ioc(key, cc, scope);
return cc;
}