MentaContainer

Compare Revisions

Ignore whitespace Rev 50 → Rev 51

/trunk/src/main/java/org/mentacontainer/impl/MentaContainer.java
21,9 → 21,11
public class MentaContainer implements Container {
 
private Map<String, Component> beans = new HashMap<String, Component>();
private Set<String> singletons = new HashSet<String>();
private Map<String, Object> singletonsCache = new HashMap<String, Object>();
 
private Map<String, Object> singletons = new HashMap<String, Object>();
 
private Set<Dependency> dependencies = new HashSet<Dependency>();
 
public <T> T get(String key) {
31,16 → 33,18
if (!beans.containsKey(key)) return null;
 
Component c = beans.get(key);
boolean isSingleton = singletons.contains(key);
 
Object target = null;
 
try {
 
if (c.isSingleton()) {
if (isSingleton) {
 
if (singletons.containsKey(key)) {
if (singletonsCache.containsKey(key)) {
 
target = singletons.get(key);
target = singletonsCache.get(key);
 
return (T) target;
 
48,7 → 52,7
 
target = c.getInstance();
 
singletons.put(key, target);
singletonsCache.put(key, target);
}
 
} else {
109,16 → 113,29
}
}
 
public Component ioc(String key, Component component) {
public Component ioc(String key, Component component, boolean isSingleton) {
beans.put(key, component);
// clear the singleton cache in case of adding again...
singletons.remove(key);
singletonsCache.remove(key); // just in case we are overriding a previous singleton bean...
if (isSingleton) {
singletons.add(key);
} else {
singletons.remove(key); // just in case...
}
 
return component;
}
public Component ioc(String key, Component component) {
return ioc(key, component, false);
}
public ConfigurableComponent ioc(String key, Class<? extends Object> klass) {
ConfigurableComponent cc = new MentaComponent(klass);
130,9 → 147,9
public ConfigurableComponent ioc(String key, Class<? extends Object> klass, boolean singleton) {
ConfigurableComponent cc = new MentaComponent(klass, singleton);
ConfigurableComponent cc = new MentaComponent(klass);
ioc(key, cc);
ioc(key, cc, singleton);
return cc;
}