KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > util > PropertySupportFactory


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.util;
20
21 import java.beans.PropertyEditor JavaDoc;
22
23 import java.io.File JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Vector JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.ResourceBundle JavaDoc;
29
30 import javax.management.Attribute JavaDoc;
31 import javax.management.MBeanAttributeInfo JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33
34 import org.openide.nodes.PropertySupport;
35 import org.openide.execution.NbClassPath;
36
37 import org.netbeans.modules.j2ee.sun.ide.runtime.nodes.DomainRootNode;
38 import org.netbeans.modules.j2ee.sun.ide.runtime.nodes.ResourceLeafNode;
39 import org.netbeans.modules.j2ee.sun.bridge.apis.AppserverMgmtActiveNode;
40
41 import org.netbeans.modules.j2ee.sun.ide.editors.NameValuePair;
42 import org.netbeans.modules.j2ee.sun.ide.editors.NameValuePairsPropertyEditor;
43 import org.netbeans.modules.j2ee.sun.ide.editors.LogLevelEditor;
44
45 /**
46  *
47  */

48 public class PropertySupportFactory {
49     
50     private static Logger JavaDoc logger;
51     private static PropertySupportFactory factory;
52     private EnhancedPropertyEditorFactory editorFactory;
53     
54     private static final String JavaDoc ARRAY_DELIM = " , ";
55     
56     ResourceBundle JavaDoc bundle = ResourceBundle.getBundle("org/netbeans/modules/j2ee/sun/util/Bundle");
57     
58     /** Used in order to determine datatype map for editors **/
59     static java.util.Map JavaDoc typeToClassesMap = new java.util.HashMap JavaDoc();
60     
61     static {
62         typeToClassesMap.put("short", String JavaDoc.class);
63         typeToClassesMap.put("long", String JavaDoc.class);
64         typeToClassesMap.put("int", String JavaDoc.class);
65         typeToClassesMap.put("boolean", String JavaDoc.class); // b/c of enhanced editor
66
typeToClassesMap.put("float", String JavaDoc.class);
67         typeToClassesMap.put("double", String JavaDoc.class);
68         typeToClassesMap.put("byte", String JavaDoc.class);
69         typeToClassesMap.put("char", String JavaDoc.class);
70         typeToClassesMap.put(String JavaDoc[].class.getName(), String JavaDoc.class);
71         typeToClassesMap.put(Boolean JavaDoc.class.getName(), String JavaDoc.class);
72         typeToClassesMap.put(java.util.Map JavaDoc.class.getName(), String JavaDoc.class);
73     };
74     
75     static {
76         logger = Logger.getLogger("org.netbeans.modules.j2ee.sun");
77     }
78     
79     /**
80      * Creates a new instance of PropertySupportFactory
81      */

82     private PropertySupportFactory() {
83         editorFactory = EnhancedPropertyEditorFactory.getInstance();
84     }
85     
86     
87     /**
88      * Returns an instance of this factory.
89      *
90      * @return An instance of PropertySupportFactory.
91      */

92     public static PropertySupportFactory getInstance() {
93         if(factory == null) {
94             factory = new PropertySupportFactory();
95         }
96         return factory;
97     }
98     
99     
100     /**
101      * Returns the appropriate PropertySupport given the MBean Attribute and its
102      * MBeanAttributeInfo. First this method determines the type of the
103      * attribute and then whether or not it's writable. After that, the
104      * appropriate PropertySupport is created with a corresponding editor.
105      *
106      * @param An instance of AppserverMgmtActiveNode. This is necessary for us
107      * to create the anonymous PropertySupport class that calls the
108      * setProperty method implementation of a subclass of an instance
109      * of AppserverMgmtActiveNode. This is unfortunate because it forces
110      * a compile-time dependency on
111      * org.netbeans.modules.j2ee.sun.ide.runtime.nodes. This maybe
112      * eliminated by placing the logic of this factory in the
113      * abstract class definition for AppserverMgmtActiveNode but not
114      * advised since we want to extend editor support without affecting
115      * the node creation hierarchy. The nodes should be agnostic to the
116      * property creation and editor support for specific and general
117      * attributes.
118      * @param attr An MBean Attribute object containing the name/value.
119      * @param info The MBeanAttributeInfo for this Attribute.
120      * @return A PropertySupport for the attribute.
121      */

122     public PropertySupport getPropertySupport(
123             final AppserverMgmtActiveNode parent, final Attribute JavaDoc attr,
124             final MBeanAttributeInfo JavaDoc info) {
125         PropertySupport support = null;
126         String JavaDoc attrName = attr.getName();
127         if(Arrays.asList(PropertyConstants.CUSTOM_PROPERTIES).contains(attrName)){
128             support = getCustomPropertyEditors(parent, attr, info, attrName);
129         }else{
130             if(attr.getValue() instanceof String JavaDoc[]){
131                 String JavaDoc[] strArray = (String JavaDoc[])attr.getValue();
132                 if(info.isWritable())
133                     support = createStringArrayWritableProperty(parent, attr, info, strArray.getClass());
134                 else
135                     support = createStringArrayReadOnlyProperty(attr, info, strArray.getClass());
136                 support.setValue ("item.separator", " ");//NOI18N
137
}else{
138                 if(info.isWritable()) {
139                     support = createReadWritePropertySupport(parent, attr, info);
140                 } else {
141                     support = createReadOnlyPropertySupport(attr, info);
142                 }
143             }
144         }
145         return support;
146     }
147     
148     private PropertySupport getCustomPropertyEditors(final AppserverMgmtActiveNode parent, final Attribute JavaDoc attr,
149             final MBeanAttributeInfo JavaDoc info, String JavaDoc attrName){
150             PropertySupport support = null;
151             if(attrName.equals(PropertyConstants.PROPERTY_PAIRS_FIELD) && (!parent.getNodeType().equals(NodeTypes.JVM))
152                 && (parent instanceof ResourceLeafNode)){
153                 ResourceLeafNode resourceNode = (ResourceLeafNode)parent;
154                 support = createExtraProperties(resourceNode, attr, info);
155             }else{
156                 if(attrName.equals(PropertyConstants.DATASOURCE_TYPE_FIELD)){
157                     if(parent.getNodeType().equals(NodeTypes.CONNECTION_POOL))
158                         support = createWritablePropertySupportWithEditor(parent, attr, info, attrName);
159                     else
160                         support = createWritablePropertySupportWithoutEditor(parent, attr, info);
161                 }else
162                     support = createWritablePropertySupportWithEditor(parent, attr, info, attrName);
163             }
164             return support;
165     }
166     
167     /**
168      * Creates a read-only PropertySupport object out of a name/value pair.
169      * This is used to pass to a Sheet.Set object for display in a NetBeans
170      * properties sheet.
171      *
172      * @param name The name of the property to display.
173      * @param value The value of the property to display.
174      *
175      * @return The read-only PropertySupport object for the name/value pair.
176      */

177     private PropertySupport createReadOnlyPropertySupport(
178             final Attribute JavaDoc attr, final MBeanAttributeInfo JavaDoc info) {
179         String JavaDoc name = attr.getName();
180         return new PropertySupport.ReadOnly(name,
181                     getClassFromStringType(info.getType()), name, name) {
182                 public Object JavaDoc getValue() {
183                     return calculateReturnObjectType(attr);
184                 }
185         };
186     }
187     
188     
189      /**
190      * Creates a read-only PropertySupport object out of a name/value pair.
191      * This is used to pass to a Sheet.Set object for display in a NetBeans
192      * properties sheet.
193      *
194      * @param name The name of the property to display.
195      * @param value The value of the property to display.
196      *
197      * @return The read-only PropertySupport object for the name/value pair.
198      */

199     private PropertySupport createReadWritePropertySupport(
200             final AppserverMgmtActiveNode parent, final Attribute JavaDoc attr,
201             final MBeanAttributeInfo JavaDoc info) {
202         PropertySupport support = null;
203         if(attr.getValue() instanceof Boolean JavaDoc) {
204             support =
205                 createWritablePropertySupportWithEditor(parent, attr, info, "Boolean"); //NOI18N
206
} else {
207             if(Arrays.asList(PropertyConstants.JVM_STR_TO_ARR).contains(attr.getName())){
208                 if(parent.isServerLocal()){
209                      support = createNetBeansClassPathProperty(parent, attr, info);
210                 }else{
211                     String JavaDoc[] arrayNames = new String JavaDoc[]{};
212                     support = createModifiedStringArrayReadOnlyProperty(parent, attr, info, arrayNames.getClass());
213                 }
214             }else{
215                 support =
216                         createWritablePropertySupportWithoutEditor(parent, attr, info);
217             }
218         }
219         return support;
220     }
221     
222     
223     /**
224      *
225      *
226      */

227     private PropertySupport createWritablePropertySupportWithEditor(
228             final AppserverMgmtActiveNode parent, final Attribute JavaDoc attr,
229             final MBeanAttributeInfo JavaDoc info, final String JavaDoc customType) {
230         String JavaDoc name = attr.getName();
231         return new PropertySupport.ReadWrite(name,
232                     getClassFromStringType(info.getType()), name, name) {
233                 Attribute JavaDoc attribute = attr;
234                 public Object JavaDoc getValue() {
235                     Object JavaDoc obj = attribute.getValue();
236                     if(obj != null) {
237                         obj = obj.toString();
238                     }
239                     return obj;
240                 }
241                 
242                 public void setValue(Object JavaDoc obj) {
243                     attribute = revertAttribute(parent, getName(), obj, attribute);
244                 }
245                 
246                 public PropertyEditor JavaDoc getPropertyEditor() {
247                    return editorFactory.getEnhancedPropertyEditor(
248                         attribute.getValue(), customType);
249                 }
250             };
251     }
252     
253     
254     /**
255      *
256      *
257      */

258     private PropertySupport createWritablePropertySupportWithoutEditor(
259             final AppserverMgmtActiveNode parent, final Attribute JavaDoc attr,
260             final MBeanAttributeInfo JavaDoc info) {
261         String JavaDoc name = attr.getName();
262         return new PropertySupport.ReadWrite(name,
263                     getClassFromStringType(info.getType()), name, name) {
264                 Attribute JavaDoc attribute = attr;
265                 public Object JavaDoc getValue() {
266                     return calculateReturnObjectType(attribute);
267                 }
268                 
269                 public void setValue(Object JavaDoc obj) {
270                     attribute = revertAttribute(parent, getName(), obj, attribute);
271                 }
272             };
273     }
274     
275     
276     
277     /**
278      *
279      *
280      */

281     private Object JavaDoc calculateReturnObjectType(Attribute JavaDoc attr) {
282         Object JavaDoc obj = attr.getValue();
283         if(obj instanceof ObjectName JavaDoc[]) {
284             ObjectName JavaDoc[] objNames = (ObjectName JavaDoc[])obj;
285             String JavaDoc[] arrayNames = new String JavaDoc[objNames.length];
286             for(int i=0; i < objNames.length; i++){
287                 arrayNames[i] = objNames[i].toString();
288             }
289             return arrayNames;
290         } if(obj instanceof String JavaDoc[]) {
291             String JavaDoc[] values = (String JavaDoc[])obj;
292             StringBuffer JavaDoc returnVals = new StringBuffer JavaDoc();
293             int pos = 0;
294             for(int i=0; i < values.length; i++){
295                 returnVals.append(values[i]);
296                 pos++;
297                 if(pos < values.length) {
298                     returnVals.append(ARRAY_DELIM);
299                 }
300             }
301             return returnVals;
302         } else if(obj != null || !(obj instanceof String JavaDoc)) {
303             if(obj == null) {
304                 return "";
305             }
306             obj = obj.toString();
307             return obj;
308         } else {
309             return "";
310         }
311     }
312     
313     
314     /**
315      *
316      *
317      */

318     private static Class JavaDoc getClassFromStringType(final String JavaDoc type) {
319         Class JavaDoc clazz = null;
320         try {
321             clazz = (Class JavaDoc) typeToClassesMap.get(type);
322             if (clazz == null) {
323                 clazz = Class.forName(type);
324             }
325             if (clazz == null) {
326                 throw new ClassNotFoundException JavaDoc(type);
327             }
328             if (!String JavaDoc.class.isAssignableFrom(clazz)
329                     && ! Number JavaDoc.class.isAssignableFrom(clazz)) {
330                 throw new ClassNotFoundException JavaDoc(type);
331             }
332         } catch(ClassNotFoundException JavaDoc e) {
333             logger.log(Level.FINE, e.getMessage(), e);
334         }
335         return clazz;
336     }
337
338     PropertySupport createExtraProperties(final ResourceLeafNode parent, final Attribute JavaDoc attr, final MBeanAttributeInfo JavaDoc info) {
339         return new PropertySupport.ReadWrite(
340         info.getName(),
341         NameValuePairsPropertyEditor.class,
342         bundle.getString("LBL_ExtParams"), //NOI18N
343
bundle.getString("DSC_ExtParams")) { //NOI18N
344
Attribute JavaDoc attribute = attr;
345             public Object JavaDoc getValue() {
346                 return attribute.getValue();
347             }
348               
349             public void setValue(Object JavaDoc obj) {
350                 if(obj instanceof Object JavaDoc[]){
351                     //Map containing previous set of properties
352
java.util.Map JavaDoc attributeMap = (java.util.Map JavaDoc)attribute.getValue();
353                                                             
354                     //Create a an array of updated properties
355
Object JavaDoc[] currentVal = (Object JavaDoc[])obj;
356                     NameValuePair[] pairs = getNameValuePairs(currentVal);
357                     java.util.Map JavaDoc propertyList = new java.util.HashMap JavaDoc();
358                     Object JavaDoc[] props = new Object JavaDoc[pairs.length];
359                     for(int i=0; i<pairs.length; i++){
360                         Attribute JavaDoc attr = new Attribute JavaDoc(pairs[i].getParamName(), pairs[i].getParamValue());
361                         props[i] = attr;
362                         propertyList.put(pairs[i].getParamName(), pairs[i].getParamValue());
363                     }
364                     parent.updateExtraProperty(props, attributeMap);
365                     
366                     //Required to do this set to update UI
367
attribute = new Attribute JavaDoc(getName(), propertyList);
368                 }
369             }
370
371             public PropertyEditor JavaDoc getPropertyEditor(){
372                 return new NameValuePairsPropertyEditor(attribute.getValue());
373             }
374             
375             
376         };
377    }//createExtraProperties
378

379     PropertySupport createStringArrayReadOnlyProperty(final Attribute JavaDoc a, final MBeanAttributeInfo JavaDoc attr, final Class JavaDoc type) {
380         return new PropertySupport.ReadOnly(
381         attr.getName(),
382         type,
383         attr.getName(),
384         attr.getName()) {
385             Attribute JavaDoc attribute = a;
386             public Object JavaDoc getValue() {
387                 Object JavaDoc val[] = (Object JavaDoc[])attribute.getValue();
388                 if (attribute.getValue() instanceof ObjectName JavaDoc[]){
389                     ObjectName JavaDoc[] objNames = (ObjectName JavaDoc[])val;
390                     String JavaDoc[] arrayNames = new String JavaDoc[objNames.length];
391                     for(int i=0; i<objNames.length; i++){
392                         arrayNames[i] = objNames[i].toString();
393                     }
394                     return (Object JavaDoc)arrayNames;
395                 }else{
396                     return attribute.getValue();
397                 }
398             }
399         };
400     }//createStringArrayReadOnlyProperty
401

402     PropertySupport createStringArrayWritableProperty(final AppserverMgmtActiveNode parent, final Attribute JavaDoc attr, final MBeanAttributeInfo JavaDoc info, final Class JavaDoc type) {
403         return new PropertySupport.ReadWrite(
404         info.getName(),
405         type,
406         info.getName(),
407         info.getName()) {
408             Attribute JavaDoc attribute = attr;
409             public Object JavaDoc getValue() {
410                 Object JavaDoc val[] = (Object JavaDoc[])attribute.getValue();
411                 if (attribute.getValue() instanceof ObjectName JavaDoc[]){
412                     ObjectName JavaDoc[] objNames = (ObjectName JavaDoc[])val;
413                     String JavaDoc[] arrayNames = new String JavaDoc[objNames.length];
414                     for(int i=0; i<objNames.length; i++){
415                         arrayNames[i] = objNames[i].toString();
416                     }
417                     return (Object JavaDoc)arrayNames;
418                 }else{
419                     String JavaDoc[] values = (String JavaDoc[])attribute.getValue();
420                     return values;
421                 }
422             }
423             public void setValue(Object JavaDoc obj) {
424                 attribute = revertAttribute(parent, getName(), obj, attribute);
425             }
426         };
427     }//createStringArrayWritableProperty
428

429     PropertySupport createNetBeansClassPathProperty(final AppserverMgmtActiveNode parent, final Attribute JavaDoc attr, final MBeanAttributeInfo JavaDoc info) {
430         return new PropertySupport.ReadWrite(
431                 info.getName(),
432                 NbClassPath.class,
433                 info.getName(),
434                 info.getName()) {
435             Attribute JavaDoc attribute = attr;
436             public Object JavaDoc getValue() {
437                 if(attribute.getValue() != null){
438                     String JavaDoc val = replacePathSeperatorToken(attribute.getValue().toString());
439                     return new NbClassPath(val);
440                 }else
441                     return null;
442             }
443             
444             public void setValue(Object JavaDoc val){
445                 String JavaDoc value = ((NbClassPath)val).getClassPath();
446                 value = stripQuotes(value);
447                 Object JavaDoc obj = replacePathSeperator(value);
448                 attribute = revertAttribute(parent, getName(), obj, attribute);
449             }
450         };
451     }//createNetBeansClassPathProperty
452

453     public PropertySupport createLogLevelProperty(final DomainRootNode parent, final Attribute JavaDoc attr, final MBeanAttributeInfo JavaDoc info) {
454         return new PropertySupport.ReadWrite(
455                 info.getName(),
456                 LogLevelEditor.class,
457                 info.getName(),
458                 info.getName()) {
459             Attribute JavaDoc attribute = attr;
460             public Object JavaDoc getValue() {
461                 return attribute.getValue();
462             }
463             
464             public void setValue(Object JavaDoc val){
465                 Attribute JavaDoc updatedAttribute = null;
466                 try {
467                     updatedAttribute = parent.setSheetProperty(getName(), val);
468                 } catch (RuntimeException JavaDoc rex) {
469                     //catching runtime exception from isServerInDebug
470
}
471                 if(updatedAttribute != null){
472                     attribute = updatedAttribute;
473                 }
474             }
475             
476             public PropertyEditor JavaDoc getPropertyEditor(){
477                 return new LogLevelEditor();
478             }
479         };
480     }//createLogLevelProperty
481

482     PropertySupport createModifiedStringArrayReadOnlyProperty(final AppserverMgmtActiveNode parent, final Attribute JavaDoc attr, final MBeanAttributeInfo JavaDoc info, final Class JavaDoc classType) {
483         return new PropertySupport.ReadOnly(
484         info.getName(),
485         classType,
486         info.getName(),
487         info.getName()) {
488             char sepChar = ';';
489             Attribute JavaDoc attribute = attr;
490             public Object JavaDoc getValue() {
491                 Object JavaDoc val = attribute.getValue();
492                 String JavaDoc[] value = null;
493                 if(val != null){
494                     String JavaDoc strVal = replacePathSeperatorToken(val.toString());
495                     if(strVal != null){
496                         sepChar = getSeperationChar(strVal);
497                     }
498                     value = createClasspathArray(strVal);
499                 }
500                 return value;
501             }
502         };
503     }//createModifiedStringArrayReadOnlyProperty
504

505     public String JavaDoc replacePathSeperatorToken(String JavaDoc tokenString){
506         String JavaDoc token = "path.separator"; //NOI18N
507
String JavaDoc resolvedToken = File.pathSeparator;
508         tokenString = tokenString.replaceAll("\\$\\{"+token+"\\}", resolvedToken); //NOI18N
509
return tokenString;
510     }
511     
512     public String JavaDoc replacePathSeperator(String JavaDoc tokenString){
513         String JavaDoc token = "path.separator"; //NOI18N
514
String JavaDoc resolvedToken = File.pathSeparator;
515         tokenString = tokenString.replaceAll(resolvedToken, "\\$\\{"+token+"\\}"); //NOI18N
516
return tokenString;
517     }
518     
519     private Attribute JavaDoc revertAttribute(AppserverMgmtActiveNode parent, String JavaDoc attrName, Object JavaDoc attrValue, Attribute JavaDoc attribute){
520         Attribute JavaDoc updatedAttribute = null;
521         try {
522             updatedAttribute = parent.setSheetProperty(attrName, attrValue);
523         } catch (RuntimeException JavaDoc rex) {
524             //catching runtime exception from isServerInDebug
525
}
526         
527         if(updatedAttribute != null){
528             return updatedAttribute;
529         }
530         
531         return attribute;
532     }
533    
534     private NameValuePair[] getNameValuePairs(Object JavaDoc[] attrVal){
535         NameValuePair[] pairs = new NameValuePair[attrVal.length];
536         for (int j = 0; j < attrVal.length; j++) {
537             NameValuePair pair = (NameValuePair)attrVal[j];
538             pairs[j] = pair;
539         }
540         return pairs;
541     }
542     
543     private static String JavaDoc[] createClasspathArray(Object JavaDoc cpath){
544         Vector JavaDoc path = new Vector JavaDoc();
545         if(cpath != null){
546             String JavaDoc classPath = cpath.toString();
547             char sepChar = getSeperationChar(classPath);
548             while(classPath.indexOf(sepChar) != -1){
549                 int index = classPath.indexOf(sepChar);
550                 String JavaDoc val = classPath.substring(0, index);
551                 path.add(val);
552                 classPath = classPath.substring(index+1, classPath.length());
553             }
554             path.add(classPath);
555         }
556         if(path != null){
557             Object JavaDoc[] finalPath = (Object JavaDoc[])path.toArray();
558             String JavaDoc[] value = new String JavaDoc[finalPath.length];
559             for(int i=0; i<finalPath.length; i++){
560                 value[i] = finalPath[i].toString();
561             }
562             
563             return value;
564         }else
565             return null;
566     }
567     
568     private static char getSeperationChar(String JavaDoc classPath){
569         if(classPath.indexOf(";") != -1) //NOI18N
570
return ';';
571         else
572             return ':';
573     }
574     
575     private String JavaDoc stripQuotes(String JavaDoc classPath){
576         if(classPath.startsWith("\"")){ //NOI18N
577
int index = classPath.indexOf("\""); //NOI18N
578
classPath = classPath.substring(index + 1, classPath.lastIndexOf("\"")); //NOI18N
579
}
580         return classPath;
581     }
582 }
583
Popular Tags