KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > util > EafConfigMBean


1 /*
2  * Created on Apr 6, 2005
3  */

4 package org.enhydra.util;
5
6 import java.util.Enumeration JavaDoc;
7 import java.util.Hashtable JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.StringTokenizer JavaDoc;
10 import java.util.Vector JavaDoc;
11
12 import javax.management.Attribute JavaDoc;
13 import javax.management.AttributeChangeNotification JavaDoc;
14 import javax.management.AttributeList JavaDoc;
15 import javax.management.AttributeNotFoundException JavaDoc;
16 import javax.management.DynamicMBean JavaDoc;
17 import javax.management.InvalidAttributeValueException JavaDoc;
18 import javax.management.MBeanAttributeInfo JavaDoc;
19 import javax.management.MBeanConstructorInfo JavaDoc;
20 import javax.management.MBeanException JavaDoc;
21 import javax.management.MBeanInfo JavaDoc;
22 import javax.management.MBeanNotificationInfo JavaDoc;
23 import javax.management.MBeanOperationInfo JavaDoc;
24 import javax.management.MBeanParameterInfo JavaDoc;
25 import javax.management.Notification JavaDoc;
26 import javax.management.NotificationBroadcasterSupport JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28 import javax.management.ReflectionException JavaDoc;
29 import javax.management.RuntimeOperationsException JavaDoc;
30
31 import com.lutris.appserver.server.Application;
32 import com.lutris.logging.LogChannel;
33 import com.lutris.logging.Logger;
34 import com.lutris.util.Config;
35 import com.lutris.util.KeywordValueException;
36
37 import java.lang.reflect.Method JavaDoc;
38
39 /**
40  * EAF's Core MBean Implementation
41  *
42  * @author Slobodan Vujasinovic
43  */

