KickJava   Java API By Example, From Geeks To Geeks.

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


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.deployment.phasing;
25
26 import com.sun.enterprise.deployment.Application;
27 import com.sun.enterprise.deployment.util.DeploymentProperties;
28
29 import com.sun.enterprise.deployment.backend.DeploymentStatus;
30 import com.sun.enterprise.deployment.backend.DeploymentRequest;
31 import com.sun.enterprise.deployment.backend.DeployableObjectType;
32 import com.sun.enterprise.deployment.backend.DeploymentLogger;
33 import com.sun.enterprise.deployment.backend.IASDeploymentException;
34
35 import com.sun.enterprise.util.i18n.StringManager;
36 import com.sun.enterprise.resource.Resource;
37 import com.sun.enterprise.admin.common.MBeanServerFactory;
38 import com.sun.enterprise.admin.common.ObjectNames;
39
40 import javax.management.MBeanServer JavaDoc;
41 import javax.management.ObjectName JavaDoc;
42 import javax.management.MBeanException JavaDoc;
43
44 import java.util.List JavaDoc;
45 import java.util.ArrayList JavaDoc;
46 import java.util.logging.Logger JavaDoc;
47
48 /**
49  * This class is the base class for resource phases
50  */

51 public abstract class ResourcePhase extends DeploymentPhase {
52
53     protected static final String JavaDoc resourcesMBeanName =
54         "com.sun.appserv:type=resources,category=config";
55     protected static final String JavaDoc CREATE_RESOURCE =
56         "createResource";
57     protected static final String JavaDoc CREATE_RESOURCE_REF =
58         "createResourceReference";
59     protected static final String JavaDoc CREATE_RESOURCE_AND_REF =
60         "createResourceAndResourceReference";
61     protected static final String JavaDoc DELETE_RESOURCE =
62         "deleteResource";
63     protected static final String JavaDoc DELETE_RESOURCE_REF =
64         "deleteResourceReference";
65     protected static final String JavaDoc DELETE_RESOURCE_AND_REF =
66         "deleteResourceAndResourceReference";
67
68     protected static final String JavaDoc DOMAIN_TARGET =
69         "domain";
70
71     protected MBeanServer JavaDoc mbs = MBeanServerFactory.getMBeanServer();
72
73     protected void doResourceOperation(DeploymentRequest req) throws Exception JavaDoc {
74         String JavaDoc targetListString = req.getResourceTargetList();
75         List JavaDoc<String JavaDoc> targetList = DeploymentServiceUtils.getTargetNamesFromTargetString(targetListString);
76
77         String JavaDoc resourceAction = req.getResourceAction();
78         if (resourceAction == null ||
79             getActualAction(resourceAction).equals(
80                 DeploymentProperties.RES_NO_OP)) {
81             return;
82         }
83
84         if (targetList == null || targetList.isEmpty()) {
85             return;
86         }
87
88         List JavaDoc<Resource> resourceList = null;
89
90         // if resource list is null, it means it needs to
91
// read in sun-configuration.xml and parse it,
92
// if resource list is empty, it means it's been processed
93
// but no sun-configuration.xml is found
94
Application app = DeploymentServiceUtils.getInstanceManager(
95             req.getType()).getRegisteredDescriptor(req.getName());
96
97         // first try to get the in-memory copy from application
98
// always re-set the list from disk for redeployment
99
if (app != null && app.getResourceList() != null &&
100             !getForceParsing(resourceAction)) {
101             resourceList = (List JavaDoc<Resource>)app.getResourceList();
102         // then try to get it from resources.xml
103
// and set in the application object
104
} else {
105             resourceList = DeploymentServiceUtils.getResources(
106                 req.getName(), req.getType());
107             // todo: resolve with the existing resources in the config
108
if (app != null) {
109                 app.setResourceList(resourceList);
110             }
111         }
112
113         // empty resource list, no resource to process
114
if (resourceList.size() == 0) {
115             return;
116         }
117
118         handleResources(resourceAction, targetList,
119             getRelevantResources(resourceList));
120     }
121
122
123     protected void handleResources(String JavaDoc resourceAction,
124         List JavaDoc<String JavaDoc> targetList, List JavaDoc<Resource> resourceList)
125         throws Exception JavaDoc {
126
127         // empty sub resource list, no resource to process
128
if (resourceList.size() == 0) {
129             return;
130         }
131
132         if (resourceAction.equals(DeploymentProperties.RES_DEPLOYMENT)) {
133             handleDeployment(targetList, resourceList);
134         } else if (resourceAction.equals(DeploymentProperties.RES_CREATE_REF)){
135             handleCreateApplicationRef(targetList, resourceList);
136         } else if (resourceAction.equals(DeploymentProperties.RES_DELETE_REF)){
137             handleDeleteApplicationRef(targetList, resourceList);
138         } else if (resourceAction.equals(
139             DeploymentProperties.RES_UNDEPLOYMENT)){
140             handleUndeployment(targetList, resourceList);
141         } else if (resourceAction.equals(
142             DeploymentProperties.RES_REDEPLOYMENT)){
143             handleRedeployment(targetList, resourceList);
144         }
145     }
146
147     // invoke with both resource and resource-ref elements created
148
// special case: when target is domain, only create resource element
149
protected void handleDeployment(List JavaDoc<String JavaDoc> targetList,
150         List JavaDoc<Resource> resourceList) throws Exception JavaDoc {
151         ObjectName JavaDoc mbeanName = new ObjectName JavaDoc(resourcesMBeanName);
152
153         // if target is domain, only create resource
154
if (targetList.size() == 1 &&
155             targetList.get(0).equals(DOMAIN_TARGET)) {
156             String JavaDoc[] signature = new String JavaDoc[]{
157                 "java.util.List", "java.lang.Boolean"};
158             Object JavaDoc[] params = new Object JavaDoc[]{resourceList, Boolean.TRUE};
159             mbs.invoke(mbeanName, CREATE_RESOURCE, params, signature);
160         } else {
161             String JavaDoc[] signature = new String JavaDoc[]{
162                 "java.util.List", "java.util.List", "java.lang.Boolean"};
163             Object JavaDoc[] params = new Object JavaDoc[]{resourceList, targetList,
164                 Boolean.TRUE};
165             mbs.invoke(mbeanName, CREATE_RESOURCE_AND_REF, params, signature);
166         }
167     }
168
169     // invoke with only resource-ref element created
170
protected void handleCreateApplicationRef(List JavaDoc<String JavaDoc> targetList,
171         List JavaDoc<Resource> resourceList) throws Exception JavaDoc {
172         ObjectName JavaDoc mbeanName = new ObjectName JavaDoc(resourcesMBeanName);
173         String JavaDoc[] signature = new String JavaDoc[]{
174             "java.util.List", "java.util.List", "java.lang.Boolean"};
175         Object JavaDoc[] params = new Object JavaDoc[]{resourceList, targetList,
176             Boolean.TRUE};
177         mbs.invoke(mbeanName, CREATE_RESOURCE_REF, params, signature);
178     }
179
180     // invoke with both resource and resource-ref elements deleted
181
// special case: when target is domain, only delete resource element
182
protected void handleUndeployment(List JavaDoc<String JavaDoc> targetList,
183         List JavaDoc<Resource> resourceList) throws Exception JavaDoc {
184         ObjectName JavaDoc mbeanName = new ObjectName JavaDoc(resourcesMBeanName);
185
186         // if target is domain, only delete resource
187
if (targetList.size() == 1 &&
188             targetList.get(0).equals(DOMAIN_TARGET)) {
189             String JavaDoc[] signature = new String JavaDoc[]{"java.util.List"};
190             Object JavaDoc[] params = new Object JavaDoc[]{resourceList};
191             mbs.invoke(mbeanName,DELETE_RESOURCE, params, signature);
192         } else {
193             String JavaDoc[] signature = new String JavaDoc[]{
194                 "java.util.List", "java.util.List"};
195             Object JavaDoc[] params = new Object JavaDoc[]{resourceList, targetList};
196             mbs.invoke(mbeanName, DELETE_RESOURCE_AND_REF, params, signature);
197         }
198     }
199
200     abstract protected void handleRedeployment(List JavaDoc<String JavaDoc> targetList,
201         List JavaDoc<Resource> resourceList) throws Exception JavaDoc;
202
203     // invoke with only resource-ref element deleted
204
protected void handleDeleteApplicationRef(List JavaDoc<String JavaDoc> targetList,
205         List JavaDoc<Resource> resourceList) throws Exception JavaDoc {
206         ObjectName JavaDoc mbeanName = new ObjectName JavaDoc(resourcesMBeanName);
207         String JavaDoc[] signature = new String JavaDoc[]{
208             "java.util.List", "java.util.List"};
209         Object JavaDoc[] params = new Object JavaDoc[]{resourceList, targetList};
210         mbs.invoke(mbeanName, DELETE_RESOURCE_REF, params, signature);
211     }
212
213     protected boolean getForceParsing(String JavaDoc resAction) {
214         return false;
215     }
216
217     protected String JavaDoc getActualAction(String JavaDoc resAction) {
218         return resAction;
219     }
220
221     abstract protected List JavaDoc<Resource> getRelevantResources(
222         List JavaDoc<Resource> allResources);
223 }
224
Popular Tags