KickJava   Java API By Example, From Geeks To Geeks.

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


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.deployment.phasing;
31
32 import com.sun.enterprise.util.i18n.StringManager;
33 import com.sun.enterprise.util.i18n.StringManagerBase;
34 import com.sun.enterprise.admin.util.IAdminConstants;
35 import com.sun.enterprise.admin.target.Target;
36 import com.sun.enterprise.admin.target.TargetType;
37 import com.sun.enterprise.admin.target.TargetBuilder;
38
39 import com.sun.enterprise.config.ConfigException;
40 import com.sun.enterprise.config.ConfigContext;
41 import com.sun.enterprise.config.serverbeans.Domain;
42 import com.sun.enterprise.config.serverbeans.Applications;
43 import com.sun.enterprise.config.serverbeans.Server;
44 import com.sun.enterprise.config.serverbeans.Cluster;
45 import com.sun.enterprise.config.serverbeans.ApplicationRef;
46 import com.sun.enterprise.config.serverbeans.ServerTags;
47 import com.sun.enterprise.config.serverbeans.ConfigAPIHelper;
48 import com.sun.enterprise.config.serverbeans.ServerHelper;
49 import com.sun.enterprise.config.serverbeans.ClusterHelper;
50
51 import com.sun.enterprise.admin.config.BaseConfigMBean;
52
53 /**
54  *
55  * @author kebbs
56  */

