KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > mbean > config > ManagedStandaloneJ2EEWebModule


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.server.core.mbean.config;
25
26 //Config imports
27
import com.sun.enterprise.config.ConfigException;
28 import com.sun.enterprise.config.serverbeans.ServerXPathHelper;
29 import com.sun.enterprise.config.serverbeans.ServerTags;
30 //import com.sun.enterprise.config.serverbeans.WebModule;
31

32 //JMX imports
33
import javax.management.Attribute JavaDoc;
34
35 //Admin imports
36
import com.sun.enterprise.admin.common.ObjectNames;
37 import com.sun.enterprise.admin.common.EntityStatus;
38 import com.sun.enterprise.admin.common.constant.ConfigAttributeName;
39 import com.sun.enterprise.admin.common.exception.J2EEWebModuleException;
40 import com.sun.enterprise.admin.common.exception.MBeanConfigException;
41
42 //for jsr77
43
import com.sun.enterprise.instance.InstanceEnvironment;
44 import com.sun.enterprise.admin.event.AdminEvent;
45 import com.sun.enterprise.admin.event.AdminEventCache;
46 import com.sun.enterprise.admin.event.AdminEventResult;
47 import com.sun.enterprise.admin.event.ModuleDeployEvent;
48 import com.sun.enterprise.admin.event.BaseDeployEvent;
49 import com.sun.enterprise.admin.event.AdminEventMulticaster;
50 import com.sun.enterprise.admin.server.core.channel.RMIClient;
51 import com.sun.enterprise.admin.server.core.channel.AdminChannel;
52 import com.sun.enterprise.admin.common.Status;
53 import com.sun.enterprise.admin.common.MBeanServerFactory;
54 import javax.management.MBeanServer JavaDoc;
55 import javax.management.ObjectName JavaDoc;
56 import com.sun.enterprise.server.ApplicationServer;
57 import com.sun.enterprise.server.ServerContext;
58 import com.sun.enterprise.management.StateManageable;
59
60
61 /**
62     A class that represents a Standalone Managed J2EE Web module. Note that this is a
63     standalone J2EE module, because it is deployed independently and not
64     as a part of an application. Such a Web Module has certain additional
65     manageable interface.
66     <p>
67     When a Web ARchive is deployed, an instance of this MBean is created in
68     the MBeanServer. The MBean is deregistered when the module is removed.
69     <p>
70     ObjectName of this MBean is:
71         ias:type=StandaloneJ2EEWebModule, name=<moduleName>
72 */

