KickJava   Java API By Example, From Geeks To Geeks.

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


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.J2eeApplication;
31

32 //JMX imports
33
import javax.management.Attribute JavaDoc;
34 import javax.management.AttributeNotFoundException JavaDoc;
35
36 //Admin Imports
37
import com.sun.enterprise.admin.AdminContext;
38 import com.sun.enterprise.admin.common.ObjectNames;
39 import com.sun.enterprise.admin.common.EntityStatus;
40 import com.sun.enterprise.admin.common.constant.ConfigAttributeName;
41 import com.sun.enterprise.admin.common.exception.J2EEApplicationException;
42 import com.sun.enterprise.admin.common.exception.MBeanConfigException;
43
44 //for jsr77
45
import com.sun.enterprise.instance.InstanceEnvironment;
46 import com.sun.enterprise.admin.event.AdminEvent;
47 import com.sun.enterprise.admin.event.AdminEventCache;
48 import com.sun.enterprise.admin.event.AdminEventResult;
49 import com.sun.enterprise.admin.event.ApplicationDeployEvent;
50 import com.sun.enterprise.admin.event.ModuleDeployEvent;
51 import com.sun.enterprise.admin.event.BaseDeployEvent;
52 import com.sun.enterprise.admin.event.AdminEventMulticaster;
53 import com.sun.enterprise.admin.server.core.channel.RMIClient;
54 import com.sun.enterprise.admin.server.core.channel.AdminChannel;
55 import com.sun.enterprise.admin.common.Status;
56 import com.sun.enterprise.admin.common.MBeanServerFactory;
57 import javax.management.MBeanServer JavaDoc;
58 import javax.management.ObjectName JavaDoc;
59 import com.sun.enterprise.server.ApplicationServer;
60 import com.sun.enterprise.server.ServerContext;
61 import com.sun.enterprise.Switch;
62
63
64 /**
65     A class that represents any deployed J2EE application to a Server Instance.
66     In other words it exposes all the parameters of an application that can be
67     managed and all the operations that can be invoked on it. Whenever
68     a J2EE application is deployed to a Server Instance, a corresponding instance
69     of this class is registered in the MBeanServer. On removal of the application,
70     the MBean will be deregistered.
71     <p>
72     There will be as many instances of this MBean as there are deployed
73     J2EE applications in the Server Instance. Note that not all deployed
74         applications are running (or enabled).
75     <p>
76     ObjectName of this MBean is:
77                 ias:type=J2EEApplication, name=<appName>, InstanceName=<instanceName>
78 */