44 public abstract class EafConfigMBean extends NotificationBroadcasterSupport JavaDoc
45                   implements DynamicMBean JavaDoc{
46     
47     protected String JavaDoc dClassName = this.getClass().getName();
48     protected MBeanInfo JavaDoc dMBeanInfo = null;
49     
50     protected ObjectName JavaDoc objectName = null;
51     protected Hashtable JavaDoc hashAttrib = new Hashtable JavaDoc();
52     protected Hashtable JavaDoc initHashAttrib = new Hashtable JavaDoc();
53     
54     protected Config config = null;
55     protected String JavaDoc prefix = null;
56     
57     protected Vector JavaDoc addedAttributes = null;
58     
59     public static String JavaDoc DOT = "_";
60     
61     protected long sequence = 0;
62     protected String JavaDoc noteTypesChange[] = {
63         "jmx.attribute.change"
64     };
65     protected String JavaDoc noteTypesSave[] = {
66         "jmx.attribute.save"
67     };
68     protected String JavaDoc noteTypesReset[] = {
69         "jmx.attribute.reset"
70     };
71     protected String JavaDoc noteTypesAdd[] = {
72         "jmx.attribute.add"
73     };
74     protected String JavaDoc noteTypesRemove[] = {
75         "jmx.attribute.remove"
76     };
77     
78     LogChannel logChannel = null;
79     
80     protected Application application = null;
81     
82     /**
83      * A constructor with no arguments is required.
84      */

85     public EafConfigMBean() {
86     }
87
88     public EafConfigMBean(Application application,
89             ObjectName JavaDoc objectName, String JavaDoc prefix, String JavaDoc [] includes, String JavaDoc [] excludes) {
90       init(application,objectName,prefix,includes,excludes);
91     }
92     
93     protected String JavaDoc toValidIdentifier (String JavaDoc value){
94         return value.replaceAll(".",DOT);
95     }
96     
97     protected String JavaDoc toOriginal (String JavaDoc value){
98         return value.replaceAll(DOT,".");
99     }
100
101
102     public void init(Application app,
103             ObjectName JavaDoc objectName, String JavaDoc prefix, String JavaDoc [] includes, String JavaDoc [] excludes) {
104       this.objectName = objectName;
105       this.prefix = prefix;
106       this.application = app;
107       
108       if ("".equals(prefix)){
109         prefix=null;
110       }
111       
112       addedAttributes = new Vector JavaDoc();
113       
114       config = application.getConfig();
115       logChannel = application.getLogChannel();
116       
117       Hashtable JavaDoc originalHashtable = null;
118       
119       try{
120         originalHashtable = config.allConfigParams(null);
121       }catch(KeywordValueException kwe){
122         originalHashtable = new Hashtable JavaDoc ();
123         logChannel.write(Logger.DEBUG, kwe.toString());
124       }
125       
126       Hashtable JavaDoc includesHashtable = null;
127       Hashtable JavaDoc excludesHashtable = null;
128         
129       if (includes!=null){
130             includesHashtable = new Hashtable JavaDoc();
131             int length = originalHashtable.size();
132             Enumeration JavaDoc keys = originalHashtable.keys();
133             for (int i = length-1; i >= 0; i--) {
134                 String JavaDoc keyName = (String JavaDoc) keys.nextElement();
135                 boolean include = false;
136                 for (int j=0; j<includes.length;j++){
137                     if (keyName.startsWith(includes[j])){
138                         include = true;
139                     }
140                 }
141                 if (include){
142                     includesHashtable.put(keyName, (String JavaDoc)originalHashtable.get(keyName));
143                 }
144             }
145       } else {
146             includesHashtable = originalHashtable;
147       }
148       
149       if (excludes!=null){
150             excludesHashtable = new Hashtable JavaDoc();
151             int length = includesHashtable.size();
152             Enumeration JavaDoc keys = includesHashtable.keys();
153             for (int i = 0; i < length; i++) {
154                 String JavaDoc keyName = (String JavaDoc) keys.nextElement();
155                 boolean exclude = false;
156                 for (int j=0; j<excludes.length;j++){
157                     if (keyName.startsWith(excludes[j])){
158                         exclude = true;
159                     }
160                 }
161                 if (exclude){
162                     excludesHashtable.put(keyName,(String JavaDoc)includesHashtable.get(keyName));
163                 }
164             }
165       } else {
166             excludesHashtable = new Hashtable JavaDoc();
167       }
168       
169       int length = includesHashtable.size();
170       Enumeration JavaDoc keys = includesHashtable.keys();
171       for (int i = 0; i < length; i++) {
172             String JavaDoc keyName = (String JavaDoc) keys.nextElement();
173             
174             if (!excludesHashtable.containsKey(keyName)){
175                 String JavaDoc keyValue = (String JavaDoc) includesHashtable.get(keyName);
176                 
177                 if (prefix!=null){
178                     keyName=keyName.substring(prefix.length()+1);
179                 }
180                 
181                 hashAttrib.put(keyName, keyValue);
182                 initHashAttrib.put(keyName, keyValue);
183             }
184       }
185       //buildDynamicMBeanInfo();
186
}
187     
188
189     
190     public void buildDynamicMBeanInfo(){
191         // MBeanInfo initialization
192
dMBeanInfo = new MBeanInfo JavaDoc(dClassName, getDescription(),
193                 getMBeanAttributesInfo(), getMBeanConstructorInfo(),
194                 getMBeanOperationsInfo(), getMBeanNotificationInfo());
195     }
196
197     /**
198      * -----------------------------------------------------
199      * ADDITIONALL PROTECTED METHODS
200      * -----------------------------------------------------
201      */

202     
203     /**
204      * Method returns MBean's MBeanAttributeInfo Array
205      * @return MBeanAttributeInfo[]
206      */

207     protected MBeanAttributeInfo JavaDoc[] getMBeanAttributesInfo () {
208         int length = hashAttrib.size();
209         MBeanAttributeInfo JavaDoc[] dAttributes = new MBeanAttributeInfo JavaDoc[length];
210         Enumeration JavaDoc keys = hashAttrib.keys();
211         for (int i = 0; i < length; i++) {
212           String JavaDoc attName = (String JavaDoc) keys.nextElement();
213           String JavaDoc attDesc = new String JavaDoc(attName + " configuration parameter!");
214           dAttributes[i] = new MBeanAttributeInfo JavaDoc(attName, "java.lang.String",
215                                                   attDesc, true, true, false);
216         }
217         return dAttributes;
218     }
219     
220     /**
221      * Method returns MBean's MBeanOperationInfo Array
222      * @return MBeanOperationInfo[]
223      */

224     protected MBeanOperationInfo JavaDoc[] getMBeanOperationsInfo () {
225         MBeanParameterInfo JavaDoc[] params = null;
226         MBeanOperationInfo JavaDoc[] dOperations = new MBeanOperationInfo JavaDoc[5];
227         dOperations[0] = new MBeanOperationInfo JavaDoc("reset",
228             "reset(): reset attributes to their initial values",
229             params, "void",
230             MBeanOperationInfo.ACTION);
231         dOperations[1] = new MBeanOperationInfo JavaDoc("saveAttributes",
232             "saveAttributes(): save the attribute values into the configuration file",
233             params, "void",
234             MBeanOperationInfo.ACTION);
235         dOperations[2] = new MBeanOperationInfo JavaDoc("getAppInfo",
236             "getAppInfo(): get application information",
237             params, "String",
238             MBeanOperationInfo.ACTION);
239         
240         params = new MBeanParameterInfo JavaDoc[2];
241         params[0] = new MBeanParameterInfo JavaDoc("attKey","java.lang.String","Attribute name! Please, " +
242                 "use UNDERLINE (_) as attribute group separator.");
243         params[1] = new MBeanParameterInfo JavaDoc("attValue","java.lang.String","Attribute value!");
244         
245         dOperations[3] = new MBeanOperationInfo JavaDoc("addAttribute",
246               "addAttribute(String key, String value): add new application parameter",
247               params, "void",
248               MBeanOperationInfo.ACTION);
249         
250         params = new MBeanParameterInfo JavaDoc[1];
251         params[0] = new MBeanParameterInfo JavaDoc("attKey","java.lang.String","Attribute name! Please, " +
252                 "use UNDERLINE (_) as attribute group separator.");
253         
254         dOperations[4] = new MBeanOperationInfo JavaDoc("removeAttribute",
255               "removeAttribute(String key): remove application parameter",
256               params, "void",
257               MBeanOperationInfo.ACTION);
258        
259         return dOperations;
260     }
261     
262     /**
263      * Method returns MBean's MBeanNotificationInfo Array
264      * @return MBeanNotificationInfo[]
265      */

266     protected MBeanNotificationInfo JavaDoc[] getMBeanNotificationInfo () {
267         MBeanNotificationInfo JavaDoc[] dNotifications = new MBeanNotificationInfo JavaDoc[5];
268         dNotifications[0] = new MBeanNotificationInfo JavaDoc(noteTypesChange,
269             "javax.management.AttributeChangeNotification",
270             "Notifies listener of the attribute change");
271         dNotifications[1] = new MBeanNotificationInfo JavaDoc(noteTypesSave,
272             "javax.management.Notification",
273             "Notifies listnere that attributes have been saved to the config file");
274         dNotifications[2] = new MBeanNotificationInfo JavaDoc(noteTypesReset,
275             "javax.management.Notification",
276             "Notifies listnere that attributes have been reset to the initial value");
277         dNotifications[3] = new MBeanNotificationInfo JavaDoc(noteTypesAdd,
278             "javax.management.Notification",
279             "Notifies listener of the attribute change");
280         dNotifications[4] = new MBeanNotificationInfo JavaDoc(noteTypesRemove,
281             "javax.management.Notification",
282             "Notifies listener of the attribute change");
283         
284         return dNotifications;
285     }
286     
287     /**
288      * Method returns MBean's MBeanConstructorInfo Array
289      * @return MBeanConstructorInfo[]
290      */

291     protected MBeanConstructorInfo JavaDoc[] getMBeanConstructorInfo () {
292         MBeanParameterInfo JavaDoc[] dParameters = new MBeanParameterInfo JavaDoc[5];
293         dParameters[0] = new MBeanParameterInfo JavaDoc("application","com.lutris.appserver.server.Application","Application Object");
294         dParameters[1] = new MBeanParameterInfo JavaDoc("objectName","javax.management.ObjectName","MBean Name Object");
295         dParameters[2] = new MBeanParameterInfo JavaDoc("prefix","java.lang.String","Configuration Parameter Prefix");
296         dParameters[3] = new MBeanParameterInfo JavaDoc("includes","java.lang.reflect.Array","Array of internal parameter prefixes to include");
297         dParameters[4] = new MBeanParameterInfo JavaDoc("excludes","java.lang.reflect.Array","Array of internal parameter prefixes to exclude");
298         
299         MBeanConstructorInfo JavaDoc[] dConstructors = new MBeanConstructorInfo JavaDoc[1];
300         dConstructors[0] = new MBeanConstructorInfo JavaDoc("ConfigMBean",
301             "ConfigMBean Object Constructor",
302             dParameters);
303         
304         return dConstructors;
305     }
306     
307     /**
308      * Method returns MBean's description
309      * @return String
310      */

311     protected String JavaDoc getDescription (){
312         String JavaDoc dDescription = "Dynamic MBean manages application configuration";
313         return dDescription;
314     }
315     
316     /*
317      * -----------------------------------------------------
318      * IMPLEMENTATION OF THE DynamicMBean INTERFACE
319      * -----------------------------------------------------
320      */

321     
322     /**
323      * Allows the value of the specified attribute of the Dynamic MBean to be obtained.
324      */

325     public Object JavaDoc getAttribute(String JavaDoc attribute_name) throws
326         AttributeNotFoundException JavaDoc,
327         MBeanException JavaDoc, ReflectionException JavaDoc {
328       // Check attribute_name is not null to avoid NullPointerException later on
329
if (attribute_name == null) {
330         throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
331             "Attribute name cannot be null"),
332             "Cannot invoke a getter of " + dClassName +
333             " with null attribute name");
334       }
335       // Check for a recognized attribute_name and call the corresponding getter
336
try {
337         return hashAttrib.get(attribute_name);
338       }
339       catch (Exception JavaDoc e) {
340         // If attribute_name has not been recognized throw an AttributeNotFoundException
341
throw (new AttributeNotFoundException JavaDoc("Cannot find " +
342                 attribute_name + " attribute in " + dClassName));
343       }
344     }
345
346     /**
347      * Sets the value of the specified attribute of the Dynamic MBean.
348      */

