MentaContainer

Compare Revisions

Ignore whitespace Rev 63 → Rev 64

/trunk/src/test/java/org/mentacontainer/impl/MentaContainerTest.java
3,6 → 3,8
import junit.framework.Assert;
 
import org.junit.Test;
import org.mentacontainer.Clearable;
import org.mentacontainer.Component;
import org.mentacontainer.Container;
import org.mentacontainer.Scope;
 
282,6 → 284,7
public MyDAO getMyDAO() { return myDAO; }
}
@Test
public void testPopulate() {
Container c = getConfiguredContainer();
303,6 → 306,74
// check if conn was also wired...
Assert.assertNotNull(conn.getMyDep());
}
private static class SomeObject {
private boolean cleared = false;
public void clear() { this.cleared = true; }
public boolean isCleared() { return cleared; }
}
private static class SomeComponent implements Component, Clearable<SomeObject> {
public <T> T getInstance() {
return (T) new SomeObject();
}
public void onCleared(SomeObject obj) {
obj.clear();
}
}
@Test
public void testClearable() {
Container c = new MentaContainer();
c.ioc("o", new SomeComponent(), Scope.SINGLETON);
SomeObject o = c.get("o");
c.clear(Scope.SINGLETON);
Assert.assertEquals(true, o.isCleared());
c.ioc("o", new SomeComponent(), Scope.THREAD);
o = c.get("o");
c.clear(Scope.SINGLETON);
Assert.assertEquals(false, o.isCleared());
c.clear(Scope.THREAD);
Assert.assertEquals(true, o.isCleared());
c.ioc("o", new SomeComponent(), Scope.NONE);
o = c.get("o");
o = c.clear("o");
Assert.assertNull(o);
c.ioc("o", new SomeComponent(), Scope.THREAD);
o = c.clear("o");
Assert.assertNull(o);
o = c.get("o");
o = c.clear("o");
Assert.assertEquals(true, o.isCleared());
}
}
/trunk/src/main/java/org/mentacontainer/impl/MentaContainer.java
6,6 → 6,7
import java.util.Map;
import java.util.Set;
 
import org.mentacontainer.Clearable;
import org.mentacontainer.Component;
import org.mentacontainer.ConfigurableComponent;
import org.mentacontainer.Container;
35,10 → 36,40
if (scope == Scope.SINGLETON) {
for(String key : singletonsCache.keySet()) {
Component comp = beans.get(key);
if (comp instanceof Clearable) {
Clearable<Object> c = (Clearable<Object>) comp;
Object value = singletonsCache.get(key);
c.onCleared(value);
}
}
singletonsCache.clear();
} else if (scope == Scope.THREAD) {
for(String key : threadLocalsCache.keySet()) {
Component comp = beans.get(key);
if (comp instanceof Clearable) {
Clearable<Object> c = (Clearable<Object>) comp;
ThreadLocal<Object> t = threadLocalsCache.get(key);
Object value = t.get();
if (value != null) c.onCleared(value);
}
}
for(ThreadLocal<Object> t : threadLocalsCache.values()) {
t.set(null);
54,8 → 85,22
if (scope == Scope.SINGLETON) {
return (T) singletonsCache.remove(key);
Object value = singletonsCache.remove(key);
if (value != null) {
Component comp = beans.get(key);
if (comp instanceof Clearable) {
Clearable<Object> c = (Clearable<Object>) comp;
c.onCleared(value);
}
}
return (T) value;
} else if (scope == Scope.THREAD) {
ThreadLocal<Object> t = threadLocalsCache.get(key);
66,6 → 111,15
if (o != null) {
Component comp = beans.get(key);
if (comp instanceof Clearable) {
Clearable<Object> c = (Clearable<Object>) comp;
c.onCleared(o);
}
t.set(null);
return (T) o;
/trunk/src/main/java/org/mentacontainer/Clearable.java
New file
0,0 → 1,6
package org.mentacontainer;
 
public interface Clearable<E> {
public void onCleared(E clearedObject);
}