Chapter 2 - Jetty读取配置文件和Loader (1)
编程技术  /  houtizong 发布于 3年前   49
public static void main(String[] args) { try { Properties properties=new Properties(); XmlConfiguration last=null; Object[] obj = new Object[args.length]; for (int i = 0; i < args.length; i++) { if (args[i].toLowerCase().endsWith(".properties")) { properties.load(Resource.newResource(args[i]).getInputStream()); } else { XmlConfiguration configuration = new XmlConfiguration(Resource.newResource(args[i]).getURL()); if (last!=null) configuration.getIdMap().putAll(last.getIdMap()); if (properties.size()>0) configuration.setProperties(properties); obj[i] = configuration.configure(); last=configuration; } } for (int i = 0; i < args.length; i++) { if (obj[i] instanceof LifeCycle) { LifeCycle lc = (LifeCycle)obj[i]; if (!lc.isRunning()) lc.start(); } } } catch (Exception e) { Log.warn(Log.EXCEPTION, e); } }
public Object configure() throws Exception { Class oClass = nodeClass(_config); String id = _config.getAttribute("id"); Object obj = id==null?null:_idMap.get(id); if (obj==null && oClass !=null) obj = oClass.newInstance(); if (oClass!=null && !oClass.isInstance(obj)) throw new ClassCastException(oClass.toString()); configure(obj, _config, 0); return obj; }
private Class nodeClass(XmlParser.Node node) throws ClassNotFoundException { String className = node.getAttribute("class"); if (className == null) return null; return Loader.loadClass(XmlConfiguration.class, className,true); }
public static Class loadClass(Class loadClass,String name,boolean checkParents) throws ClassNotFoundException { ClassNotFoundException ex=null; Class c =null; ClassLoader loader=Thread.currentThread().getContextClassLoader(); while (c==null && loader!=null ) { try { c=loader.loadClass(name); } catch (ClassNotFoundException e) {if(ex==null)ex=e;} loader=(c==null&&checkParents)?loader.getParent():null; } loader=loadClass==null?null:loadClass.getClassLoader(); while (c==null && loader!=null ) { try { c=loader.loadClass(name); } catch (ClassNotFoundException e) {if(ex==null)ex=e;} loader=(c==null&&checkParents)?loader.getParent():null; } if (c==null) { try { c=Class.forName(name); } catch (ClassNotFoundException e) {if(ex==null)ex=e;} } if (c!=null) return c; throw ex; }
private void configure(Object obj, XmlParser.Node cfg, int i) throws Exception { String id = cfg.getAttribute("id"); if (id!=null) _idMap.put(id,obj); for (; i < cfg.size(); i++) { Object o = cfg.get(i); if (o instanceof String) continue; XmlParser.Node node = (XmlParser.Node) o; try { String tag = node.getTag(); if ("Set".equals(tag)) set(obj, node); else if ("Put".equals(tag)) put(obj, node); else if ("Call".equals(tag)) call(obj, node); else if ("Get".equals(tag)) get(obj, node); else if ("New".equals(tag)) newObj(obj, node); else if ("Array".equals(tag)) newArray(obj, node); else if ("Ref".equals(tag)) refObj(obj, node); else if ("Property".equals(tag)) propertyObj(obj, node); else throw new IllegalStateException("Unknown tag: " + tag); } catch (Exception e) { Log.warn("Config error at " + node, e.toString()); throw e; } } }
<Set name="ThreadPool"><New class="org.mortbay.thread.QueuedThreadPool"><Set name="minThreads">10</Set><Set name="maxThreads">200</Set><Set name="lowThreads">20</Set></New></Set>
String attr = node.getAttribute("name"); String name = "set" + attr.substring(0, 1).toUpperCase() + attr.substring(1); Object value = value(obj, node); Object[] arg = { value}; vClass[0] = value.getClass();
Method set = oClass.getMethod(name, vClass); set.invoke(obj, arg);
Field type = vClass[0].getField("TYPE");vClass[0] = (Class) type.get(null);Method set = oClass.getMethod(name, vClass);set.invoke(obj, arg);
Field field = oClass.getField(attr);if (Modifier.isPublic(field.getModifiers())){field.set(obj, value);return;}
Method[] sets = oClass.getMethods(); if (name.equals(sets[s].getName()) && sets[s].getParameterTypes().length == 1) set = sets[s]; sets[s].invoke(obj, arg);
Class sClass = set.getParameterTypes()[0];if (sClass.isPrimitive()){for (int t = 0; t < __primitives.length; t++){if (sClass.equals(__primitives[t])){sClass = __primitiveHolders[t];break;}}}Constructor cons = sClass.getConstructor(vClass);arg[0] = cons.newInstance(arg);set.invoke(obj, arg);
throw new NoSuchMethodException(oClass + "." + name + "(" + vClass[0] + ")");
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接