73
74 public class ManagedStandaloneJ2EEWebModule extends ConfigMBeanBase implements ConfigAttributeName.StandaloneWebModule
75 {
76     /**
77      * Methods start and stop will use this attribute to store
78      * the current state, RUNNING or STOPPED
79      * These states correspond to jsr77 states
80      * RUNNING_STATE (1) and STOPPED_STATE (3)
81      */

82     private int state = StateManageable.RUNNING_STATE;
83
84     private static final String JavaDoc[][] MAPLIST =
85     {
86         {kName , ATTRIBUTE + ServerTags.NAME},
87         {kContextRoot , ATTRIBUTE + ServerTags.CONTEXT_ROOT},
88         {kLocation , ATTRIBUTE + ServerTags.LOCATION},
89         {kVirtualServers , ATTRIBUTE + ServerTags.VIRTUAL_SERVERS},
90 //ms1 {kEnabled , ATTRIBUTE + ServerTags.ENABLED},
91
{kDescription , ATTRIBUTE + PSEUDO_ATTR_DESCRIPTION},
92     };
93
94     private static final String JavaDoc[] ATTRIBUTES =
95     {
96         kName + ", String, R" ,
97         kContextRoot + ", String, RW" ,
98         kLocation + ", String, RW" ,
99         kVirtualServers + ", String, RW" ,
100 //ms1 kEnabled + ", boolean, RW" ,
101
kDescription + ", String, RW" ,
102     };
103
104     private static final String JavaDoc[] OPERATIONS =
105     {
106         "getStatus(), INFO",
107         "enable(), ACTION",
108         "disable(), ACTION",
109         "start(), ACTION",
110         "stop(), ACTION",
111         "getState(), INFO"
112     };
113
114     /**
115         Default constructor sets MBean description tables
116     */

117     public ManagedStandaloneJ2EEWebModule() throws MBeanConfigException
118     {
119         this.setDescriptions(MAPLIST, ATTRIBUTES, OPERATIONS);
120     }
121
122     public ManagedStandaloneJ2EEWebModule(String JavaDoc instanceName, String JavaDoc moduleName)
123         throws MBeanConfigException
124     {
125         this(); //set description tables
126
initialize(ObjectNames.kStandaloneWebModule, new String JavaDoc[]{instanceName, moduleName});
127     }
128
129     /**
130      * Returns the Status of this module.
131      * @throws J2EEEjbJarModuleException if the status can't be retrieved.
132     */

133     public EntityStatus getStatus() throws J2EEWebModuleException
134     {
135         EntityStatus status = null;
136         try
137         {
138             boolean isModuleEnabled = true; //FIXME for RI only
139
// boolean isModuleEnabled = ((Boolean)this.getAttribute(kEnabled)).booleanValue();
140
status = new EntityStatus();
141             if (isModuleEnabled)
142             {
143                 status.setEnabled();
144             }
145             else
146             {
147                 status.setDisabled();
148             }
149         }
150         catch (Exception JavaDoc e)
151         {
152             sLogger.throwing(getClass().getName(), "getStatus", e);
153             throw new J2EEWebModuleException(e.getMessage());
154         }
155         return status;
156     }
157
158     /**
159      * Disables this module.
160      * @throws J2EEEjbJarModuleException if there is some error during
161      * disabling.
162     */

163     public void disable() throws J2EEWebModuleException
164     {
165         return; //FIXME for RI only;
166
/* try
167         {
168             this.setAttribute(new Attribute(kEnabled, new Boolean(false)));
169             super.getConfigContext().flush();
170         }
171         catch (Exception e)
172         {
173             sLogger.throwing(getClass().getName(), "disable", e);
174             throw new J2EEWebModuleException(e.getMessage());
175         }
176 */

177     }
178
179
180     /**
181      * Disables the application. Difference between this method
182      * and disable is persistence of the state. Disable method persists
183      * the state and this method does not persist the state.
184      */

185     public void stop() throws J2EEWebModuleException {
186     state = StateManageable.STOPPING_STATE;
187         try {
188             String JavaDoc moduleName = (String JavaDoc)this.getAttribute(kName);
189             multicastAdminEvent(moduleName, BaseDeployEvent.DISABLE);
190         state = StateManageable.STOPPED_STATE;
191         } catch (Exception JavaDoc e) {
192             sLogger.throwing(getClass().getName(), "stop", e);
193         state = StateManageable.FAILED_STATE;
194             throw new J2EEWebModuleException(e.getMessage());
195         }
196     }
197
198     /**
199      * Gets the jsr77 state corresponding to this module
200      */

201     public Integer JavaDoc getState() throws J2EEWebModuleException {
202
203         return (new Integer JavaDoc(state));
204
205     /**
206      * Commented the following since Tomcat container is
207      * not ready with state management functionality
208      * Until such time it will return the value for state
209      * set by start and stop.
210      *
211      * Once the Tomcat functionality is ready this needs to be modified
212      *
213      */

214
215     /*
216         try {
217             MBeanServer mbs = MBeanServerFactory.getMBeanServer();
218             ServerContext serverContext = ApplicationServer.getServerContext();
219             ObjectName objName = new ObjectName(
220                 serverContext.getDefaultDomainName() + ":" +
221                 "j2eeType=WebModule," +
222                 "name=" + ((String)this.getAttribute(kName)) + "," +
223         "J2EEApplication=" + "null" + "," +
224         "J2EEServer=" + serverContext.getInstanceName());
225             Integer intObj = (Integer) mbs.getAttribute(objName, "state");
226         return intObj;
227         } catch (Exception e) {
228             sLogger.throwing(getClass().getName(), "getState", e);
229             throw new J2EEWebModuleException(e.getMessage());
230         }
231     */

232     }
233
234
235     /**
236      * Enables this module.
237      * @throws J2EEEjbJarModuleException if there is some error during
238      * enablement.
239     */

240     public void enable() throws J2EEWebModuleException
241     {
242         return; //FIXME for RI only;
243
/* try
244         {
245             this.setAttribute(new Attribute(kEnabled, new Boolean(true)));
246             super.getConfigContext().flush();
247         }
248         catch (Exception e)
249         {
250             sLogger.throwing(getClass().getName(), "enable", e);
251             throw new J2EEWebModuleException(e.getMessage());
252         }
253 */

254     }
255
256     /**
257      * Enables the application. Difference between this method
258      * and enable is persistence of the state. Enable method persists
259      * the state and this method does not persist the state.
260      */

261     public void start() throws J2EEWebModuleException {
262     state = StateManageable.STARTING_STATE;
263         try {
264             String JavaDoc moduleName = (String JavaDoc)this.getAttribute(kName);
265             multicastAdminEvent(moduleName, BaseDeployEvent.ENABLE);
266         state = StateManageable.RUNNING_STATE;
267         } catch (Exception JavaDoc e) {
268             sLogger.throwing(getClass().getName(), "start", e);
269         state = StateManageable.FAILED_STATE;
270             throw new J2EEWebModuleException(e.getMessage());
271         }
272     }
273
274     public String JavaDoc[] getServlets() throws J2EEWebModuleException
275     {
276         try
277         {
278             String JavaDoc location = (String JavaDoc)this.getAttribute(kLocation);
279             return ModulesXMLHelper.getServletsForWebModule(location, null);
280         }
281         catch (Exception JavaDoc e)
282         {
283             sLogger.throwing(getClass().getName(), "getServlets", e);
284             throw new J2EEWebModuleException(e.getMessage());
285         }
286     }
287
288
289     /*
290         all the other ias-ejb-jar.xml parameters that are exposed are
291         taken from the super class ManagedJ2EEEjbJarModule
292     */

293
294     /**
295      * Multicasts the admin event so that the application gets loaded
296      * dynamically without the need for reconfig.
297      */

298     private void multicastAdminEvent(String JavaDoc entityName, String JavaDoc actionCode)
299                         throws J2EEWebModuleException {
300         String JavaDoc instanceName = super.getServerInstanceName();
301         InstanceEnvironment instEnv = new InstanceEnvironment(instanceName);
302         /*
303         try {
304                 instEnv.applyServerXmlChanges(false);
305         } catch (Exception e) {
306             sLogger.throwing(getClass().getName(), "getAttr", e);
307             throw new J2EEWebModuleException(e.getMessage());
308         }
309          **/

310         AdminEvent event = new ModuleDeployEvent(instanceName, entityName, "web", actionCode);
311         //AdminEventCache.populateConfigChange(super.getConfigContext(), event);
312
RMIClient serverInstancePinger = AdminChannel.getRMIClient(instanceName);
313         if (serverInstancePinger.getInstanceStatusCode() != Status.kInstanceRunningCode) {
314                     return;
315         }
316         AdminEventResult multicastResult = AdminEventMulticaster.multicastEvent(event);
317         if (!AdminEventResult.SUCCESS.equals(multicastResult.getResultCode())) {
318                 AdminEventCache cache = AdminEventCache.getInstance(instanceName);
319                     cache.setRestartNeeded(true);
320         }
321     }
322 }
323
Popular Tags