MentaContainer

Compare Revisions

Ignore whitespace Rev 142 → Rev 143

/trunk/src/test/java/org/mentacontainer/impl/GenericFactoryTest.java
4,7 → 4,7
 
import org.junit.Test;
import org.mentacontainer.Container;
import org.mentacontainer.Creator;
import org.mentacontainer.Factory;
 
public class GenericFactoryTest {
25,7 → 25,7
Container c = new MentaContainer();
// giveMeSomething => method that will be called to return object
Creator<String> generic = new GenericFactory<String>(factory, "giveMeSomething");
Factory<String> generic = new GenericFactory<String>(factory, "giveMeSomething");
c.ioc("myFactory", generic);
/trunk/src/test/java/org/mentacontainer/impl/WrapperFactoryTest.java
4,7 → 4,7
 
import org.junit.Test;
import org.mentacontainer.Container;
import org.mentacontainer.Creator;
import org.mentacontainer.Factory;
 
public class WrapperFactoryTest {
13,7 → 13,7
String s = new String("saoj");
Creator<String> ic = new WrapperFactory<String>(s);
Factory<String> ic = new WrapperFactory<String>(s);
Container c = new MentaContainer();
/trunk/src/test/java/org/mentacontainer/impl/ClassFactoryTest.java
3,7 → 3,7
import junit.framework.Assert;
 
import org.junit.Test;
import org.mentacontainer.Creator;
import org.mentacontainer.Factory;
 
public class ClassFactoryTest {
31,13 → 31,13
MentaContainer container = new MentaContainer();
Creator<User> c1 = new ClassFactory<User>(container, User.class).addInitValue("saoj");
Factory<User> c1 = new ClassFactory<User>(container, User.class).addInitValue("saoj");
Creator<User> c2 = new ClassFactory<User>(container, User.class).addPropertyValue("username", "soliveira");
Factory<User> c2 = new ClassFactory<User>(container, User.class).addPropertyValue("username", "soliveira");
User u1 = c1.createInstance();
User u1 = c1.getInstance();
User u2 = c2.createInstance();
User u2 = c2.getInstance();
Assert.assertTrue(u1 != u2);
66,10 → 66,10
c.ioc("myString", String.class).addInitValue("hello");
Creator<TestObj1> c1 = new ClassFactory<TestObj1>(c, TestObj1.class).addConstructorDependency("myString").addInitValue(20).addInitValue(30000L)
Factory<TestObj1> c1 = new ClassFactory<TestObj1>(c, TestObj1.class).addConstructorDependency("myString").addInitValue(20).addInitValue(30000L)
.addInitValue(new User("saoj"));
TestObj1 o = c1.createInstance();
TestObj1 o = c1.getInstance();
Assert.assertEquals("hello", o.s);
Assert.assertEquals(20, o.i);
95,9 → 95,9
c.ioc("myString", String.class).addInitValue("hello");
Creator<TestObj1_1> c1 = new ClassFactory<TestObj1_1>(c, TestObj1_1.class).addPropertyDependency("s", "myString").addPropertyValue("i", 30);
Factory<TestObj1_1> c1 = new ClassFactory<TestObj1_1>(c, TestObj1_1.class).addPropertyDependency("s", "myString").addPropertyValue("i", 30);
TestObj1_1 o = c1.createInstance();
TestObj1_1 o = c1.getInstance();
Assert.assertEquals("hello", o.s);
Assert.assertEquals(30, o.i);
119,9 → 119,9
MentaContainer container = new MentaContainer();
Creator<TestObj2> f = new ClassFactory<TestObj2>(container, TestObj2.class).addPropertyValue("integer", 20);
Factory<TestObj2> f = new ClassFactory<TestObj2>(container, TestObj2.class).addPropertyValue("integer", 20);
TestObj2 o = f.createInstance();
TestObj2 o = f.getInstance();
Assert.assertEquals(20, o.x);
}
131,9 → 131,9
MentaContainer container = new MentaContainer();
Creator<TestObj2> f = new ClassFactory<TestObj2>(container, TestObj2.class).addPropertyValue("integer", new Integer(20));
Factory<TestObj2> f = new ClassFactory<TestObj2>(container, TestObj2.class).addPropertyValue("integer", new Integer(20));
TestObj2 o = f.createInstance();
TestObj2 o = f.getInstance();
Assert.assertEquals(20, o.x);
}
154,9 → 154,9
MentaContainer container = new MentaContainer();
Creator<TestObj2_1> f = new ClassFactory<TestObj2_1>(container, TestObj2_1.class).addPropertyValue("integer", 20);
Factory<TestObj2_1> f = new ClassFactory<TestObj2_1>(container, TestObj2_1.class).addPropertyValue("integer", 20);
TestObj2_1 o = f.createInstance();
TestObj2_1 o = f.getInstance();
Assert.assertEquals(20, o.x);
}
166,9 → 166,9
MentaContainer container = new MentaContainer();
Creator<TestObj2_1>f = new ClassFactory<TestObj2_1>(container, TestObj2_1.class).addPropertyValue("integer", new Integer(20));
Factory<TestObj2_1>f = new ClassFactory<TestObj2_1>(container, TestObj2_1.class).addPropertyValue("integer", new Integer(20));
TestObj2_1 o = f.createInstance();
TestObj2_1 o = f.getInstance();
Assert.assertEquals(20, o.x);
}
188,9 → 188,9
MentaContainer container = new MentaContainer();
Creator<TestObj3> f = new ClassFactory<TestObj3>(container, TestObj3.class).addInitValue(20);
Factory<TestObj3> f = new ClassFactory<TestObj3>(container, TestObj3.class).addInitValue(20);
TestObj3 o = f.createInstance();
TestObj3 o = f.getInstance();
Assert.assertEquals(20, o.x);
}
209,9 → 209,9
MentaContainer container = new MentaContainer();
Creator<TestObj3_1> f = new ClassFactory<TestObj3_1>(container, TestObj3_1.class).addInitValue(20);
Factory<TestObj3_1> f = new ClassFactory<TestObj3_1>(container, TestObj3_1.class).addInitValue(20);
TestObj3_1 o = f.createInstance();
TestObj3_1 o = f.getInstance();
Assert.assertEquals(20, o.x);
}
232,9 → 232,9
MentaContainer container = new MentaContainer();
Creator<TestObj3_2> f = new ClassFactory<TestObj3_2>(container, TestObj3_2.class).addInitValue(20).addInitPrimitive(30L);
Factory<TestObj3_2> f = new ClassFactory<TestObj3_2>(container, TestObj3_2.class).addInitValue(20).addInitPrimitive(30L);
TestObj3_2 o = f.createInstance();
TestObj3_2 o = f.getInstance();
Assert.assertEquals(20, o.x);
Assert.assertEquals(30L, o.l);
260,9 → 260,9
MentaContainer container = new MentaContainer();
Creator<TestObj4> f = new ClassFactory<TestObj4>(container, TestObj4.class).addInitPrimitive(20).addInitValue("hello").addInitValue(20L).addInitPrimitive(20L);
Factory<TestObj4> f = new ClassFactory<TestObj4>(container, TestObj4.class).addInitPrimitive(20).addInitValue("hello").addInitValue(20L).addInitPrimitive(20L);
TestObj4 o = f.createInstance();
TestObj4 o = f.getInstance();
Assert.assertEquals(20, o.x);
Assert.assertEquals("hello", o.s);
280,9 → 280,9
MentaContainer container = new MentaContainer();
Creator<TestObj22> f = new ClassFactory<TestObj22>(container, TestObj22.class);
Factory<TestObj22> f = new ClassFactory<TestObj22>(container, TestObj22.class);
TestObj22 o = f.createInstance();
TestObj22 o = f.getInstance();
Assert.assertNotNull(o);
/trunk/src/main/java/org/mentacontainer/Creator.java
File deleted
/trunk/src/main/java/org/mentacontainer/impl/GenericFactory.java
2,10 → 2,10
 