79
80 public class ManagedJ2EEApplication extends ConfigMBeanBase implements ConfigAttributeName.J2EEApplication
81 {
82     private static final String JavaDoc[][] MAPLIST =
83     {
84         {kName , ATTRIBUTE + ServerTags.NAME},
85         {kLocation , ATTRIBUTE + ServerTags.LOCATION},
86         {kVirtualServers , ATTRIBUTE + ServerTags.VIRTUAL_SERVERS},
87 // {kEnabled , ATTRIBUTE + ServerTags.ENABLED},
88
{kDescription , ATTRIBUTE + PSEUDO_ATTR_DESCRIPTION},
89     };
90
91     private static final String JavaDoc[] ATTRIBUTES =
92     {
93         kName + ", String, R" ,
94         kLocation + ", String, RW" ,
95         kVirtualServers + ", String, RW" ,
96 // kEnabled + ", boolean, RW" ,
97
kDescription + ", String, RW" ,
98     };
99     
100
101     private static final String JavaDoc[] OPERATIONS =
102     {
103         "getModules(), INFO",
104         "getEjbModules(), INFO",
105         "getWebModules(), INFO",
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 ManagedJ2EEApplication() throws MBeanConfigException
118     {
119         this.setDescriptions(MAPLIST, ATTRIBUTES, OPERATIONS);
120     }
121
122
123     public ManagedJ2EEApplication(String JavaDoc instanceName, String JavaDoc appName)
124         throws MBeanConfigException
125     {
126         this(instanceName, appName, null);
127     }
128
129     public ManagedJ2EEApplication(String JavaDoc instanceName, String JavaDoc appName,
130         AdminContext adminContext)
131         throws MBeanConfigException
132     {
133         this(); //set description tables
134
setAdminContext(adminContext);
135         initialize(ObjectNames.kApplication, new String JavaDoc[]{instanceName, appName});
136     }
137
138     public String JavaDoc[] getModules() throws J2EEApplicationException
139     {
140         return getModulesByType(ModulesXMLHelper.MODULE_TYPE_ALL);
141     }
142
143     public String JavaDoc[] getEjbModules() throws J2EEApplicationException
144     {
145         return getModulesByType(ModulesXMLHelper.MODULE_TYPE_EJB);
146     }
147
148     public String JavaDoc[] getWebModules() throws J2EEApplicationException
149     {
150         return getModulesByType(ModulesXMLHelper.MODULE_TYPE_WEB);
151     }
152
153     private String JavaDoc[] getModulesByType(int moduleTypes) throws J2EEApplicationException
154     {
155         try
156         {
157             String JavaDoc location = (String JavaDoc)this.getAttribute(kLocation);
158             return ModulesXMLHelper.getModulesFromApplicationLocation(location, moduleTypes);
159         }
160         catch (Exception JavaDoc e)
161         {
162             sLogger.throwing(getClass().getName(), "getModulesByType", e);
163             throw new J2EEApplicationException(e.getMessage());
164         }
165     }
166         
167     
168     /**
169             Returns the Status of this Application.
170
171             @throws J2EEApplicationException if the status can't be retrieved.
172     */

173     public EntityStatus getStatus() throws J2EEApplicationException
174     {
175         EntityStatus status = null;
176
177         try
178         {
179             boolean isAppEnabled = true; //FIXME for RI only
180
// boolean isAppEnabled = ((Boolean)this.getAttribute(kEnabled)).booleanValue();
181
status = new EntityStatus();
182             if (isAppEnabled)
183             {
184                 status.setEnabled();
185             }
186             else
187             {
188                 status.setDisabled();
189             }
190         }
191         catch (Exception JavaDoc e)
192         {
193             sLogger.throwing(getClass().getName(), "getStatus", e);
194             throw new J2EEApplicationException(e.getMessage());
195         }
196         return status;
197     }
198
199     /**
200             Disables this application. The application should be enabled to begin
201             with. There will
202             be some time elapsed till the application is disabled. This method
203             will return asynchronously. This is to exhibit the quiece capabilities.
204
205             @throws J2EEApplicationException if the application is already disabled
206                     or there is some error during disablement
207     */

208     public void disable() throws J2EEApplicationException
209     {
210         return; //FIXME: for RI only
211
/* try
212         {
213             this.setAttribute(new Attribute(kEnabled, new Boolean(false)));
214             super.getConfigContext().flush();
215         }
216         catch (Exception e)
217         {
218             sLogger.throwing(getClass().getName(), "disable", e);
219             throw new J2EEApplicationException(e.getMessage());
220         }
221 */

222     }
223
224     /**
225      * Disables the application. Difference between this method
226      * and disable is persistence of the state. Disable method persists
227      * the state and this method does not persist the state.
228      */

229     public void stop() throws J2EEApplicationException {
230         try {
231             String JavaDoc appName = (String JavaDoc)this.getAttribute(kName);
232             multicastAdminEvent(appName, BaseDeployEvent.DISABLE);
233         } catch (Exception JavaDoc e) {
234             sLogger.throwing(getClass().getName(), "stop", e);
235             throw new J2EEApplicationException(e.getMessage());
236         }
237     }
238
239     /**
240      * Gets the jsr77 state corresponding to this module
241      */

242
243     public Integer JavaDoc getState() throws J2EEApplicationException {
244         try {
245             ServerContext serverContext = ApplicationServer.getServerContext();
246             String JavaDoc namePattern = (
247                 serverContext.getDefaultDomainName() + ":" +
248                 "j2eeType=J2EEApplication," +
249         "name=" + ((String JavaDoc)this.getAttribute(kName)) + "," +
250         "J2EEServer=" + serverContext.getInstanceName() + "," +
251         "*");
252             Integer JavaDoc intObj = (Integer JavaDoc)
253             Switch.getSwitch().getManagementObjectManager().getState(namePattern);
254         return intObj;
255         } catch (Exception JavaDoc e) {
256             sLogger.throwing(getClass().getName(), "getState", e);
257             throw new J2EEApplicationException(e.getMessage());
258         }
259     }
260
261     /*
262     public Integer getState() throws J2EEApplicationException {
263         try {
264             MBeanServer mbs = MBeanServerFactory.getMBeanServer();
265             ServerContext serverContext = ApplicationServer.getServerContext();
266             ObjectName objName = new ObjectName(
267                 serverContext.getDefaultDomainName() + ":" +
268                 "j2eeType=J2EEApplication," +
269         "name=" + ((String)this.getAttribute(kName)) + "," +
270         "J2EEApplication=" + ((String)this.getAttribute(kName)) + "," +
271         "J2EEServer=" + serverContext.getInstanceName());
272             Integer intObj = (Integer) mbs.getAttribute(objName, "state");
273         return intObj;
274         } catch (Exception e) {
275             sLogger.throwing(getClass().getName(), "getState", e);
276             throw new J2EEApplicationException(e.getMessage());
277         }
278     }
279     */

280
281
282     /**
283             Enables this application. The application should be disabled to begin
284             with. There will
285             be some time elapsed till the application is enabled. This method
286             will return asynchronously.
287
288             @throws J2EEApplicationException if the application is already enabled
289                     or there is some error during enablement
290     */

291     public void enable() throws J2EEApplicationException
292     {
293         return; //FIXME: for RI only
294
/* try
295         {
296              this.setAttribute(new Attribute(kEnabled, new Boolean(true)));
297             super.getConfigContext().flush();
298         }
299         catch (Exception e)
300         {
301             sLogger.throwing(getClass().getName(), "enable", e);
302             throw new J2EEApplicationException(e.getMessage());
303         }
304 */

305     }
306
307     
308     /**
309      * Enables the application. Difference between this method
310      * and enable is persistence of the state. Enable method persists
311      * the state and this method does not persist the state.
312      */

313     public void start() throws J2EEApplicationException {
314         try {
315             String JavaDoc appName = (String JavaDoc)this.getAttribute(kName);
316             multicastAdminEvent(appName, BaseDeployEvent.ENABLE);
317         } catch (Exception JavaDoc e) {
318             sLogger.throwing(getClass().getName(), "start", e);
319             throw new J2EEApplicationException(e.getMessage());
320         }
321     }
322
323     public static final void main(String JavaDoc[] args) throws Exception JavaDoc
324     {
325         System.setProperty("com.sun.aas.instanceRoot", "e:\\tmp");
326         ManagedJ2EEApplication appMBean =
327             new ManagedJ2EEApplication("adminapp", "admserv");
328         EntityStatus status = appMBean.getStatus();
329         sLogger.info("======== Status = " + status.getStatusString());
330         if (status.isDisabled())
331         {
332             sLogger.info("======== Enabling app");
333             appMBean.enable();
334             status = appMBean.getStatus();
335             sLogger.info("======== Status = " + status.getStatusString());
336         }
337         else
338         {
339             sLogger.info("======== Disabling app");
340             appMBean.disable();
341             status = appMBean.getStatus();
342             sLogger.info("======== Status = " + status.getStatusString());
343         }
344     }
345
346     /**
347      * Multicasts the admin event so that the application gets loaded
348      * dynamically without the need for reconfig.
349      */

350     private void multicastAdminEvent(String JavaDoc entityName, String JavaDoc actionCode)
351                         throws J2EEApplicationException {
352         String JavaDoc instanceName = super.getServerInstanceName();
353         InstanceEnvironment instEnv = new InstanceEnvironment(instanceName);
354         try {
355                 instEnv.applyServerXmlChanges(false);
356         } catch (Exception JavaDoc e) {
357             sLogger.throwing(getClass().getName(), "getAttr", e);
358             throw new J2EEApplicationException(e.getMessage());
359         }
360         AdminEvent event = new ApplicationDeployEvent(instanceName, entityName, actionCode);
361         //AdminEventCache.populateConfigChange(super.getConfigContext(), event);
362
RMIClient serverInstancePinger = AdminChannel.getRMIClient(instanceName);
363         if (serverInstancePinger.getInstanceStatusCode() != Status.kInstanceRunningCode) {
364                     return;
365         }
366         AdminEventResult multicastResult = AdminEventMulticaster.multicastEvent(event);
367         if (!AdminEventResult.SUCCESS.equals(multicastResult.getResultCode())) {
368                 AdminEventCache cache = AdminEventCache.getInstance(instanceName);
369                     cache.setRestartNeeded(true);
370         }
371     }
372 }
373
Popular Tags