Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.tomcat.util.modeler.modules;
 
 import java.util.List;
 
 
 
 {
 
     Registry registry;
     String location;
     String type;
     Object source;
     List mbeans=new ArrayList();
 
     public void setRegistry(Registry reg) {
         this.=reg;
     }
 
     public void setLocationString loc ) {
         this.=loc;
     }

    
Used if a single component is loaded

Parameters:
type
 
     public void setTypeString type ) {
        this.=type;
     }
 
     public void setSourceObject source ) {
         this.=source;
     }
 
     public List loadDescriptorsRegistry registryString location,
                                  String typeObject source)
             throws Exception
     {
         setRegistry(registry);
         setLocation(location);
         setType(type);
         setSource(source);
         execute();
         return ;
     }
 
     public void execute() throws Exception {
         if==null ) =Registry.getRegistry();
         try {
             ManagedBean managed=createManagedBean(null, (Class));
             ifmanaged==null ) return;
             managed.setName );
 
             .add(managed);
 
         } catchException ex ) {
         }
     }
 
 
 
     // ------------ Implementation for non-declared introspection classes
 
     static Hashtable specialMethods=new Hashtable();
     static {
         .put"preDeregister""");
        .put"postDeregister""");
    }
    private static String strArray[]=new String[0];
    private static ObjectName objNameArray[]=new ObjectName[0];
    // createMBean == registerClass + registerMBean
    private static Class[] supportedTypes  = new Class[] {
        Boolean.class,
        .,
        Byte.class,
        .,
        Character.class,
        .,
        Short.class,
        .,
        Integer.class,
        .,
        Long.class,
        .,
        Float.class
        .,
        Double.class,
        .,
        String.class,
        .getClass(),
        BigDecimal.class,
        BigInteger.class,
        ObjectName.class,
        .getClass(),
        java.io.File.class,
    };
    
    
Check if this class is one of the supported types. If the class is supported, returns true. Otherwise, returns false.

Parameters:
ret The class to check
Returns:
boolean True if class is supported
 
    private boolean supportedType(Class ret) {
        for (int i = 0; i < .i++) {
            if (ret == [i]) {
                return true;
            }
        }
        if (isBeanCompatible(ret)) {
            return true;
        }
        return false;
    }

    
Check if this class conforms to JavaBeans specifications. If the class is conformant, returns true.

Parameters:
javaType The class to check
Returns:
boolean True if the class is compatible.
    protected boolean isBeanCompatible(Class javaType) {
        // Must be a non-primitive and non array
        if (javaType.isArray() || javaType.isPrimitive()) {
            return false;
        }
        // Anything in the java or javax package that
        // does not have a defined mapping is excluded.
        if (javaType.getName().startsWith("java.") || 
            javaType.getName().startsWith("javax.")) {
            return false;
        }
        try {
            javaType.getConstructor(new Class[]{});
        } catch (java.lang.NoSuchMethodException e) {
            return false;
        }
        // Make sure superclass is compatible
        Class superClass = javaType.getSuperclass();
        if (superClass != null && 
            superClass != java.lang.Object.class && 
            superClass != java.lang.Exception.class && 
            superClass != java.lang.Throwable.class) {
            if (!isBeanCompatible(superClass)) {
                return false;
            }
        }
        return true;
    }
    
    
Process the methods and extract 'attributes', methods, etc

Parameters:
realClass The class to process
methods The methods to process
attMap The attribute map (complete)
getAttMap The readable attributess map
setAttMap The settable attributes map
invokeAttMap The invokable attributes map
    private void initMethods(Class realClass,
                             Method methods[],
                             Hashtable attMapHashtable getAttMap,
                             Hashtable setAttMapHashtable invokeAttMap)
    {
        for (int j = 0; j < methods.length; ++j) {
            String name=methods[j].getName();
            if( Modifier.isStatic(methods[j].getModifiers()))
                continue;
            if( ! Modifier.isPublicmethods[j].getModifiers() ) ) {
                if..isDebugEnabled())
                    ..debug("Not public " + methods[j] );
                continue;
            }
            ifmethods[j].getDeclaringClass() == Object.class )
                continue;
            Class params[]=methods[j].getParameterTypes();
            ifname.startsWith"get" ) && params.length==0) {
                Class ret=methods[j].getReturnType();
                if( ! supportedTyperet ) ) {
                    if..isDebugEnabled() )
                        ..debug("Unsupported type " + methods[j]);
                    continue;
                }
                name=unCapitalizename.substring(3));
                getAttMap.putnamemethods[j] );
                // just a marker, we don't use the value
                attMap.putnamemethods[j] );
            } else ifname.startsWith"is" ) && params.length==0) {
                Class ret=methods[j].getReturnType();
                if. != ret  ) {
                    if..isDebugEnabled() )
                        ..debug("Unsupported type " + methods[j] + " " + ret );
                    continue;
                }
                name=unCapitalizename.substring(2));
                getAttMap.putnamemethods[j] );
                // just a marker, we don't use the value
                attMap.putnamemethods[j] );
            } else ifname.startsWith"set" ) && params.length==1) {
                if( ! supportedTypeparams[0] ) ) {
                    if..isDebugEnabled() )
                        ..debug("Unsupported type " + methods[j] + " " + params[0]);
                    continue;
                }
                name=unCapitalizename.substring(3));
                setAttMap.putnamemethods[j] );
                attMap.putnamemethods[j] );
            } else {
                ifparams.length == 0 ) {
                    if.getmethods[j].getName() ) != null )
                        continue;
                    invokeAttMap.putnamemethods[j]);
                } else {
                    boolean supported=true;
                    forint i=0; i<params.lengthi++ ) {
                        if( ! supportedTypeparams[i])) {
                            supported=false;
                            break;
                        }
                    }
                    ifsupported )
                        invokeAttMap.putnamemethods[j]);
                }
            }
        }
    }

    
