View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.log4j.jmx;
19  
20  //import java.lang.reflect.Constructor;
21  import java.util.Iterator;
22  import javax.management.DynamicMBean;
23  import javax.management.AttributeList;
24  import javax.management.Attribute;
25  import javax.management.RuntimeOperationsException;
26  import javax.management.MBeanRegistration;
27  import javax.management.MBeanServer;
28  import javax.management.ObjectName;
29  
30  import org.apache.log4j.Logger;
31  
32  public abstract class AbstractDynamicMBean implements DynamicMBean,
33                                                        MBeanRegistration {
34  
35    String dClassName;
36    MBeanServer server;
37  
38    /***
39     * Enables the to get the values of several attributes of the Dynamic MBean.
40     */
41    public
42    AttributeList getAttributes(String[] attributeNames) {
43  
44      // Check attributeNames is not null to avoid NullPointerException later on
45      if (attributeNames == null) {
46        throw new RuntimeOperationsException(
47  			   new IllegalArgumentException("attributeNames[] cannot be null"),
48  			   "Cannot invoke a getter of " + dClassName);
49      }
50  
51      AttributeList resultList = new AttributeList();
52  
53      // if attributeNames is empty, return an empty result list
54      if (attributeNames.length == 0)
55        return resultList;
56  
57      // build the result attribute list
58      for (int i=0 ; i<attributeNames.length ; i++){
59        try {
60  	Object value = getAttribute((String) attributeNames[i]);
61  	resultList.add(new Attribute(attributeNames[i],value));
62        } catch (Exception e) {
63  	e.printStackTrace();
64        }
65      }
66      return(resultList);
67    }
68  
69    /***
70     * Sets the values of several attributes of the Dynamic MBean, and returns the
71     * list of attributes that have been set.
72     */
73    public AttributeList setAttributes(AttributeList attributes) {
74  
75      // Check attributes is not null to avoid NullPointerException later on
76      if (attributes == null) {
77        throw new RuntimeOperationsException(
78                      new IllegalArgumentException("AttributeList attributes cannot be null"),
79  		    "Cannot invoke a setter of " + dClassName);
80      }
81      AttributeList resultList = new AttributeList();
82  
83      // if attributeNames is empty, nothing more to do
84      if (attributes.isEmpty())
85        return resultList;
86  
87      // for each attribute, try to set it and add to the result list if successfull
88      for (Iterator i = attributes.iterator(); i.hasNext();) {
89        Attribute attr = (Attribute) i.next();
90        try {
91  	setAttribute(attr);
92  	String name = attr.getName();
93  	Object value = getAttribute(name);
94  	resultList.add(new Attribute(name,value));
95        } catch(Exception e) {
96  	e.printStackTrace();
97        }
98      }
99      return(resultList);
100   }
101 
102   protected
103   abstract
104   Logger getLogger();
105 
106   public
107   void postDeregister() {
108     getLogger().debug("postDeregister is called.");
109   }
110 
111   public
112   void postRegister(java.lang.Boolean registrationDone) {
113   }
114 
115 
116 
117   public
118   void preDeregister() {
119     getLogger().debug("preDeregister called.");
120   }
121 
122   public
123   ObjectName preRegister(MBeanServer server, ObjectName name) {
124     getLogger().debug("preRegister called. Server="+server+ ", name="+name);
125     this.server = server;
126     return name;
127   }
128 
129 
130 
131 }