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();
}
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());
}
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;
}
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;
}
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));
}
Aggregations