349     public void setAttribute(Attribute JavaDoc attribute) throws
350         AttributeNotFoundException JavaDoc,
351         InvalidAttributeValueException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc {
352       
353       // Check attribute is not null to avoid NullPointerException later on
354
if (attribute == null) {
355         throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
356             "Attribute cannot be null"),
357             "Cannot invoke a setter of " + dClassName +
358             " with null attribute");
359       }
360       
361       String JavaDoc name = attribute.getName();
362       String JavaDoc value = (String JavaDoc) attribute.getValue();
363       
364       try {
365         /**
366         boolean newOne = !hashAttrib.containsKey(name);
367         if (prefix==null){
368             newOne = !config.containsKey(name);
369         } else {
370             newOne = !config.containsKey(prefix+DOT+name);
371         }
372         */

373         
374         String JavaDoc oldValue = (String JavaDoc) hashAttrib.get(name);
375         
376         hashAttrib.put(name, value);
377         
378         String JavaDoc[] stringArray = null;
379         if (name.endsWith("_Array")) {
380           int len = name.length();
381           name = name.substring(0, len-6);
382           StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(value, new String JavaDoc(","));
383           stringArray = new String JavaDoc[tok.countTokens()];
384           int i = 0;
385           while (tok.hasMoreTokens()) {
386             stringArray[i] = tok.nextToken().trim();
387             i++;
388           }
389           
390           try{
391             if (prefix==null){
392                 config.set(toOriginal(name), stringArray);
393                 config.getConfigFile().addEntry(toOriginal(name),stringArray,"");
394             } else {
395                 config.set(toOriginal(prefix+DOT+name), stringArray);
396                 config.getConfigFile().addEntry(toOriginal(prefix+DOT+name),stringArray,"");
397             }
398           } catch (Exception JavaDoc e) {
399               logChannel.write(Logger.DEBUG, e.toString());
400           }
401         }
402         else {
403           try {
404             if (prefix==null){
405                 config.set(toOriginal(name), value);
406                 config.getConfigFile().addEntry(toOriginal(name),value,"");
407             } else {
408                 config.set(toOriginal(prefix+DOT+name), value);
409                 config.getConfigFile().addEntry(toOriginal(prefix+DOT+name),value,"");
410             }
411           } catch (Exception JavaDoc e) {
412               logChannel.write(Logger.DEBUG, e.toString());
413           }
414         }
415         
416         String JavaDoc notificationString = "Attribute " + name + " has been set from " + oldValue + " to " + value;
417         Notification JavaDoc note = new AttributeChangeNotification JavaDoc(objectName,
418             ++sequence,
419             System.currentTimeMillis(), notificationString, name, "String",
420             oldValue, value);
421         sendNotification(note);
422       }
423       catch (Exception JavaDoc e) {
424         throw (new AttributeNotFoundException JavaDoc("Cannot find " + name +
425                                               " attribute in "
426                                               + dClassName));
427       }
428     }
429     
430     /**
431      * Sets the values of several attributes of the Dynamic MBean, and returns the
432      * list of attributes that have been set.
433      */

