MentaContainer

Compare Revisions

Ignore whitespace Rev 105 → Rev 106

/trunk/src/test/java/org/mentacontainer/impl/MentaContainerTest.java
3,7 → 3,7
import junit.framework.Assert;
 
import org.junit.Test;
import org.mentacontainer.Interceptable;
import org.mentacontainer.Interceptor;
import org.mentacontainer.Factory;
import org.mentacontainer.Container;
import org.mentacontainer.Scope;
400,14 → 400,19
private static class SomeObject {
private boolean cleared = false;
private boolean destroyed = false;
private boolean created = false;
public void clear() { this.cleared = true; }
public void destroyed() { this.destroyed = true; }
public boolean isCleared() { return cleared; }
public boolean isDestroyed() { return destroyed; }
public void created() { this.created = true; }
public boolean isCreated() { return created; }
}
private static class SomeFactory implements Factory, Interceptable<SomeObject> {
private static class SomeFactory implements Factory, Interceptor<SomeObject> {
@Override
public <T> T getInstance() {
422,20 → 427,20
}
@Override
public void onCleared(SomeObject obj) {
public void onDestroyed(SomeObject obj) {
obj.clear();
obj.destroyed();
}
@Override
public void onCreated(SomeObject obj) {
obj.created();
}
}
@Test
public void testClearable() {
public void testInterceptor() {
Container c = new MentaContainer();
443,26 → 448,32
SomeObject o = c.get("o");
Assert.assertTrue(o.isCreated());
c.clear(Scope.SINGLETON);
Assert.assertEquals(true, o.isCleared());
Assert.assertEquals(true, o.isDestroyed());
c.ioc("o", new SomeFactory(), Scope.THREAD);
o = c.get("o");
Assert.assertTrue(o.isCreated());
c.clear(Scope.SINGLETON);
Assert.assertEquals(false, o.isCleared());
Assert.assertEquals(false, o.isDestroyed());
c.clear(Scope.THREAD);
Assert.assertEquals(true, o.isCleared());
Assert.assertEquals(true, o.isDestroyed());
c.ioc("o", new SomeFactory(), Scope.NONE);
o = c.get("o");
Assert.assertTrue(o.isCreated());
o = c.clear("o");
Assert.assertNull(o);
475,9 → 486,11
o = c.get("o");
Assert.assertTrue(o.isCreated());
o = c.clear("o");
Assert.assertEquals(true, o.isCleared());
Assert.assertEquals(true, o.isDestroyed());
}
}
/trunk/src/main/java/org/mentacontainer/Interceptable.java
File deleted
\ No newline at end of file
/trunk/src/main/java/org/mentacontainer/impl/GenericFactory.java
3,9 → 3,10
import java.lang.reflect.Method;
 
import org.mentacontainer.Factory;
import org.mentacontainer.Interceptor;
import org.mentacontainer.util.FindMethod;
 
public class GenericFactory implements Factory {
public class GenericFactory<E> implements Factory, Interceptor<E> {
private final Object factory;
13,6 → 14,8
private final Class<? extends Object> type;
private Interceptor<E> interceptor = null;
public GenericFactory(Object factory, String methodName) {
this.factory = factory;
31,7 → 34,26
}
}
public void setInterceptor(Interceptor<E> interceptor) {
this.interceptor = interceptor;
}
@Override
public void onCreated(E createdObject) {
if (interceptor != null) {
interceptor.onCreated(createdObject);
}
}
@Override
public void onDestroyed(E destroyedObject) {
if (interceptor != null) {
interceptor.onDestroyed(destroyedObject);
}
}
@Override
public <T> T getInstance() {
try {
/trunk/src/main/java/org/mentacontainer/impl/MentaContainer.java
12,7 → 12,7
import org.mentacontainer.ConfigurableFactory;
import org.mentacontainer.Container;
import org.mentacontainer.Factory;
import org.mentacontainer.Interceptable;
import org.mentacontainer.Interceptor;
import org.mentacontainer.Scope;
import org.mentacontainer.util.InjectionUtils;
import org.mentacontainer.util.InjectionUtils.Provider;
59,11 → 59,11
for(String key : singletonsCache.keySet()) {
Factory comp = factoriesByName.get(key);
Factory factory = factoriesByName.get(key);
if (comp instanceof Interceptable) {
if (factory instanceof Interceptor) {
Interceptable<Object> c = (Interceptable<Object>) comp;
Interceptor c = (Interceptor) factory;
Object value = singletonsCache.get(key);
86,11 → 86,11
for(String key : threadLocalsCache.keySet()) {
Factory comp = factoriesByName.get(key);
Factory factory = factoriesByName.get(key);
if (comp instanceof Interceptable) {
if (factory instanceof Interceptor) {
Interceptable<Object> c = (Interceptable<Object>) comp;
Interceptor c = (Interceptor) factory;
ThreadLocal<Object> t = threadLocalsCache.get(key);
131,18 → 131,18
if (value != null) {
Factory comp = factoriesByName.get(key);
Factory factory = factoriesByName.get(key);
if (comp instanceof Interceptable) {
if (factory instanceof Interceptor) {
Interceptable<Object> c = (Interceptable<Object>) comp;
Interceptor c = (Interceptor) factory;
cp = new ClearableHolder(c, value);
}
}
}
if (cp != null) cp.c.onCleared(cp.value);
if (cp != null) cp.c.onDestroyed(cp.value);
return (T) value;
162,11 → 162,11
if (o != null) {
Factory comp = factoriesByName.get(key);
Factory factory = factoriesByName.get(key);
if (comp instanceof Interceptable) {
if (factory instanceof Interceptor) {
Interceptable<Object> c = (Interceptable<Object>) comp;
Interceptor c = (Interceptor) factory;
cp = new ClearableHolder(c, o);
}
178,7 → 178,7
}
}
if (cp != null) cp.c.onCleared(cp.value);
if (cp != null) cp.c.onDestroyed(cp.value);
return (T) retVal;
353,11 → 353,11
private final void checkInterceptable(Factory f, Object value) {
if (f instanceof Interceptable) {
if (f instanceof Interceptor) {
Interceptable i = (Interceptable) f;
Interceptor i = (Interceptor) f;
((Interceptable) f).onCreated(value);
((Interceptor) f).onCreated(value);
}
}
517,16 → 517,16
private static class ClearableHolder {
 
private Interceptable<Object> c;
private Interceptor c;
private Object value;
public ClearableHolder(Interceptable<Object> c, Object value) {
public ClearableHolder(Interceptor c, Object value) {
this.c = c;
this.value = value;
}
public void clear() {
c.onCleared(value);
c.onDestroyed(value);
}
}
/trunk/src/main/java/org/mentacontainer/Interceptor.java
New file
0,0 → 1,27
package org.mentacontainer;
 
/**
* Some factories can also implement this interface to perform some cleanup
* when the instance is cleared. For example, a connection pool will want
* to know when the connection instance is cleared so it can return it to
* the pool.
*
* It makes more sense to use this interface for factories that will be placed
* in the THREAD scope, but you can also use it with factories in the SINGLETON
* scope.
*
* This is particular useful for the THREAD scope for dealing with thread pools, so
* when the thread is returned to the thread pool you will want to clear the THREAD
* scope. That's pretty much how web containers work: one thread per request coming from
* a thread pool.
*
* @author sergio.oliveira.jr@gmail.com
*
* @param <E>
*/
public interface Interceptor<E> {
public void onCreated(E createdObject);
public void onDestroyed(E destroyedObject);
}