KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > event > DependencyResolver


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 package com.sun.enterprise.admin.event;
24
25 import java.util.List JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import com.sun.enterprise.admin.server.core.AdminService;
28 import com.sun.enterprise.config.ConfigContext;
29 import com.sun.enterprise.config.serverbeans.JdbcResource;
30 import com.sun.enterprise.config.serverbeans.ResourceAdapterConfig;
31 import com.sun.enterprise.config.serverbeans.Domain;
32 import com.sun.enterprise.config.serverbeans.Server;
33 import com.sun.enterprise.config.serverbeans.Cluster;
34 import com.sun.enterprise.config.serverbeans.ResourceRef;
35 import com.sun.enterprise.config.serverbeans.Resources;
36 import com.sun.enterprise.config.serverbeans.ServerHelper;
37 import com.sun.enterprise.config.serverbeans.ApplicationHelper;
38 import com.sun.enterprise.config.serverbeans.ClusterHelper;
39 import com.sun.enterprise.config.serverbeans.JdbcConnectionPool;
40 import com.sun.enterprise.config.serverbeans.ConnectorResource;
41 import com.sun.enterprise.config.serverbeans.ConnectorConnectionPool;
42 import com.sun.enterprise.config.serverbeans.PersistenceManagerFactoryResource;
43 import com.sun.enterprise.config.ConfigBean;
44 import com.sun.enterprise.config.ConfigAdd;
45 import com.sun.enterprise.config.ConfigDelete;
46 import com.sun.enterprise.config.ConfigChangeFactory;
47 import com.sun.enterprise.config.ConfigException;
48
49 /**
50  * Returns config change elements for dependent resources. This is a helper
51  * class that makes an event self contained.
52  *
53  * <p> Example: If a resource references a connection pool, corresponding
54  * config change elements for that pool is sent as dependent config
55  * changes in the event. If the pool is not initialized by the targeted
56  * server already, it is initialized based on the dependent change list.
57  *
58  * @author Nazrul Islam
59  * @since JDK1.4
60  */