434     public AttributeList JavaDoc setAttributes(AttributeList JavaDoc attributes) {
435         // Check attributes is not null to avoid NullPointerException later on
436
if (attributes == null) {
437           throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
438               "AttributeList attributes cannot be null"),
439               "Cannot invoke a setter of " + dClassName);
440         }
441         AttributeList JavaDoc resultList = new AttributeList JavaDoc();
442         
443         // if attributeNames is empty, nothing more to do
444
if (attributes.isEmpty()) {
445           return resultList;
446         }
447         
448         // for each attribute, try to set it and add to the result list if successfull
449
for (Iterator JavaDoc i = attributes.iterator(); i.hasNext(); ) {
450           Attribute JavaDoc attr = (Attribute JavaDoc) i.next();
451           try {
452             setAttribute(attr);
453             String JavaDoc name = attr.getName();
454             Object JavaDoc value = getAttribute(name);
455             resultList.add(new Attribute JavaDoc(name, value));
456           }
457           catch (Exception JavaDoc e) {
458               logChannel.write(Logger.DEBUG, e.toString());
459           }
460         }
461         return resultList;
462     }
463     
464     /**
465      * get the values of several attributes of the Dynamic MBean.
466      */

467     public AttributeList JavaDoc getAttributes(String JavaDoc[] attributeNames) {
468
469       // Check attributeNames is not null to avoid NullPointerException later on
470
if (attributeNames == null) {
471         throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
472             "attributeNames[] cannot be null"),
473             "Cannot invoke a getter of " + dClassName);
474       }
475       
476       AttributeList JavaDoc resultList = new AttributeList JavaDoc();
477       
478       // if attributeNames is empty, return an empty result list
479
if (attributeNames.length == 0) {
480         return resultList;
481       }
482       
483       // build the result attribute list
484
for (int i = 0; i < attributeNames.length; i++) {
485         try {
486           Object JavaDoc value = getAttribute((String JavaDoc)attributeNames[i]);
487           resultList.add(new Attribute JavaDoc(attributeNames[i], value));
488         }
489         catch (Exception JavaDoc e) {
490             logChannel.write(Logger.DEBUG, e.toString());
491         }
492       }
493       return resultList;
494     }
495     
496     /**
497      * This method provides the exposed attributes and operations of the Dynamic MBean.
498      * It provides this information using an MBeanInfo object.
499      */

