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;
/trunk/src/main/java/org/mentacontainer/Container.java
30,7 → 30,7
* @param key The key representing the factory to use. The name of the bean in the container.
* @return The fully initialized and wired bean.
*/
public <T> T get(String key);
public <T> T get(Object key);
/**
* Get the type of the instances returned by the associated factory.
38,7 → 38,7
* @param key The factory
* @return The type returned by this factory
*/
public Class<? extends Object> getType(String key);
public Class<? extends Object> getType(Object key);
/**
* Configure a bean to be returned with the given implementation when {@link #get(String)} is called.
50,7 → 50,7
* @return The factory created as a ConfigurableFactory. (Fluent API)
* @see Scope
*/
public ConfigurableFactory ioc(String key, Class<? extends Object> klass, Scope scope);
public ConfigurableFactory ioc(Object key, Class<? extends Object> klass, Scope scope);
/**
* Same as {@link #ioc(String, Class, Scope)} except that it assumes
61,7 → 61,7
* @return The factory created as a ConfigurableFactory. (Fluent API)
* @see Scope
*/
public ConfigurableFactory ioc(String key, Class<?extends Object> klass);
public ConfigurableFactory ioc(Object key, Class<?extends Object> klass);
/**
* Set up a factory for the given key. The scope assumed is NONE.
71,7 → 71,7
* @return The factory passed as a parameter. (Fluent API)
* @see Factory
*/
public Factory ioc(String key, Factory factory);
public Factory ioc(Object key, Factory factory);
/**
* Set up a factory for the given key in the given scope.
83,7 → 83,7
* @see Factory
* @see Scope
*/
public Factory ioc(String key, Factory factory, Scope scope);
public Factory ioc(Object key, Factory factory, Scope scope);
/**
* Configure a bean dependency to be auto-wired by the container.
91,7 → 91,7
*
* @param sourceFromContainer The bean inside the container that will be wired automatically inside any other bean the depends on it.
*/
public void autowire(String sourceFromContainer);
public void autowire(Object sourceFromContainer);
/**
* Configure a bean dependency to be auto-wired by the container.
100,7 → 100,7
* @param sourceFromContainer The bean inside the container that will be wired automatically inside any other bean the depends on it.
* @param property The name of the property to inject, whey trying auto-wiring by setter.
*/
public void autowire(String sourceFromContainer, String property);
public void autowire(Object sourceFromContainer, String property);
/**
* Take a given bean and populate its properties with other beans coming from this container.
128,7 → 128,7
* @param key The key representing the bean inside the container.
* @return true if the container has an instance cached in the scope for this key
*/
public boolean check(String key);
public boolean check(Object key);
/**
* Clear all cached instances for that scope. If you have a thread-pool for example you will
152,5 → 152,5
* @param key The key representing the bean inside the container.
* @return The value that was cached and it is not anymore (was cleared) or null if nothing was cleared
*/
public <T> T clear(String key);
public <T> T clear(Object key);
}