KickJava   Java API By Example, From Geeks To Geeks.

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


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

32 //JMX imports
33
import javax.management.Attribute JavaDoc;
34
35 //Admin imports
36
import com.sun.enterprise.admin.AdminContext;
37 import com.sun.enterprise.admin.common.ObjectNames;
38 import com.sun.enterprise.admin.common.exception.J2EEEjbJarModuleException;
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.MBeanConfigException;
42
43 //for jsr77
44
import com.sun.enterprise.instance.InstanceEnvironment;
45 import com.sun.enterprise.admin.event.AdminEvent;
46 import com.sun.enterprise.admin.event.AdminEventCache;
47 import com.sun.enterprise.admin.event.AdminEventResult;
48 import com.sun.enterprise.admin.event.ModuleDeployEvent;
49 import com.sun.enterprise.admin.event.BaseDeployEvent;
50 import com.sun.enterprise.admin.event.AdminEventMulticaster;
51 import com.sun.enterprise.admin.server.core.channel.RMIClient;
52 import com.sun.enterprise.admin.server.core.channel.AdminChannel;
53 import com.sun.enterprise.admin.common.Status;
54 import com.sun.enterprise.admin.common.MBeanServerFactory;
55 import javax.management.MBeanServer JavaDoc;
56 import javax.management.ObjectName JavaDoc;
57 import com.sun.enterprise.server.ApplicationServer;
58 import com.sun.enterprise.server.ServerContext;
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=StandaloneJ2EEEjbJarModule, ModuleName=<moduleName>
72 */

