MentaContainer

Compare Revisions

Ignore whitespace Rev 50 → Rev 51

/trunk/src/test/java/org/mentacontainer/impl/MentaComponentTest.java
41,19 → 41,4
Assert.assertTrue(!u1.getUsername().equals(u2.getUsername()));
}
@Test
public void testSingleton() {
Component c1 = new MentaComponent(User.class, true).addInitValue("saoj");
User u1 = c1.getInstance();
User u2 = c1.getInstance();
Assert.assertTrue(u1 == u2);
Assert.assertTrue(u1.getUsername().equals(u2.getUsername()));
}
}
/trunk/src/main/java/org/mentacontainer/impl/GenericComponent.java
49,7 → 49,4
throw new UnsupportedOperationException();
}
public boolean isSingleton() { return false; }
}
/trunk/src/main/java/org/mentacontainer/impl/MentaComponent.java
28,24 → 28,11
private Map<String, Method> cache = null;
private final boolean singleton;
private Object singletonValue = null;
 
public MentaComponent(Class<? extends Object> klass, boolean singleton) {
public MentaComponent(Class<? extends Object> klass) {
this.klass = klass;
this.singleton = singleton;
}
public MentaComponent(Class<? extends Object> klass) {
this(klass, false);
}
public boolean isSingleton() { return singleton; }
public ConfigurableComponent addProperty(String name, Object value) {
if (props == null) {
196,8 → 183,6
public <T> T getInstance() {
if (singleton && singletonValue != null) return (T) singletonValue;
Object obj = null;
if (initValues != null && initValues.size() > 0) {
249,8 → 234,6
}
}
if (obj != null && singleton) singletonValue = obj;
return (T) obj;
}
}
/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;
}
/trunk/src/main/java/org/mentacontainer/Component.java
13,12 → 13,4
* @return The instantiated bean based on the container configuration.
*/
public <T> T getInstance();
/**
* Is this component a singleton component, in other words,
* it always returns the same instance?
*
* @return true if it is a singleton component
*/
public boolean isSingleton();
}
/trunk/src/main/java/org/mentacontainer/Container.java
60,6 → 60,17
public Component ioc(String key, Component component);
/**
* Set up IoC based on the component passed. Specify whether this will be a singleton component.
*
* @param key The key representing the bean to return. The name of the bean in the container.
* @param component The component for the IoC.
* @param isSingleton Should this component be treated as a singleton?
* @return The component passed as a parameter.
* @see Component
*/
public Component ioc(String key, Component component, boolean isSingleton);
/**
* Configure a bean dependency to be auto-wired by the container. In general you want the
* type of the dependency to be an interface, for loosely couple dependencies. It works like that:<br/><br/>
*