KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > phasing > ResourceAdapterStopPhase


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  * ResourceAdapterStopPhase.java
26  *
27  * Created on May 20, 2003, 3:19 PM
28  * @author sandhyae
29  * <BR> <I>$Source: /cvs/glassfish/appserv-core/src/java/com/sun/enterprise/deployment/phasing/ResourceAdapterStopPhase.java,v $
30  *
31  */

32
33 package com.sun.enterprise.deployment.phasing;
34
35 import javax.management.MBeanServer JavaDoc;
36 import javax.management.ObjectName JavaDoc;
37 import javax.management.MBeanException JavaDoc;
38
39 import com.sun.enterprise.deployment.backend.IASDeploymentException;
40 import com.sun.enterprise.deployment.backend.DeploymentRequest;
41 import com.sun.enterprise.deployment.backend.DeploymentEvent;
42 import com.sun.enterprise.deployment.backend.DeploymentEventType;
43 import com.sun.enterprise.deployment.backend.DeploymentEventInfo;
44 import com.sun.enterprise.deployment.backend.DeploymentLogger;
45 import com.sun.enterprise.deployment.backend.DeploymentStatus;
46 import com.sun.enterprise.deployment.backend.DeployableObjectType;
47 import com.sun.enterprise.deployment.util.DeploymentProperties;
48 import com.sun.enterprise.deployment.Application;
49 import com.sun.enterprise.util.i18n.StringManager;
50 import com.sun.enterprise.admin.event.BaseDeployEvent;
51 import com.sun.enterprise.admin.common.MBeanServerFactory;
52 import com.sun.enterprise.admin.common.ObjectNames;
53 import com.sun.enterprise.admin.server.core.AdminNotificationHelper;
54 import com.sun.enterprise.admin.server.core.AdminService;
55 import com.sun.enterprise.admin.AdminContext;
56 import com.sun.enterprise.server.Constants;
57
58 import java.util.logging.Level JavaDoc;
59 import java.util.logging.Logger JavaDoc;
60
61 /**
62  * Phase that is responsible to send stop events when an application is undeployed or
63  * disassociated
64  * @author Sandhya E
65  */

