KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > jmx > AbstractDynamicMBean


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j.jmx;
18
19 //import java.lang.reflect.Constructor;
20
import java.util.Iterator JavaDoc;
21 import javax.management.DynamicMBean JavaDoc;
22 import javax.management.AttributeList JavaDoc;
23 import javax.management.Attribute JavaDoc;
24 import javax.management.RuntimeOperationsException JavaDoc;
25 import javax.management.MBeanRegistration JavaDoc;
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28
29 import org.apache.log4j.Logger;
30
31 public abstract class AbstractDynamicMBean implements DynamicMBean JavaDoc,
32                                                       MBeanRegistration JavaDoc {
33
34   String JavaDoc dClassName;
35   MBeanServer JavaDoc server;
36
37   /**
38    * Enables the to get the values of several attributes of the Dynamic MBean.
39    */

40   public
41   AttributeList JavaDoc getAttributes(String JavaDoc[] attributeNames) {
42
43     // Check attributeNames is not null to avoid NullPointerException later on
44
if (attributeNames == null) {
45       throw new RuntimeOperationsException JavaDoc(
46                new IllegalArgumentException JavaDoc("attributeNames[] cannot be null"),
47                "Cannot invoke a getter of " + dClassName);
48     }
49
50     AttributeList JavaDoc resultList = new AttributeList JavaDoc();
51
52     // if attributeNames is empty, return an empty result list
53
if (attributeNames.length == 0)
54       return resultList;
55
56     // build the result attribute list
57
for (int i=0 ; i<attributeNames.length ; i++){
58       try {
59     Object JavaDoc value = getAttribute((String JavaDoc) attributeNames[i]);
60     resultList.add(new Attribute JavaDoc(attributeNames[i],value));
61       } catch (Exception JavaDoc e) {
62     e.printStackTrace();
63       }
64     }
65     return(resultList);
66   }
67
68   /**
69    * Sets the values of several attributes of the Dynamic MBean, and returns the
70    * list of attributes that have been set.
71    */

72   public AttributeList JavaDoc setAttributes(AttributeList JavaDoc attributes) {
73
74     // Check attributes is not null to avoid NullPointerException later on
75
if (attributes == null) {
76       throw new RuntimeOperationsException JavaDoc(
77                     new IllegalArgumentException JavaDoc("AttributeList attributes cannot be null"),
78             "Cannot invoke a setter of " + dClassName);
79     }
80     AttributeList JavaDoc resultList = new AttributeList JavaDoc();
81
82     // if attributeNames is empty, nothing more to do
83
if (attributes.isEmpty())
84       return resultList;
85
86     // for each attribute, try to set it and add to the result list if successfull
87
for (Iterator JavaDoc i = attributes.iterator(); i.hasNext();) {
88       Attribute JavaDoc attr = (Attribute JavaDoc) i.next();
89       try {
90     setAttribute(attr);
91     String JavaDoc name = attr.getName();
92     Object JavaDoc value = getAttribute(name);
93     resultList.add(new Attribute JavaDoc(name,value));
94       } catch(Exception JavaDoc e) {
95     e.printStackTrace();
96       }
97     }
98     return(resultList);
99   }
100
101   protected
102   abstract
103   Logger getLogger();
104
105   public
106   void postDeregister() {
107     getLogger().debug("postDeregister is called.");
108   }
109
110   public
111   void postRegister(java.lang.Boolean JavaDoc registrationDone) {
112   }
113
114
115
116   public
117   void preDeregister() {
118     getLogger().debug("preDeregister called.");
119   }
120
121   public
122   ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name) {
123     getLogger().debug("preRegister called. Server="+server+ ", name="+name);
124     this.server = server;
125     return name;
126   }
127
128
129
130 }
131
Popular Tags