import java.lang.reflect.Method;
 
import org.mentacontainer.Creator;
import org.mentacontainer.Factory;
import org.mentacontainer.util.FindMethod;
 
public class GenericFactory<E> implements Creator<E> {
public class GenericFactory<E> implements Factory<E> {
private final Object factory;
32,7 → 32,7
}
@Override
public E createInstance() {
public E getInstance() {
try {
/trunk/src/main/java/org/mentacontainer/impl/MentaContainer.java
11,7 → 11,7
 
import org.mentacontainer.ConfigurableFactory;
import org.mentacontainer.Container;
import org.mentacontainer.Creator;
import org.mentacontainer.Factory;
import org.mentacontainer.Pool;
import org.mentacontainer.Scope;
import org.mentacontainer.util.InjectionUtils;
24,7 → 24,7
*/
public class MentaContainer implements Container {
 
private Map<String, Pool<?>> factoriesByName = new Hashtable<String, Pool<?>>();
private Map<String, Pool<?>> poolsByName = new Hashtable<String, Pool<?>>();
private Map<String, Scope> scopes = new Hashtable<String, Scope>();
43,7 → 43,7
String k = InjectionUtils.getKeyName(key);
Creator<?> factory = factoriesByName.get(k);
Factory<?> factory = poolsByName.get(k);
if (factory == null) return null;
61,7 → 61,7
for(String key : singletonsCache.keySet()) {
Pool<?> pool = factoriesByName.get(key);
Pool<?> pool = poolsByName.get(key);
Object value = singletonsCache.get(key);
83,7 → 83,7
for(String key : threadLocalsCache.keySet()) {
Pool<?> factory = factoriesByName.get(key);
Pool<?> factory = poolsByName.get(key);
ThreadLocal<Object> t = threadLocalsCache.get(key);
109,7 → 109,7
String keyString = InjectionUtils.getKeyName(key);
if (!factoriesByName.containsKey(keyString)) return null;
if (!poolsByName.containsKey(keyString)) return null;
Scope scope = scopes.get(keyString);
125,7 → 125,7
if (value != null) {
Pool<?> factory = factoriesByName.get(keyString);
Pool<?> factory = poolsByName.get(keyString);
cp = new ClearableHolder(factory, value);
}
151,7 → 151,7
if (o != null) {
Pool<?> factory = factoriesByName.get(keyString);
Pool<?> factory = poolsByName.get(keyString);
cp = new ClearableHolder(factory, o);
181,9 → 181,9
String keyString = InjectionUtils.getKeyName(key);
 
if (!factoriesByName.containsKey(keyString)) return null;
if (!poolsByName.containsKey(keyString)) return null;
 
Creator<?> c = factoriesByName.get(keyString);
Factory<?> c = poolsByName.get(keyString);
Scope scope = scopes.get(keyString);
213,7 → 213,7
// getInstance needs to be in a non-synchronized block
target = c.createInstance();
target = c.getInstance();
synchronized(this) {
 
265,7 → 265,7
// getInstance needs to be in a non-synchronized block
target = c.createInstance();
target = c.getInstance();
t.set(target);
}
280,7 → 280,7
} else if (scope == Scope.NONE) {
 
target = c.createInstance();
target = c.getInstance();
} else {
331,13 → 331,13
}
}
private static <T> Pool<T> fromFactory(final Creator<T> creator) {
private static <T> Pool<T> fromFactory(final Factory<T> creator) {
return new Pool<T>() {
 
@Override
public T createInstance() {
return creator.createInstance();
public T getInstance() {
return creator.getInstance();
}
 
@Override
353,7 → 353,7
}
@Override
public <T> Creator<T> ioc(Object key, Creator<T> factory, Scope scope) {
public <T> Factory<T> ioc(Object key, Factory<T> factory, Scope scope) {
return ioc(key, fromFactory(factory), scope);
}
362,7 → 362,7
String keyString = InjectionUtils.getKeyName(key);
factoriesByName.put(keyString, factory);
poolsByName.put(keyString, factory);
singletonsCache.remove(keyString); // just in case we are overriding a previous singleton bean...
379,7 → 379,7
}
@Override
public <T> Creator<T> ioc(Object key, Creator<T> factory) {
public <T> Factory<T> ioc(Object key, Factory<T> factory) {
return ioc(key, fromFactory(factory));
}
467,7 → 467,7
ClassFactory<T> f = new ClassFactory<T>(this, klass, forConstructMethod);
return f.createInstance();
return f.getInstance();
}
 
@Override
504,7 → 504,7
String key = InjectionUtils.getKeyName(obj);
if (!factoriesByName.containsKey(key)) return false;
if (!poolsByName.containsKey(key)) return false;
Scope scope = scopes.get(key);
/trunk/src/main/java/org/mentacontainer/impl/WrapperFactory.java
1,8 → 1,8
package org.mentacontainer.impl;
 
import org.mentacontainer.Creator;
import org.mentacontainer.Factory;
 
public class WrapperFactory<E> implements Creator<E> {
public class WrapperFactory<E> implements Factory<E> {
private final E instance;
17,7 → 17,7
}
@Override
public E createInstance() {
public E getInstance() {
return instance;
}
/trunk/src/main/java/org/mentacontainer/impl/ClassFactory.java
290,7 → 290,7
}
@Override
public E createInstance() {
public E getInstance() {
Object obj = null;
/trunk/src/main/java/org/mentacontainer/Container.java
71,7 → 71,7
* @return The factory passed as a parameter. (Fluent API)
* @see Factory
*/
public <T> Creator<T> ioc(Object key, Creator<T> factory);
public <T> Factory<T> ioc(Object key, Factory<T> factory);
/**
* Set up a pool for the given key. The scope assumed is NONE.
93,7 → 93,7
* @see Factory
* @see Scope
*/
public <T> Creator<T> ioc(Object key, Creator<T> factory, Scope scope);
public <T> Factory<T> ioc(Object key, Factory<T> factory, Scope scope);
/**
* Set up a pool for the given key in the given scope.
/trunk/src/main/java/org/mentacontainer/Factory.java
New file
0,0 → 1,9
package org.mentacontainer;
 
public interface Factory<E> {
public E getInstance();
public Class<E> getType();
}
/trunk/src/main/java/org/mentacontainer/ConfigurableFactory.java
12,7 → 12,7
* @author sergio.oliveira.jr@gmail.com
* @see Interceptor
*/
public interface ConfigurableFactory<E> extends Creator<E> {
public interface ConfigurableFactory<E> extends Factory<E> {
/**
* Add a constructor parameter to be used when the bean is instantiated. It can be called more than once to
/trunk/src/main/java/org/mentacontainer/Pool.java
1,6 → 1,6
package org.mentacontainer;
 
public interface Pool<E> extends Creator<E> {
public interface Pool<E> extends Factory<E> {
public void releaseInstance(E e);