61 class DependencyResolver {
62
63     /**
64      * Constructor
65      */

66     DependencyResolver(ConfigContext ctx, String JavaDoc target) {
67         _ctx = ctx;
68         _target = target;
69     }
70
71     /**
72      * Resolves resource dependencies for resource creation.
73      *
74      * <pre>
75      * Dependencies:
76      * JDBC Resource - Depends on JDBC connection pool.
77      * Persistence Manager Factory - Depends on JDBC resource.
78      * Connector Resource - Depends on Connector connection pool.
79      * </pre>
80      *
81      * @param resName name of the resource
82      * @param action type of event (example, deploy, undeploy, redeploy)
83      * @param typeInEvent resource type as defined in ResourceDeployEvent
84      *
85      * @return a list of dependent config change objects
86      *
87      * @throws ConfigException if an error while parsing configuration
88      */

89     List JavaDoc resolveResources(String JavaDoc resName, String JavaDoc action, String JavaDoc typeInEvent)
90             throws ConfigException {
91
92         List JavaDoc list = new ArrayList JavaDoc();
93         if (resName == null
94                 //|| BaseDeployEvent.REDEPLOY.equals(action)
95
|| BaseDeployEvent.REMOVE_REFERENCE.equals(action)
96                 || BaseDeployEvent.UNDEPLOY.equals(action)) {
97             return list;
98         }
99             
100         ConfigBean res = findResource(resName, typeInEvent);
101         if (res == null) {
102             return list;
103         }
104         /*
105         else if (BaseDeployEvent.REMOVE_REFERENCE.equals(action))
106         {
107             // add ConfigDelete change element for referenced resource
108             // it will not harm config because applying of changes
109             // will be done over runtime config
110             list.addAll( getConfigDeleteForResource(res) );
111             return list;
112         }
113         */

114         
115         // JDBC resource
116
if (ResourceDeployEvent.RES_TYPE_JDBC.equals(typeInEvent)) {
117
118             String JavaDoc poolName = ((JdbcResource) res).getPoolName();
119             list.addAll(resolveJdbcConnectionPool(poolName));
120
121         // Connector resource
122
} else if (ResourceDeployEvent.RES_TYPE_CR.equals(typeInEvent)) {
123
124             String JavaDoc poolName = ((ConnectorResource) res).getPoolName();
125             list.addAll(resolveConnectorConnectionPool(poolName));
126
127         // PMF resource
128
} else if (ResourceDeployEvent.RES_TYPE_PMF.equals(typeInEvent)) {
129
130             String JavaDoc jdbcResName = ((PersistenceManagerFactoryResource) res).
131                                                     getJdbcResourceJndiName();
132             list.addAll( resolveResources(jdbcResName, action,
133                                 ResourceDeployEvent.RES_TYPE_JDBC) );
134         }
135
136         list.addAll( addConfigChangeForResource(res) );
137
138         return list;
139     }
140
141     /**
142      * Resolves application dependencies.
143      *
144      * @param applicationName name of the application
145      * @param action type of event (example, deploy, undeploy, redeploy)
146      *
147      * @return a list of dependent config change objects
148      *
149      * @throws ConfigException if an error while parsing configuration
150      */

151     List JavaDoc resolveApplications(String JavaDoc applicationName, String JavaDoc action)
152             throws ConfigException {
153
154         List JavaDoc list = new ArrayList JavaDoc();
155
156         if (applicationName == null
157                 || BaseDeployEvent.REMOVE_REFERENCE.equals(action)
158                 || BaseDeployEvent.REDEPLOY.equals(action)
159                 || BaseDeployEvent.UNDEPLOY.equals(action)) {
160             return list;
161         }
162         ConfigBean app=ApplicationHelper.findApplication(_ctx,applicationName);
163         if (app == null) {
164             return list;
165         }
166         // xpath for this application
167
String JavaDoc xpath = app.getXPath();
168
169         // config change object for the application entry
170
ConfigAdd configAdd = ConfigChangeFactory.createConfigAdd(_ctx, xpath);
171         list.add(configAdd);
172
173         // add resource adapter config
174
ResourceAdapterConfig raConfig =
175             findResourceAdapterConfigByName(applicationName);
176
177         // if resource adapter config is found
178
if (raConfig != null) {
179             ConfigAdd raConfigChange =
180                 ConfigChangeFactory.createConfigAdd(_ctx, raConfig.getXPath());
181
182             // add the resource adapter config to the dependent config changes
183
list.add(raConfigChange);
184         }
185
186         return list;
187     }
188
189     /**
190      * Returns config change for the given config bean.
191      *
192      * @param res config bean
193      * @return config change for the given bean
194      *
195      * @throws ConfigException if a configuration parsing error
196      */

197     private List JavaDoc addConfigChangeForResource(ConfigBean res)
198             throws ConfigException {
199         
200         List JavaDoc list = new ArrayList JavaDoc();
201         if (res == null) {
202             return list;
203         }
204
205         String JavaDoc xpath = res.getXPath();
206         ConfigAdd configAdd = ConfigChangeFactory.createConfigAdd(_ctx, xpath);
207         list.add(configAdd);
208
209         return list;
210     }
211
212     /**
213      * Returns the config delete change objects for an application.
214      *
215      * @param applicationName name of the application
216      *
217      * @return config delete objects for the given application
218      * @throws ConfigException if an error while parsing the config
219      */

220     List JavaDoc getConfigDeleteForApplication(String JavaDoc applicationName)
221             throws ConfigException {
222         
223         List JavaDoc list = new ArrayList JavaDoc();
224
225         if (applicationName != null) {
226             ConfigBean app =
227                 ApplicationHelper.findApplication(_ctx,applicationName);
228             String JavaDoc xpath = app.getXPath();
229             ConfigDelete configDelete =
230                 ConfigChangeFactory.createConfigDelete(xpath);
231             list.add(configDelete);
232         }
233         return list;
234     }
235
236     /**
237      * Returns the config delete objects for the given resource.
238      *
239      * @param resName name of the resource
240      * @param resTypeInEvent tyep of the resource defined
241      * in ResourceDeployEvent
242      *
243      * @return returns the config delete objects for a resource
244      * @throws ConfigException if an error while parsing config
245      */

246     List JavaDoc getConfigDeleteForResource(String JavaDoc resName, String JavaDoc resTypeInEvent)
247             throws ConfigException {
248
249         // type is used in finding resource since resource
250
// name space is not flat
251
ConfigBean res = findResource(resName, resTypeInEvent);
252         return getConfigDeleteForResource(res);
253     }
254
255     /**
256      * Creates ConfigDelete change object for referenced resource.
257      *
258      * @param res config bean
259      * @return list containing config delete for the given bean
260      *
261      * @throws ConfigException if an error while parsing config
262      */

263     List JavaDoc getConfigDeleteForResource(ConfigBean res)
264             throws ConfigException {
265         
266         List JavaDoc list = new ArrayList JavaDoc();
267         if (res == null) {
268             return list;
269         }
270         String JavaDoc xpath = res.getXPath();
271         ConfigDelete configDelete=ConfigChangeFactory.createConfigDelete(xpath);
272         list.add(configDelete);
273         return list;
274     }
275
276     /**
277      * Returns dependency list for jdbc connection pool.
278      *
279      * @param poolName name of jdbc connection pool
280      * @return dependency list for jdbc connection pool
281      *
282      * @throws ConfigException if a configuration parsing error
283      */

284     private List JavaDoc resolveJdbcConnectionPool(String JavaDoc poolName)
285             throws ConfigException {
286
287         List JavaDoc list = new ArrayList JavaDoc();
288
289         if (poolName == null) {
290             return list;
291         }
292
293         Resources root = ((Domain)_ctx.getRootConfigBean()).getResources();
294         JdbcConnectionPool pool = root.getJdbcConnectionPoolByName(poolName);
295
296         // no pool found
297
if (pool == null) {
298             return list;
299         }
300         list.addAll( addConfigChangeForResource(pool) );
301
302         return list;
303     }
304
305     /**
306      * Returns dependency list for a connector connection pool.
307      *
308      * @param poolName name of connector connection pool
309      * @return dependency list for connector connection pool
310      *
311      * @throws ConfigException if a configuration parsing error
312      */

313     private List JavaDoc resolveConnectorConnectionPool(String JavaDoc poolName)
314             throws ConfigException {
315
316         List JavaDoc list = new ArrayList JavaDoc();
317
318         if (poolName == null) {
319             return list;
320         }
321
322         Resources root = ((Domain)_ctx.getRootConfigBean()).getResources();
323         ConnectorConnectionPool pool =
324             root.getConnectorConnectionPoolByName(poolName);
325
326         if (pool == null) {
327             return list;
328         }
329
330         list.addAll( addConfigChangeForResource(pool) );
331
332         return list;
333     }
334
335     /**
336      * Returns the resource adapter config if it matches the given
337      * application name. RA in an EAR is defined as AppName#RAName.
338      * This method matches with the AppName.
339      *
340      * @param name application name
341      * @throws ConfigException if an error while parsing the config
342      */

343     ResourceAdapterConfig findResourceAdapterConfigByName(String JavaDoc name)
344             throws ConfigException {
345
346         // all resources
347
Resources root = ((Domain)_ctx.getRootConfigBean()).getResources();
348
349         // all resource adapter configs in the system
350
ResourceAdapterConfig[] configs = root.getResourceAdapterConfig();
351
352
353         if (configs != null) {
354             for (int i=0; i<configs.length; i++) {
355                 String JavaDoc fullRAName = configs[i].getResourceAdapterName();
356                 String JavaDoc raName = getApplicationNameFromRAName(fullRAName);
357
358                 if (raName.equals(name)) {
359                     return configs[i];
360                 }
361             }
362         }
363
364         // did not find any RA config that matches the name
365
return null;
366     }
367
368     /**
369      * Returns the application name from a RA name. RA name is defined
370      * as AppName#RAName. This method returns the AppName.
371      *
372      * @param fullRAName name of a resource adapter
373      * @return application name
374      */

375     String JavaDoc getApplicationNameFromRAName(String JavaDoc fullRAName) {
376
377         String JavaDoc appName = null;
378
379         // strip off the portion after #
380
int idx = fullRAName.indexOf("#");
381         if (idx > 0) {
382             appName = fullRAName.substring(0, idx);
383         } else {
384             appName = fullRAName;
385         }
386
387         return appName;
388     }
389
390     /**
391      * Locates a resource based on name and type.
392      *
393      * @param resName name of resource
394      * @param type type of resource
395      *
396      * @return config bean for the resource
397      * @throws ConfigException if a configuration parsing error
398      */

399     ConfigBean findResource(String JavaDoc resName, String JavaDoc type)
400             throws ConfigException {
401
402         ConfigBean res = null;
403         Resources root = ((Domain)_ctx.getRootConfigBean()).getResources();
404
405         if (ResourceDeployEvent.RES_TYPE_JDBC.equals(type)) {
406             res = root.getJdbcResourceByJndiName(resName);
407         } else if (ResourceDeployEvent.RES_TYPE_MAIL.equals(type)) {
408             res = root.getMailResourceByJndiName(resName);
409         } else if (ResourceDeployEvent.RES_TYPE_CUSTOM.equals(type)) {
410             res = root.getCustomResourceByJndiName(resName);
411         } else if (ResourceDeployEvent.RES_TYPE_EXTERNAL_JNDI.equals(type)) {
412             res = root.getExternalJndiResourceByJndiName(resName);
413         } else if (ResourceDeployEvent.RES_TYPE_PMF.equals(type)) {
414             res = root.getPersistenceManagerFactoryResourceByJndiName(resName);
415         } else if (ResourceDeployEvent.RES_TYPE_AOR.equals(type)) {
416             res = root.getAdminObjectResourceByJndiName(resName);
417         } else if (ResourceDeployEvent.RES_TYPE_CR.equals(type)) {
418             res = root.getConnectorResourceByJndiName(resName);
419         } else if (ResourceDeployEvent.RES_TYPE_JCP.equals(type)) {
420             res = root.getJdbcConnectionPoolByName(resName);
421         } else if (ResourceDeployEvent.RES_TYPE_CCP.equals(type)) {
422             res = root.getConnectorConnectionPoolByName(resName);
423         } else if (ResourceDeployEvent.RES_TYPE_RAC.equals(type)) {
424             res = root.getResourceAdapterConfigByResourceAdapterName(resName);
425         }
426
427         return res;
428     }
429
430     // ---- VARIABLE(S) - PRIVATE ------------------------
431
private ConfigContext _ctx;
432     private String JavaDoc _target;
433 }
434
Popular Tags