KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > util > jmx > ObjectNamesSourceGenerator


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28
29 /* ObjectNameGenerator.java
30  * Indentation Information:
31  * 0. Please (try to) preserve these settings.
32  * 1. Spaces are preferred over tabs.
33  * 2. In vi/vim -
34  * :set tabstop=4 :set shiftwidth=4 :set softtabstop=4
35  * :set list and make sure you do not introduce any ^Is.
36  * 3. In S1 Studio -
37  * 1. Tools->Options->Editor Settings->Java Editor->Tab Size = 4
38  * 2. Tools->Options->Indentation Engines->Java Indentation Engine->Expand Tabs to Spaces = True.
39  * 3. Tools->Options->Indentation Engines->Java Indentation Engine->Number of Spaces per Tab = 4.
40  */

41
42 package com.sun.enterprise.admin.util.jmx;
43 import java.util.List JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.regex.Matcher JavaDoc;
46 import java.util.regex.Pattern JavaDoc;
47 import java.io.File JavaDoc;
48 import java.io.OutputStream JavaDoc;
49 import java.io.InputStream JavaDoc;
50 import java.io.BufferedInputStream JavaDoc;
51 import java.io.FileInputStream JavaDoc;
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 /**
58  * @author Kedar Mhaswade
59  * @since Appserver 8.1
60  * @version $Revision: 1.3 $
61  */

