KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ws7 > nodes > WS70JVMManagedObject


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
20 /*
21  * WS70JVMManagedObject.java
22  *
23  */

24
25 package org.netbeans.modules.j2ee.sun.ws7.nodes;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Arrays JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Vector JavaDoc;
35 import org.openide.nodes.Sheet;
36 import org.openide.nodes.PropertySupport;
37 import org.openide.util.NbBundle;
38 import org.openide.execution.NbClassPath;
39
40 import org.netbeans.modules.j2ee.sun.ws7.ide.editors.TaggedValue;
41 import org.netbeans.modules.j2ee.sun.ws7.dm.WS70SunDeploymentManager;
42 import org.netbeans.modules.j2ee.sun.ws7.ui.Util;
43
44 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
45 /**
46  *
47  * @author Administrator
48  */

49 public class WS70JVMManagedObject extends WS70ManagedObjectBase{
50     private WS70SunDeploymentManager manager;
51     private String JavaDoc configName;
52     private HashMap JavaDoc attributes;
53     private List JavaDoc jvmOptions;
54     /** Creates a new instance of WS70JVMManagedObject */
55     public WS70JVMManagedObject(DeploymentManager manager, String JavaDoc config, HashMap JavaDoc attrs,
56                                 List JavaDoc jvmOptions) {
57         this.manager = ((WS70SunDeploymentManager)manager);
58         this.configName = config;
59         this.attributes = this.constructJVMAttributes(attrs);
60         this.jvmOptions = jvmOptions;
61         Iterator JavaDoc it = attributes.keySet().iterator();
62         while(it.hasNext()){
63             Attribute a = (Attribute)it.next();
64             if(a.getName().equals(JVM_DEBUG_OPTION)){
65                 String JavaDoc debugString = a.getValue().toString();
66                 this.manager.setDebugOptions(debugString);
67             }
68             if(a.getName().equals(JVM_DEBUG)){
69                 boolean isDebug = ((Boolean JavaDoc)a.getValue()).booleanValue();
70                 this.manager.setDebugModeEnabled(isDebug);
71             }
72         }
73  
74     }
75     public String JavaDoc getDisplayName(){
76         return NbBundle.getMessage(WS70JVMManagedObject.class, "LBL_JVM_NODE_NAME"); // NOI18N
77
}
78    
79     public Attribute setAttribute(String JavaDoc attribute, Object JavaDoc value) throws Exception JavaDoc{
80         HashMap JavaDoc map = new HashMap JavaDoc();
81         map.put(attribute, value.toString());
82         try{
83             manager.setJVMProps(configName, map);
84             if(attributes.containsKey(attribute)){
85                 attributes.put(attribute, value);
86             }
87             Util.showInformation(
88                     NbBundle.getMessage(WS70JVMManagedObject.class, "MSG_RESTART_SERVER")); // NOI18N);
89
}catch(Exception JavaDoc ex){
90             throw ex;
91         }
92         if(attribute.equals(JVM_DEBUG_OPTION)){
93             // update DM about change in the debug options.
94
this.manager.setDebugOptions(value.toString());
95         }
96         if(attribute.equals(JVM_DEBUG)){
97             // update DM about change in the debug mode.
98
this.manager.setDebugModeEnabled(((Boolean JavaDoc)value).booleanValue());
99         }
100         return new Attribute(attribute, value);
101     }
102     private String JavaDoc setJVMOptions(List JavaDoc options) throws Exception JavaDoc{
103         try{
104             manager.setJVMOptions(configName, options,
105                                          java.lang.Boolean.FALSE, null);
106             return NbBundle.getMessage(WS70JVMManagedObject.class, "MSG_RESTART_SERVER"); // NOI18N
107
}catch(Exception JavaDoc ex){
108             throw ex;
109         }
110         
111     }
112     public Sheet updateSheet(Sheet sheet) {
113         Sheet.Set ps = sheet.get(Sheet.PROPERTIES);
114         try {
115             for(Iterator JavaDoc itr = attributes.keySet().iterator(); itr.hasNext(); ) {
116                 Attribute a = (Attribute) itr.next();
117                 AttributeInfo attr = (AttributeInfo)attributes.get(a);
118                 String JavaDoc shortDescription = getShortDescription(attr);
119                 if (attr == null || ! attr.isReadable()) {
120                     continue;
121                 }
122                
123                 Class JavaDoc type = getSupportedType(attr.getType());
124                 if (attr.isWritable()) {
125                     Set JavaDoc classpathAttrs = new HashSet JavaDoc(Arrays.asList(JVM_STR_TO_ARR));
126                     if(classpathAttrs.contains(a.getName())){
127                         if (a.getValue()!= null) {
128                             ps.put(createNetBeansClassPathProperty(a, attr,
129                                                                 shortDescription));
130                         } else {
131                             ps.put(createReadOnlyProperty(a, attr,
132                                                           shortDescription));
133                         }
134                     } else if (type != null) {
135                         if (a.getValue() instanceof TaggedValue) {
136                             ps.put(createTaggedProperty(a, attr, shortDescription, type));
137                         }else {
138                             ps.put(createWritableProperty(a, attr, shortDescription, type));
139                         } // end of else
140

141                     } else {
142                         ps.put(createReadOnlyProperty(a, attr, shortDescription));
143                     }
144                 }else {
145                     Set JavaDoc classpathAttrs = new HashSet JavaDoc(Arrays.asList(JVM_STR_TO_ARR));
146                     if(classpathAttrs.contains(a.getName())){
147                         if (a.getValue()!= null) {
148                             ps.put(createNetBeansClassPathProperty(a, attr,
149                                                                 shortDescription));
150                         } else {
151                             ps.put(createReadOnlyProperty(a, attr,
152                                                           shortDescription));
153                         }
154                     } else {
155                         ps.put(createReadOnlyProperty(a, attr,
156                                                       shortDescription));
157                     }
158                 }//attr is writable
159
} //for loop ends
160
// for jvm-options
161
Object JavaDoc[] opts = jvmOptions.toArray();
162             String JavaDoc[] values = new String JavaDoc[opts.length];
163             for(int i = 0;i<opts.length;i++){
164                 values[i] = (String JavaDoc)opts[i];
165             }
166             String JavaDoc jvmOptionsName = NbBundle.getMessage(WS70JVMManagedObject.class, "LBL_JVM_OPTIONS"); // NOI18N
167
Attribute attr = new Attribute(jvmOptionsName, values);
168             AttributeInfo attrInfo = new AttributeInfo(jvmOptionsName, "java.lang.String[]",
169                                                 null, true, true, false);
170             
171             ps.put(createModifiedStringArrayWritableProperty(attr, attrInfo, ""));
172             
173         } catch (Exception JavaDoc t) {
174             t.printStackTrace();
175         }
176
177         return sheet;
178     }
179
180     private HashMap JavaDoc constructJVMAttributes(HashMap JavaDoc attrMap){
181         HashMap JavaDoc attributes = new HashMap JavaDoc();
182         for(Iterator JavaDoc itr = attrMap.keySet().iterator(); itr.hasNext(); ) {
183             String JavaDoc attrName = (String JavaDoc) itr.next();
184             Object JavaDoc attrValue = attrMap.get(attrName);
185             Attribute attr = null;
186             AttributeInfo attrInfo = null;
187             Set JavaDoc classpathAttrs = new HashSet JavaDoc(Arrays.asList(JVM_STR_TO_ARR));
188             Set JavaDoc booleans = new HashSet JavaDoc(Arrays.asList(JVM_BOOLEAN_VALS));
189   /* if(classpathAttrs.contains(attrName)){
190                 String[] values = createClasspathArray(attrValue);
191                 attr = new Attribute(attrName, values);
192                 attrInfo = new AttributeInfo(attrName, "java.lang.String[]", null,
193                                                        true, true, false);
194             }else */
if(booleans.contains(attrName)){
195                 Object JavaDoc obj = Boolean.getValue((java.lang.Boolean JavaDoc)attrValue);
196                 attr = new Attribute(attrName, obj);
197                 attrInfo = new AttributeInfo(attrName, obj.getClass().getName(),
198                                                 null, true, true, true);
199             }else{
200                 attr = new Attribute(attrName, attrValue);
201                 attrInfo = new AttributeInfo(attrName, "java.lang.String",
202                                                 null, true, true, false);
203                 
204             }
205
206             attributes.put(attr, attrInfo);
207  
208         }
209         return attributes;
210     }
211     PropertySupport createModifiedStringArrayWritableProperty(final Attribute a,
212                                                               final AttributeInfo attr,
213                                                               final String JavaDoc shortDescription) {
214         return new PropertySupport.ReadWrite(attr.getName(),
215                                              String JavaDoc[].class,
216                                              attr.getName(),
217                                              shortDescription) {
218                 Attribute attribute = a;
219                 public Object JavaDoc getValue() {
220                     return attribute.getValue();
221                 }
222                 public void setValue(Object JavaDoc value) {
223                     try {
224                         String JavaDoc[] values = (String JavaDoc[])value;
225                         java.util.ArrayList JavaDoc list = new java.util.ArrayList JavaDoc();
226                         if (values.length >= 1) {
227                             for (int i = 0; i < values.length; i++) {
228                                 list.add(values[i]);
229                             }
230                             try{
231                                 String JavaDoc retval = setJVMOptions(list);
232                                 attribute.setValue(values);
233                                 Util.showInformation(retval, "JVM Options setting"); // doi18n
234
}catch(Exception JavaDoc ex){
235                                 Util.showError(ex.getMessage());
236                             }
237                         }
238                     } catch (Exception JavaDoc e) {
239                         Util.showError(e.getLocalizedMessage());
240                     }
241                 }
242                 
243             };
244     }
245     
246     PropertySupport createNetBeansClassPathProperty(final Attribute a,
247                                                     final AttributeInfo attr,
248                                                     final String JavaDoc shortDescription) {
249         return new PropertySupport.ReadWrite(attr.getName(),
250                                              NbClassPath.class,
251                                              attr.getName(),
252                                              shortDescription) {
253                 Attribute attribute = a;
254
255                 public Object JavaDoc getValue() {
256                     try {
257                         if (attribute.getValue() != null) {
258                             String JavaDoc x = attribute.getValue().toString();
259                             return new NbClassPath(attribute.getValue().toString());
260                         }
261                         else {
262                             return null;
263                         } // end of else
264
} catch (Exception JavaDoc ex) {
265                         return null;
266                     }
267                 }
268             
269                 public void setValue(Object JavaDoc val) {
270                     try {
271                         String JavaDoc value = ((NbClassPath)val).getClassPath();
272                         attribute = setAttribute(getName(), value);
273                     } catch (Exception JavaDoc e) {
274                         Util.showError(e.getLocalizedMessage());
275                     }
276                 }
277             };
278     }
279     
280     static String JavaDoc[] createClasspathArray(Object JavaDoc cpath) {
281         Vector JavaDoc path = new Vector JavaDoc();
282
283         if (cpath != null) {
284             String JavaDoc classPath = cpath.toString();
285             char sepChar = getSeperationChar(classPath);
286
287             while (classPath.indexOf(sepChar) != -1) {
288                 int index = classPath.indexOf(sepChar);
289                 String JavaDoc val = classPath.substring(0, index);
290                 path.add(val);
291                 classPath = classPath.substring(index + 1,
292                                                 classPath.length());
293             }
294
295             path.add(classPath);
296         }
297
298         if (path != null) {
299             Object JavaDoc[] finalPath = (Object JavaDoc[])path.toArray();
300             String JavaDoc[] value = new String JavaDoc[finalPath.length];
301
302             for (int i = 0; i < finalPath.length; i++) {
303                 value[i] = finalPath[i].toString();
304             }
305
306             return value;
307         }
308         else {
309             return null;
310         } // end of else
311
}
312     
313     private static char getSeperationChar(String JavaDoc classPath){
314         if((classPath.indexOf(":") != -1) && // NOI18N
315
(classPath.indexOf(";") != -1)) { // NOI18N
316
return ';'; // NOI18N
317
}
318         else {
319             return ':'; // NOI18N
320
} // end of else
321
}
322     
323 }
Popular Tags