500     public MBeanInfo JavaDoc getMBeanInfo() {
501       // return the information we want to expose for management:
502
// the dMBeanInfo private field has been built at instanciation time,
503
return dMBeanInfo;
504     }
505     
506     
507     /**
508      * -----------------------------------------------------
509      * ADDITIONALL PUBLIC METHODS
510      * -----------------------------------------------------
511      */

512     
513     /**
514      * Save Config Attributes
515      */

516     public void saveAttributes() {
517       try {
518         ConfigFileInterface configFile = config.getConfigFile();
519         
520         configFile.write();
521         
522         Notification JavaDoc note = new Notification JavaDoc(noteTypesSave[0], objectName,
523                                              ++sequence,
524                                              System.currentTimeMillis());
525         sendNotification(note);
526       } catch (Exception JavaDoc e) {
527         logChannel.write(Logger.DEBUG, e.toString());
528       }
529     }
530     
531     /**
532      * Add new attribute to config
533      */

534     public void addAttribute(String JavaDoc name, String JavaDoc value) {
535       try {
536         if (name!=null && value!=null){
537           
538           name = name.trim();
539           
540           boolean newOne = true;
541           String JavaDoc tempName = name;
542           if (tempName.endsWith("_Array")) {
543             int len = tempName.length();
544             tempName = tempName.substring(0, len-6);
545           }
546           
547           if (prefix==null && config.containsKey(toOriginal(tempName))){
548                 newOne = false;
549           } else if (config.containsKey(toOriginal(prefix+DOT+tempName))) {
550                 newOne = false;
551           }
552           
553           Attribute JavaDoc att = new Attribute JavaDoc(name, (String JavaDoc)value);
554           setAttribute(att);
555           
556           if (newOne){
557             addedAttributes.add(tempName);
558           }
559           
560           buildDynamicMBeanInfo();
561           
562           Notification JavaDoc note = new Notification JavaDoc(noteTypesAdd[0], (NotificationBroadcasterSupport JavaDoc)this,
563                                                  ++sequence,
564                                                  System.currentTimeMillis());
565           sendNotification(note);
566         }
567       }
568       catch (Exception JavaDoc e) {
569          logChannel.write(Logger.DEBUG, e.toString());
570       }
571     }
572
573     /**
574      * Operation: reset attributes to their initial values
575      */