57 public class ApplicationReferenceHelper implements IAdminConstants
58 {
59     private static final StringManager _strMgr =
60         StringManager.getManager(ApplicationReferenceHelper.class);
61
62     private ConfigContext _configContext = null;
63     
64     /** Creates a new instance of EEApplicationsConfigMBean */
65     public ApplicationReferenceHelper(ConfigContext configContext)
66     {
67         _configContext = configContext;
68     }
69         
70     private ConfigContext getConfigContext()
71     {
72         return _configContext;
73     }
74     
75     /**
76      * Add an application reference to the cluster.
77      *
78      * NOTE: Much of this functionality is duplicated in ResourcesMBeanHelper. As
79      * such be aware that bugs fixed here should be fixed there as well.
80      */

81     private void addApplicationReferenceToCluster(Cluster cluster, boolean enabled,
82         String JavaDoc virtualServers, String JavaDoc referenceName) throws ConfigException
83     {
84         ApplicationRef ref = cluster.getApplicationRefByRef(referenceName);
85         if (ref != null) {
86             //Application ref already exists in cluster
87
throw new ConfigException(_strMgr.getString("clusterApplicationRefAlreadyExists",
88                 referenceName, cluster.getName()));
89         }
90         ref = new ApplicationRef();
91         ref.setEnabled(enabled);
92         ref.setRef(referenceName);
93         ref.setVirtualServers(virtualServers);
94         cluster.addApplicationRef(ref, BaseConfigMBean.OVERWRITE);
95     }
96     
97     /**
98      * Add an application reference to the server instances in the cluster.
99      *
100      * NOTE: Much of this functionality is duplicated in ResourcesMBeanHelper. As
101      * such be aware that bugs fixed here should be fixed there as well.
102      */

103     private void addApplicationReferenceToClusteredServers(Cluster cluster, Server[] servers, boolean enabled,
104         String JavaDoc virtualServers, String JavaDoc referenceName) throws ConfigException
105     {
106         for (int i = 0; i < servers.length; i++) {
107             final ApplicationRef ref = servers[i].getApplicationRefByRef(referenceName);
108             if (ref != null) {
109                 //This indicates that the cluster is in an inconsistent state. Some of the
110
//instances in the cluster have the ref and some do not.
111
throw new ConfigException(_strMgr.getString("clusterApplicationRefInconsistency",
112                     referenceName, cluster.getName(), servers[i].getName()));
113             }
114             addApplicationReferenceToServer(servers[i], enabled, virtualServers,
115                 referenceName);
116         }
117     }
118
119     /**
120      * Add an application reference to a single server instance.
121      *
122      * NOTE: Much of this functionality is duplicated in ResourcesMBeanHelper. As
123      * such be aware that bugs fixed here should be fixed there as well.
124      */

125     private void addApplicationReferenceToServer(Server server, boolean enabled,
126         String JavaDoc virtualServers, String JavaDoc referenceName) throws ConfigException
127     {
128         ApplicationRef ref = server.getApplicationRefByRef(referenceName);
129         if (ref != null) {
130             //Resource ref already exists in server
131
throw new ConfigException(_strMgr.getString("serverApplicationRefAlreadyExists",
132                 referenceName, server.getName()));
133         }
134         ref = new ApplicationRef();
135         ref.setEnabled(enabled);
136         ref.setRef(referenceName);
137         ref.setVirtualServers(virtualServers);
138         server.addApplicationRef(ref, BaseConfigMBean.OVERWRITE);
139     }
140     
141     /**
142      * Given the application name, return its type.
143      *
144      * NOTE: Much of this functionality is duplicated in ResourcesMBeanHelper. As
145      * such be aware that bugs fixed here should be fixed there as well.
146      */

147     protected String JavaDoc getApplicationType (String JavaDoc appName) throws ConfigException
148     {
149         final ConfigContext configContext = getConfigContext();
150         final Domain domain = ConfigAPIHelper.getDomainConfigBean(configContext);
151         final Applications applications = domain.getApplications();
152         if (applications.getAppclientModuleByName(appName) != null) {
153             return ServerTags.APPCLIENT_MODULE;
154         } else if (applications.getConnectorModuleByName(appName) != null) {
155             return ServerTags.CONNECTOR_MODULE;
156         } else if (applications.getEjbModuleByName(appName) != null) {
157             return ServerTags.EJB_MODULE;
158         } else if (applications.getJ2eeApplicationByName(appName) != null) {
159             return ServerTags.J2EE_APPLICATION;
160         } else if (applications.getLifecycleModuleByName(appName) != null) {
161             return ServerTags.LIFECYCLE_MODULE;
162         } else if (applications.getWebModuleByName(appName) != null) {
163             return ServerTags.WEB_MODULE;
164         } else {
165             throw new ConfigException(_strMgr.getString("applicationDoesNotExist",
166                 appName));
167         }
168     }
169     
170     /**
171      * Create an application reference in the specified target (cluster
172      * unclustered server).
173      *
174      * NOTE: Much of this functionality is duplicated in ResourcesMBeanHelper. As
175      * such be aware that bugs fixed here should be fixed there as well.
176      */

177     public void createApplicationReference(TargetType[] validTypes,
178         String JavaDoc targetName, boolean enabled,
179         String JavaDoc virtualServers, String JavaDoc referenceName) throws ConfigException
180     {
181         final ConfigContext configContext = getConfigContext();
182         //Validate that there is indeed a resource with the specified name.
183
final String JavaDoc type = getApplicationType(referenceName);
184         final Target target = TargetBuilder.INSTANCE.createTarget(
185             validTypes, targetName, configContext);
186         if (target.getType() == TargetType.CLUSTER) {
187             final Cluster cluster = ClusterHelper.getClusterByName(configContext, target.getName());
188             addApplicationReferenceToCluster(cluster, enabled, virtualServers, referenceName);
189             final Server[] servers = ServerHelper.getServersInCluster(configContext, target.getName());
190             addApplicationReferenceToClusteredServers(cluster, servers, enabled,
191                 virtualServers, referenceName);
192         } else if (target.getType() == TargetType.SERVER ||
193             target.getType() == TargetType.DAS) {
194             final Server server = ServerHelper.getServerByName(configContext, target.getName());
195             addApplicationReferenceToServer(server, enabled, virtualServers, referenceName);
196         } else {
197             throw new ConfigException(_strMgr.getString("invalidClusterOrServerTarget",
198                 target.getName()));
199         }
200     }
201         
202     /**
203      * Delete an application reference from the cluster.
204      *
205      * NOTE: Much of this functionality is duplicated in ResourcesMBeanHelper. As
206      * such be aware that bugs fixed here should be fixed there as well.
207      */

208     private void deleteApplicationReferenceFromCluster(Cluster cluster,
209         String JavaDoc referenceName) throws ConfigException
210     {
211         final ApplicationRef ref = cluster.getApplicationRefByRef(referenceName);
212         if (ref == null) {
213             //Application ref already exists in cluster
214
throw new ConfigException(_strMgr.getString("clusterApplicationRefDoesNotExist",
215                 cluster.getName(), referenceName));
216         }
217         cluster.removeApplicationRef(ref, BaseConfigMBean.OVERWRITE);
218     }
219     
220     /**
221      * Delete the application references from all servers that are part of a cluster.
222      *
223      * NOTE: Much of this functionality is duplicated in ResourcesMBeanHelper. As
224      * such be aware that bugs fixed here should be fixed there as well.
225      */

226     private void deleteApplicationReferenceFromClusteredServers(Cluster cluster, Server[] servers,
227         String JavaDoc referenceName) throws ConfigException
228     {
229         for (int i = 0; i < servers.length; i++) {
230             final ApplicationRef ref = servers[i].getApplicationRefByRef(referenceName);
231             if (ref == null) {
232                 //This indicates that the cluster is in an inconsistent state. Some of the
233
//instances in the cluster have the ref and some do not.
234
throw new ConfigException(_strMgr.getString("clusterApplicationRefInconsistency",
235                     referenceName, cluster.getName(), servers[i].getName()));
236             }
237             deleteApplicationReferenceFromServer(servers[i], referenceName);
238         }
239     }
240
241     /**
242      * Delete application reference from a single server instance.
243      *
244      * NOTE: Much of this functionality is duplicated in ResourcesMBeanHelper. As
245      * such be aware that bugs fixed here should be fixed there as well.
246      */

247     private void deleteApplicationReferenceFromServer(Server server,
248         String JavaDoc referenceName) throws ConfigException
249     {
250         final ApplicationRef ref = server.getApplicationRefByRef(referenceName);
251         if (ref == null) {
252             //Application ref already exists in server
253
throw new ConfigException(_strMgr.getString("serverApplicationRefDoesNotExist",
254                  server.getName(), referenceName));
255         }
256         server.removeApplicationRef(ref, BaseConfigMBean.OVERWRITE);
257     }
258
259     /**
260      * Delete application reference from the given target (cluster or unclustered
261      * server instance).
262      *
263      * NOTE: Much of this functionality is duplicated in ResourcesMBeanHelper. As
264      * such be aware that bugs fixed here should be fixed there as well.
265      */

266     public void deleteApplicationReference(TargetType[] validTypes,
267         String JavaDoc targetName, String JavaDoc referenceName)
268         throws ConfigException
269     {
270         final ConfigContext configContext = getConfigContext();
271         //Validate that there is indeed a resource with the specified name.
272
final String JavaDoc type = getApplicationType(referenceName);
273         final Target target = TargetBuilder.INSTANCE.createTarget(
274             validTypes, targetName, configContext);
275         if (target.getType() == TargetType.SERVER ||
276             target.getType() == TargetType.DAS) {
277             final Server server = ServerHelper.getServerByName(configContext, targetName);
278             deleteApplicationReferenceFromServer(server, referenceName);
279         } else if (target.getType() == TargetType.CLUSTER) {
280             final Cluster cluster = ClusterHelper.getClusterByName(configContext, targetName);
281             deleteApplicationReferenceFromCluster(cluster, referenceName);
282             final Server[] servers = ServerHelper.getServersInCluster(configContext, targetName);
283             deleteApplicationReferenceFromClusteredServers(cluster, servers, referenceName);
284         } else {
285             throw new ConfigException(_strMgr.getString("invalidClusterOrServerTarget",
286                 targetName));
287         }
288     }
289 }
290
Popular Tags