KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > configbeans > ResourceReferenceHelper


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  * ConfigsConfigMBean.java
26  *
27  * Created on October 27, 2003, 9:45 AM
28  */

29
30 package com.sun.enterprise.admin.configbeans;
31
32 import java.util.List JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import com.sun.enterprise.util.i18n.StringManager;
35 import com.sun.enterprise.util.i18n.StringManagerBase;
36 import com.sun.enterprise.admin.util.IAdminConstants;
37 import com.sun.enterprise.admin.target.Target;
38 import com.sun.enterprise.admin.target.TargetType;
39 import com.sun.enterprise.admin.target.TargetBuilder;
40 import com.sun.enterprise.config.ConfigException;
41 import com.sun.enterprise.config.ConfigContext;
42 import com.sun.enterprise.config.serverbeans.Domain;
43 import com.sun.enterprise.config.serverbeans.Resources;
44 import com.sun.enterprise.config.serverbeans.Server;
45 import com.sun.enterprise.config.serverbeans.Cluster;
46 import com.sun.enterprise.config.serverbeans.ResourceRef;
47 import com.sun.enterprise.config.serverbeans.ServerTags;
48 import com.sun.enterprise.config.serverbeans.ConfigAPIHelper;
49 import com.sun.enterprise.config.serverbeans.ResourceHelper;
50 import com.sun.enterprise.config.serverbeans.ServerHelper;
51 import com.sun.enterprise.config.serverbeans.ClusterHelper;
52
53 /**
54  *
55  * @author kebbs
56  */

