KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > configbean > Utils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.sun.share.configbean;
20
21 import java.awt.event.ItemEvent JavaDoc;
22 import java.lang.reflect.Array JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.ResourceBundle JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.net.URLClassLoader JavaDoc;
28 import java.text.MessageFormat JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import javax.swing.SwingUtilities JavaDoc;
31
32 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
33
34 import org.openide.util.HelpCtx;
35 import org.openide.util.Lookup;
36 import org.netbeans.api.javahelp.Help;
37 import org.netbeans.modules.j2ee.sun.dd.api.CommonDDBean;
38 import org.openide.ErrorManager;
39
40
41 /**
42  *
43  * @author vkraemer
44  */

45 public class Utils implements org.netbeans.modules.j2ee.sun.share.Constants {
46     
47     /** Creates a new instance of Utils */
48     private Utils() {
49     }
50     
51     private static final String JavaDoc KEYSEPSTR = "|"; // NOI18N
52

53     private static ResourceBundle JavaDoc ubundle =
54         ResourceBundle.getBundle("org.netbeans.modules.j2ee.sun.share.configbean.Bundle"); // NOI18N
55

56     public static String JavaDoc getFQNKey(String JavaDoc uri, String JavaDoc fname) {
57         String JavaDoc key = "";
58         if (null != uri)
59             key += uri;
60         if (key.indexOf(KEYSEPSTR)>-1)
61             throw new IllegalArgumentException JavaDoc("uri");
62         key += KEYSEPSTR + fname;
63         assert !key.equals(KEYSEPSTR);
64         return key;
65     }
66     
67     public static String JavaDoc getUriFromKey(String JavaDoc key) {
68         String JavaDoc uri = "";
69         int sepLocation = key.indexOf(KEYSEPSTR);
70         if (sepLocation > 1)
71             uri = key.substring(0,sepLocation);
72         return uri;
73     }
74     
75     public static String JavaDoc getFilenameFromKey(String JavaDoc key) {
76         int sepLocation = key.indexOf(KEYSEPSTR);
77         //if (sepLocation > 1)
78
//uri = key.substring(0,sepLocation);
79
String JavaDoc fname = key.substring(sepLocation+1);
80         return fname;
81     }
82     
83     public static boolean notEmpty(String JavaDoc testedString) {
84         return (testedString != null) && (testedString.length() > 0);
85     }
86     
87     public static boolean strEmpty(String JavaDoc testedString) {
88         return testedString == null || testedString.length() == 0;
89     }
90     
91     public static boolean strEquals(String JavaDoc one, String JavaDoc two) {
92         boolean result = false;
93         
94         if(one == null) {
95             result = (two == null);
96         } else {
97             if(two == null) {
98                 result = false;
99             } else {
100                 result = one.equals(two);
101             }
102         }
103         return result;
104     }
105     
106     public static boolean strEquivalent(String JavaDoc one, String JavaDoc two) {
107         boolean result = false;
108         
109         if(strEmpty(one) && strEmpty(two)) {
110             result = true;
111         } else if(one != null && two != null) {
112             result = one.equals(two);
113         }
114         
115         return result;
116     }
117     
118     public static int strCompareTo(String JavaDoc one, String JavaDoc two) {
119         int result;
120         
121         if(one == null) {
122             if(two == null) {
123                 result = 0;
124             } else {
125                 result = -1;
126             }
127         } else {
128             if(two == null) {
129                 result = 1;
130             } else {
131                 result = one.compareTo(two);
132             }
133         }
134         
135         return result;
136     }
137     
138
139     public static boolean hasTrailingSlash(String JavaDoc path) {
140         return (path.charAt(path.length()-1) == '/');
141     }
142     
143     public static boolean containsWhitespace(String JavaDoc data) {
144         boolean result = false;
145         
146         if(notEmpty(data)) {
147             for(int i = 0, datalength = data.length(); i < datalength; i++) {
148                 if(Character.isSpaceChar(data.charAt(i))) {
149                     result = true;
150                     break;
151                 }
152             }
153         }
154         
155         return result;
156     }
157     
158     public static boolean isJavaIdentifier(final String JavaDoc id) {
159         boolean result = true;
160         
161         if(!notEmpty(id) || !Character.isJavaIdentifierStart(id.charAt(0))) {
162             result = false;
163         } else {
164             for(int i = 1, idlength = id.length(); i < idlength; i++) {
165                 if(!Character.isJavaIdentifierPart(id.charAt(i))) {
166                     result = false;
167                     break;
168                 }
169             }
170         }
171         
172         return result;
173     }
174
175     public static boolean isJavaPackage(final String JavaDoc pkg) {
176         boolean result = false;
177         
178         if(notEmpty(pkg)) {
179             int state = 0;
180             for(int i = 0, pkglength = pkg.length(); i < pkglength && state < 2; i++) {
181                 switch(state) {
182                 case 0:
183                     if(Character.isJavaIdentifierStart(pkg.charAt(i))) {
184                         state = 1;
185                     } else {
186                         state = 2;
187                     }
188                     break;
189                 case 1:
190                     if(pkg.charAt(i) == '.') {
191                         state = 0;
192                     } else if(!Character.isJavaIdentifierPart(pkg.charAt(i))) {
193                         state = 2;
194                     }
195                     break;
196                 }
197             }
198             
199             if(state == 1) {
200                 result = true;
201             }
202         }
203         
204         return result;
205     }
206     
207     public static boolean isJavaClass(final String JavaDoc cls) {
208         return isJavaPackage(cls);
209     }
210
211     public static CommonDDBean [] listToArray(List JavaDoc list, Class JavaDoc targetClass) {
212         CommonDDBean [] result = null;
213         if(list != null) {
214             int size = list.size();
215             if(size != 0) {
216                 result = (CommonDDBean []) Array.newInstance(targetClass, size);
217                 for(int i = 0; i < size; i++) {
218                     CommonDDBean property = (CommonDDBean) list.get(i);
219                     result[i] = (CommonDDBean) property.clone();
220                 }
221             }
222         }
223         return result;
224     }
225
226     public static CommonDDBean [] listToArray(List JavaDoc list, Class JavaDoc targetClass, String JavaDoc newVersion) {
227         CommonDDBean [] result = null;
228         if(list != null) {
229             int size = list.size();
230             if(size != 0) {
231                 result = (CommonDDBean []) Array.newInstance(targetClass, size);
232                 for(int i = 0; i < size; i++) {
233                     CommonDDBean property = (CommonDDBean) list.get(i);
234                     result[i] = (CommonDDBean) property.cloneVersion(newVersion);
235                 }
236             }
237         }
238         return result;
239     }
240
241     public static List JavaDoc arrayToList(CommonDDBean[] beans) {
242         List JavaDoc result = null;
243
244         if(beans != null && beans.length > 0) {
245             result = new ArrayList JavaDoc(beans.length+3);
246             for(int i = 0; i < beans.length; i++) {
247                 result.add(beans[i]);
248             }
249         }
250
251         return result;
252     }
253
254     private static final String JavaDoc [] booleanStrings = {
255         "0", "1", // NOI18N
256
"false", "true", // NOI18N
257
"no", "yes", // NOI18N
258
"off", "on" // NOI18N
259
};
260
261     public static boolean booleanValueOf(String JavaDoc val) {
262         boolean result = false;
263         int valueIndex = -1;
264
265         if(val != null && val.length() > 0) {
266             val = val.trim();
267             for(int i = 0; i < booleanStrings.length; i++) {
268                 if(val.compareToIgnoreCase(booleanStrings[i]) == 0) {
269                     valueIndex = i;
270                     break;
271                 }
272             }
273         }
274
275         if(valueIndex >= 0) {
276             if(valueIndex%2 == 1) {
277                 result = true;
278             }
279         }
280
281         return result;
282     }
283     
284     public static String JavaDoc encodeUrlField(String JavaDoc url) {
285         String JavaDoc encodedUrl = url;
286         
287         // Change spaces to underscores - this step might be redundant now, considering
288
// the UTF8 encoding being done now.
289
if(encodedUrl != null) {
290             encodedUrl = encodedUrl.replace (' ', '_'); //NOI18N
291
}
292         
293         // For each url element, do UTF encoding of that element.
294
if(encodedUrl != null) { // see bug 56280
295
try {
296                 StringBuffer JavaDoc result = new StringBuffer JavaDoc(encodedUrl.length() + 10);
297                 String JavaDoc s[] = encodedUrl.split("/"); // NOI18N
298
for(int i = 0; i < s.length; i++) {
299                     result.append(java.net.URLEncoder.encode(s[i], "UTF-8")); // NOI18N
300
if(i != s.length - 1) {
301                         result.append("/"); // NOI18N
302
}
303                 }
304                 encodedUrl = result.toString();
305             } catch (Exception JavaDoc ex){
306                 // log this
307
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
308             }
309         }
310         
311         return encodedUrl;
312     }
313     
314     public static URL JavaDoc getResourceURL(String JavaDoc resource, Class JavaDoc relatedClass) {
315         URL JavaDoc result = null;
316         ClassLoader JavaDoc classLoader = relatedClass.getClassLoader();
317
318         if(classLoader instanceof java.net.URLClassLoader JavaDoc) {
319             URLClassLoader JavaDoc urlClassLoader = (java.net.URLClassLoader JavaDoc) classLoader;
320             result = urlClassLoader.findResource(resource);
321         }
322         else {
323             result = classLoader.getResource(resource);
324         }
325
326         return result;
327     }
328
329     public static void invokeHelp(String JavaDoc helpId) {
330         invokeHelp(new HelpCtx(helpId));
331     }
332     
333     public static void invokeHelp(final HelpCtx helpCtx) {
334         SwingUtilities.invokeLater(new Runnable JavaDoc() {
335             public void run() {
336                 ((Help) Lookup.getDefault().lookup(Help.class)).showHelp(helpCtx);
337             }
338         });
339     }
340
341     public static boolean interpretCheckboxState(ItemEvent JavaDoc e) {
342         boolean state = false;
343         
344         if(e.getStateChange() == ItemEvent.SELECTED) {
345             state = true;
346         } else if(e.getStateChange() == ItemEvent.DESELECTED) {
347             state = false;
348         }
349         
350         return state;
351     }
352     
353     /** Select an appropriate default value for a cmp-resource
354      * @reurns the value to place in the jndi-name element
355      */

356     static String JavaDoc getDefaultCmpResourceJndiName(EjbJarRoot jarDCB) {
357         return "jdo/pmf";
358     }
359            
360     static ConfigurationException JavaDoc makeCE(String JavaDoc messageKey, Object JavaDoc[] params, Throwable JavaDoc cause) {
361         String JavaDoc format = null;
362         boolean poorFormat = false;
363         try {
364             format = ubundle.getString(messageKey);
365         }
366         catch (RuntimeException JavaDoc re) {
367             poorFormat = true;
368             jsr88Logger.throwing(Utils.class.getName(), "makeCE", re);
369             format = ubundle.getString("DEF_ConfigurationExceptionFormat");
370             int len = 1;
371             if (null != params)
372                 len += params.length;
373             Object JavaDoc tparams[] = new Object JavaDoc[len];
374             tparams[0] = messageKey;
375             for (int i = 1; i < len; i++) {
376                 tparams[i] = params[i-1];
377             }
378             params = tparams;
379         }
380             
381         String JavaDoc message = MessageFormat.format(format, params);
382         ConfigurationException JavaDoc retVal = new ConfigurationException JavaDoc(message);
383         if (null != cause) {
384             retVal.initCause(cause);
385         }
386         if (poorFormat)
387             jsr88Logger.severe(message);
388         return retVal;
389     }
390     
391     /** This method walks the DCB tree from the root of the DConfigBean tree looking
392      * for the service ref of the specified name. Used by MessageSecurityProviderImpl.
393      *
394      * Optimize later... there is probably a nice OOP way to do this. I didn't
395      * want to build the capability into the DCB tree directly because it's a bit
396      * specialized, and performing this search requires access to getChildren()
397      * which is package protected and probably should not be public (although getParent()
398      * is public, as required by JSR-88 -- go figure).
399      */

400     public static ServiceRef findServiceRef(SunONEDeploymentConfiguration config, String JavaDoc serviceRefName) {
401         ServiceRef result = null;
402         BaseRoot rootDCB = config.getMasterDCBRoot();
403         
404         if(rootDCB instanceof EjbJarRoot) {
405             Iterator JavaDoc childIter = rootDCB.getChildren().iterator();
406             while(childIter.hasNext() && result == null) {
407                Object JavaDoc child = childIter.next();
408                if(child instanceof BaseEjb) {
409                    Iterator JavaDoc subChildIter = ((BaseEjb) child).getChildren().iterator();
410                    while(subChildIter.hasNext() && result == null) {
411                        Object JavaDoc subChild = subChildIter.next();
412                        if(subChild instanceof ServiceRef) {
413                            ServiceRef serviceRef = (ServiceRef) subChild;
414                            if(serviceRefName.equals(serviceRef.getServiceRefName())) {
415                                result = serviceRef;
416                            }
417                        }
418                    }
419                }
420            }
421         } else {
422             Iterator JavaDoc childIter = rootDCB.getChildren().iterator();
423             while(childIter.hasNext()) {
424                 Object JavaDoc child = childIter.next();
425                 if(child instanceof ServiceRef) {
426                     ServiceRef serviceRef = (ServiceRef) child;
427                     if(serviceRefName.equals(serviceRef.getServiceRefName())) {
428                         result = serviceRef;
429                         break;
430                     }
431                 }
432             }
433         }
434         
435         return result;
436     }
437 }
438
Popular Tags