FileDocCategorySizeDatePackage
ObjectNamesSourceGenerator.javaAPI DocGlassfish v2 API10562Fri May 04 22:25:58 BST 2007com.sun.enterprise.admin.util.jmx

ObjectNamesSourceGenerator

public class ObjectNamesSourceGenerator extends Object
author
Kedar Mhaswade
since
Appserver 8.1
version
$Revision: 1.4 $

Fields Summary
private final File
regFile
private final OutputStream
out
private final com.sun.org.apache.commons.modeler.Registry
reg
private String
pkgName
private String
className
private static final String
PARAM
private static final char
ONDELIMITER
private static final String
ANY_GETTER
private static final String
DEFAULT_PACKAGE_NAME
private static final String
DEFAULT_CLASS_NAME
Constructors Summary
public ObjectNamesSourceGenerator(File regFile, OutputStream out)

	         
        if (regFile == null || !regFile.canRead() || out == null) {
            throw new IllegalArgumentException("Either the registry file is unreadable or stream could not be written to");
        }
        this.regFile = regFile;
        this.out = out;
        this.pkgName = DEFAULT_PACKAGE_NAME;
        this.className = DEFAULT_CLASS_NAME;
        this.reg = new Registry();
        //reg.loadRegistry(new BufferedInputStream(new FileInputStream(regFile)));
        final InputStream stream = new BufferedInputStream(new FileInputStream(regFile));
        reg.loadDescriptors("MbeansDescriptorsDOMSource", stream, null);
	
Methods Summary
public java.lang.StringasString()

        final String[] s = reg.findManagedBeans();
        final StringBuffer sb = new StringBuffer();
        sb.append(getClassPreamble());
        for (int i = 0 ; i < s.length ; i ++) {
            sb.append(mb2s(s[i]));
        }
        sb.append('\n" + "}");
        return ( sb.toString() );
    
private java.lang.StringgetClassJavadoc()

        final StringBuffer sb = new StringBuffer("/** An Auto-generated MBean ObjectName Factory */");
        sb.append('\n");
        sb.append("public class ConfigMBeanNames  {");
        sb.append('\n");
        return ( sb.toString() );
    
private java.lang.StringgetClassPreamble()

        final StringBuffer sb = new StringBuffer();
        sb.append(getPackageDeclaration());
        sb.append(getImports());
        sb.append(getClassJavadoc());
        return ( sb.toString() );
    
private java.lang.StringgetImports()

        return ("import javax.management.ObjectName;" + '\n");
    
private java.lang.StringgetMethodBody(com.sun.org.apache.commons.modeler.ManagedBean mb)

        final String onv = getObjectNameValue(mb);
        final String params = getParameterList(onv);
        final String nodm = onv.substring(onv.indexOf(ONDELIMITER) + 1);
        //System.out.println(nodm);
        //System.out.println(params);
        final String ong = positionalReplace(nodm);
        final String ret = getReturnStatement(ong);
        final StringBuffer sb = new StringBuffer("{").append('\n").append(ret).append('\n").append("}");
        return ( sb.toString() );
    
private java.lang.StringgetMethodSignature(com.sun.org.apache.commons.modeler.ManagedBean mb)

        final StringBuffer sb = new StringBuffer("public static final ObjectName ");
        sb.append("get");
        final String humped = AttributeListUtils.dash2CamelCase(mb.getName());
        sb.append(getValidJavaMethodName(humped));
        sb.append("ObjectName(");
        final String onv = getObjectNameValue(mb);
        sb.append(getParameterList(onv));
        sb.append(" )");
        return ( sb.toString() );
    
private java.lang.StringgetObjectNameValue(com.sun.org.apache.commons.modeler.ManagedBean mb)

        final List fields = mb.getFields();
        final Iterator iter = fields.iterator();
        String val = null;
        while (iter.hasNext()) {
            final FieldInfo f = (FieldInfo)iter.next();
            if ("ObjectName".equals(f.getName())) {
                val = (String) f.getValue();
            }
        }
        return ( val );
    
private java.lang.StringgetPackageDeclaration()

        final StringBuffer sb = new StringBuffer();
        sb.append("package TBD...").append('\n");
        sb.append('\n");
        return ( sb.toString() );
    
private java.lang.StringgetParameterList(java.lang.String on)

        return ( Tok2Params.convert2ParamList(on) );
    
private java.lang.StringgetReturnStatement(java.lang.String s)

        final StringBuffer sb = new StringBuffer();
        final String qs = "\"" + s + "\"";
        sb.append("return ( ").append(ANY_GETTER).append("( ").append(qs).append(" )").append(" )");
        return ( sb.toString() );
    
private java.lang.StringgetValidJavaMethodName(java.lang.String s)

        /* Currently '#' is the only culprit */
        String n = s.replace('#", '_");
        return ( n );
    
public static voidmain(java.lang.String[] args)

        try {
            final File r = new File("/export/home/kedar/ee/LATEST/admin-core/admin/dtds/admin-mbeans-descriptors.xml");
            final OutputStream op = System.out;
            final ObjectNamesSourceGenerator o = new ObjectNamesSourceGenerator(r, op);
            final String s = o.asString();
            op.write(s.getBytes());
            System.out.println("Done");
            System.out.println("The length of Java source file would be, Characters: " + s.length());
        }
        catch (final Throwable e) {
            e.printStackTrace();
        }
    
private java.lang.Stringmb2s(java.lang.String name)

        final ManagedBean mb = reg.findManagedBean(name);
        final StringBuffer sb = new StringBuffer();
        final String method = getMethodSignature(mb);
        sb.append(method).append('\n");
        sb.append(getMethodBody(mb)).append('\n");
        //System.out.println(sb.toString());
        return ( sb.toString() );
    
private java.lang.StringpositionalReplace(java.lang.String tk)

        final String rs = Tok2Params.replacePositionalTokens(tk);
        return ( rs );
    
private voidsetClassName(java.lang.String c)

        this.className = c;
    
private voidsetPackageName(java.lang.String p)

        this.pkgName = p;
    
public voidwrite()

        out.write(this.asString().getBytes());