66 public class ResourceAdapterStopPhase extends DeploymentPhase {
67     
68     /** Deployment Logger object for this class */
69     public static final Logger sLogger = DeploymentLogger.get();
70     
71     /** string manager */
72     private static StringManager localStrings =
73         StringManager.getManager( ResourceAdapterStopPhase.class );
74     
75     /**
76      * Creates a new instance of Class
77      * @param deploymentCtx DeploymentContext object
78      */

79     public ResourceAdapterStopPhase(DeploymentContext deploymentCtx)
80     {
81         this.deploymentCtx = deploymentCtx;
82         this.name = RA_STOP;
83     }
84     
85     /**
86      *
87      */

88     public void prePhase(DeploymentPhaseContext phaseCtx)
89     {
90         DeploymentStatus status = phaseCtx.getDeploymentStatus();
91         DeploymentRequest req = phaseCtx.getDeploymentRequest();
92         String JavaDoc resourceAction = req.getResourceAction();
93
94         if(!req.getCascade() && resourceAction.equals(
95                 DeploymentProperties.RES_UNDEPLOYMENT)) {
96             try {
97                 if(checkConnectorDependentResources(
98                     req.getName(), status)) {
99                     status.setStageStatus(DeploymentStatus.FAILURE);
100                     String JavaDoc dependentResource = status.getStageStatusMessage();
101                     status.setStageStatusMessage(localStrings.getString(
102                         "enterprise.deployment.phasing.stop.dependentresexist", new Object JavaDoc[] {dependentResource, req.getName()}));
103                 }
104             } catch(Throwable JavaDoc t){
105                 status.setStageStatus(DeploymentStatus.FAILURE);
106                 status.setStageException(t);
107                 status.setStageStatusMessage(t.getMessage());
108             }
109         }
110     }
111
112     /**
113      * Sends stop events to the required target
114      * @param req DeploymentRequest object
115      * @param phaseCtx the DeploymentPhaseContext object
116      */

117     public void runPhase(DeploymentPhaseContext phaseCtx)
118     {
119         String JavaDoc type = null;
120         
121         DeploymentRequest req = phaseCtx.getDeploymentRequest();
122
123         DeploymentTarget target = (DeploymentTarget)req.getTarget();
124         DeploymentStatus status = phaseCtx.getDeploymentStatus();
125
126         boolean containsRar = false;
127
128         if(!req.isApplication())
129         {
130             type = DeploymentServiceUtils.getModuleTypeString(req.getType());
131             if (req.isConnectorModule()) {
132                 containsRar = true;
133             }
134         } else {
135             Application app = DeploymentServiceUtils.getInstanceManager(
136                DeployableObjectType.APP).getRegisteredDescriptor(req.getName());
137
138             if ( (app != null) && (app.getRarComponentCount() != 0) ) {
139                 containsRar = true;
140             }
141         }
142         
143         if (!containsRar) {
144             status.setStageStatus(DeploymentStatus.SUCCESS);
145             return;
146         }
147
148         try {
149             if (req.getCascade() && !req.isForced()){
150                 deleteConnectorDependentResources(req.getName(),
151                     target.getName());
152                 deploymentCtx.getConfigContext().flush();
153                 AdminContext adminContext =
154                         AdminService.getAdminService().getAdminContext();
155                 new AdminNotificationHelper(adminContext).sendNotification();
156             }
157         } catch(Throwable JavaDoc t){
158             status.setStageStatus(DeploymentStatus.FAILURE);
159             status.setStageException(t);
160             status.setStageStatusMessage(t.getMessage());
161             return;
162         }
163
164         prePhaseNotify(getPrePhaseEvent(req));
165         
166         boolean success;
167         try {
168             success = target.sendStopEvent(req.getActionCode(), req.getName(), type, req.getCascade(), req.isForced(), Constants.UNLOAD_RAR);
169         } catch(DeploymentTargetException dte) {
170             status.setStageStatus(DeploymentStatus.FAILURE);
171             if (dte.getCause()!=null) {
172                 status.setStageException(dte.getCause());
173                 status.setStageStatusMessage(dte.getMessage());
174             }
175             return;
176         }
177         if (success) {
178             status.setStageStatus(DeploymentStatus.SUCCESS);
179         } else {
180             status.setStageStatus(DeploymentStatus.FAILURE);
181             status.setStageStatusMessage("Application failed to stop");
182         }
183         
184         postPhaseNotify(getPostPhaseEvent(req));
185         
186         // if any exception is thrown. we let the stack unroll, it
187
// will be processed in the DeploymentService.
188
}
189     
190     /**
191      * Event that will be broadcasted at the start of the phase
192      * @param req Deployment request object
193      * @return DeploymentEvent
194      */

195     protected DeploymentEvent getPrePhaseEvent(DeploymentRequest req)
196     {
197         return new DeploymentEvent(DeploymentEventType.PRE_RA_STOP, new DeploymentEventInfo(req));
198     }
199     
200     /**
201      * Event that will be broadcasted at the end of the phase
202      * @param req Deployment request object
203      * @return DeploymentEvent
204      */

205     protected DeploymentEvent getPostPhaseEvent(DeploymentRequest req)
206     {
207         return new DeploymentEvent(DeploymentEventType.POST_RA_STOP,new DeploymentEventInfo(req) );
208     }
209
210     private boolean checkConnectorDependentResources(
211         String JavaDoc id, DeploymentStatus status) throws Exception JavaDoc {
212         MBeanServer JavaDoc mbs = MBeanServerFactory.getMBeanServer();
213         ObjectName JavaDoc mbeanName =
214             new ObjectName JavaDoc("com.sun.appserv:type=resources,category=config");
215
216         Object JavaDoc[] params = new Object JavaDoc[] {};
217         String JavaDoc[] signature = new String JavaDoc[] {};
218
219         // check admin objects
220
ObjectName JavaDoc[] adminObjs =
221             (ObjectName JavaDoc[]) mbs.invoke(mbeanName, LIST_ADMIN_OBJECTS,
222             params, signature);
223         for(int i = 0 ; i< adminObjs.length;i++) {
224             String JavaDoc raName =
225                 (String JavaDoc)mbs.getAttribute(adminObjs[i],"res_adapter");
226             if(id.equals(raName)) {
227                 String JavaDoc adminObjName =
228                     (String JavaDoc)mbs.getAttribute(adminObjs[i], "jndi_name");
229                 status.setStageStatusMessage("admin object [" + adminObjName
230                     + "]");
231                 return true;
232             }
233         }
234
235         // check pool and resources
236
ObjectName JavaDoc[] poolNames = (ObjectName JavaDoc[])mbs.invoke(mbeanName,
237             LIST_CONNECTOR_CONNECTION_POOLS, params, signature);
238         for (int i = 0 ; i< poolNames.length;i++) {
239             String JavaDoc raName = (String JavaDoc)mbs.getAttribute(poolNames[i],
240                 "resource_adapter_name");
241             if(id.equals(raName)) {
242                 String JavaDoc poolName = (String JavaDoc)mbs.getAttribute(poolNames[i],
243                     "name");
244                 status.setStageStatusMessage("connector connection pool [" +
245                     poolName + "]");
246                 return true;
247             }
248         }
249             
250         // check resource adapter config
251
ObjectName JavaDoc[] resAdapterConfigs = (ObjectName JavaDoc[])mbs.invoke(mbeanName,
252             LIST_RESOURCE_ADAPTER_CONFIGS, params, signature);
253         for (int i = 0 ; i< resAdapterConfigs.length;i++) {
254             String JavaDoc raName = (String JavaDoc)mbs.getAttribute(resAdapterConfigs[i],
255                 "resource_adapter_name");
256             if(id.equals(raName)) {
257                 status.setStageStatusMessage("resource adapter config");
258                 return true;
259             }
260         }
261             
262         return false;
263     }
264                 
265     private void deleteConnectorDependentResources(
266         String JavaDoc id, String JavaDoc targetName) throws Exception JavaDoc {
267         try{
268             MBeanServer JavaDoc mbs = MBeanServerFactory.getMBeanServer();
269             ObjectName JavaDoc mbeanName =
270                 new ObjectName JavaDoc("com.sun.appserv:type=resources,category=config");
271         
272             Object JavaDoc[] params = new Object JavaDoc[] {};
273             String JavaDoc[] signature = new String JavaDoc[] {};
274             
275             //delete admin objects
276
ObjectName JavaDoc[] adminObjs =
277                 (ObjectName JavaDoc[]) mbs.invoke(mbeanName, LIST_ADMIN_OBJECTS,
278                 params, signature);
279             String JavaDoc[] adminObjSignature = new String JavaDoc[]{
280                 "java.lang.String", "java.lang.String"};
281             for(int i = 0 ; i< adminObjs.length;i++) {
282                 String JavaDoc raName =
283                     (String JavaDoc)mbs.getAttribute(adminObjs[i],"res_adapter");
284                 if(id.equals(raName)) {
285                     String JavaDoc adminObjName =
286                         (String JavaDoc)mbs.getAttribute(adminObjs[i],"jndi_name");
287                     Object JavaDoc[] deleteAdminParams =
288                         new Object JavaDoc[]{adminObjName, (String JavaDoc)targetName};
289                     mbs.invoke(mbeanName,
290                         DELETE_ADMIN_OBJECT, deleteAdminParams,
291                         adminObjSignature);
292                 }
293             }
294             //end delete admin objects
295

296             //delete pools and resources
297
ObjectName JavaDoc[] poolNames = (ObjectName JavaDoc[])mbs.invoke(mbeanName,
298                 LIST_CONNECTOR_CONNECTION_POOLS, params, signature);
299             String JavaDoc[] deletePoolSignature = new String JavaDoc[] {"java.lang.String",
300                 "java.lang.Boolean", "java.lang.String"};
301             for(int i = 0 ; i < poolNames.length ; i++) {
302                 String JavaDoc raName = (String JavaDoc)mbs.getAttribute(poolNames[i],
303                     "resource_adapter_name");
304                 if(id.equals(raName)) {
305                     String JavaDoc poolName = (String JavaDoc)mbs.getAttribute(poolNames[i],
306                         "name");
307                     Object JavaDoc[] deletePoolParams = new Object JavaDoc[] {poolName,
308                         Boolean.TRUE, (String JavaDoc)targetName};
309                     mbs.invoke(mbeanName, DELETE_CONNECTOR_CONNECTION_POOL,
310                         deletePoolParams, deletePoolSignature);
311                 }
312             }
313             //end delete pools and resources
314

315             //delete resource adapter config
316
ObjectName JavaDoc[] resAdapterConfigs = (ObjectName JavaDoc[])mbs.invoke(
317                 mbeanName, LIST_RESOURCE_ADAPTER_CONFIGS, params, signature);
318             String JavaDoc[] adapterConfigSignature = new String JavaDoc[]{
319                 "java.lang.String", "java.lang.String"};
320             Object JavaDoc[] adapterConfigParams = new Object JavaDoc[]{id, (String JavaDoc)targetName};
321             for(int i = 0 ; i < resAdapterConfigs.length ; i++) {
322                 String JavaDoc raName = (String JavaDoc)mbs.getAttribute(resAdapterConfigs[i],
323                     "resource_adapter_name");
324                 if(id.equals(raName)) {
325                     mbs.invoke(mbeanName, DELETE_RESOURCE_ADAPTER_CONFIG,
326                         adapterConfigParams, adapterConfigSignature);
327                 }
328             }
329             //end delete resource adapter config
330

331         }catch(Exception JavaDoc e){
332             //FIXME i18n
333
throw new DeploymentPhaseException(getName(), "Exception occured while deleting dependent connector resources", e);
334         }
335     }
336                 
337     private static final String JavaDoc LIST_ADMIN_OBJECTS =
338         "getAdminObjectResource";
339     private static final String JavaDoc DELETE_ADMIN_OBJECT =
340         "deleteAdminObjectResource";
341     private static final String JavaDoc LIST_CONNECTOR_CONNECTION_POOLS =
342         "getConnectorConnectionPool";
343     private static final String JavaDoc DELETE_CONNECTOR_CONNECTION_POOL =
344         "deleteConnectorConnectionPool";
345     private static final String JavaDoc LIST_RESOURCE_ADAPTER_CONFIGS =
346         "getResourceAdapterConfig";
347     private static final String JavaDoc DELETE_RESOURCE_ADAPTER_CONFIG =
348         "deleteResourceAdapterConfig";
349
350 }
351
Popular Tags