KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > ConfigMBean


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 /*
25  * $Id: ConfigMBean.java,v 1.3 2005/12/25 03:42:13 tcfujii Exp $
26  */

27
28 package com.sun.enterprise.admin.mbeans;
29
30 import java.lang.IllegalArgumentException JavaDoc;
31
32 //JMX imports
33
import javax.management.AttributeList JavaDoc;
34 import javax.management.Attribute JavaDoc;
35 import javax.management.MBeanException JavaDoc;
36 import javax.management.ReflectionException JavaDoc;
37 import javax.management.AttributeNotFoundException JavaDoc;
38
39 import com.sun.enterprise.admin.meta.MBeanRegistryFactory;
40
41 //event handling
42
import com.sun.enterprise.admin.event.EventContext;
43 import com.sun.enterprise.admin.event.DynamicReconfigEvent;
44
45 //config imports
46
import com.sun.enterprise.admin.config.BaseConfigMBean;
47 import com.sun.enterprise.config.serverbeans.ServerTags;
48 import com.sun.enterprise.admin.AdminContext;
49
50 // Logging
51
import java.util.logging.Level JavaDoc;
52
53 import com.sun.enterprise.admin.servermgmt.pe.PEInstancesManager;
54 import com.sun.enterprise.admin.servermgmt.RepositoryConfig;
55 import com.sun.enterprise.admin.servermgmt.RuntimeStatus;
56 import com.sun.enterprise.admin.servermgmt.InstanceException;
57 import com.sun.enterprise.util.SystemPropertyConstants;
58 import com.sun.enterprise.util.i18n.StringManager;
59
60 public class ConfigMBean extends BaseConfigMBean
61 {
62     
63      /****************************************************************************************************************
64      * Hook for standard setAttributes() to detect change for
65      * dynamic-reconfiguration-enabled attribute Sets the values of several MBean's attributes.
66      * @param attrList A list of attributes: The identification of the attributes to be set and the values they are to be set to.
67      * @return The list of attributes that were set, with their new values.
68      */

69     public AttributeList JavaDoc setAttributes(AttributeList JavaDoc list) {
70             
71             boolean bEnabled = false;
72             int reconfigIdx = -1;
73             if(list!=null)
74                 for(int i=0; i<list.size(); i++)
75                 {
76                     Attribute JavaDoc attr = (Attribute JavaDoc)list.get(i);
77                     if(ServerTags.DYNAMIC_RECONFIGURATION_ENABLED.equals(attr.getName()))
78                     {
79                         try {
80                             bEnabled = validateDynamicReconfigEvent(attr.getValue());
81                         } catch(Exception JavaDoc e) {
82                             reconfigIdx = i;
83                         }
84                     }
85                        
86                 }
87
88             if (reconfigIdx != -1) {
89                 list.remove(reconfigIdx);
90             }
91             //then, call super to perform operation
92
list = super.setAttributes(list);
93
94             if (reconfigIdx != -1) {
95                 return list;
96             }
97
98             //now analyse if dynamic-reconfiguration-enabled attribute changed
99
if(list!=null)
100                 for(int i=0; i<list.size(); i++)
101                 {
102                     Attribute JavaDoc attr = (Attribute JavaDoc)list.get(i);
103                     if(ServerTags.DYNAMIC_RECONFIGURATION_ENABLED.equals(attr.getName()))
104                     {
105                         emitDynamicReconfigEvent(bEnabled);
106                     }
107                        
108                 }
109             return list;
110     }
111
112     /**
113      * Set the value of a specific attribute of this MBean.
114      *
115      * @param attr The identification of the attribute to be set
116      * and the new value
117      *
118      * @exception AttributeNotFoundException if this attribute is not
119      * supported by this MBean
120      * @exception MBeanException if the initializer of an object
121      * throws an exception
122      * @exception ReflectionException if a Java reflection exception
123      * occurs when invoking the getter
124      */

125     public void setAttribute(Attribute JavaDoc attr)
126         throws AttributeNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
127     {
128         boolean bEnabled = false;
129         //first analyse if dynamic-reconfiguration-enabled attribute changed
130
if(ServerTags.DYNAMIC_RECONFIGURATION_ENABLED.equals(attr.getName()))
131         {
132             bEnabled = validateDynamicReconfigEvent(attr.getValue());
133         }
134         //next, call super to perform operation
135
super.setAttribute(attr);
136         if(ServerTags.DYNAMIC_RECONFIGURATION_ENABLED.equals(attr.getName()))
137         {
138             emitDynamicReconfigEvent(bEnabled);
139         }
140     }
141     
142     private boolean validateDynamicReconfigEvent(Object JavaDoc value) throws IllegalArgumentException JavaDoc, MBeanException JavaDoc
143     {
144         boolean bEnabled = false;
145         if(value instanceof Boolean JavaDoc)
146             bEnabled = ((Boolean JavaDoc)value).booleanValue();
147         else
148             if("true".equalsIgnoreCase(value.toString()) ||
149                "yes".equalsIgnoreCase(value.toString()) )
150                 bEnabled = true;
151
152         boolean restartRequired = false;
153         try {
154             restartRequired = getRuntimeStatus().isRestartNeeded();
155         } catch(InstanceException ie) {
156             throw new MBeanException JavaDoc(ie);
157         }
158
159         if((bEnabled == true) && ( restartRequired == true)) {
160             String JavaDoc msg = _strMgr.getString(
161                 "admin.mbeans.configMBean.serverRequiresRestart");
162             Exception JavaDoc e = new Exception JavaDoc(msg);
163             throw new MBeanException JavaDoc(e,msg);
164         }
165
166         return bEnabled;
167     }
168     private void emitDynamicReconfigEvent(boolean bEnabled)
169     {
170         try
171         {
172             AdminContext adminContext = MBeanRegistryFactory.getAdminContext();
173             String JavaDoc instanceName = adminContext.getServerName();
174             int action = bEnabled?DynamicReconfigEvent.ACTION_ENABLED:DynamicReconfigEvent.ACTION_DISABLED;
175             DynamicReconfigEvent event = new DynamicReconfigEvent(instanceName, action);
176             String JavaDoc configName = (String JavaDoc)getAttribute(ServerTags.NAME);
177             event.setTargetDestination(configName);
178             EventContext.addEvent(event);
179         }
180         catch (Exception JavaDoc e)
181         {
182             e.printStackTrace();
183
184             //throw new MBeanException(e.getMessage(), e);
185
}
186     }
187
188     private RuntimeStatus getRuntimeStatus() throws InstanceException
189     {
190         String JavaDoc serverName =
191             System.getProperty(SystemPropertyConstants.SERVER_NAME);
192         PEInstancesManager manager = new PEInstancesManager(new RepositoryConfig());
193         return RuntimeStatus.getRuntimeStatus(serverName, manager);
194     }
195     ///////////////////////////////////////////////////////////////////////////
196

197     private static final StringManager _strMgr =
198                 StringManager.getManager(ConfigMBean.class);
199     ///////////////////////////////////////////////////////////////////////////
200

201 }
202
Popular Tags