73
74 public class ManagedStandaloneJ2EEEjbJarModule extends ConfigMBeanBase implements ConfigAttributeName.StandaloneEjbJarModule
75 {
76     private static final String JavaDoc[][] MAPLIST =
77     {
78         {kName , ATTRIBUTE + ServerTags.NAME},
79         {kLocation , ATTRIBUTE + ServerTags.LOCATION},
80 // {kEnabled , ATTRIBUTE + ServerTags.ENABLED},
81
{kDescription , ATTRIBUTE + PSEUDO_ATTR_DESCRIPTION},
82     };
83
84     private static final String JavaDoc[] ATTRIBUTES =
85     {
86         kName + ", String, R" ,
87         kLocation + ", String, RW" ,
88 // kEnabled + ", boolean, RW" ,
89
kDescription + ", String, RW" ,
90     };
91
92     private static final String JavaDoc[] OPERATIONS =
93     {
94         "getStatus(), INFO",
95         "enable(), ACTION",
96         "disable(), ACTION",
97         "start(), ACTION",
98         "stop(), ACTION",
99         "getState(), INFO",
100         "getEnterpriseBeans(), INFO",
101         "getSessionEJBs(), INFO",
102         "getEntityEJBs(), INFO"
103     };
104
105
106     /**
107         Default constructor sets MBean description tables
108     */

109     public ManagedStandaloneJ2EEEjbJarModule() throws MBeanConfigException
110     {
111         this.setDescriptions(MAPLIST, ATTRIBUTES, OPERATIONS);
112     }
113
114     public ManagedStandaloneJ2EEEjbJarModule(String JavaDoc instanceName, String JavaDoc moduleName)
115         throws MBeanConfigException
116     {
117         this(instanceName, moduleName, null);
118     }
119
120     public ManagedStandaloneJ2EEEjbJarModule(String JavaDoc instanceName,
121         String JavaDoc moduleName, AdminContext adminContext)
122         throws MBeanConfigException
123     {
124         this(); //set description tables
125
setAdminContext(adminContext);
126         initialize(ObjectNames.kStandaloneEjbModule, 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 J2EEEjbJarModuleException
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 J2EEEjbJarModuleException(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 J2EEEjbJarModuleException
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 J2EEEjbJarModuleException(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 J2EEEjbJarModuleException {
186         try {
187             String JavaDoc moduleName = (String JavaDoc)this.getAttribute(kName);
188             multicastAdminEvent(moduleName, BaseDeployEvent.DISABLE);
189         } catch (Exception JavaDoc e) {
190             sLogger.throwing(getClass().getName(), "stop", e);
191             throw new J2EEEjbJarModuleException(e.getMessage());
192         }
193     }
194
195     /**
196      * Gets the jsr77 state corresponding to this module
197      */

198     public Integer JavaDoc getState() throws J2EEEjbJarModuleException {
199         try {
200             MBeanServer JavaDoc mbs = MBeanServerFactory.getMBeanServer();
201             ServerContext serverContext = ApplicationServer.getServerContext();
202             ObjectName JavaDoc objName = new ObjectName JavaDoc(
203                 serverContext.getDefaultDomainName() + ":" +
204                 "j2eeType=EJBModule," +
205                 "name=" + ((String JavaDoc)this.getAttribute(kName)) + "," +
206         "J2EEApplication=" + "null" + "," +
207         "J2EEServer=" + serverContext.getInstanceName());
208             Integer JavaDoc intObj = (Integer JavaDoc) mbs.getAttribute(objName, "state");
209         return intObj;
210         } catch (Exception JavaDoc e) {
211             sLogger.throwing(getClass().getName(), "stop", e);
212             throw new J2EEEjbJarModuleException(e.getMessage());
213         }
214     }
215
216     /**
217      * Enables this module.
218      * @throws J2EEEjbJarModuleException if there is some error during
219      * enablement.
220     */

221     public void enable() throws J2EEEjbJarModuleException
222     {
223         return; //FIXME for RI only;
224
/* try
225         {
226             this.setAttribute(new Attribute(kEnabled, new Boolean(true)));
227             super.getConfigContext().flush();
228         }
229         catch (Exception e)
230         {
231             sLogger.throwing(getClass().getName(), "enable", e);
232             throw new J2EEEjbJarModuleException(e.getMessage());
233         }
234 */

235     }
236
237     /**
238      * Enables the application. Difference between this method
239      * and enable is persistence of the state. Enable method persists
240      * the state and this method does not persist the state.
241      */

242     public void start() throws J2EEEjbJarModuleException {
243         try {
244             String JavaDoc moduleName = (String JavaDoc)this.getAttribute(kName);
245             multicastAdminEvent(moduleName, BaseDeployEvent.ENABLE);
246         } catch (Exception JavaDoc e) {
247             sLogger.throwing(getClass().getName(), "start", e);
248             throw new J2EEEjbJarModuleException(e.getMessage());
249         }
250     }
251
252     public String JavaDoc[] getEnterpriseBeans() throws J2EEEjbJarModuleException
253     {
254         return getBeansByType(ModulesXMLHelper.EJB_TYPE_ALL);
255     }
256
257     public String JavaDoc[] getSessionEJBs() throws J2EEEjbJarModuleException
258     {
259         return getBeansByType(ModulesXMLHelper.EJB_TYPE_SESSION);
260     }
261
262     public String JavaDoc[] getEntityEJBs() throws J2EEEjbJarModuleException
263     {
264         return getBeansByType(ModulesXMLHelper.EJB_TYPE_ENTITY);
265     }
266
267     private String JavaDoc[] getBeansByType(int ejbType) throws J2EEEjbJarModuleException
268     {
269         try
270         {
271             String JavaDoc location = (String JavaDoc)this.getAttribute(kLocation);
272             return ModulesXMLHelper.getEnterpriseBeansForEjbModule(location, null, ejbType);
273         }
274         catch (Exception JavaDoc e)
275         {
276             sLogger.throwing(getClass().getName(), "getBeansByType", e);
277             throw new J2EEEjbJarModuleException(e.getMessage());
278         }
279     }
280
281     /**
282      * Multicasts the admin event so that the application gets loaded
283      * dynamically without the need for reconfig.
284      */

285     private void multicastAdminEvent(String JavaDoc entityName, String JavaDoc actionCode)
286                         throws J2EEEjbJarModuleException {
287         String JavaDoc instanceName = super.getServerInstanceName();
288         InstanceEnvironment instEnv = new InstanceEnvironment(instanceName);
289         try {
290                 instEnv.applyServerXmlChanges(false);
291         } catch (Exception JavaDoc e) {
292             sLogger.throwing(getClass().getName(), "getBeansByType", e);
293             throw new J2EEEjbJarModuleException(e.getMessage());
294         }
295         AdminEvent event = new ModuleDeployEvent(instanceName, entityName, "ejb", actionCode);
296         //AdminEventCache.populateConfigChange(super.getConfigContext(), event);
297
RMIClient serverInstancePinger = AdminChannel.getRMIClient(instanceName);
298         if (serverInstancePinger.getInstanceStatusCode() != Status.kInstanceRunningCode) {
299                     return;
300         }
301         AdminEventResult multicastResult = AdminEventMulticaster.multicastEvent(event);
302         if (!AdminEventResult.SUCCESS.equals(multicastResult.getResultCode())) {
303                 AdminEventCache cache = AdminEventCache.getInstance(instanceName);
304                     cache.setRestartNeeded(true);
305         }
306     }
307 }
308
Popular Tags