MentaContainer

Compare Revisions

Ignore whitespace Rev 134 → Rev 135

/trunk/src/main/java/org/mentacontainer/impl/MentaContainer.java
39,9 → 39,9
private Set<ConstructorDependency> forConstructMethod = Collections.synchronizedSet(new HashSet<ConstructorDependency>());
@Override
public Class<? extends Object> getType(String key) {
public Class<? extends Object> getType(Object key) {
Factory factory = factoriesByName.get(key);
Factory factory = factoriesByName.get(key.toString());
if (factory == null) return null;
113,12 → 113,14
}
@Override
public <T> T clear(String key) {
public <T> T clear(Object key) {
if (!factoriesByName.containsKey(key)) return null;
String keyString = key.toString();
Scope scope = scopes.get(key);
if (!factoriesByName.containsKey(keyString)) return null;
Scope scope = scopes.get(keyString);
if (scope == Scope.SINGLETON) {
ClearableHolder cp = null;
127,11 → 129,11
synchronized(this) {
value = singletonsCache.remove(key);
value = singletonsCache.remove(keyString);
if (value != null) {
Factory factory = factoriesByName.get(key);
Factory factory = factoriesByName.get(keyString);
if (factory instanceof Interceptor) {
154,7 → 156,7
synchronized(this) {
ThreadLocal<Object> t = threadLocalsCache.get(key);
ThreadLocal<Object> t = threadLocalsCache.get(keyString);
if (t != null) {
162,7 → 164,7
if (o != null) {
Factory factory = factoriesByName.get(key);
Factory factory = factoriesByName.get(keyString);
if (factory instanceof Interceptor) {
193,13 → 195,15
}
 
@Override
public <T> T get(String key) {
public <T> T get(Object key) {
String keyString = key.toString();
 
if (!factoriesByName.containsKey(key)) return null;
if (!factoriesByName.containsKey(keyString)) return null;
 
Factory c = factoriesByName.get(key);
Factory c = factoriesByName.get(keyString);
Scope scope = scopes.get(key);
Scope scope = scopes.get(keyString);
Object target = null;
 
211,9 → 215,9
synchronized(this) {
 
if (singletonsCache.containsKey(key)) {
if (singletonsCache.containsKey(keyString)) {
target = singletonsCache.get(key);
target = singletonsCache.get(keyString);
return (T) target; // no need to wire again...
233,7 → 237,7
synchronized(this) {
 
singletonsCache.put(key, target);
singletonsCache.put(keyString, target);
}
}
247,9 → 251,9
synchronized(this) {
if (threadLocalsCache.containsKey(key)) {
if (threadLocalsCache.containsKey(keyString)) {
t = threadLocalsCache.get(key);
t = threadLocalsCache.get(keyString);
target = t.get();
292,7 → 296,7
synchronized(this) {
threadLocalsCache.put(key, t);
threadLocalsCache.put(keyString, t);
}
}
318,7 → 322,7
 
String sourceKey = d.getSource();
 
if (sourceKey.equals(key)) {
if (sourceKey.equals(keyString)) {
 
// cannot depend on itself... also avoid recursive StackOverflow...
 
362,32 → 366,34
}
@Override
public Factory ioc(String key, Factory factory, Scope scope) {
public Factory ioc(Object key, Factory factory, Scope scope) {
factoriesByName.put(key, factory);
String keyString = key.toString();
singletonsCache.remove(key); // just in case we are overriding a previous singleton bean...
factoriesByName.put(keyString, factory);
ThreadLocal<Object> threadLocal = threadLocalsCache.remove(key); // just in case we are overriding a previous thread local...
singletonsCache.remove(keyString); // just in case we are overriding a previous singleton bean...
ThreadLocal<Object> threadLocal = threadLocalsCache.remove(keyString); // just in case we are overriding a previous thread local...
if (threadLocal != null) {
threadLocal.remove();
}
scopes.put(key, scope);
scopes.put(keyString, scope);
forConstructMethod.add(new ConstructorDependency(key, factory.getType()));
forConstructMethod.add(new ConstructorDependency(keyString, factory.getType()));
return factory;
}
@Override
public Factory ioc(String key, Factory factory) {
public Factory ioc(Object key, Factory factory) {
return ioc(key, factory, Scope.NONE);
}
@Override
public ConfigurableFactory ioc(String key, Class<? extends Object> klass) {
public ConfigurableFactory ioc(Object key, Class<? extends Object> klass) {
ConfigurableFactory cc = new ClassFactory(this, klass);
397,7 → 403,7
}
@Override
public ConfigurableFactory ioc(String key, Class<? extends Object> klass, Scope scope) {
public ConfigurableFactory ioc(Object key, Class<? extends Object> klass, Scope scope) {
ConfigurableFactory cc = new ClassFactory(this, klass);
407,23 → 413,27
}
@Override
public void autowire(String sourceFromContainer) {
public void autowire(Object sourceFromContainer) {
// autowire by constructor and setter...
autowireBySetter(sourceFromContainer);
String s = sourceFromContainer.toString();
autowireByConstructor(sourceFromContainer);
autowireBySetter(s);
autowireByConstructor(s);
}
@Override
public void autowire(String sourceFromContainer, String beanProperty) {
public void autowire(Object sourceFromContainer, String beanProperty) {
// autowire by constructor and setter...
autowireBySetter(beanProperty, sourceFromContainer);
String s = sourceFromContainer.toString();
autowireByConstructor(sourceFromContainer);
autowireBySetter(beanProperty, s);
autowireByConstructor(s);
}
 
private void autowireBySetter(String targetProperty, String sourceFromContainer) {
492,23 → 502,25
}
 
@Override
public synchronized boolean check(String key) {
public synchronized boolean check(Object key) {
if (!factoriesByName.containsKey(key)) return false;
String keyString = key.toString();
Scope scope = scopes.get(key);
if (!factoriesByName.containsKey(keyString)) return false;
Scope scope = scopes.get(keyString);
if (scope == Scope.NONE) {
return false; // always...
} else if (scope == Scope.SINGLETON) {
return singletonsCache.containsKey(key);
return singletonsCache.containsKey(keyString);
} else if (scope == Scope.THREAD) {
ThreadLocal<Object> t = threadLocalsCache.get(key);
ThreadLocal<Object> t = threadLocalsCache.get(keyString);
if (t != null) return t.get() != null;