62 public class ObjectNamesSourceGenerator {
63     
64     private final File JavaDoc regFile;
65     private final OutputStream JavaDoc out;
66     private final Registry reg;
67     private String JavaDoc pkgName;
68     private String JavaDoc className;
69     private final static String JavaDoc PARAM = "param";
70     private final static char ONDELIMITER = ':';
71     private final static String JavaDoc ANY_GETTER = "getConfigBeanObjectName";
72     private final static String JavaDoc DEFAULT_PACKAGE_NAME = null;
73     private final static String JavaDoc DEFAULT_CLASS_NAME = "ConfigMBeanNames";
74     public ObjectNamesSourceGenerator(final File JavaDoc regFile, final OutputStream JavaDoc out) throws Exception JavaDoc {
75         if (regFile == null || !regFile.canRead() || out == null) {
76             throw new IllegalArgumentException JavaDoc("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         //reg.loadRegistry(new BufferedInputStream(new FileInputStream(regFile)));
84
final InputStream JavaDoc stream = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(regFile));
85         reg.loadDescriptors("MbeansDescriptorsDOMSource", stream, null);
86     }
87     private void setClassName(final String JavaDoc c) {
88         this.className = c;
89     }
90     private void setPackageName(final String JavaDoc p) {
91         this.pkgName = p;
92     }
93     
94     public String JavaDoc asString() throws Exception JavaDoc {
95         final String JavaDoc[] s = reg.findManagedBeans();
96         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc {
106         out.write(this.asString().getBytes());
107     }
108     private String JavaDoc getClassPreamble() {
109         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
110         sb.append(getPackageDeclaration());
111         sb.append(getImports());
112         sb.append(getClassJavadoc());
113         return ( sb.toString() );
114     }
115     private String JavaDoc getPackageDeclaration() {
116         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
117         sb.append("package TBD...").append('\n');
118         sb.append('\n');
119         return ( sb.toString() );
120     }
121     private String JavaDoc getClassJavadoc() {
122         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc("/** 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 JavaDoc getImports() {
129         return ("import javax.management.ObjectName;" + '\n');
130     }
131     private String JavaDoc mb2s(final String JavaDoc name) {
132         final ManagedBean mb = reg.findManagedBean(name);
133         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
134         final String JavaDoc method = getMethodSignature(mb);
135         sb.append(method).append('\n');
136         sb.append(getMethodBody(mb)).append('\n');
137         //System.out.println(sb.toString());
138
return ( sb.toString() );
139     }
140     private String JavaDoc getMethodSignature(final ManagedBean mb) {
141         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc("public static final ObjectName ");
142         sb.append("get");
143         final String JavaDoc humped = AttributeListUtils.dash2CamelCase(mb.getName());
144         sb.append(getValidJavaMethodName(humped));
145         sb.append("ObjectName(");
146         final String JavaDoc onv = getObjectNameValue(mb);
147         sb.append(getParameterList(onv));
148         sb.append(" )");
149         return ( sb.toString() );
150     }
151     private String JavaDoc getMethodBody(final ManagedBean mb) {
152         final String JavaDoc onv = getObjectNameValue(mb);
153         final String JavaDoc params = getParameterList(onv);
154         final String JavaDoc nodm = onv.substring(onv.indexOf(ONDELIMITER) + 1);
155         //System.out.println(nodm);
156
//System.out.println(params);
157
final String JavaDoc ong = positionalReplace(nodm);
158         final String JavaDoc ret = getReturnStatement(ong);
159         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc("{").append('\n').append(ret).append('\n').append("}");
160         return ( sb.toString() );
161     }
162     private String JavaDoc positionalReplace(final String JavaDoc tk) {
163         final String JavaDoc rs = Tok2Params.replacePositionalTokens(tk);
164         return ( rs );
165     }
166     private String JavaDoc getReturnStatement(final String JavaDoc s) {
167         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
168         final String JavaDoc qs = "\"" + s + "\"";
169         sb.append("return ( ").append(ANY_GETTER).append("( ").append(qs).append(" )").append(" )");
170         return ( sb.toString() );
171     }
172     private String JavaDoc getObjectNameValue(final ManagedBean mb) {
173         final List JavaDoc fields = mb.getFields();
174         final Iterator JavaDoc iter = fields.iterator();
175         String JavaDoc val = null;
176         while (iter.hasNext()) {
177             final FieldInfo f = (FieldInfo)iter.next();
178             if ("ObjectName".equals(f.getName())) {
179                 val = (String JavaDoc) f.getValue();
180             }
181         }
182         return ( val );
183     }
184     private String JavaDoc getValidJavaMethodName(final String JavaDoc s) {
185         /* Currently '#' is the only culprit */
186         String JavaDoc n = s.replace('#', '_');
187         return ( n );
188     }
189     private String JavaDoc getParameterList(final String JavaDoc on) {
190         return ( Tok2Params.convert2ParamList(on) );
191     }
192     public static void main(final String JavaDoc[] args) {
193         try {
194             final File JavaDoc r = new File JavaDoc("/export/home/kedar/ee/LATEST/admin-core/admin/dtds/admin-mbeans-descriptors.xml");
195             final OutputStream JavaDoc op = System.out;
196             final ObjectNamesSourceGenerator o = new ObjectNamesSourceGenerator(r, op);
197             final String JavaDoc 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 JavaDoc e) {
203             e.printStackTrace();
204         }
205     }
206     
207     private static class Tok2Params {
208         private final static String JavaDoc REGEX1 = "a*\\{\\d\\}a*";
209         private static String JavaDoc convert2ParamList(final String JavaDoc s) {
210             final Pattern JavaDoc p = Pattern.compile(REGEX1);
211             final Matcher JavaDoc 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 JavaDoc getStringParamList(final int n) {
219             final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc replacePositionalTokens(final String JavaDoc ts) {
229             final Pattern JavaDoc p = Pattern.compile(REGEX1);
230             final Matcher JavaDoc m = p.matcher(ts);
231             final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
232             while(m.find()) {
233                 final String JavaDoc gr = m.group(); // this has to be of the form {d}
234
final char ch = gr.charAt(1);
235                 final String JavaDoc replacement = PARAM + ch;
236                 m.appendReplacement(sb, replacement);
237             }
238             m.appendTail(sb);
239             return ( sb.toString() );
240         }
241     }
242 }
243
Popular Tags