Search in sources :

Example 1 with IocContext

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

the class DefaultValueTypes method test_el.

@Test
public void test_el() {
    IocContext context = new ScopeContext("abc");
    String json = "{obj:{type:'org.nutz.ioc.val.DefaultValueTypes', fields:{name:{el:'sys[\"os.arch\"]'}}}}";
    System.out.println(Json.toJson(Json.fromJson(json)));
    Ioc2 ioc = new NutIoc(new MapLoader(json), context, "abc");
    DefaultValueTypes self = ioc.get(DefaultValueTypes.class, "obj");
    assertEquals(System.getProperties().get("os.arch"), self.name);
}
Also used : NutIoc(org.nutz.ioc.impl.NutIoc) IocContext(org.nutz.ioc.IocContext) ScopeContext(org.nutz.ioc.impl.ScopeContext) MapLoader(org.nutz.ioc.loader.map.MapLoader) Ioc2(org.nutz.ioc.Ioc2) Test(org.junit.Test)

Example 2 with IocContext

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

the class ModuleProcessor method process.

public void process(ActionContext ac) throws Throwable {
    RequestIocContext reqContext = null;
    try {
        if (null != moduleObj) {
            ac.setModule(moduleObj);
        } else {
            Ioc ioc = ac.getIoc();
            Object obj;
            /*
                 * 如果 Ioc 容器实现了高级接口,那么会为当前请求设置上下文对象
                 */
            if (NutSessionListener.isSessionScopeEnable && ioc instanceof Ioc2) {
                reqContext = new RequestIocContext(ac.getRequest());
                HttpSession sess = Mvcs.getHttpSession(false);
                IocContext myContext = null;
                // 如果容器可以创建 Session ...
                if (null != sess) {
                    SessionIocContext sessionContext = new SessionIocContext(sess);
                    myContext = new ComboContext(reqContext, sessionContext);
                } else // 如果容器禁止了 Session ...
                {
                    myContext = reqContext;
                }
                Mvcs.setIocContext(myContext);
                obj = ((Ioc2) ioc).get(moduleType, injectName, myContext);
            } else
                /*
                 * 否则,则仅仅简单的从容器获取
                 */
                obj = ioc.get(moduleType, injectName);
            ac.setModule(obj);
        }
        ac.setMethod(method);
        // if (log.isDebugEnabled()) //打印实际执行的Method信息
        // log.debugf("Handle URL[%s] by Method[%s]",ac.getPath(),method);
        doNext(ac);
    } finally {
        if (reqContext != null)
            try {
                reqContext.depose();
            } catch (Throwable e) {
                if (log.isDebugEnabled())
                    log.debug("ReqContext depose fail?!", e);
            }
    }
}
Also used : ComboContext(org.nutz.ioc.impl.ComboContext) SessionIocContext(org.nutz.mvc.ioc.SessionIocContext) RequestIocContext(org.nutz.mvc.ioc.RequestIocContext) IocContext(org.nutz.ioc.IocContext) SessionIocContext(org.nutz.mvc.ioc.SessionIocContext) HttpSession(javax.servlet.http.HttpSession) Ioc2(org.nutz.ioc.Ioc2) Ioc(org.nutz.ioc.Ioc) RequestIocContext(org.nutz.mvc.ioc.RequestIocContext)

Example 3 with IocContext

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

the class ReferTypeValue method get.

public Object get(IocMaking ing) {
    Ioc ioc = ing.getIoc();
    IocContext ctx = ing.getContext();
    if (typeFirst) {
        String[] names;
        if (ioc instanceof Ioc2) {
            names = ((Ioc2) ioc).getNamesByType(type, ctx);
            if (names != null && names.length == 1) {
                return ((Ioc2) ioc).get(type, names[0], ctx);
            }
        } else {
            names = ioc.getNamesByType(type);
            if (names != null && names.length == 1) {
                return ioc.get(type, names[0]);
            }
        }
    }
    if (ioc.has(name)) {
        if (ioc instanceof Ioc2)
            return ((Ioc2) ioc).get(type, name, ctx);
        return ioc.get(type, name);
    }
    if (log.isDebugEnabled())
        log.debugf("name=%s not found, search for type=%s", name, type.getName());
    if (ioc instanceof Ioc2)
        return ((Ioc2) ioc).getByType(type, ctx);
    else
        return ioc.getByType(type);
}
Also used : IocContext(org.nutz.ioc.IocContext) Ioc2(org.nutz.ioc.Ioc2) Ioc(org.nutz.ioc.Ioc)

Example 4 with IocContext

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

the class DefaultValueTypes method test_refer_context.

@Test
public void test_refer_context() {
    IocContext context = new ScopeContext("abc");
    String json = "{obj:{singleton:false,fields:{ic:{refer:'$conText'}}}}";
    Ioc2 ioc = new NutIoc(new MapLoader(json), context, "abc");
    TestReferContext trc = ioc.get(TestReferContext.class);
    assertTrue(context == trc.ic);
    IocContext context2 = new ScopeContext("rrr");
    trc = ioc.get(TestReferContext.class, "obj", context2);
    assertTrue(trc.ic instanceof ComboContext);
}
Also used : NutIoc(org.nutz.ioc.impl.NutIoc) ComboContext(org.nutz.ioc.impl.ComboContext) IocContext(org.nutz.ioc.IocContext) ScopeContext(org.nutz.ioc.impl.ScopeContext) MapLoader(org.nutz.ioc.loader.map.MapLoader) Ioc2(org.nutz.ioc.Ioc2) Test(org.junit.Test)

Example 5 with IocContext

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

the class NutIoc method get.

public <T> T get(Class<T> type, String name, IocContext context) throws IocException {
    if (log.isDebugEnabled())
        log.debugf("Get '%s'<%s>", name, type == null ? "" : type);
    try {
        if (this.mirrors instanceof LifeCycle)
            ((LifeCycle) this.mirrors).init();
    } catch (Exception e) {
        throw new IocException("_mirror_factory_init", e, "Mirror Factory init fail");
    }
    // 创建对象创建时
    IocMaking ing = makeIocMaking(context, name);
    IocContext cntx = ing.getContext();
    // 从上下文缓存中获取对象代理
    ObjectProxy op = cntx.fetch(name);
    // 如果未发现对象
    if (null == op) {
        // 线程同步
        synchronized (lock_get) {
            // 再次读取
            op = cntx.fetch(name);
            // 如果未发现对象
            if (null == op) {
                try {
                    if (log.isDebugEnabled())
                        log.debug("\t >> Load definition name=" + name);
                    // 读取对象定义
                    IocObject iobj = loader.load(createLoading(), name);
                    if (null == iobj) {
                        for (String iocBeanName : loader.getName()) {
                            // 感觉没必要..没有就没有呗
                            if (3 > LevenshteinDistance.computeLevenshteinDistance(name.toLowerCase(), iocBeanName.toLowerCase())) {
                                throw new IocException(name, "Undefined object '%s' but found similar name '%s'", name, iocBeanName);
                            }
                        }
                        throw new IocException(name, "Undefined object '%s'", name);
                    }
                    // 修正对象类型
                    if (null == iobj.getType())
                        if (null == type && Strings.isBlank(iobj.getFactory()))
                            throw new IocException(name, "NULL TYPE object '%s'", name);
                        else
                            iobj.setType(type);
                    // 检查对象级别
                    if (Strings.isBlank(iobj.getScope()))
                        iobj.setScope(defaultScope);
                    // 根据对象定义,创建对象,maker 会自动的缓存对象到 context 中
                    if (log.isDebugEnabled())
                        log.debugf("\t >> Make...'%s'<%s>", name, type == null ? "" : type);
                    op = maker.make(ing, iobj);
                }// 处理异常
                 catch (IocException e) {
                    ((IocException) e).addBeanNames(name);
                    throw e;
                } catch (Throwable e) {
                    throw new IocException(name, e, "For object [%s] - type:[%s]", name, type == null ? "" : type);
                }
            }
        }
    }
    synchronized (lock_get) {
        T re = op.get(type, ing);
        if (!name.startsWith("$") && re instanceof IocLoader) {
            loader.addLoader((IocLoader) re);
        }
        return re;
    }
}
Also used : LifeCycle(org.nutz.lang.util.LifeCycle) IocContext(org.nutz.ioc.IocContext) IocMaking(org.nutz.ioc.IocMaking) IocException(org.nutz.ioc.IocException) IocObject(org.nutz.ioc.meta.IocObject) ComboIocLoader(org.nutz.ioc.loader.combo.ComboIocLoader) IocLoader(org.nutz.ioc.IocLoader) ObjectProxy(org.nutz.ioc.ObjectProxy) IocException(org.nutz.ioc.IocException)

Aggregations

IocContext (org.nutz.ioc.IocContext)7 Ioc2 (org.nutz.ioc.Ioc2)4 IocException (org.nutz.ioc.IocException)3 ObjectProxy (org.nutz.ioc.ObjectProxy)3 IocObject (org.nutz.ioc.meta.IocObject)3 Test (org.junit.Test)2 Ioc (org.nutz.ioc.Ioc)2 ComboContext (org.nutz.ioc.impl.ComboContext)2 NutIoc (org.nutz.ioc.impl.NutIoc)2 ScopeContext (org.nutz.ioc.impl.ScopeContext)2 MapLoader (org.nutz.ioc.loader.map.MapLoader)2 ArrayList (java.util.ArrayList)1 HttpSession (javax.servlet.http.HttpSession)1 IocLoader (org.nutz.ioc.IocLoader)1 IocMaking (org.nutz.ioc.IocMaking)1 ComboIocLoader (org.nutz.ioc.loader.combo.ComboIocLoader)1 LifeCycle (org.nutz.lang.util.LifeCycle)1 RequestIocContext (org.nutz.mvc.ioc.RequestIocContext)1 SessionIocContext (org.nutz.mvc.ioc.SessionIocContext)1