📄 qfactory.java
字号:
mserver.invoke (objectName, "stop", null, null); mserver.invoke (objectName, "destroy", null, null); } if (objectName != null) mserver.unregisterMBean (objectName); } public void configureQBean(MBeanServer server, ObjectName objectName, Element e) throws ConfigurationException { try { AttributeList attributeList = getAttributeList(e); Iterator attributes = attributeList.iterator(); while (attributes.hasNext()) server.setAttribute(objectName,(Attribute)attributes.next()); } catch (Exception e1) { throw new ConfigurationException(e1); } } public AttributeList getAttributeList(Element e) throws ConfigurationException { AttributeList attributeList = new AttributeList(); List childs = e.getChildren("attr"); Iterator childsIterator = childs.iterator(); while (childsIterator.hasNext()) { Element childElement = (Element)childsIterator.next(); String name = childElement.getAttributeValue("name"); name = getAttributeName(name); Attribute attr = new Attribute(name,getObject(childElement)); attributeList.add(attr); } return attributeList; } /** creates an object from a definition element. * The element may have an attribute called type indicating the type of the object * to create, if this attribute is not present java.lang.String is assumed. * int, long and boolean are converted to their wrappers. * @return The created object. * @param childElement Dom Element with the definition of the object. * @throws ConfigurationException If an exception is found trying to create the object. */ protected Object getObject(Element childElement) throws ConfigurationException { String type = childElement.getAttributeValue("type","java.lang.String"); if ("int".equals (type)) type = "java.lang.Integer"; else if ("long".equals (type)) type = "java.lang.Long"; else if ("boolean".equals (type)) type = "java.lang.Boolean"; String value = childElement.getText(); try { Class attributeType = Class.forName(type); if(Collection.class.isAssignableFrom(attributeType)) return getCollection(attributeType, childElement); else{ Class[] parameterTypes = {"".getClass()}; Object[] parameterValues = {value}; return attributeType.getConstructor(parameterTypes).newInstance(parameterValues); } } catch (Exception e1) { throw new ConfigurationException(e1); } } /** Creats a collection from a definition element with the format. * <PRE> * <{attr|item} type="..."> * <item [type="..."]>...</item> * ... * </attr> * </PRE> * @param type * @param e * @throws ConfigurationException * @return */ protected Collection getCollection(Class type, Element e) throws ConfigurationException { try{ Collection col = (Collection)type.newInstance(); Iterator childs = e.getChildren("item").iterator(); while(childs.hasNext()) col.add(getObject((Element)childs.next())); return col; }catch(Exception e1){ throw new ConfigurationException(e1); } } /** * sets the first character of the string to the upper case * @param name * @return attribute name */ public String getAttributeName(String name) { StringBuffer tmp = new StringBuffer(name); tmp.setCharAt(0,name.toUpperCase().charAt(0)) ; return tmp.toString(); } public Object newInstance (String clazz) throws ConfigurationException { try { MBeanServer mserver = q2.getMBeanServer(); return mserver.instantiate (clazz, loaderName); } catch (Exception e) { throw new ConfigurationException (clazz, e); } } public Configuration getConfiguration (Element e) throws ConfigurationException { Properties props = new Properties (); Iterator iter = e.getChildren ("property").iterator(); while (iter.hasNext()) { Element property = (Element) iter.next (); String name = property.getAttributeValue("name"); String value = property.getAttributeValue("value"); String file = property.getAttributeValue("file"); if (file != null) try { props.load (new FileInputStream (new File (file))); } catch (Exception ex) { throw new ConfigurationException (file, ex); } else if (name != null && value != null) { Object obj = props.get (name); if (obj instanceof String[]) { String[] mobj = (String[]) obj; String[] m = new String[mobj.length + 1]; System.arraycopy(mobj,0,m,0,mobj.length); m[mobj.length] = value; props.put (name, m); } else if (obj instanceof String) { String[] m = new String[2]; m[0] = (String) obj; m[1] = value; props.put (name, m); } else props.put (name, value); } } return new SimpleConfiguration (props); } public void setLogger (Object obj, Element e) { if (obj instanceof LogSource) { String loggerName = e.getAttributeValue ("logger"); if (loggerName != null) { String realm = e.getAttributeValue ("realm"); if (realm == null) realm = e.getName(); Logger logger = Logger.getLogger (loggerName); ((LogSource)obj).setLogger (logger, realm); } } } public void setConfiguration (Object obj, Element e) throws ConfigurationException { try { if (obj instanceof Configurable) ((Configurable)obj).setConfiguration (getConfiguration (e)); if (obj instanceof XmlConfigurable) ((XmlConfigurable)obj).setConfiguration(e); } catch (ConfigurationException ex) { throw new ConfigurationException (ex); } } /** * Try to invoke a method (usually a setter) on the given object * silently ignoring if method does not exist * @param obj the object * @param m method to invoke * @param p parameter * @throws ConfigurationException if method happens to throw an exception */ public static void invoke (Object obj, String m, Object p) throws ConfigurationException { invoke (obj, m, p, p != null ? p.getClass() : null); } /** * Try to invoke a method (usually a setter) on the given object * silently ignoring if method does not exist * @param obj the object * @param m method to invoke * @param p parameter * @param pc parameter class * @throws ConfigurationException if method happens to throw an exception */ public static void invoke (Object obj, String m, Object p, Class pc) throws ConfigurationException { try { Class[] paramTemplate = { pc }; Method method = obj.getClass().getMethod(m, paramTemplate); Object[] param = new Object[1]; param[0] = p; method.invoke (obj, param); } catch (NoSuchMethodException e) { } catch (NullPointerException e) { } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { throw new ConfigurationException ( obj.getClass().getName() + "." + m + "("+p.toString()+")" , ((Exception) e.getTargetException()) ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -