MentaContainer

Compare Revisions

Regard whitespace Rev 19 → Rev 20

/trunk/src/main/java/org/mentacontainer/impl/MentaContainer.java
6,9 → 6,16
import java.util.Map;
import java.util.Set;
 
import org.mentacontainer.Component;
import org.mentacontainer.Container;
import org.mentacontainer.Dependency;
import org.mentacontainer.util.InjectionUtils;
 
/**
* The implementation of the IoC container.
*
* @author sergio.oliveira.jr@gmail.com
*/
public class MentaContainer implements Container {
 
private Map<String, Component> beans = new HashMap<String, Component>();
53,7 → 60,7
for (Dependency d : dependencies) {
 
// has dependency ?
Method m = d.getMethod(target.getClass());
Method m = d.check(target.getClass());
 
if (m != null) {
 
61,8 → 68,7
 
if (sourceKey.equals(key)) {
 
// cannot depend on itself...
// also avoid recursive StackOverflow...
// cannot depend on itself... also avoid recursive StackOverflow...
 
continue;
 
70,7 → 76,7
 
Object source = get(sourceKey);
 
boolean isAssignable = source != null && d.getDependencyClass().isAssignableFrom(source.getClass());
boolean isAssignable = source != null && d.getType().isAssignableFrom(source.getClass());
 
// check if we can find the dependency and if it is
// assignable to the target dependency
94,7 → 100,6
}
 
return target; // return target nicely with all the dependencies
// injected
 
} catch (Exception e) {
 
102,61 → 107,46
}
}
 
public Container ioc(String key, Class<? extends Object> klass, boolean singleton) {
public Component ioc(Component component) {
 
String key = component.getName();
 
if (beans.containsKey(key)) throw new RuntimeException("Container already set for the bean key: " + key);
 
Component c = new Component(klass, singleton);
beans.put(key, component);
 
beans.put(key, c);
 
return this;
return component;
}
 
public Container ioc(String key, Class<? extends Object> klass) {
public Component ioc(String key, Class<? extends Object> klass) {
 
return ioc(key, klass, false);
return ioc(new MentaComponent(key, klass));
}
 
public Container autowire(String property, Class<? extends Object> klass, String source) {
public Component ioc(String key, Class<? extends Object> klass, boolean singleton) {
 
Dependency d = new Dependency(property, klass, source);
return ioc(new MentaComponent(key, klass, singleton));
}
 
if (dependencies.contains(d)) throw new RuntimeException("Dependency is already set: " + property);
public Dependency autowire(Dependency d) {
if (!klass.isInterface()) throw new RuntimeException("Dependency must be an interface!");
if (dependencies.contains(d)) throw new RuntimeException("Dependency is already set: " + d.getTarget());
 
dependencies.add(d);
 
return this;
return d;
}
 
public Container autowire(String property, Class<? extends Object> klass) {
public Dependency autowire(String property, Class<? extends Object> klass) {
return autowire(property, klass, property);
}
 
public Container init(String key, Object value) {
public Dependency autowire(String property, Class<? extends Object> klass, String source) {
 
if (!beans.containsKey(key)) throw new RuntimeException("Container does not have this bean set: " + key);
 
Component c = beans.get(key);
 
c.addInitValue(value);
 
return this;
return autowire(new MentaDependency(property, klass, source));
}
 
public Container set(String key, String name, Object value) {
 
if (!beans.containsKey(key)) throw new RuntimeException("Container does not have this bean set: " + key);
 
Component c = beans.get(key);
 
c.addProperty(name, value);
 
return this;
}
 
public Container populate(Object bean) throws Exception {
 
InjectionUtils.getObject(bean, this, false, null, true, false, true);