Search in sources :

Example 6 with ObjectProxy

use of org.nutz.ioc.ObjectProxy in project nutz by nutzam.

the class ScopeContext method clear.

public void clear() {
    checkBuffer();
    List<Entry<String, ObjectProxy>> list = new ArrayList<Entry<String, ObjectProxy>>(objs.entrySet());
    Collections.reverse(list);
    List<Entry<String, ObjectProxy>> tmp = new ArrayList<Entry<String, ObjectProxy>>();
    for (Entry<String, ObjectProxy> en : list) {
        try {
            ObjectProxy op = en.getValue();
            Object obj = op.getObj();
            if (obj != null && obj instanceof DataSource) {
                tmp.add(en);
                continue;
            }
        } catch (Throwable e) {
        }
        if (log.isDebugEnabled())
            log.debugf("Depose object '%s' ...", en.getKey());
        en.getValue().depose();
    }
    for (Entry<String, ObjectProxy> en : tmp) {
        if (log.isDebugEnabled())
            log.debugf("Depose object '%s' ...", en.getKey());
        en.getValue().depose();
    }
    objs.clear();
}
Also used : Entry(java.util.Map.Entry) ArrayList(java.util.ArrayList) ObjectProxy(org.nutz.ioc.ObjectProxy) DataSource(javax.sql.DataSource)

Example 7 with ObjectProxy

use of org.nutz.ioc.ObjectProxy 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 8 with ObjectProxy

use of org.nutz.ioc.ObjectProxy 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

ObjectProxy (org.nutz.ioc.ObjectProxy)8 IocException (org.nutz.ioc.IocException)4 IocObject (org.nutz.ioc.meta.IocObject)4 ArrayList (java.util.ArrayList)3 IocContext (org.nutz.ioc.IocContext)3 Test (org.junit.Test)2 IocMaking (org.nutz.ioc.IocMaking)2 Method (java.lang.reflect.Method)1 Entry (java.util.Map.Entry)1 DataSource (javax.sql.DataSource)1 NutDao (org.nutz.dao.impl.NutDao)1 Ioc2 (org.nutz.ioc.Ioc2)1 IocLoader (org.nutz.ioc.IocLoader)1 ValueProxy (org.nutz.ioc.ValueProxy)1 NutIoc (org.nutz.ioc.impl.NutIoc)1 ScopeContext (org.nutz.ioc.impl.ScopeContext)1 Animal (org.nutz.ioc.json.pojo.Animal)1 ComboIocLoader (org.nutz.ioc.loader.combo.ComboIocLoader)1 IocEventSet (org.nutz.ioc.meta.IocEventSet)1 IocField (org.nutz.ioc.meta.IocField)1