⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objectmbean.java

📁 jetty SERVER連接資料庫用的軟體
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    /* ------------------------------------------------------------ */    public void setAttribute(Attribute attr) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException    {        if (attr == null)            return;        if (Log.isDebugEnabled())            Log.debug("setAttribute " + _managed + ":" +attr.getName() + "=" + attr.getValue());        Method setter = (Method) _setters.get(attr.getName());        if (setter == null)            throw new AttributeNotFoundException(attr.getName());        try        {            Object o = _managed;            if (setter.getDeclaringClass().isInstance(this))                o = this;            // get the value            Object value = attr.getValue();            // convert from ObjectName if need be            if (value!=null && _convert.contains(attr.getName()))            {                if (value.getClass().isArray())                {                    Class t=setter.getParameterTypes()[0].getComponentType();                    Object na = Array.newInstance(t,Array.getLength(value));                    for (int i=Array.getLength(value);i-->0;)                        Array.set(na, i, _mbeanContainer.findBean((ObjectName)Array.get(value, i)));                    value=na;                }                else                    value=_mbeanContainer.findBean((ObjectName)value);            }            // do the setting            setter.invoke(o, new Object[]{ value });        }        catch (IllegalAccessException e)        {            Log.warn(Log.EXCEPTION, e);            throw new AttributeNotFoundException(e.toString());        }        catch (InvocationTargetException e)        {            Log.warn(Log.EXCEPTION, e);            throw new ReflectionException((Exception) e.getTargetException());        }    }    /* ------------------------------------------------------------ */    public AttributeList setAttributes(AttributeList attrs)    {        Log.debug("setAttributes");        AttributeList results = new AttributeList(attrs.size());        Iterator iter = attrs.iterator();        while (iter.hasNext())        {            try            {                Attribute attr = (Attribute) iter.next();                setAttribute(attr);                results.add(new Attribute(attr.getName(), getAttribute(attr.getName())));            }            catch (Exception e)            {                Log.warn(Log.EXCEPTION, e);            }        }        return results;    }    /* ------------------------------------------------------------ */    public Object invoke(String name, Object[] params, String[] signature) throws MBeanException, ReflectionException    {        if (Log.isDebugEnabled())            Log.debug("invoke " + name);        String methodKey = name + "(";        if (signature != null)            for (int i = 0; i < signature.length; i++)                methodKey += (i > 0 ? "," : "") + signature[i];        methodKey += ")";        ClassLoader old_loader=Thread.currentThread().getContextClassLoader();        try        {            Thread.currentThread().setContextClassLoader(_loader);            Method method = (Method) _methods.get(methodKey);            if (method == null)                throw new NoSuchMethodException(methodKey);            Object o = _managed;            if (method.getDeclaringClass().isInstance(this))                o = this;            return method.invoke(o, params);        }        catch (NoSuchMethodException e)        {            Log.warn(Log.EXCEPTION, e);            throw new ReflectionException(e);        }        catch (IllegalAccessException e)        {            Log.warn(Log.EXCEPTION, e);            throw new MBeanException(e);        }        catch (InvocationTargetException e)        {            Log.warn(Log.EXCEPTION, e);            throw new ReflectionException((Exception) e.getTargetException());        }        finally        {            Thread.currentThread().setContextClassLoader(old_loader);        }    }    private static Object findInfluences(Object influences, Class aClass)    {        if (aClass!=null)        {            // This class is an influence            influences=LazyList.add(influences,aClass);            // So are the super classes            influences=findInfluences(influences,aClass.getSuperclass());            // So are the interfaces            Class[] ifs = aClass.getInterfaces();            for (int i=0;ifs!=null && i<ifs.length;i++)                influences=findInfluences(influences,ifs[i]);        }        return influences;    }    /* ------------------------------------------------------------ */    /**     * Define an attribute on the managed object. The meta data is defined by looking for standard     * getter and setter methods. Descriptions are obtained with a call to findDescription with the     * attribute name.     *     * @param name     * @param metaData "description" or "access:description" or "type:access:description"  where type is     * one of: <ul>     * <li>"Object" The field/method is on the managed object.     * <li>"MBean" The field/method is on the mbean proxy object     * <li>"MObject" The field/method is on the managed object and value should be converted to MBean reference     * <li>"MMBean" The field/method is on the mbean proxy object and value should be converted to MBean reference     * </ul>     * the access is either "RW" or "RO".     */    public MBeanAttributeInfo defineAttribute(String name, String metaData)    {        String description = "";        boolean writable = true;        boolean onMBean = false;        boolean convert = false;        if (metaData!= null)        {            String[] tokens = metaData.split(":", 3);            for (int t=0;t<tokens.length-1;t++)            {                tokens[t]=tokens[t].trim();                if ("RO".equals(tokens[t]))                    writable=false;                else                 {                    onMBean=("MMBean".equalsIgnoreCase(tokens[t]) || "MBean".equalsIgnoreCase(tokens[t]));                    convert=("MMBean".equalsIgnoreCase(tokens[t]) || "MObject".equalsIgnoreCase(tokens[t]));                }            }            description=tokens[tokens.length-1];        }                String uName = name.substring(0, 1).toUpperCase() + name.substring(1);        Class oClass = onMBean ? this.getClass() : _managed.getClass();        if (Log.isDebugEnabled())            Log.debug("defineAttribute "+name+" "+onMBean+":"+writable+":"+oClass+":"+description);        Class type = null;        Method getter = null;        Method setter = null;        Method[] methods = oClass.getMethods();        for (int m = 0; m < methods.length; m++)        {            if ((methods[m].getModifiers() & Modifier.PUBLIC) == 0)                continue;            // Look for a getter            if (methods[m].getName().equals("get" + uName) && methods[m].getParameterTypes().length == 0)            {                if (getter != null)                    throw new IllegalArgumentException("Multiple getters for attr " + name+ " in "+oClass);                getter = methods[m];                if (type != null && !type.equals(methods[m].getReturnType()))                    throw new IllegalArgumentException("Type conflict for attr " + name+ " in "+oClass);                type = methods[m].getReturnType();            }            // Look for an is getter            if (methods[m].getName().equals("is" + uName) && methods[m].getParameterTypes().length == 0)            {                if (getter != null)                    throw new IllegalArgumentException("Multiple getters for attr " + name+ " in "+oClass);                getter = methods[m];                if (type != null && !type.equals(methods[m].getReturnType()))                    throw new IllegalArgumentException("Type conflict for attr " + name+ " in "+oClass);                type = methods[m].getReturnType();            }            // look for a setter            if (writable && methods[m].getName().equals("set" + uName) && methods[m].getParameterTypes().length == 1)            {                if (setter != null)                    throw new IllegalArgumentException("Multiple setters for attr " + name+ " in "+oClass);                setter = methods[m];                if (type != null && !type.equals(methods[m].getParameterTypes()[0]))                    throw new IllegalArgumentException("Type conflict for attr " + name+ " in "+oClass);                type = methods[m].getParameterTypes()[0];            }        }                if (convert && type.isPrimitive() && !type.isArray())            throw new IllegalArgumentException("Cannot convert primative " + name);        if (getter == null && setter == null)            throw new IllegalArgumentException("No getter or setters found for " + name+ " in "+oClass);        try        {            // Remember the methods            _getters.put(name, getter);            _setters.put(name, setter);            MBeanAttributeInfo info=null;            if (convert)            {                _convert.add(name);                if (type.isArray())                    info= new MBeanAttributeInfo(name,OBJECT_NAME_ARRAY_CLASS,description,getter!=null,setter!=null,getter!=null&&getter.getName().startsWith("is"));                else                    info= new MBeanAttributeInfo(name,OBJECT_NAME_CLASS,description,getter!=null,setter!=null,getter!=null&&getter.getName().startsWith("is"));            }            else                info= new MBeanAttributeInfo(name,description,getter,setter);            return info;        }        catch (Exception e)        {            Log.warn(Log.EXCEPTION, e);            throw new IllegalArgumentException(e.toString());        }    }    /* ------------------------------------------------------------ */    /**     * Define an operation on the managed object. Defines an operation with parameters. Refection is     * used to determine find the method and it's return type. The description of the method is     * found with a call to findDescription on "name(signature)". The name and description of each     * parameter is found with a call to findDescription with "name(signature)[n]", the returned     * description is for the last parameter of the partial signature and is assumed to start with     * the parameter name, followed by a colon.     *     * @param metaData "description" or "impact:description" or "type:impact:description", type is     * the "Object","MBean", "MMBean" or "MObject" to indicate the method is on the object, the MBean or on the     * object but converted to an MBean reference, and impact is either "ACTION","INFO","ACTION_INFO" or "UNKNOWN".     */    private MBeanOperationInfo defineOperation(String signature, String metaData, ResourceBundle bundle)    {        String[] tokens=metaData.split(":",3);        int i=tokens.length-1;        String description=tokens[i--];        String impact_name = i<0?"UNKNOWN":tokens[i--].trim();        if (i==0)            tokens[0]=tokens[0].trim();        boolean onMBean= i==0 && ("MBean".equalsIgnoreCase(tokens[0])||"MMBean".equalsIgnoreCase(tokens[0]));        boolean convert= i==0 && ("MObject".equalsIgnoreCase(tokens[0])||"MMBean".equalsIgnoreCase(tokens[0]));        if (Log.isDebugEnabled())            Log.debug("defineOperation "+signature+" "+onMBean+":"+impact_name+":"+description);        Class oClass = onMBean ? this.getClass() : _managed.getClass();        try        {            // Resolve the impact            int impact=MBeanOperationInfo.UNKNOWN;            if (impact_name==null || impact_name.equals("UNKNOWN"))                impact=MBeanOperationInfo.UNKNOWN;            else if (impact_name.equals("ACTION"))                impact=MBeanOperationInfo.ACTION;            else if (impact_name.equals("INFO"))                impact=MBeanOperationInfo.INFO;            else if (impact_name.equals("ACTION_INFO"))                impact=MBeanOperationInfo.ACTION_INFO;            else                Log.warn("Unknown impact '"+impact_name+"' for "+signature);            // split the signature            String[] parts=signature.split("[\\(\\)]");            String method_name=parts[0];            String arguments=parts.length==2?parts[1]:null;            String[] args=arguments==null?new String[0]:arguments.split(" *, *");            // Check types and normalize signature.            Class[] types = new Class[args.length];            MBeanParameterInfo[] pInfo = new MBeanParameterInfo[args.length];            signature=method_name;            for (i = 0; i < args.length; i++)            {                Class type = TypeUtil.fromName(args[i]);                if (type == null)                    type = Thread.currentThread().getContextClassLoader().loadClass(args[i]);                types[i] = type;                args[i] = type.isPrimitive() ? TypeUtil.toName(type) : args[i];                signature+=(i>0?",":"(")+args[i];            }            signature+=(i>0?")":"()");            // Build param infos            for (i = 0; i < args.length; i++)            {                String param_desc = bundle.getString(signature + "[" + i + "]");                parts=param_desc.split(" *: *",2);                if (Log.isDebugEnabled())                    Log.debug(parts[0]+": "+parts[1]);                pInfo[i] = new MBeanParameterInfo(parts[0].trim(), args[i], parts[1].trim());            }            // build the operation info            Method method = oClass.getMethod(method_name, types);            Class returnClass = method.getReturnType();            _methods.put(signature, method);            if (convert)                _convert.add(signature);            return new MBeanOperationInfo(method_name, description, pInfo, returnClass.isPrimitive() ? TypeUtil.toName(returnClass) : (returnClass.getName()), impact);        }        catch (Exception e)        {            Log.warn("Operation '"+signature+"'", e);            throw new IllegalArgumentException(e.toString());        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -