Search in sources :

Example 1 with IocObject

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

the class XmlIocLoaderTest method testXmlIocLoader.

@Test
public void testXmlIocLoader() throws ObjectLoadException {
    IocLoader iocLoader = getNew("org/nutz/ioc/loader/xml/conf/zzh-offered.xml");
    assertTrue(iocLoader.getName() != null);
    assertTrue(iocLoader.getName().length > 0);
    for (String name : iocLoader.getName()) {
        assertNotNull(name);
        assertNotNull(iocLoader.load(null, name));
        IocObject iocObject = iocLoader.load(null, name);
        if (iocObject.hasArgs()) {
            for (IocValue iocValue : iocObject.getArgs()) {
                iocValue.getType();
                iocValue.getValue();
                checkValue(iocValue);
            }
        }
        if (iocObject.getFields() != null) {
            for (IocField iocField : iocObject.getFields().values()) {
                assertNotNull(iocField.getName());
                if (iocField.getValue() != null) {
                    IocValue iocValue = iocField.getValue();
                    checkValue(iocValue);
                }
            }
        }
    }
    iocLoader.load(null, "obj").getFields().values().iterator().next().getValue().getValue();
}
Also used : IocValue(org.nutz.ioc.meta.IocValue) IocObject(org.nutz.ioc.meta.IocObject) IocLoader(org.nutz.ioc.IocLoader) IocField(org.nutz.ioc.meta.IocField) Test(org.junit.Test)

Example 2 with IocObject

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

the class AnnotationIocLoaderTest method testLoad.

@Test
public void testLoad() throws Throwable {
    IocObject iocObject = iocLoader.load(null, "classB");
    assertNotNull(iocObject);
    assertNotNull(iocObject.getFields());
    assertTrue(iocObject.getFields().size() == 1);
    System.out.println(Json.toJson(iocObject));
    assertEquals("refer", iocObject.getFields().values().iterator().next().getValue().getType());
}
Also used : IocObject(org.nutz.ioc.meta.IocObject) Test(org.junit.Test)

Example 3 with IocObject

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

the class IocLoading method map2iobj.

@SuppressWarnings("unchecked")
public IocObject map2iobj(Map<String, Object> map) throws ObjectLoadException {
    final IocObject iobj = new IocObject();
    if (!isIocObject(map)) {
        for (Entry<String, Object> en : map.entrySet()) {
            IocField ifld = new IocField();
            ifld.setName(en.getKey());
            ifld.setValue(object2value(en.getValue()));
            iobj.addField(ifld);
        }
    //            if (log.isWarnEnabled()) // TODO 移除这种兼容性
    //                log.warn("Using *Declared* ioc-define (without type or events)!!! Pls use Standard Ioc-Define!!"
    //                            + " Bean will define as:\n"
    //                            + Json.toJson(iobj));
    } else {
        Object v = map.get("type");
        // type
        try {
            String typeName = (String) v;
            if (!Strings.isBlank(typeName)) {
                iobj.setType(Lang.loadClass(typeName));
            }
        } catch (Exception e) {
            throw E(e, "Wrong type name: '%s'", v);
        }
        // singleton
        try {
            v = map.get("singleton");
            if (null != v)
                iobj.setSingleton(Castors.me().castTo(v, boolean.class));
        } catch (FailToCastObjectException e) {
            throw E(e, "Wrong singleton: '%s'", v);
        }
        // scope
        v = map.get("scope");
        if (null != v)
            iobj.setScope(v.toString());
        // events
        try {
            v = map.get("events");
            if (null != v) {
                IocEventSet ies = Lang.map2Object((Map<?, ?>) v, IocEventSet.class);
                iobj.setEvents(ies);
            }
        } catch (Exception e) {
            throw E(e, "Wrong events: '%s'", v);
        }
        // args
        try {
            v = map.get("args");
            if (null != v) {
                Lang.each(v, new Each<Object>() {

                    public void invoke(int i, Object ele, int length) {
                        iobj.addArg(object2value(ele));
                    }
                });
            }
        } catch (Exception e) {
            throw E(e, "Wrong args: '%s'", v);
        }
        // fields
        try {
            v = map.get("fields");
            if (null != v) {
                Map<String, Object> fields = (Map<String, Object>) v;
                for (Entry<String, Object> en : fields.entrySet()) {
                    IocField ifld = new IocField();
                    ifld.setName(en.getKey());
                    ifld.setValue(object2value(en.getValue()));
                    iobj.addField(ifld);
                }
            }
        } catch (Exception e) {
            throw E(e, "Wrong args: '%s'", v);
        }
        // factory方法
        v = map.get("factory");
        if (v != null && !Strings.isBlank(v.toString())) {
            iobj.setFactory(v.toString());
        }
    }
    return iobj;
}
Also used : IocEventSet(org.nutz.ioc.meta.IocEventSet) FailToCastObjectException(org.nutz.castor.FailToCastObjectException) IocObject(org.nutz.ioc.meta.IocObject) Iocs.isIocObject(org.nutz.ioc.Iocs.isIocObject) IocField(org.nutz.ioc.meta.IocField) IocObject(org.nutz.ioc.meta.IocObject) Iocs.isIocObject(org.nutz.ioc.Iocs.isIocObject) HashMap(java.util.HashMap) Map(java.util.Map) FailToCastObjectException(org.nutz.castor.FailToCastObjectException)