576     public void reset() throws AttributeNotFoundException JavaDoc {
577       
578       try {
579         int length = 0;
580         Enumeration JavaDoc keys = null;
581         
582         // hashAttrib table reinitialization
583
length = initHashAttrib.size();
584         keys = initHashAttrib.keys();
585         hashAttrib.clear();
586         for (int i = 0; i < length; i++) {
587           String JavaDoc keyName = (String JavaDoc) keys.nextElement();
588           String JavaDoc keyValue = (String JavaDoc) initHashAttrib.get(keyName);
589           hashAttrib.put(keyName,keyValue);
590         }
591         
592         length = hashAttrib.size();
593         keys = hashAttrib.keys();
594         
595         // adapt config attribute value (or add attribute if previously removed)
596
for (int i = 0; i < length; i++) {
597           String JavaDoc keyName = (String JavaDoc) keys.nextElement();
598           String JavaDoc keyValue = (String JavaDoc) hashAttrib.get(keyName);
599           
600           if (keyName.endsWith("_Array")) {
601             int len = keyName.length();
602             keyName = keyName.substring(0, len-6);
603             StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(keyValue, new String JavaDoc(","));
604             String JavaDoc [] stringArray = new String JavaDoc[tok.countTokens()];
605             int j = 0;
606             while (tok.hasMoreTokens()) {
607               stringArray[j] = tok.nextToken().trim();
608               j++;
609             }
610             
611             try{
612                 if (prefix==null){
613                     config.set(toOriginal(keyName), stringArray);
614                     config.getConfigFile().addEntry(toOriginal(keyName),stringArray,"");
615                 } else {
616                     config.set(toOriginal(prefix+DOT+keyName), stringArray);
617                     config.getConfigFile().addEntry(toOriginal(prefix+DOT+keyName),stringArray,"");
618                 }
619             } catch (Exception JavaDoc e) {
620                 logChannel.write(Logger.DEBUG, e.toString());
621             }
622           } else {
623             try {
624               if (prefix==null){
625                 config.set(toOriginal(keyName), keyValue);
626                 config.getConfigFile().addEntry(toOriginal(keyName),keyValue,"");
627               } else {
628                 config.set(toOriginal(prefix+DOT+keyName), keyValue);
629                 config.getConfigFile().addEntry(toOriginal(prefix+DOT+keyName),keyValue,"");
630               }
631             } catch (Exception JavaDoc e) {
632                 logChannel.write(Logger.DEBUG, e.toString());
633             }
634           }
635           
636         }
637         
638         // remove previously added atributes
639
length = addedAttributes.size();
640         for (int i = 0; i < length; i++) {
641             String JavaDoc keyName = (String JavaDoc) addedAttributes.elementAt(i);
642             if (!hashAttrib.contains(keyName)){
643                 if (prefix==null){
644                     config.remove(toOriginal(keyName));
645                     config.getConfigFile().removeEntry(toOriginal(keyName));
646                 } else {
647                     config.remove(toOriginal(prefix+DOT+keyName));
648                     config.getConfigFile().removeEntry(toOriginal(prefix+DOT+keyName));
649                 }
650             }
651         }
652         addedAttributes.clear();
653       }
654       catch (Exception JavaDoc e) {
655          logChannel.write(Logger.DEBUG, e.toString());
656       }
657       
658       buildDynamicMBeanInfo();
659       Notification JavaDoc note = new Notification JavaDoc(noteTypesReset[0], objectName,
660                                            ++sequence,
661                                            System.currentTimeMillis());
662       sendNotification(note);
663     }
664     
665     /*
666      * -----------------------------------------------------
667      * Remove attribute from config
668      * -----------------------------------------------------
669      */

