Search in sources :

Example 11 with IocObject

use of org.nutz.ioc.meta.IocObject in project nutz by nutzam.

the class MapLoader method load.

/**
     * {@link ObjectLoadException}
     */
public IocObject load(IocLoading loading, String name) throws ObjectLoadException {
    Map<String, Object> m = getMap(name);
    if (null == m)
        throw new ObjectLoadException("Object '" + name + "' without define!");
    if (log.isDebugEnabled())
        log.debug("Loading define for name=" + name);
    // If has parent
    Object p = m.get("parent");
    if (null != p) {
        checkParents(name);
        IocObject parent = load(loading, p.toString());
        // create new map without parent
        Map<String, Object> newMap = new HashMap<String, Object>();
        for (Entry<String, Object> en : m.entrySet()) {
            if ("parent".equals(en.getKey()))
                continue;
            newMap.put(en.getKey(), en.getValue());
        }
        // Create self IocObject
        IocObject self = loading.map2iobj(newMap);
        // Merge with parent
        return Iocs.mergeWith(self, parent);
    }
    return loading.map2iobj(m);
}
Also used : ObjectLoadException(org.nutz.ioc.ObjectLoadException) HashMap(java.util.HashMap) IocObject(org.nutz.ioc.meta.IocObject) IocObject(org.nutz.ioc.meta.IocObject)

Example 12 with IocObject

use of org.nutz.ioc.meta.IocObject in project nutz by nutzam.

the class Iocs method wrap.

public static IocObject wrap(Object obj) {
    IocObject iobj = new IocObject();
    //iobj.setType(obj.getClass());
    iobj.setFactory(Iocs.class.getName() + "#self");
    IocValue ival = new IocValue(null, new StaticValue(obj));
    iobj.addArg(ival);
    return iobj;
}
Also used : IocValue(org.nutz.ioc.meta.IocValue) IocObject(org.nutz.ioc.meta.IocObject) StaticValue(org.nutz.ioc.val.StaticValue)

Example 13 with IocObject

use of org.nutz.ioc.meta.IocObject in project nutz by nutzam.

the class SimpleAopMaker method load.

public IocObject load(IocLoading loading, String name) throws ObjectLoadException {
    IocObject iobj = Iocs.wrap(this);
    iobj.setType(getClass());
    IocEventSet events = new IocEventSet();
    events.setDepose("depose");
    events.setCreate("init");
    events.setFetch("fetch");
    iobj.setEvents(events);
    return iobj;
}
Also used : IocEventSet(org.nutz.ioc.meta.IocEventSet) IocObject(org.nutz.ioc.meta.IocObject)

Example 14 with IocObject

use of org.nutz.ioc.meta.IocObject in project nutz by nutzam.

the class NutIoc method getNamesByType.

public String[] getNamesByType(Class<?> klass, IocContext context) {
    List<String> names = new ArrayList<String>();
    for (String name : getNames()) {
        try {
            IocObject iobj = loader.load(createLoading(), name);
            if (iobj != null && iobj.getType() != null && klass.isAssignableFrom(iobj.getType()))
                names.add(name);
        } catch (Exception e) {
        }
    }
    IocContext cntx;
    if (null == context || context == this.context)
        cntx = this.context;
    else
        cntx = new ComboContext(context, this.context);
    for (String name : cntx.names()) {
        ObjectProxy op = cntx.fetch(name);
        if (op.getObj() != null && klass.isAssignableFrom(op.getObj().getClass()))
            names.add(name);
    }
    return new LinkedHashSet<String>(names).toArray(new String[names.size()]);
}
Also used : IocContext(org.nutz.ioc.IocContext) ArrayList(java.util.ArrayList) IocObject(org.nutz.ioc.meta.IocObject) ObjectProxy(org.nutz.ioc.ObjectProxy) IocException(org.nutz.ioc.IocException)

Example 15 with IocObject

use of org.nutz.ioc.meta.IocObject in project nutz by nutzam.

the class NutIoc method getByType.

public <K> K getByType(Class<K> klass, IocContext context) {
    String _name = null;
    IocContext cntx;
    if (null == context || context == this.context)
        cntx = this.context;
    else
        cntx = new ComboContext(context, this.context);
    for (String name : cntx.names()) {
        ObjectProxy op = cntx.fetch(name);
        if (op.getObj() != null && klass.isAssignableFrom(op.getObj().getClass())) {
            _name = name;
            break;
        }
    }
    if (_name != null)
        return get(klass, _name, context);
    for (String name : getNames()) {
        try {
            IocObject iobj = loader.load(createLoading(), name);
            if (iobj != null && iobj.getType() != null && klass.isAssignableFrom(iobj.getType()))
                _name = name;
        } catch (Exception e) {
            continue;
        }
        if (_name != null)
            return get(klass, name, context);
    }
    throw new IocException("class:" + klass.getName(), "none ioc bean match class=" + klass.getName());
}
Also used : IocContext(org.nutz.ioc.IocContext) IocObject(org.nutz.ioc.meta.IocObject) IocException(org.nutz.ioc.IocException) ObjectProxy(org.nutz.ioc.ObjectProxy) IocException(org.nutz.ioc.IocException)

Aggregations

IocObject (org.nutz.ioc.meta.IocObject)16 IocException (org.nutz.ioc.IocException)6 ArrayList (java.util.ArrayList)5 ObjectProxy (org.nutz.ioc.ObjectProxy)4 IocEventSet (org.nutz.ioc.meta.IocEventSet)4 IocField (org.nutz.ioc.meta.IocField)4 IocValue (org.nutz.ioc.meta.IocValue)4 IocContext (org.nutz.ioc.IocContext)3 IocLoader (org.nutz.ioc.IocLoader)3 ObjectLoadException (org.nutz.ioc.ObjectLoadException)3 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Test (org.junit.Test)2 ValueProxy (org.nutz.ioc.ValueProxy)2 Collection (java.util.Collection)1 Entry (java.util.Map.Entry)1 AsyncAopIocLoader (org.nutz.aop.interceptor.async.AsyncAopIocLoader)1 TransIocLoader (org.nutz.aop.interceptor.ioc.TransIocLoader)1