Example 4 with IocObject

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

the class DefaultValueProxyMaker method make.

@SuppressWarnings("unchecked")
public ValueProxy make(IocMaking ing, IocValue iv) {
    Object value = iv.getValue();
    String type = iv.getType();
    // Null
    if ("null".equals(type) || null == value) {
        return new StaticValue(null);
    }
    if (value instanceof ValueProxy) {
        return (ValueProxy) value;
    } else // String, Number, .....
    if ("normal".equals(type) || null == type) {
        // Array
        if (value.getClass().isArray()) {
            Object[] vs = (Object[]) value;
            IocValue[] tmp = new IocValue[vs.length];
            for (int i = 0; i < tmp.length; i++) tmp[i] = (IocValue) vs[i];
            return new ArrayValue(ing, tmp);
        } else // Map
        if (value instanceof Map<?, ?>) {
            return new MapValue(ing, (Map<String, IocValue>) value, (Class<? extends Map<String, Object>>) value.getClass());
        } else // Collection
        if (value instanceof Collection<?>) {
            return new CollectionValue(ing, (Collection<IocValue>) value, (Class<? extends Collection<Object>>) value.getClass());
        } else // Inner Object
        if (value instanceof IocObject) {
            return new InnerValue((IocObject) value);
        }
        return new StaticValue(value);
    } else // Refer
    if ("refer".equals(type)) {
        String s = value.toString();
        if (null != s) {
            String renm = s.toLowerCase();
            // $Ioc
            if ("$ioc".equals(renm)) {
                return new IocSelfValue();
            } else // $Name
            if ("$name".equals(renm)) {
                return new ObjectNameValue();
            } else // $Context
            if ("$context".equals(renm)) {
                return new IocContextObjectValue();
            }
        }
        return new ReferValue(s);
    } else // Refer_Type
    if (IocValue.TYPE_REFER_TYPE.equals(type)) {
        if (value instanceof CharSequence) {
            String[] tmp = value.toString().split("#");
            return new ReferTypeValue(tmp[0], Lang.forName(tmp[1], Object.class));
        } else if (value instanceof Field) {
            return new ReferTypeValue((Field) value);
        }
        throw new IocException(ing.getObjectName(), "unspported refer_type:'%s'", value);
    } else // Java
    if ("java".equals(type)) {
        return new JavaValue(value.toString());
    } else // File
    if ("file".equals(type)) {
        return new FileValue(value.toString());
    } else // Env
    if ("env".equals(type)) {
        return new EnvValue(value);
    } else // System Properties
    if ("sys".equals(type)) {
        return new SysPropValue(value);
    } else // Inner
    if ("inner".equals(type)) {
        return new InnerValue((IocObject) value);
    } else // JNDI
    if ("jndi".equals(type)) {
        return new JNDI_Value(value.toString());
    } else if ("el".equals(type)) {
        return new EL_Value(value.toString());
    }
    return null;
}
Also used : IocException(org.nutz.ioc.IocException) Field(java.lang.reflect.Field) ValueProxy(org.nutz.ioc.ValueProxy) IocObject(org.nutz.ioc.meta.IocObject) IocValue(org.nutz.ioc.meta.IocValue) Collection(java.util.Collection) IocObject(org.nutz.ioc.meta.IocObject) Map(java.util.Map)

Example 5 with IocObject

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

the class PropertiesIocLoader method reload.

@SuppressWarnings("rawtypes")
public void reload() {
    List<String> beanNames = new ArrayList<String>();
    for (String key : keys()) {
        if (!key.startsWith("ioc.") || key.length() < 5)
            continue;
        String[] tmp = key.split("[.]");
        if (tmp.length == 3) {
            if (tmp[2].equals("type") || tmp[2].equals("factory")) {
                beanNames.add(tmp[1]);
            }
        }
    }
    for (String beanName : beanNames) {
        ObjectNaviNode no = new ObjectNaviNode();
        String prefix = "ioc." + beanName + ".";
        String pre = "";
        ParamExtractor pe = Params.makeParamExtractor(null, this.toMap());
        for (Object name : pe.keys()) {
            String na = (String) name;
            if (na.startsWith(prefix)) {
                no.put(pre + na, pe.extractor(na));
            }
        }
        Object model = no.get();
        Object re = Mapl.maplistToObj(((Map) model).get(beanName), IocObject.class);
        this.objs.put(beanName, (IocObject) re);
    }
// 插入自身
//this.objs.put("conf", Iocs.wrap(this));
}
Also used : ArrayList(java.util.ArrayList) IocObject(org.nutz.ioc.meta.IocObject) ObjectNaviNode(org.nutz.mvc.adaptor.injector.ObjectNaviNode) ParamExtractor(org.nutz.mvc.adaptor.ParamExtractor)

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