57 public class ResourceReferenceHelper extends BaseConfigBean
58 {
59     private static final TargetType[] VALID_CREATE_DELETE_TYPES = new TargetType[] {
60         TargetType.CLUSTER, TargetType.UNCLUSTERED_SERVER, TargetType.DAS};
61
62     private static final StringManager _strMgr =
63         StringManager.getManager(ResourceReferenceHelper.class);
64
65     public ResourceReferenceHelper(ConfigContext configContext)
66     {
67         super(configContext);
68     }
69
70     private String JavaDoc validateResourceAndGetType(String JavaDoc referenceName,
71         boolean allowSystemRefs) throws ConfigException
72     {
73         //Validate that there is indeed a resource with the specified name.
74
final String JavaDoc type = getResourceType(referenceName);
75         //Check to see whether we are creating or deleting a system resource
76
//reference.
77
if (!allowSystemRefs) {
78             if (ResourceHelper.isSystemResource(getConfigContext(), referenceName)) {
79                 throw new ConfigException(_strMgr.getString("systemResourceRefNotAllowed",
80                     referenceName));
81             }
82         }
83         return type;
84     }
85     
86     /**
87      * Add the resource reference to the cluster (not its server instances).
88      *
89      * NOTE: Much of this functionality is duplicated in ApplicationReferenceHelper. As
90      * such be aware that bugs fixed here should be fixed there as well.
91      */

92     private void addResourceReferenceToCluster(Cluster cluster, boolean enabled,
93         String JavaDoc referenceName) throws ConfigException
94     {
95         ResourceRef ref = cluster.getResourceRefByRef(referenceName);
96         if (ref != null) {
97             //Resource ref already exists in cluster
98
throw new ConfigException(_strMgr.getString("clusterResourceRefAlreadyExists",
99                 referenceName, cluster.getName()));
100         }
101         ref = new ResourceRef();
102         ref.setEnabled(enabled);
103         ref.setRef(referenceName);
104         cluster.addResourceRef(ref, OVERWRITE);
105     }
106     
107     /**
108      * Add the resource reference to the instances belonging to a cluster
109      *
110      * NOTE: Much of this functionality is duplicated in ApplicationReferenceHelper. As
111      * such be aware that bugs fixed here should be fixed there as well.
112      */

113     private void addResourceReferenceToClusteredServers(Cluster cluster, Server[] servers, boolean enabled,
114         String JavaDoc referenceName) throws ConfigException
115     {
116         for (int i = 0; i < servers.length; i++) {
117             final ResourceRef ref = servers[i].getResourceRefByRef(referenceName);
118             if (ref != null) {
119                 //This indicates that the cluster is in an inconsistent state. Some of the
120
//instances in the cluster have the ref and some do not.
121
throw new ConfigException(_strMgr.getString("clusterResourceRefInconsistency",
122                     referenceName, cluster.getName(), servers[i].getName()));
123             }
124             addResourceReferenceToServer(servers[i], enabled, referenceName);
125         }
126     }
127
128     /**
129      * Add the resource reference to a single server instance.
130      *
131      * NOTE: Much of this functionality is duplicated in ApplicationReferenceHelper. As
132      * such be aware that bugs fixed here should be fixed there as well.
133      */

134     private void addResourceReferenceToServer(Server server, boolean enabled,
135         String JavaDoc referenceName) throws ConfigException
136     {
137         ResourceRef ref = server.getResourceRefByRef(referenceName);
138         if (ref != null) {
139             //Resource ref already exists in server
140
throw new ConfigException(_strMgr.getString("serverResourceRefAlreadyExists",
141                 referenceName, server.getName()));
142         }
143         ref = new ResourceRef();
144         ref.setEnabled(enabled);
145         ref.setRef(referenceName);
146         server.addResourceRef(ref, OVERWRITE);
147     }
148         
149     /**
150      * Return the resource type given the resource name. The assumption is that
151      * all resource names are unique.
152      */

153     public String JavaDoc getResourceType (String JavaDoc resourceName) throws ConfigException
154     {
155         final ConfigContext configContext = getConfigContext();
156         final Domain domain = ConfigAPIHelper.getDomainConfigBean(configContext);
157         final Resources resources = domain.getResources();
158         if (resources.getAdminObjectResourceByJndiName(resourceName) != null) {
159             return ServerTags.ADMIN_OBJECT_RESOURCE;
160         } else if (resources.getConnectorConnectionPoolByName(resourceName) != null) {
161             return ServerTags.CONNECTOR_CONNECTION_POOL;
162         } else if (resources.getConnectorResourceByJndiName(resourceName) != null) {
163             return ServerTags.CONNECTOR_RESOURCE;
164         } else if (resources.getCustomResourceByJndiName(resourceName) != null) {
165             return ServerTags.CUSTOM_RESOURCE;
166         } else if (resources.getExternalJndiResourceByJndiName(resourceName) != null) {
167             return ServerTags.EXTERNAL_JNDI_RESOURCE;
168         } else if (resources.getJdbcConnectionPoolByName(resourceName) != null) {
169             return ServerTags.JDBC_CONNECTION_POOL;
170         } else if (resources.getJdbcResourceByJndiName(resourceName) != null) {
171             return ServerTags.JDBC_RESOURCE_JNDI_NAME;
172         } else if (resources.getMailResourceByJndiName(resourceName) != null) {
173             return ServerTags.MAIL_RESOURCE;
174         } else if (resources.getPersistenceManagerFactoryResourceByJndiName(resourceName) != null) {
175             return ServerTags.PERSISTENCE_MANAGER_FACTORY_RESOURCE;
176         } else if (resources.getResourceAdapterConfigByResourceAdapterName(resourceName) != null) {
177             return ServerTags.RESOURCE_ADAPTER_CONFIG;
178         } else {
179             throw new ConfigException(_strMgr.getString("resourceDoesNotExist",
180                 resourceName));
181         }
182     }
183
184     /**
185      * Add the resource reference to the specified target (cluster or
186      * unclustered server).
187      *
188      * NOTE: Much of this functionality is duplicated in ApplicationReferenceHelper. As
189      * such be aware that bugs fixed here should be fixed there as well.
190      */

191     public void createResourceReference(String JavaDoc targetName, boolean enabled,
192         String JavaDoc referenceName, boolean allowSystemRefs) throws ConfigException
193     {
194         createResourceReference(VALID_CREATE_DELETE_TYPES, targetName, enabled,
195             referenceName, allowSystemRefs);
196     }
197     
198     public void createResourceReference(String JavaDoc targetName, boolean enabled,
199         String JavaDoc referenceName) throws ConfigException
200     {
201         createResourceReference(targetName, enabled, referenceName, false);
202     }
203     
204     public void createResourceReference(TargetType[] validTypes,
205         String JavaDoc targetName, boolean enabled,
206         String JavaDoc referenceName) throws ConfigException
207     {
208         createResourceReference(validTypes, targetName, enabled, referenceName, false);
209     }
210     
211     public void createResourceReference(TargetType[] validTypes,
212         String JavaDoc targetName, boolean enabled,
213         String JavaDoc referenceName,
214         boolean allowSystemRefs) throws ConfigException
215     {
216         //Validate that there is indeed a resource with the specified name and
217
//check to see whether we are creating or deleting a system resource
218
//reference.
219
final String JavaDoc type = validateResourceAndGetType(referenceName,
220             allowSystemRefs);
221         final ConfigContext configContext = getConfigContext();
222         final Target target = TargetBuilder.INSTANCE.createTarget(
223             validTypes, targetName, configContext);
224         if (target.getType() == TargetType.CLUSTER) {
225             final Cluster cluster = ClusterHelper.getClusterByName(configContext, target.getName());
226             addResourceReferenceToCluster(cluster, enabled, referenceName);
227             final Server[] servers = ServerHelper.getServersInCluster(configContext, target.getName());
228             addResourceReferenceToClusteredServers(cluster, servers, enabled, referenceName);
229         } else if (target.getType() == TargetType.SERVER ||
230             target.getType() == TargetType.DAS) {
231             final Server server = ServerHelper.getServerByName(configContext, target.getName());
232             addResourceReferenceToServer(server, enabled, referenceName);
233         } else {
234             throw new ConfigException(_strMgr.getString("invalidClusterOrServerTarget",
235                 target.getName()));
236         }
237     }
238     
239     /**
240      * Delete resource reference from the cluster.
241      *
242      * NOTE: Much of this functionality is duplicated in ApplicationReferenceHelper. As
243      * such be aware that bugs fixed here should be fixed there as well.
244      */

245     private void deleteResourceReferenceFromCluster(Cluster cluster,
246         String JavaDoc referenceName) throws ConfigException
247     {
248         final ResourceRef ref = cluster.getResourceRefByRef(referenceName);
249         if (ref == null) {
250             //Resource ref already exists in cluster
251
throw new ConfigException(_strMgr.getString("clusterResourceRefDoesNotExist",
252                 cluster.getName(), referenceName));
253         }
254         cluster.removeResourceRef(ref, OVERWRITE);
255     }
256     
257     /**
258      * Delete resource reference from the server instances that are part of the cluster.
259      *
260      * NOTE: Much of this functionality is duplicated in ApplicationReferenceHelper. As
261      * such be aware that bugs fixed here should be fixed there as well.
262      */

263     private void deleteResourceReferenceFromClusteredServers(Cluster cluster, Server[] servers,
264         String JavaDoc referenceName) throws ConfigException
265     {
266         for (int i = 0; i < servers.length; i++) {
267             final ResourceRef ref = servers[i].getResourceRefByRef(referenceName);
268             if (ref == null) {
269                 //This indicates that the cluster is in an inconsistent state. Some of the
270
//instances in the cluster have the ref and some do not.
271
throw new ConfigException(_strMgr.getString("clusterResourceRefInconsistency",
272                     referenceName, cluster.getName(), servers[i].getName()));
273             }
274             deleteResourceReferenceFromServer(servers[i], referenceName);
275         }
276     }
277
278     /**
279      * Delete resource reference from a single server instance.
280      *
281      * NOTE: Much of this functionality is duplicated in ApplicationReferenceHelper. As
282      * such be aware that bugs fixed here should be fixed there as well.
283      */

284     private void deleteResourceReferenceFromServer(Server server,
285         String JavaDoc referenceName) throws ConfigException
286     {
287         final ResourceRef ref = server.getResourceRefByRef(referenceName);
288         if (ref == null) {
289             //Resource ref already exists in server
290
throw new ConfigException(_strMgr.getString("serverResourceRefDoesNotExist",
291                  server.getName(), referenceName));
292         }
293         server.removeResourceRef(ref, OVERWRITE);
294     }
295
296     /**
297      * Delete the resource reference from the specified target (cluster or
298      * or unclustered server).
299      *
300      * NOTE: Much of this functionality is duplicated in ApplicationReferenceHelper. As
301      * such be aware that bugs fixed here should be fixed there as well.
302      */

303     public void deleteResourceReference(String JavaDoc targetName, String JavaDoc referenceName,
304         boolean allowSystemRefs) throws ConfigException
305     {
306         deleteResourceReference(VALID_CREATE_DELETE_TYPES, targetName,
307             referenceName, allowSystemRefs);
308     }
309     
310     public void deleteResourceReference(String JavaDoc targetName, String JavaDoc referenceName)
311         throws ConfigException
312     {
313         deleteResourceReference(targetName, referenceName, false);
314     }
315     
316     public void deleteResourceReference(TargetType[] validTypes,
317         String JavaDoc targetName, String JavaDoc referenceName)
318         throws ConfigException
319     {
320         deleteResourceReference(validTypes, targetName, referenceName, false);
321     }
322     
323     public void deleteResourceReference(TargetType[] validTypes,
324         String JavaDoc targetName, String JavaDoc referenceName, boolean allowSystemRefs)
325         throws ConfigException
326     {
327         //Validate that there is indeed a resource with the specified name and
328
//check to see whether we are creating or deleting a system resource
329
//reference.
330
final String JavaDoc type = validateResourceAndGetType(referenceName,
331             allowSystemRefs);
332         final ConfigContext configContext = getConfigContext();
333         final Target target = TargetBuilder.INSTANCE.createTarget(
334             validTypes, targetName, configContext);
335         
336         if (target.getType() == TargetType.SERVER ||
337             target.getType() == TargetType.DAS) {
338             final Server server = ServerHelper.getServerByName(configContext, target.getName());
339             deleteResourceReferenceFromServer(server, referenceName);
340         } else if (target.getType().equals(TargetType.CLUSTER)) {
341             final Cluster cluster = ClusterHelper.getClusterByName(configContext, target.getName());
342             deleteResourceReferenceFromCluster(cluster, referenceName);
343             final Server[] servers = ServerHelper.getServersInCluster(configContext, target.getName());
344             deleteResourceReferenceFromClusteredServers(cluster, servers, referenceName);
345         } else {
346             throw new ConfigException(_strMgr.getString("invalidClusterOrServerTarget",
347                 target.getName()));
348         }
349
350     }
351 }
352
Popular Tags