KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > ConfigBeansFactory


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 package com.sun.enterprise.config;
25
26 import java.util.ArrayList JavaDoc;
27
28 import java.lang.reflect.Method JavaDoc;
29 import java.text.CharacterIterator JavaDoc;
30 import java.text.StringCharacterIterator JavaDoc;
31
32 import java.util.logging.Logger JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 //import com.sun.logging.LogDomains;
35

36
37 /**
38  * A factory to create ConfigContext objects.
39  */

40 public class ConfigBeansFactory {
41     
42     //private static final Logger _logger = LogDomains.getLogger(LogDomains.CONFIG_LOGGER);
43

44     /*
45      * will only work if xpath is of the form
46      * <PRE>
47      * expression := /tagName | /tagName/tagExpression
48      * tagExpression := tagName| tagName[name='value'] | tagName/tagExpression | tagName[name='value']/tagExpression
49      * <PRE>
50      */

51     
52     public static ConfigBean getConfigBeanByXPath(ConfigContext ctx, String JavaDoc xpath)
53                 throws ConfigException {
54          ConfigBean cb = ctx.getRootConfigBean();
55          
56          if(cb == null) throw new ConfigException("getConfigBeanByXPath: null context");
57          cb.setConfigContext(ctx);
58          return getConfigBeanByXPath(cb, xpath);
59     }
60     
61     
62     /**
63      * Utility method to get the bean name based on an xpath
64      *
65      */

66     public static String JavaDoc getConfigBeanNameByXPath(String JavaDoc xpath) {
67         XpathSupport xp = new XpathSupport(xpath);
68         ArrayList JavaDoc arr = xp.getClassNameArray();
69         
70         if(arr.size() == 0) return null;
71         return (String JavaDoc)arr.get(arr.size()-1);
72     }
73     
74     public static ConfigBean getConfigBeanByXPath(ConfigBean server, String JavaDoc xpath)
75                 throws ConfigException {
76         Object JavaDoc ret = server;
77         try {
78             XpathSupport xp = new XpathSupport(xpath);
79             ArrayList JavaDoc arr = xp.getMethodArray();
80             
81             if(arr.size() == 0 || arr.size() == 1) {
82                 ((ConfigBean) ret).setXPath(xpath); //fixed on 040102
83
return (ConfigBean) ret;
84             }
85             
86             for(int i=1;i<arr.size();i++) {
87                 MethodCaller methodCaller = (MethodCaller) arr.get(i);
88                 //_logger.log(Level.FINE,"Method = "+methodCaller);
89
Method JavaDoc m = ret.getClass().getMethod(methodCaller.getMethodName(), methodCaller.getParamClasses());
90                 //_logger.log(Level.FINE,"m=" + m);
91
ret = m.invoke(ret, methodCaller.getParams());
92             }
93             
94             ((ConfigBean) ret).setConfigContext(server.getConfigContext());
95             ((ConfigBean) ret).setXPath(xpath);
96             return (ConfigBean) ret;
97         } catch(Throwable JavaDoc t) {
98             //_logger.log(Level.INFO,"config.get_config_bean_xpath_exception",t);
99
}
100         return null;
101         
102     }
103     
104     private static class XpathSupport {
105         private String JavaDoc xpath;
106         private ArrayList JavaDoc arr = new ArrayList JavaDoc();
107         private ArrayList JavaDoc nameArr = new ArrayList JavaDoc();
108         
109         XpathSupport(String JavaDoc xpath) {
110             this.xpath = xpath;
111             process();
112         }
113         ArrayList JavaDoc getMethodArray() {
114             return arr;
115         }
116         ArrayList JavaDoc getClassNameArray() {
117             return nameArr;
118         }
119
120         private static char SEPARATOR_CHAR = '/';
121         private static char OPENBRACKET_CHAR = '[';
122         private static char CLOSEBRACKET_CHAR = ']';
123         private static char ESCAPE_CHAR = '\\';
124         
125         private static String JavaDoc[] getListOfNodes(String JavaDoc xpath) {
126             // String[] ret = new String[];
127
ArrayList JavaDoc arr = new ArrayList JavaDoc();
128             char ch;
129             boolean insideBracket = false;
130             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
131             
132             if (xpath == null)
133                 return new String JavaDoc[]{};
134             
135             for(int i=0;i<xpath.length();i++) {
136                 ch = xpath.charAt(i);
137                 if(ch == SEPARATOR_CHAR && !insideBracket) {
138                     if(sb.length() > 0) {
139                         arr.add(sb.toString());
140                         sb = new StringBuffer JavaDoc();
141                     }
142                 } else if(ch == SEPARATOR_CHAR && insideBracket) {
143                     sb.append(ch);
144                 } else if(ch == OPENBRACKET_CHAR) {
145                     sb.append(ch);
146                     insideBracket = true;
147                 } else if (ch == CLOSEBRACKET_CHAR) {
148                     sb.append(ch);
149                     insideBracket = false;
150                 } else {
151                        sb.append(ch);
152                 }
153             }
154                 if(sb.length() >0)
155                     arr.add(sb.toString());
156             
157                 // TEMP FIX
158
String JavaDoc[] ret = new String JavaDoc[arr.size()];
159                 for(int j=0;j<arr.size();j++) {
160                     ret[j] = (String JavaDoc) arr.get(j);
161                 }
162                     
163             return ret;
164         }
165          
166         private static String JavaDoc getTagName(String JavaDoc node) {
167             int n = node.indexOf('[');
168             if(n == -1) n = node.length();
169             String JavaDoc tag = node.substring(0, n);
170             //_logger.log(Level.FINE,"Tag"+tag);
171
return tag;
172         }
173         private static String JavaDoc getParamName(String JavaDoc node) {
174             int n = node.indexOf('[');
175             if(n == -1) return null;
176             String JavaDoc p = node.substring(n+2);
177             int eq = p.indexOf("=");
178             String JavaDoc ret = p.substring(0, eq);
179             //_logger.log(Level.FINE,"p="+ret);
180
return ret;
181         }
182         private static String JavaDoc getParamValue(String JavaDoc node) {
183             int n = node.indexOf('[');
184             if(n == -1) return null;
185             String JavaDoc p = node.substring(n+2);
186             int eq = p.indexOf("=");
187             String JavaDoc ret = p.substring(eq+2, p.length()-2);
188             //_logger.log(Level.FINE,"v="+ret);
189
return ret;
190         }
191         void process() {
192             // separate the xpaths and put it in mc
193
String JavaDoc[] n = XpathSupport.getListOfNodes(xpath);
194             //StringTokenizer st = new StringTokenizer(xpath, "/");
195
//while (st.hasMoreTokens()) {
196
// String node = st.nextToken();
197
for(int i = 0;i<n.length;i++) {
198                 String JavaDoc node = n[i];
199                 MethodCaller mc = new MethodCaller();
200                 String JavaDoc tagName = XpathSupport.getTagName(node);
201                 String JavaDoc methodName = "get" + convertName(tagName);
202                 
203                 String JavaDoc paramName = XpathSupport.getParamName(node);
204                 String JavaDoc paramValue = XpathSupport.getParamValue(node);
205                 
206                 if(paramName!=null && !paramName.equals("")) {
207                     methodName += "By" + convertName(paramName);
208                     mc.setMethodName(methodName);
209                     mc.setParamClasses(new Class JavaDoc[] {String JavaDoc.class});
210                     mc.setParams(new Object JavaDoc[] {paramValue});
211                 } else {
212                     mc.setMethodName(methodName);
213                     mc.setParamClasses(new Class JavaDoc[] {});
214                     mc.setParams(new Object JavaDoc[] {});
215                 }
216                 arr.add(mc);
217                 nameArr.add(convertName(tagName));
218             }
219         }
220     }
221     private static class MethodCaller {
222         private String JavaDoc methodName;
223         private Class JavaDoc[] c;
224         private Object JavaDoc[] o;
225         
226         public String JavaDoc toString() {
227             return "MethodCaller: " + methodName + ",o=" + o;
228         }
229         void setMethodName(String JavaDoc m) {
230             methodName = m;
231         }
232         String JavaDoc getMethodName(){
233             return methodName;
234         }
235         void setParamClasses(Class JavaDoc[] cl) {
236             c = cl;
237         }
238         Class JavaDoc[] getParamClasses() {
239             return c;
240         }
241         void setParams(Object JavaDoc[] ob) {
242             o=ob;
243         }
244         Object JavaDoc[] getParams() {
245             return o;
246         }
247     }
248     /**
249      * Convert a DTD name into a bean name:
250      *
251      * Any - or _ character is removed. The letter following - and _
252      * is changed to be upper-case.
253      * If the user mixes upper-case and lower-case, the case is not
254      * changed.
255      * If the Word is entirely in upper-case, the word is changed to
256      * lower-case (except the characters following - and _)
257      * The first letter is always upper-case.
258      */

259     private static String JavaDoc convertName(String JavaDoc name) {
260         CharacterIterator JavaDoc ci;
261         StringBuffer JavaDoc n = new StringBuffer JavaDoc();
262         boolean up = true;
263         boolean keepCase = false;
264         char c;
265         
266         // hack need for schema2beans limitation.
267
if(name.equals("property")) {
268             name = "element-property";
269         }
270         
271         ci = new StringCharacterIterator JavaDoc(name);
272         c = ci.first();
273         
274         // If everything is uppercase, we'll lowercase the name.
275
while (c != CharacterIterator.DONE) {
276             if (Character.isLowerCase(c)) {
277                 keepCase = true;
278                 break;
279             }
280             c = ci.next();
281         }
282         
283         c = ci.first();
284         while (c != CharacterIterator.DONE) {
285             if (c == '-' || c == '_')
286                 up = true;
287             else {
288                 if (up)
289                     c = Character.toUpperCase(c);
290                 else
291                     if (!keepCase)
292                         c = Character.toLowerCase(c);
293                 n.append(c);
294                 up = false;
295             }
296             c = ci.next();
297         }
298         return n.toString();
299     }
300 }
301
Popular Tags