670     public void removeAttribute(String JavaDoc name) {
671       try {
672         if (name!=null){
673           name = name.trim();
674           if (hashAttrib.containsKey(name)){
675               hashAttrib.remove(name);
676               
677               if (name.endsWith("_Array")) {
678                 int len = name.length();
679                 name = name.substring(0, len-6);
680               }
681               
682               if (prefix==null){
683                 config.remove(toOriginal(name));
684                 config.getConfigFile().removeEntry(toOriginal(name));
685               } else {
686                 config.remove(toOriginal(prefix+DOT+name));
687                 config.getConfigFile().removeEntry(toOriginal(prefix+DOT+name));
688               }
689               
690               buildDynamicMBeanInfo();
691               
692               Notification JavaDoc note = new Notification JavaDoc(noteTypesAdd[0], (NotificationBroadcasterSupport JavaDoc)this,
693                                                      ++sequence,
694                                                      System.currentTimeMillis());
695               sendNotification(note);
696           }
697         }
698       }
699       catch (Exception JavaDoc e) {
700          logChannel.write(Logger.DEBUG, e.toString());
701       }
702     }
703     
704     /**
705      * Allows an operation to be invoked on the Dynamic MBean.
706      */

707     public Object JavaDoc invoke(String JavaDoc operationName, Object JavaDoc params[],
708                          String JavaDoc signature[]) throws MBeanException JavaDoc,
709         ReflectionException JavaDoc {
710       // Check operationName is not null to avoid NullPointerException later on
711
if (operationName == null) {
712         throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
713             "Operation name cannot be null"),
714             "Cannot invoke a null operation in " + dClassName);
715       }
716       // Check for a recognized operation name and call the corresponding operation
717
if (operationName.equals("reset")) {
718         try {
719           reset();
720         }
721         catch (Exception JavaDoc e) {
722           throw new MBeanException JavaDoc(e);
723         }
724         return null;
725       }
726       else if (operationName.equals("saveAttributes")) {
727         saveAttributes();
728         return null;
729       } else if (operationName.equals("addAttribute")) {
730         if (params!=null && params.length==2){
731             addAttribute((String JavaDoc)params[0],(String JavaDoc)params[1]);
732         }
733         return null;
734       } else if (operationName.equals("removeAttribute")) {
735         if (params!=null && params.length==1){
736             removeAttribute((String JavaDoc)params[0]);
737         }
738         return null;
739       } else if (operationName.equals("getAppInfo")) {
740         return getAppInfo();
741       }else {
742         // unrecognized operation name:
743
throw new ReflectionException JavaDoc(new NoSuchMethodException JavaDoc(operationName),
744                                       "Cannot find the operation " +
745                                       operationName + " in " + dClassName);
746       }
747     }
748     
749     public String JavaDoc getAppInfo() {
750       String JavaDoc appInfo = null;
751       Class JavaDoc appClass = application.getClass();
752       try {
753         Method JavaDoc toHtmlMethod = appClass.getMethod("toHtml", null);
754         appInfo = (String JavaDoc)toHtmlMethod.invoke(application, null);
755       }catch (Exception JavaDoc exc){
756         appInfo = application.toString();
757       }
758         return appInfo;
759       }
760     
761 }
762
Popular Tags