XXX Find if the 'className' is the name of the MBean or the real class ( I suppose first ) XXX Read (optional) descriptions from a .properties, generated from source XXX Deal with constructors

Parameters:
registry The Bean registry (not used)
domain The bean domain (not used)
realClass The class to analyze
type The bean type
Returns:
ManagedBean The create MBean
    public ManagedBean createManagedBean(Registry registryString domain,
                                         Class realClassString type)
    {
        ManagedBean mbeannew ManagedBean();
        Method methods[]=null;
        Hashtable attMap=new Hashtable();
        // key: attribute val: getter method
        Hashtable getAttMap=new Hashtable();
        // key: attribute val: setter method
        Hashtable setAttMap=new Hashtable();
        // key: operation val: invoke method
        Hashtable invokeAttMap=new Hashtable();
        methods = realClass.getMethods();
        initMethods(realClassmethodsattMapgetAttMapsetAttMapinvokeAttMap );
        try {
            Enumeration en=attMap.keys();
            whileen.hasMoreElements() ) {
                String name=(String)en.nextElement();
                AttributeInfo ai=new AttributeInfo();
                ai.setNamename );
                Method gm=(Method)getAttMap.get(name);
                ifgm!=null ) {
                    //ai.setGetMethodObj( gm );
                    ai.setGetMethodgm.getName());
                    Class t=gm.getReturnType();
                    ift!=null )
                        ai.setTypet.getName() );
                }
                Method sm=(Method)setAttMap.get(name);
                ifsm!=null ) {
                    //ai.setSetMethodObj(sm);
                    Class t=sm.getParameterTypes()[0];
                    ift!=null )
                        ai.setTypet.getName());
                    ai.setSetMethodsm.getName());
                }
                ai.setDescription("Introspected attribute " + name);
                if..isDebugEnabled()) ..debug("Introspected attribute " +
                        name + " " + gm + " " + sm);
                ifgm==null )
                    ai.setReadable(false);
                ifsm==null )
                    ai.setWriteable(false);
                ifsm!=null || gm!=null )
                    mbean.addAttribute(ai);
            }
            en=invokeAttMap.keys();
            whileen.hasMoreElements() ) {
                String name=(String)en.nextElement();
                Method m=(Method)invokeAttMap.get(name);
                ifm!=null && name != null ) {
                    OperationInfo op=new OperationInfo();
                    op.setName(name);
                    op.setReturnType(m.getReturnType().getName());
                    op.setDescription("Introspected operation " + name);
                    Class parms[]=m.getParameterTypes();
                    for(int i=0; i<parms.lengthi++ ) {
                        ParameterInfo pi=new ParameterInfo();
                        pi.setType(parms[i].getName());
                        pi.setName"param" + i);
                        pi.setDescription("Introspected parameter param" + i);
                        op.addParameter(pi);
                    }
                    mbean.addOperation(op);
                } else {
                    ..debug("Null arg " + name + " " + m );
                }
            }
            /*Constructor[] constructors = realClass.getConstructors();
            for(int i=0;i<constructors.length;i++) {
                ConstructorInfo info = new ConstructorInfo();
                String className = realClass.getName();
                int nIndex = -1;
                if((nIndex = className.lastIndexOf('.'))!=-1) {
                    className = className.substring(nIndex+1);
                }
                info.setName(className);
                info.setDescription(constructors[i].getName());
                Class classes[] = constructors[i].getParameterTypes();
                for(int j=0;j<classes.length;j++) {
                    ParameterInfo pi = new ParameterInfo();
                    pi.setType(classes[j].getName());
                    pi.setName("param" + j);
                    pi.setDescription("Introspected parameter param" + j);
                    info.addParameter(pi);
                }
                mbean.addConstructor(info);
            }
            */
            
            if..isDebugEnabled())
                ..debug("Setting name: " + type );
            mbean.setNametype );
            return mbean;
        } catchException ex ) {
            return null;
        }
    }
    // -------------------- Utils --------------------
    
Converts the first character of the given String into lower-case.

Parameters:
name The string to convert
Returns:
String
    private static String unCapitalize(String name) {
        if (name == null || name.length() == 0) {
            return name;
        }
        char chars[] = name.toCharArray();
        chars[0] = Character.toLowerCase(chars[0]);
        return new String(chars);
    }
// End of class: MbeanDescriptorsIntrospectionSource
New to GrepCode? Check out our FAQ X