JAL-3048 test updated for AlignExportSettings changes
[jalview.git] / srcjar2 / org / apache / log4j / jmx / AbstractDynamicMBean.java
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.util.Enumeration;
21 import java.util.Iterator;
22 import java.util.Vector;
23
24 import javax.management.Attribute;
25 import javax.management.AttributeList;
26 import javax.management.DynamicMBean;
27 import javax.management.InstanceAlreadyExistsException;
28 import javax.management.InstanceNotFoundException;
29 import javax.management.JMException;
30 import javax.management.MBeanRegistration;
31 import javax.management.MBeanRegistrationException;
32 import javax.management.MBeanServer;
33 import javax.management.NotCompliantMBeanException;
34 import javax.management.ObjectName;
35 import javax.management.RuntimeOperationsException;
36
37 import org.apache.log4j.Logger;
38 import org.apache.log4j.Appender;
39
40 public abstract class AbstractDynamicMBean implements DynamicMBean,
41                                                       MBeanRegistration {
42
43   String dClassName;
44   MBeanServer server;
45   private final Vector mbeanList = new Vector();
46
47     /**
48      * Get MBean name.
49      * @param appender appender, may not be null.
50      * @return name.
51      * @since 1.2.16
52      */
53   static protected String getAppenderName(final Appender appender){
54       String name = appender.getName();
55       if (name == null || name.trim().length() == 0) {
56           // try to get some form of a name, because null is not allowed (exception), and empty string certainly isn't useful in JMX..
57           name = appender.toString();
58       }
59       return name;
60   }
61       
62
63   /**
64    * Enables the to get the values of several attributes of the Dynamic MBean.
65    */
66   public
67   AttributeList getAttributes(String[] attributeNames) {
68
69     // Check attributeNames is not null to avoid NullPointerException later on
70     if (attributeNames == null) {
71       throw new RuntimeOperationsException(
72                            new IllegalArgumentException("attributeNames[] cannot be null"),
73                            "Cannot invoke a getter of " + dClassName);
74     }
75
76     AttributeList resultList = new AttributeList();
77
78     // if attributeNames is empty, return an empty result list
79     if (attributeNames.length == 0) {
80         return resultList;
81     }
82
83     // build the result attribute list
84     for (int i=0 ; i<attributeNames.length ; i++){
85       try {
86         Object value = getAttribute(attributeNames[i]);
87         resultList.add(new Attribute(attributeNames[i],value));
88       } catch (JMException e) {
89              e.printStackTrace();
90       } catch (RuntimeException e) {
91              e.printStackTrace();
92       }
93     }
94     return(resultList);
95   }
96
97   /**
98    * Sets the values of several attributes of the Dynamic MBean, and returns the
99    * list of attributes that have been set.
100    */
101   public AttributeList setAttributes(AttributeList attributes) {
102
103     // Check attributes is not null to avoid NullPointerException later on
104     if (attributes == null) {
105       throw new RuntimeOperationsException(
106                     new IllegalArgumentException("AttributeList attributes cannot be null"),
107                     "Cannot invoke a setter of " + dClassName);
108     }
109     AttributeList resultList = new AttributeList();
110
111     // if attributeNames is empty, nothing more to do
112     if (attributes.isEmpty()) {
113         return resultList;
114     }
115
116     // for each attribute, try to set it and add to the result list if successfull
117     for (Iterator i = attributes.iterator(); i.hasNext();) {
118       Attribute attr = (Attribute) i.next();
119       try {
120         setAttribute(attr);
121         String name = attr.getName();
122         Object value = getAttribute(name);
123         resultList.add(new Attribute(name,value));
124       } catch(JMException e) {
125             e.printStackTrace();
126       } catch(RuntimeException e) {
127             e.printStackTrace();
128       }
129     }
130     return(resultList);
131   }
132
133   protected
134   abstract
135   Logger getLogger();
136
137   public
138   void postDeregister() {
139     getLogger().debug("postDeregister is called.");
140   }
141
142   public
143   void postRegister(java.lang.Boolean registrationDone) {
144   }
145
146
147
148   public
149   ObjectName preRegister(MBeanServer server, ObjectName name) {
150     getLogger().debug("preRegister called. Server="+server+ ", name="+name);
151     this.server = server;
152     return name;
153   }
154   /**
155    * Registers MBean instance in the attached server. Must <em>NOT</em>
156    * be called before registration of this instance.
157    */
158   protected
159   void registerMBean(Object mbean, ObjectName objectName)
160   throws InstanceAlreadyExistsException, MBeanRegistrationException,
161                    NotCompliantMBeanException {
162     server.registerMBean(mbean, objectName);
163     mbeanList.add(objectName);
164   }
165
166   /**
167    * Performs cleanup for deregistering this MBean. Default implementation
168    * unregisters MBean instances which are registered using 
169    * {@link #registerMBean(Object mbean, ObjectName objectName)}.
170    */
171    public
172    void preDeregister() {
173      getLogger().debug("preDeregister called.");
174      
175     Enumeration iterator = mbeanList.elements();
176     while (iterator.hasMoreElements()) {
177       ObjectName name = (ObjectName) iterator.nextElement();
178       try {
179         server.unregisterMBean(name);
180       } catch (InstanceNotFoundException e) {
181    getLogger().warn("Missing MBean " + name.getCanonicalName());
182       } catch (MBeanRegistrationException e) {
183    getLogger().warn("Failed unregistering " + name.getCanonicalName());
184       }
185     }
186    }
187
188
189 }