KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > custom > InProcessMBeanElementChangeEventListenerImpl


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.admin.mbeans.custom;
25
26 import com.sun.enterprise.admin.common.MBeanServerFactory;
27 import com.sun.enterprise.admin.common.constant.AdminConstants;
28 import com.sun.enterprise.admin.event.AdminEventListenerException;
29 import com.sun.enterprise.admin.event.MBeanElementChangeEvent;
30 import com.sun.enterprise.admin.event.MBeanElementChangeEventListener;
31 import com.sun.enterprise.admin.mbeans.custom.loading.CustomMBeanRegistrationImpl;
32 import com.sun.enterprise.admin.mbeans.custom.loading.MBeanAttributeSetter;
33 import com.sun.enterprise.admin.server.core.CustomMBeanRegistration;
34 import com.sun.enterprise.config.ConfigAdd;
35 import com.sun.enterprise.config.ConfigChange;
36 import com.sun.enterprise.config.ConfigContext;
37 import com.sun.enterprise.config.ConfigDelete;
38 import com.sun.enterprise.config.serverbeans.ElementProperty;
39 import com.sun.enterprise.config.serverbeans.Mbean;
40 import com.sun.enterprise.config.serverbeans.ApplicationRef;
41 import com.sun.enterprise.config.serverbeans.ApplicationHelper;
42 import com.sun.enterprise.config.serverbeans.Server;
43 import java.util.ArrayList JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45 import javax.management.MBeanServer JavaDoc;
46 import javax.management.ObjectName JavaDoc;
47 import javax.management.JMException JavaDoc;
48 import com.sun.enterprise.config.ConfigException;
49
50 public class InProcessMBeanElementChangeEventListenerImpl implements MBeanElementChangeEventListener {
51     
52     private static final Logger JavaDoc logger = Logger.getLogger(AdminConstants.kLoggerName);
53     private final MBeanServer JavaDoc mbs = MBeanServerFactory.getMBeanServer();
54     public InProcessMBeanElementChangeEventListenerImpl() {
55     }
56
57     public void handleUpdate(final MBeanElementChangeEvent event) throws AdminEventListenerException {
58             /* Note that none of the attributes of "mbean" element except "enabled" and "object-name" are modifiable dynamically. Only the properties in this element,
59             that are actually the attributes of runtime mbean are modifiable dynamically. Also, note that there need
60             not be any validation of the attribute changes in this listener, because that is supposed to happen at
61             the configuration time. Hence a ConfigChange has to be only creation/deletion/update of a property element or the two attributes discussed above.
62             Every such change should result in setting the corresponding attribute on the MBean that is already registered. */

63         try {
64             final ConfigContext rcc = event.getConfigContext();
65             final ArrayList JavaDoc<ConfigChange> changes = event.getConfigChangeList();
66             for (ConfigChange change : changes) {
67                 handleUpdate(rcc, change);
68             }
69         } catch (final AdminEventListenerException ae) {
70             throw ae;
71         } catch (final Exception JavaDoc e) {
72             throw new AdminEventListenerException(e);
73         }
74     }
75
76     public void handleCreate(final MBeanElementChangeEvent event) throws AdminEventListenerException {
77         try {
78             final ConfigContext rcc = event.getConfigContext();
79             final ArrayList JavaDoc<ConfigAdd> additions = event.getConfigChangeList();
80             //the list should is supposed to contain ConfigAdd elements only. Hence the above assignment should be safe
81
for (ConfigAdd added : additions) {
82                 final String JavaDoc xp = added.getXPath();
83                 if (xp != null) {
84                     Object JavaDoc co = rcc.exactLookup(xp);
85                     if (co instanceof ApplicationRef) {
86                         co = ApplicationHelper.findApplication(rcc,
87                                 ((ApplicationRef)co).getRef());
88                         if (co instanceof Mbean) {
89                             Mbean mb = (Mbean)co;
90                             if(mb.isEnabled())
91                                 register(mb);
92                         }
93                     }
94                 }
95             }
96         } catch (final Exception JavaDoc e) {
97             throw new AdminEventListenerException(e);
98         }
99     }
100
101     public void handleDelete(final MBeanElementChangeEvent event) throws AdminEventListenerException {
102         try {
103             final ConfigContext occ = event.getOldConfigContext();
104             final ArrayList JavaDoc<ConfigDelete> deletions = event.getConfigChangeList();
105             //the list should is supposed to contain Delete elements only. Hence the above assignment should be safe
106
for (ConfigDelete deleted : deletions) {
107                 final String JavaDoc xp = deleted.getXPath();
108                 if (xp != null) {
109                     Object JavaDoc co = occ.exactLookup(xp);
110                     if (co instanceof ApplicationRef) {
111                         co = ApplicationHelper.findApplication(occ,
112                                 ((ApplicationRef)co).getRef());
113                         if (co instanceof Mbean) {
114                             final Mbean mbean = (Mbean)co;
115                             unregister(mbean);
116                             logger.info(CMBStrings.get("cmb.successfulDelete", mbean.getName()));
117                         }
118                     }
119                 }
120             }
121         } catch (final Exception JavaDoc e) {
122             throw new AdminEventListenerException(e);
123         }
124     }
125
126     protected void register(final Mbean mbean) throws Exception JavaDoc {
127         final ObjectName JavaDoc on = CustomMBeanRegistrationImpl.getCascadingAwareObjectName(mbean);
128         if (mbs.isRegistered(on)) {
129             logger.info(CMBStrings.get("cmb.unsuccessfulRegistration", on));
130         }
131         else {
132             final CustomMBeanRegistration cmr = new CustomMBeanRegistrationImpl(mbs);
133             cmr.registerMBean(mbean);
134             logger.info(CMBStrings.get("cmb.successfulRegistration", mbean.getName()));
135         }
136     }
137     
138     protected void unregister(final Mbean mbean) throws Exception JavaDoc {
139         // this is the best-case effort
140
// final String ons = mbean.getObjectName();
141
// final ObjectName on = new ObjectName(ons);
142
final ObjectName JavaDoc on = CustomMBeanRegistrationImpl.getCascadingAwareObjectName(mbean);
143         if (mbs.isRegistered(on)) {
144             mbs.unregisterMBean(on);
145             logger.info(CMBStrings.get("cmb.successfulUnRegistration", on));
146         }
147         else {
148             logger.info(CMBStrings.get("cmb.unsuccessfulUnRegistration", on));
149         }
150     }
151     ///// Private methods /////
152
///// Private methods /////
153
private void handleUpdate(ConfigContext rcc, ConfigChange change) throws JMException JavaDoc, ConfigException, AdminEventListenerException {
154         final String JavaDoc xp = change.getXPath();
155
156         if (xp == null)
157             return;
158
159         logger.fine(CMBStrings.get("cmb.gotConfigChange", xp));
160
161         final Object JavaDoc changedObject = rcc.exactLookup(xp);
162
163         final String JavaDoc parentPath = xp.substring(0, xp.lastIndexOf("/"));
164         Object JavaDoc parent = null;
165         try {
166             parent = rcc.exactLookup(parentPath);
167         } catch(Exception JavaDoc e) {
168             // ignore for now
169
}
170         
171         if (changedObject instanceof Mbean) {
172             handleUpdate((Mbean)changedObject);
173         } else if (changedObject instanceof ElementProperty) {
174             handleUpdate(parent, (ElementProperty)changedObject);
175         } else if (changedObject instanceof ApplicationRef) {
176             handleUpdate(rcc, (ApplicationRef)changedObject);
177         }
178         else {
179             throw new AdminEventListenerException(CMBStrings.get("InternalError", "Can't handle this: ", changedObject.getClass()));
180         }
181     }
182     
183     private void handleUpdate(Mbean mbean){
184         logger.info(CMBStrings.get("cmb.illegalHandleUpdate", mbean.getName()));
185     }
186
187     private void handleUpdate(Object JavaDoc parent, ElementProperty ep) throws JMException JavaDoc{
188         final Mbean mbean = (Mbean) parent;
189         if (! mbean.isEnabled()) {
190             logger.info(CMBStrings.get("cmb.mbeanIsDisabled", mbean.getName()));
191             return;
192         }
193         final ObjectName JavaDoc on = new ObjectName JavaDoc(mbean.getObjectName());
194         final MBeanAttributeSetter mas = new MBeanAttributeSetter(mbs, on);
195         mas.setIt(ep.getName(), ep.getValue());
196     }
197     
198     private void handleUpdate(ConfigContext rcc, ApplicationRef ref) throws JMException JavaDoc, ConfigException, AdminEventListenerException{
199         String JavaDoc mbeanName = ref.getRef();
200         Mbean mbean = (Mbean)ApplicationHelper.findApplication(rcc, mbeanName);
201
202         // note: register and unregister declares "throws Exception". The cleanest
203
// way to deal with that is to simply wrap "Exception" and throw it back out.
204
// Otherwise I'd have to declare "throws Exception" on each method all the way back up the
205
// call stack
206

207         try {
208             if(ref.isEnabled()) {
209                 register(mbean);
210             }
211             else {
212                 unregister(mbean);
213             }
214         }catch(Exception JavaDoc e) {
215             throw new AdminEventListenerException(e);
216         }
217     }
218 }
Popular Tags