1 23 24 28 29 41 42 package com.sun.enterprise.admin.util.jmx; 43 import java.util.List ; 44 import java.util.Iterator ; 45 import java.util.regex.Matcher ; 46 import java.util.regex.Pattern ; 47 import java.io.File ; 48 import java.io.OutputStream ; 49 import java.io.InputStream ; 50 import java.io.BufferedInputStream ; 51 import java.io.FileInputStream ; 52 import com.sun.org.apache.commons.modeler.Registry; 53 import com.sun.org.apache.commons.modeler.ManagedBean; 54 import com.sun.org.apache.commons.modeler.FieldInfo; 55 56 import com.sun.enterprise.admin.util.jmx.AttributeListUtils; 57 62 public class ObjectNamesSourceGenerator { 63 64 private final File regFile; 65 private final OutputStream out; 66 private final Registry reg; 67 private String pkgName; 68 private String className; 69 private final static String PARAM = "param"; 70 private final static char ONDELIMITER = ':'; 71 private final static String ANY_GETTER = "getConfigBeanObjectName"; 72 private final static String DEFAULT_PACKAGE_NAME = null; 73 private final static String DEFAULT_CLASS_NAME = "ConfigMBeanNames"; 74 public ObjectNamesSourceGenerator(final File regFile, final OutputStream out) throws Exception { 75 if (regFile == null || !regFile.canRead() || out == null) { 76 throw new IllegalArgumentException ("Either the registry file is unreadable or stream could not be written to"); 77 } 78 this.regFile = regFile; 79 this.out = out; 80 this.pkgName = DEFAULT_PACKAGE_NAME; 81 this.className = DEFAULT_CLASS_NAME; 82 this.reg = new Registry(); 83 final InputStream stream = new BufferedInputStream (new FileInputStream (regFile)); 85 reg.loadDescriptors("MbeansDescriptorsDOMSource", stream, null); 86 } 87 private void setClassName(final String c) { 88 this.className = c; 89 } 90 private void setPackageName(final String p) { 91 this.pkgName = p; 92 } 93 94 public String asString() throws Exception { 95 final String [] s = reg.findManagedBeans(); 96 final StringBuffer sb = new StringBuffer (); 97 sb.append(getClassPreamble()); 98 for (int i = 0 ; i < s.length ; i ++) { 99 sb.append(mb2s(s[i])); 100 } 101 sb.append('\n' + "}"); 102 return ( sb.toString() ); 103 } 104 105 public void write() throws Exception { 106 out.write(this.asString().getBytes()); 107 } 108 private String getClassPreamble() { 109 final StringBuffer sb = new StringBuffer (); 110 sb.append(getPackageDeclaration()); 111 sb.append(getImports()); 112 sb.append(getClassJavadoc()); 113 return ( sb.toString() ); 114 } 115 private String getPackageDeclaration() { 116 final StringBuffer sb = new StringBuffer (); 117 sb.append("package TBD...").append('\n'); 118 sb.append('\n'); 119 return ( sb.toString() ); 120 } 121 private String getClassJavadoc() { 122 final StringBuffer sb = new StringBuffer ("/** An Auto-generated MBean ObjectName Factory */"); 123 sb.append('\n'); 124 sb.append("public class ConfigMBeanNames {"); 125 sb.append('\n'); 126 return ( sb.toString() ); 127 } 128 private String getImports() { 129 return ("import javax.management.ObjectName;" + '\n'); 130 } 131 private String mb2s(final String name) { 132 final ManagedBean mb = reg.findManagedBean(name); 133 final StringBuffer sb = new StringBuffer (); 134 final String method = getMethodSignature(mb); 135 sb.append(method).append('\n'); 136 sb.append(getMethodBody(mb)).append('\n'); 137 return ( sb.toString() ); 139 } 140 private String getMethodSignature(final ManagedBean mb) { 141 final StringBuffer sb = new StringBuffer ("public static final ObjectName "); 142 sb.append("get"); 143 final String humped = AttributeListUtils.dash2CamelCase(mb.getName()); 144 sb.append(getValidJavaMethodName(humped)); 145 sb.append("ObjectName("); 146 final String onv = getObjectNameValue(mb); 147 sb.append(getParameterList(onv)); 148 sb.append(" )"); 149 return ( sb.toString() ); 150 } 151 private String getMethodBody(final ManagedBean mb) { 152 final String onv = getObjectNameValue(mb); 153 final String params = getParameterList(onv); 154 final String nodm = onv.substring(onv.indexOf(ONDELIMITER) + 1); 155 final String ong = positionalReplace(nodm); 158 final String ret = getReturnStatement(ong); 159 final StringBuffer sb = new StringBuffer ("{").append('\n').append(ret).append('\n').append("}"); 160 return ( sb.toString() ); 161 } 162 private String positionalReplace(final String tk) { 163 final String rs = Tok2Params.replacePositionalTokens(tk); 164 return ( rs ); 165 } 166 private String getReturnStatement(final String s) { 167 final StringBuffer sb = new StringBuffer (); 168 final String qs = "\"" + s + "\""; 169 sb.append("return ( ").append(ANY_GETTER).append("( ").append(qs).append(" )").append(" )"); 170 return ( sb.toString() ); 171 } 172 private String getObjectNameValue(final ManagedBean mb) { 173 final List fields = mb.getFields(); 174 final Iterator iter = fields.iterator(); 175 String val = null; 176 while (iter.hasNext()) { 177 final FieldInfo f = (FieldInfo)iter.next(); 178 if ("ObjectName".equals(f.getName())) { 179 val = (String ) f.getValue(); 180 } 181 } 182 return ( val ); 183 } 184 private String getValidJavaMethodName(final String s) { 185 186 String n = s.replace('#', '_'); 187 return ( n ); 188 } 189 private String getParameterList(final String on) { 190 return ( Tok2Params.convert2ParamList(on) ); 191 } 192 public static void main(final String [] args) { 193 try { 194 final File r = new File ("/export/home/kedar/ee/LATEST/admin-core/admin/dtds/admin-mbeans-descriptors.xml"); 195 final OutputStream op = System.out; 196 final ObjectNamesSourceGenerator o = new ObjectNamesSourceGenerator(r, op); 197 final String s = o.asString(); 198 op.write(s.getBytes()); 199 System.out.println("Done"); 200 System.out.println("The length of Java source file would be, Characters: " + s.length()); 201 } 202 catch (final Throwable e) { 203 e.printStackTrace(); 204 } 205 } 206 207 private static class Tok2Params { 208 private final static String REGEX1 = "a*\\{\\d\\}a*"; 209 private static String convert2ParamList(final String s) { 210 final Pattern p = Pattern.compile(REGEX1); 211 final Matcher m = p.matcher(s); 212 int n = 0; 213 while (m.find()) { 214 n++; 215 } 216 return ( getStringParamList(n-1) ) ; 217 } 218 private static String getStringParamList(final int n) { 219 final StringBuffer sb = new StringBuffer (); 220 for (int i = 0 ; i < n ; i++) { 221 sb.append(" final String ").append(PARAM).append(i+1); 222 if (i != n-1) { 223 sb.append(','); 224 } 225 } 226 return ( sb.toString() ); 227 } 228 private static String replacePositionalTokens(final String ts) { 229 final Pattern p = Pattern.compile(REGEX1); 230 final Matcher m = p.matcher(ts); 231 final StringBuffer sb = new StringBuffer (); 232 while(m.find()) { 233 final String gr = m.group(); final char ch = gr.charAt(1); 235 final String replacement = PARAM + ch; 236 m.appendReplacement(sb, replacement); 237 } 238 m.appendTail(sb); 239 return ( sb.toString() ); 240 } 241 } 242 } 243 | Popular Tags |