KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > handlers > PropertiesHandlers


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.tools.admingui.handlers;
25
26 import java.util.List JavaDoc;
27 import java.util.EventObject JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.Enumeration JavaDoc;
30
31 import com.iplanet.jato.RequestContext;
32 import com.iplanet.jato.RequestManager;
33 import com.iplanet.jato.view.View;
34 import com.iplanet.jato.view.ViewBase;
35 import com.iplanet.jato.view.ViewBean;
36 import com.iplanet.jato.model.DefaultModel;
37 import com.iplanet.jato.model.ModelControlException;
38 import com.sun.enterprise.tools.admingui.util.Util;
39
40 import com.sun.web.ui.model.CCActionTableModel;
41 import javax.management.AttributeList JavaDoc;
42 import javax.management.Attribute JavaDoc;
43
44 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
45 import com.sun.enterprise.tools.guiframework.view.DescriptorContainerView;
46 import com.sun.enterprise.tools.guiframework.view.HandlerContext;
47 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor;
48 import com.sun.enterprise.tools.guiframework.view.descriptors.CCActionTableDescriptor;
49 import com.sun.enterprise.tools.guiframework.view.event.BeforeCreateEvent;
50
51 import com.sun.web.ui.model.CCActionTableModelInterface;
52
53 import com.sun.enterprise.tools.admingui.util.MBeanUtil;
54 import com.sun.enterprise.tools.admingui.util.Util;
55
56
57 public class PropertiesHandlers {
58
59     public static final String JavaDoc PROPERTY_NAME = "propertyName";
60     public static final String JavaDoc PROPERTY_VALUE = "propertyValue";
61     public static final String JavaDoc PROPERTY_WEIGHT = "weight";
62     public static final String JavaDoc PROPERTY_DEFAULT_VALUE = "defaultValue";
63     public static final String JavaDoc PROPERTY_ORIGINAL_OVERRIDE_VALUE = "originalOverrideValue";
64
65     private CCActionTableModelInterface getModel(HandlerContext handlerCtx) {
66         CCActionTableModelInterface model =
67             (CCActionTableModelInterface)handlerCtx.getInputValue("propertiesModel");
68         //model.setRowSelectionType("multiple");
69

70         if (model == null) {
71             throw new FrameworkException("PropertiesHandler.getModel: Parameter 'propertiesModel' not specified");
72         }
73         return model;
74     }
75
76     private void loadModel(CCActionTableModelInterface model, AttributeList JavaDoc attrs, Object JavaDoc excludes)
77         throws ModelControlException {
78     RequestContext ctx = RequestManager.getRequestContext();
79         if (attrs == null)
80             return;
81     // Log some info...
82
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
83     if (Util.isLoggableFINER()) {
84             for(int rowNo = 0; rowNo < attrs.size(); rowNo++) {
85                 Attribute JavaDoc attr = (Attribute JavaDoc)attrs.get(rowNo);
86                 buf.append("properties table : name = " + attr.getName() + " , value = " + attr.getValue()+"\n");
87             }
88         Util.logFINER(buf.toString());
89     }
90         model.beforeFirst();
91         for (int rowNo = 0; rowNo < attrs.size(); rowNo++) {
92             
93             Attribute JavaDoc attr = (Attribute JavaDoc)attrs.get(rowNo);
94             if (! shouldExclude(attr.getName(), excludes)){
95                 model.appendRow();
96                 model.setValue(PROPERTY_NAME, attr.getName());
97                 model.setValue(PROPERTY_VALUE, attr.getValue());
98                 model.setRowSelected(false);
99             }
100         }
101     }
102
103     private void loadModel(CCActionTableModelInterface model, Properties JavaDoc props, Object JavaDoc excludes)
104         throws ModelControlException {
105         if (props == null)
106             return;
107         //System.out.println("loading model, props = " + props);
108
((DefaultModel)model).clear();
109         model.beforeFirst();
110         Enumeration JavaDoc ee = props.propertyNames();
111         while (ee.hasMoreElements()) {
112             String JavaDoc name = (String JavaDoc)ee.nextElement();
113             if (!shouldExclude(name, excludes)){
114                 model.appendRow();
115                 model.setValue(PROPERTY_NAME, name);
116                 model.setValue(PROPERTY_VALUE, props.getProperty(name));
117                 model.setRowSelected(false);
118                 if (Util.isLoggableFINER()) {
119                     Util.logFINER("properties table : " +
120                         name + ":" + props.getProperty(name));
121                 }
122             }
123         }
124     }
125     
126     private boolean shouldExclude(String JavaDoc name, Object JavaDoc excludes){
127         List JavaDoc list = (List JavaDoc) excludes;
128         if(list == null || list.isEmpty())
129             return false;
130         for(int i=0; i< list.size(); i++){
131             if (list.get(i).equals(name)){
132                 return true;
133             }
134         }
135         return false;
136     }
137     
138     private void loadModel(CCActionTableModelInterface model,
139                 Properties JavaDoc configProps, Properties JavaDoc targetProps,
140                 boolean instPropsOnly)
141         throws ModelControlException {
142         if (configProps == null)
143             return;
144         model.beforeFirst();
145         Enumeration JavaDoc ee = configProps.propertyNames();
146         if (instPropsOnly == false) {
147             while (ee.hasMoreElements()) {
148                 String JavaDoc name = (String JavaDoc)ee.nextElement();
149                 model.appendRow();
150                 model.setValue(PROPERTY_NAME, name);
151                 model.setValue(PROPERTY_DEFAULT_VALUE, configProps.getProperty(name));
152                 if (targetProps != null) {
153                     model.setValue(PROPERTY_VALUE, targetProps.getProperty(name, ""));
154                     model.setValue(PROPERTY_ORIGINAL_OVERRIDE_VALUE, targetProps.getProperty(name, ""));
155                 } else {
156                     model.setValue(PROPERTY_VALUE, "");
157                     model.setValue(PROPERTY_ORIGINAL_OVERRIDE_VALUE, "");
158                 }
159                 model.setRowSelected(false);
160                 if (Util.isLoggableFINER()) {
161                     String JavaDoc msg = "properties table : " + name + ":"
162                         + configProps.getProperty(name);
163                     if (targetProps != null)
164                         msg += ":" + targetProps.getProperty(name, "");
165                     //System.out.println("::: "+msg);
166
Util.logFINER( msg);
167                 }
168             }
169         } else {
170         // add any props that are just on the target and not on the config
171
ee = targetProps.propertyNames();
172             while (ee.hasMoreElements()) {
173                 String JavaDoc name = (String JavaDoc)ee.nextElement();
174                 String JavaDoc value = configProps.getProperty(name, null);
175                 if (value == null) {
176                     model.appendRow();
177                     model.setValue(PROPERTY_NAME, name);
178                     model.setValue(PROPERTY_VALUE, targetProps.getProperty(name, ""));
179                     model.setValue(PROPERTY_ORIGINAL_OVERRIDE_VALUE, targetProps.getProperty(name, ""));
180                  }
181             }
182         }
183     }
184     
185     
186     public void loadProperties(RequestContext ctx, HandlerContext handlerCtx) {
187         try {
188             CCActionTableModelInterface model = getModel(handlerCtx);
189             ((DefaultModel)model).clear();
190             Object JavaDoc obj = handlerCtx.getInputValue("properties");
191             Object JavaDoc excludes = handlerCtx.getInputValue("excludes");
192             if (obj instanceof AttributeList JavaDoc) {
193                 loadModel(model, (AttributeList JavaDoc)obj, excludes);
194             }
195             else {
196                 loadModel(model, (Properties JavaDoc)obj, excludes);
197             }
198         } catch (Exception JavaDoc ex) {
199             throw new FrameworkException("loadProperties: Loading error. ", ex);
200         }
201     }
202     
203     public void loadSystemProperties(RequestContext ctx, HandlerContext handlerCtx) {
204         try {
205             CCActionTableModelInterface model = getModel(handlerCtx);
206             String JavaDoc objectName = (String JavaDoc)handlerCtx.getInputValue("objectName");
207             String JavaDoc methodName = (String JavaDoc)handlerCtx.getInputValue("methodName");
208             String JavaDoc configName = (String JavaDoc)handlerCtx.getInputValue("configName");
209             String JavaDoc targetName = (String JavaDoc)handlerCtx.getInputValue("targetName");
210             Boolean JavaDoc inherit = (Boolean JavaDoc)handlerCtx.getInputValue("inherit");
211             Boolean JavaDoc instPropsOnly = (Boolean JavaDoc)handlerCtx.getInputValue("instPropsOnly");
212             if (objectName == null || methodName == null || configName == null) {
213                 throw new FrameworkException("Missing input parameter to loadSystemProperties()");
214             }
215             
216             String JavaDoc[] types = new String JavaDoc[]{"java.lang.String", "boolean"};
217             Object JavaDoc[] params = new Object JavaDoc[]{configName, inherit};
218             Properties JavaDoc configProps = (Properties JavaDoc)
219                 MBeanUtil.invoke(objectName, methodName, params, types);
220             
221             Properties JavaDoc targetProps = null;
222             if (targetName != null) {
223                 params = new Object JavaDoc[]{targetName, inherit};
224                 targetProps = (Properties JavaDoc)
225                     MBeanUtil.invoke(objectName, methodName, params, types);
226             }
227             loadModel(model, configProps, targetProps, instPropsOnly.booleanValue());
228         } catch (Exception JavaDoc ex) {
229             throw new FrameworkException("loadSystemProperties: Loading error. ", ex);
230         }
231     }
232     
233     public void saveProperties(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException {
234         Object JavaDoc objectName = handlerCtx.getInputValue("objectName");
235     if (objectName == null) {
236         throw new FrameworkException(
237                 "objectName not specified in saveProperties.");
238     }
239         
240         String JavaDoc setMethodName = (String JavaDoc)handlerCtx.getInputValue("setMethodName");
241     if (setMethodName == null) {
242             setMethodName="setProperty";
243     }
244
245         CCActionTableModelInterface model = getModel(handlerCtx);
246         //model.setRowSelectionType("multiple");
247
model.beforeFirst();
248
249         /* get the current properties, delete those and set new ones */
250         AttributeList JavaDoc originalProperties = (AttributeList JavaDoc)handlerCtx.getInputValue("originalProperties");
251         AttributeList JavaDoc attrs = new AttributeList JavaDoc();
252         while(model.next()) {
253             String JavaDoc name = (String JavaDoc) model.getValue(PROPERTY_NAME);
254             String JavaDoc value = (String JavaDoc) model.getValue(PROPERTY_VALUE);
255             if (!Util.isEmpty(name) && !Util.isEmpty(value)) {
256                 attrs.add(new Attribute JavaDoc(name, value));
257             }
258         }
259
260         String JavaDoc[] type = new String JavaDoc[]{"javax.management.Attribute"};
261
262         if (originalProperties != null) {
263             for (int i = 0; i < originalProperties.size(); i++) {
264                 Attribute JavaDoc originalProperty = (Attribute JavaDoc)originalProperties.get(i);
265                 boolean found = false;
266                 for (int j = 0; j < attrs.size(); j++) {
267                     Attribute JavaDoc attr = (Attribute JavaDoc)(attrs.get(j));
268                     if (attr.getName().equals(originalProperty.getName())) {
269                         found = true;
270                         break;
271                     }
272                 }
273                 if (!found) {
274                     Attribute JavaDoc propertyToClear = new Attribute JavaDoc(originalProperty.getName(), null);
275                     Object JavaDoc[] params = new Object JavaDoc[]{propertyToClear};
276                     MBeanUtil.invoke(objectName.toString(), setMethodName, params, type);
277                 }
278             }
279         }
280         for (int i = 0; i < attrs.size(); i++) {
281             Attribute JavaDoc attr = (Attribute JavaDoc)attrs.get(i);
282             Object JavaDoc[] params = new Object JavaDoc[]{attr};
283             MBeanUtil.invoke(objectName.toString(), setMethodName, params, type);
284         }
285     }
286
287     
288     public void saveSystemProperties(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException {
289         Object JavaDoc objectName = handlerCtx.getInputValue("objectName");
290     if (objectName == null) {
291         throw new FrameworkException(
292                 "objectName not specified in saveSystemProperties.");
293     }
294         String JavaDoc target = (String JavaDoc)handlerCtx.getInputValue("target");
295     if (target == null) {
296         throw new FrameworkException(
297                 "target not specified in saveSystemProperties.");
298     }
299         String JavaDoc createMethodName = (String JavaDoc)handlerCtx.getInputValue("createMethodName");
300         if (createMethodName == null)
301             createMethodName = "createSystemProperties";
302         String JavaDoc deleteMethodName = (String JavaDoc)handlerCtx.getInputValue("deleteMethodName");
303         if (deleteMethodName == null)
304             deleteMethodName = "deleteSystemProperty";
305
306         CCActionTableModelInterface model = getModel(handlerCtx);
307         //model.setRowSelectionType("multiple");
308
model.beforeFirst();
309
310         Properties JavaDoc originalProperties = (Properties JavaDoc)handlerCtx.getInputValue("originalProperties");
311         Properties JavaDoc propertiesToCreate = new Properties JavaDoc();
312         while(model.next()) {
313             String JavaDoc name = (String JavaDoc) model.getValue(PROPERTY_NAME);
314             if (name != null && (!name.trim().equals(""))) {
315                 //if (originalProperties.getProperty(name) == null) {
316
String JavaDoc value = (String JavaDoc) model.getValue(PROPERTY_VALUE);
317                     propertiesToCreate.setProperty(name, value);
318                 //}
319
}
320         }
321         String JavaDoc[] types = null;
322         if (originalProperties != null) {
323             Enumeration JavaDoc deleteProps = originalProperties.propertyNames();
324             types = new String JavaDoc[]{"java.lang.String", "java.lang.String"};
325             while(deleteProps.hasMoreElements()) {
326                 String JavaDoc name = (String JavaDoc)deleteProps.nextElement();
327                 Object JavaDoc[] params = new Object JavaDoc[]{name, target};
328                 MBeanUtil.invoke(objectName.toString(), deleteMethodName, params, types);
329             }
330         }
331         types = new String JavaDoc[]{"java.util.Properties", "java.lang.String"};
332         Object JavaDoc[] params = new Object JavaDoc[]{propertiesToCreate, target};
333         MBeanUtil.invoke(objectName.toString(), createMethodName, params, types);
334     }
335     
336     public void deleteInstOnlyProps(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException {
337         String JavaDoc deleteMethodName = (String JavaDoc)handlerCtx.getInputValue("deleteMethodName");
338         if (deleteMethodName == null)
339             deleteMethodName = "deleteSystemProperty";
340
341         String JavaDoc objectName = (String JavaDoc)handlerCtx.getInputValue("objectName");
342         String JavaDoc methodName = (String JavaDoc)handlerCtx.getInputValue("methodName");
343         String JavaDoc configName = (String JavaDoc)handlerCtx.getInputValue("configName");
344         String JavaDoc targetName = (String JavaDoc)handlerCtx.getInputValue("targetName");
345         Boolean JavaDoc inherit = (Boolean JavaDoc)handlerCtx.getInputValue("inherit");
346         if (objectName == null || methodName == null || configName == null || targetName == null) {
347             throw new FrameworkException("Missing input parameter to deleteInstOnlyProps()");
348         }
349
350         String JavaDoc[] types = new String JavaDoc[]{"java.lang.String", "boolean"};
351         Object JavaDoc[] params = new Object JavaDoc[]{configName, inherit};
352         Properties JavaDoc configProps = (Properties JavaDoc)
353             MBeanUtil.invoke(objectName, methodName, params, types);
354
355         Properties JavaDoc targetProps = null;
356         params = new Object JavaDoc[]{targetName, inherit};
357         targetProps = (Properties JavaDoc)
358             MBeanUtil.invoke(objectName, methodName, params, types);
359             
360         types = new String JavaDoc[]{"java.lang.String", "java.lang.String"};
361         Enumeration JavaDoc ee = targetProps.propertyNames();
362         while (ee.hasMoreElements()) {
363             String JavaDoc name = (String JavaDoc)ee.nextElement();
364             String JavaDoc value = configProps.getProperty(name, null);
365             if (value == null) {
366                 params = new Object JavaDoc[]{name, targetName};
367                 MBeanUtil.invoke(objectName, deleteMethodName, params, types);
368              }
369         }
370     }
371  
372
373     public void saveSystemPropertiesValues(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException {
374         Object JavaDoc objectName = handlerCtx.getInputValue("objectName");
375         //System.out.println("saveSystemPropertiesValues: "+objectName);
376
if (objectName == null) {
377         throw new FrameworkException(
378                 "objectName not specified in saveSystemProperties.");
379     }
380         String JavaDoc target = (String JavaDoc)handlerCtx.getInputValue("target");
381     if (target == null) {
382         throw new FrameworkException(
383                 "target not specified in saveSystemProperties.");
384     }
385         String JavaDoc createMethodName = (String JavaDoc)handlerCtx.getInputValue("createMethodName");
386         if (createMethodName == null)
387             createMethodName = "createSystemProperties";
388         String JavaDoc deleteMethodName = (String JavaDoc)handlerCtx.getInputValue("deleteMethodName");
389         if (deleteMethodName == null)
390             deleteMethodName = "deleteSystemProperty";
391
392         CCActionTableModelInterface model = getModel(handlerCtx);
393         //model.setRowSelectionType("multiple");
394
model.beforeFirst();
395         String JavaDoc[] deleteTypes = new String JavaDoc[]{"java.lang.String", "java.lang.String"};
396
397         Properties JavaDoc propertiesToCreate = new Properties JavaDoc();
398         while(model.next()) {
399             String JavaDoc name = (String JavaDoc) model.getValue(PROPERTY_NAME);
400             String JavaDoc originalValue = (String JavaDoc) model.getValue(PROPERTY_ORIGINAL_OVERRIDE_VALUE);
401             String JavaDoc newValue = (String JavaDoc) model.getValue(PROPERTY_VALUE);
402             //System.out.println("name: "+name+" oValue: "+originalValue+" newValue: "+newValue);
403
if (name == null || (name.trim().length() == 0))
404                 continue;
405             if (originalValue.trim().equals(newValue.trim())) {
406                 //System.out.println("same values -- continue");
407
continue;
408             }
409             if (originalValue.trim().length() != 0 && newValue.trim().length() == 0) {
410                 // delete the prop.
411
Object JavaDoc[] params = new Object JavaDoc[]{name, target};
412                 //System.out.println("deleting: "+name+" on target: "+target);
413
try {
414                     MBeanUtil.invoke(objectName.toString(), deleteMethodName, params, deleteTypes);
415                 } catch (Exception JavaDoc ex) {
416                     System.out.println("error while trying to delete a system property!!");
417                 }
418                 //System.out.println("deleted");
419
}
420             if (newValue.trim().length() != 0) {
421                 // set a new value or replace existing value
422
propertiesToCreate.setProperty(name, newValue);
423                 //System.out.println("create new: "+name+" value: "+newValue);
424
}
425         }
426         if (propertiesToCreate.size() > 0) {
427             String JavaDoc[] types = new String JavaDoc[]{"java.util.Properties", "java.lang.String"};
428             Object JavaDoc[] params = new Object JavaDoc[]{propertiesToCreate, target};
429             try {
430                 MBeanUtil.invoke(objectName.toString(), createMethodName, params, types);
431             } catch (Exception JavaDoc ex) {
432                 //System.out.println("Error while creating sys props!!");
433
}
434         }
435         //System.out.println("DONE!!");
436
}
437     
438     public void deleteProperties(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException {
439         CCActionTableModelInterface model = getModel(handlerCtx);
440         AttributeList JavaDoc attrs = new AttributeList JavaDoc();
441         model.setRowSelectionType("multiple");
442         model.beforeFirst();
443         while(model.next()) {
444             if (!model.isRowSelected()) {
445                 String JavaDoc name = (String JavaDoc) model.getValue(PROPERTY_NAME);
446                 String JavaDoc value = (String JavaDoc)model.getValue(PROPERTY_VALUE);
447                 attrs.add(new Attribute JavaDoc(name, value));
448             }
449         }
450         ((DefaultModel)model).clear();
451         model.beforeFirst();
452         for (int i = 0; i < attrs.size(); i++) {
453             model.appendRow();
454             Attribute JavaDoc attr = (Attribute JavaDoc)attrs.get(i);
455             model.setValue(PROPERTY_NAME, attr.getName());
456             model.setValue(PROPERTY_VALUE, attr.getValue());
457             model.setRowSelected(false);
458         }
459     }
460
461     public void clearProperties(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException {
462         CCActionTableModelInterface model = getModel(handlerCtx);
463         ((DefaultModel)model).clear();
464         model.beforeFirst();
465     }
466
467     public void addProperty(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException {
468         CCActionTableModelInterface model = getModel(handlerCtx);
469         model.setRowSelectionType("multiple");
470         model.appendRow();
471         model.setValue(PROPERTY_VALUE, "");
472         model.setValue(PROPERTY_NAME, "");
473         model.sort();
474         model.beforeFirst();
475     }
476
477     // returns Properties Object for table model
478
public void getPropertiesObjectFromTable(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException {
479     Boolean JavaDoc ignoreEmptyValues = (Boolean JavaDoc)handlerCtx.getInputValue("ignoreEmptyValues");
480         String JavaDoc propValue = (String JavaDoc)handlerCtx.getInputValue("propValue");
481         CCActionTableModelInterface model = getModel(handlerCtx);
482         //model.setRowSelectionType("multiple");
483
//((DefaultModel)model).dumpValues(System.out);
484
model.beforeFirst();
485         CCActionTableModel cct = (CCActionTableModel) model;
486         Properties JavaDoc properties = new Properties JavaDoc();
487         while (model.next()) {
488             String JavaDoc name = (String JavaDoc) model.getValue(PROPERTY_NAME);
489             String JavaDoc value;
490             if(propValue != null) {
491                 value = (String JavaDoc) model.getValue(propValue);
492             } else {
493                 value = (String JavaDoc) model.getValue(PROPERTY_VALUE);
494             }
495             if (!Util.isEmpty(name)) {
496                 if (Util.isEmpty(value)) {
497                     if (ignoreEmptyValues!=null && ignoreEmptyValues.booleanValue()==true)
498                         continue;
499                 }
500                 properties.setProperty(name, value);
501             }
502         }
503         handlerCtx.setOutputValue("properties", properties);
504     }
505     
506     
507     public void sortModel(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException {
508         CCActionTableModelInterface model = getModel(handlerCtx);
509         if (model != null) {
510             model.sort();
511         }
512     }
513     
514     public void addTableRow(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException {
515         CCActionTableModelInterface model =
516             (CCActionTableModelInterface)handlerCtx.getInputValue("model");
517         model.setRowSelectionType("multiple");
518         model.appendRow();
519         model.beforeFirst();
520     }
521 }
522
Popular Tags