KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > serverbeans > ClusterHelper


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  * ClusterHelper.java
26  *
27  * Created on October 23, 2003, 11:29 AM
28  */

29
30 package com.sun.enterprise.config.serverbeans;
31
32 import com.sun.enterprise.config.ConfigContext;
33 import com.sun.enterprise.config.ConfigException;
34
35 import com.sun.enterprise.config.serverbeans.Domain;
36 import com.sun.enterprise.config.serverbeans.Clusters;
37 import com.sun.enterprise.config.serverbeans.Cluster;
38 import com.sun.enterprise.config.serverbeans.Server;
39 import com.sun.enterprise.config.serverbeans.ServerRef;
40 import com.sun.enterprise.config.serverbeans.ApplicationRef;
41 import com.sun.enterprise.config.serverbeans.ResourceRef;
42 import com.sun.enterprise.config.serverbeans.Config;
43
44 import java.util.ArrayList JavaDoc;
45
46 /**
47  *
48  * @author kebbs
49  */

50 public class ClusterHelper extends ConfigAPIHelper {
51     
52     /**
53      * Return the given cluster array as a comma separated string
54      */

55     public static String JavaDoc getClustersAsString(Cluster[] clusters)
56     {
57         String JavaDoc result = "";
58         for (int i = 0; i < clusters.length; i++) {
59             result += clusters[i].getName();
60             if (i < clusters.length - 1) {
61                 result += ",";
62             }
63         }
64         return result;
65     }
66     
67     /**
68      * Return all the clusters in the domain or a zero length list.
69      */

70     public static Cluster[] getClustersInDomain(ConfigContext configContext)
71         throws ConfigException
72     {
73         final Domain domain = getDomainConfigBean(configContext);
74         final Clusters clusters = domain.getClusters();
75         if (clusters != null) {
76             if (clusters.getCluster() != null) {
77                 return clusters.getCluster();
78             }
79         }
80         return new Cluster[0];
81     }
82      
83     /**
84      * Return true if the given clusterName corresponds to the name of a cluster.
85      */

86     public static boolean isACluster(ConfigContext configContext, String JavaDoc clusterName)
87         throws ConfigException
88     {
89         final Domain domain = getDomainConfigBean(configContext);
90         final Clusters clusters = domain.getClusters();
91         if (clusters == null) {
92             return false;
93         }
94         final Cluster cluster = clusters.getClusterByName(clusterName);
95         return (cluster != null ? true : false);
96     }
97         
98     /**
99      * Return the named cluster. If the cluster does not exist, an exception will
100      * be thrown.
101      */

102     public static Cluster getClusterByName(ConfigContext configContext, String JavaDoc clusterName)
103         throws ConfigException
104     {
105         final Domain domain = getDomainConfigBean(configContext);
106         final Cluster cluster = domain.getClusters().getClusterByName(clusterName);
107         if (cluster == null) {
108             throw new ConfigException(_strMgr.getString("noSuchCluster",
109                 clusterName));
110         }
111         return cluster;
112     }
113     
114     /**
115      * Return the cluster associated with the given instanceName. An exception is thrown
116      * if the givne instanceName does not exist or is not a clustered server instance.
117      *
118      * FIXTHIS: We really need to give the server a reference to its cluster; rather than the
119      * other way around. This will make this operation much faster and management easier in general.
120      **/

121     public static Cluster getClusterForInstance(ConfigContext configContext, String JavaDoc instanceName)
122         throws ConfigException
123     {
124         Cluster[] clusters = getClustersInDomain(configContext);
125         for (int i = 0; i < clusters.length; i++) {
126             ServerRef[] servers = clusters[i].getServerRef();
127             for (int j = 0; j < servers.length; j++) {
128                 if (servers[j].getRef().equals(instanceName)) {
129                     // check to see if the server exists as a sanity check.
130
// NOTE: we are not checking for duplicate server instances here.
131
Server server = ServerHelper.getServerByName(configContext, instanceName);
132                     return (clusters[i]);
133                 }
134             }
135         }
136         throw new ConfigException(_strMgr.getString("noSuchClusteredInstance",
137             instanceName));
138     }
139     
140     
141     /**
142      * Return all the clusters referencing the given configuration or a zero length list if
143      * there are none.
144      */

145     public static Cluster[] getClustersReferencingConfig(ConfigContext configContext, String JavaDoc configName)
146         throws ConfigException
147     {
148         //First ensure that the config exists
149
Config config = getConfigByName(configContext, configName);
150         
151         //Now find all server instances that reference that config.
152
Cluster[] clusters = getClustersInDomain(configContext);
153         ArrayList JavaDoc result = new ArrayList JavaDoc();
154         for (int i = 0; i < clusters.length; i++) {
155             if (clusters[i].getConfigRef().equals(configName)) {
156                 result.add(clusters[i]);
157             }
158         }
159         return (Cluster[])result.toArray(new Cluster[result.size()]);
160     }
161    
162     /**
163      * Return all the clusters associasted with a node agent. In other words return all the
164      * clusters for the instances managed by the given agentName.
165      */

166     public static Cluster[] getClustersForNodeAgent(ConfigContext configContext, String JavaDoc agentName)
167         throws ConfigException
168     {
169         Cluster[] clusters = getClustersInDomain(configContext);
170         Server[] servers = ServerHelper.getServersOfANodeAgent(configContext, agentName);
171         ArrayList JavaDoc result = new ArrayList JavaDoc();
172         for (int i = 0; i < clusters.length; i++) {
173             ServerRef[] serverRefs = clusters[i].getServerRef();
174             for (int j = 0; j < serverRefs.length; j++) {
175                 for (int k = 0; k < servers.length; k++) {
176                     if (serverRefs[j].getRef().equals(servers[k].getName())) {
177                         if (!result.contains(clusters[i])) {
178                             result.add(clusters[i]);
179                         }
180                     }
181                 }
182             }
183         }
184         return (Cluster[])result.toArray(new Cluster[result.size()]);
185     }
186     
187     /**
188      * Return the configuration associated with the given clusterName. An exception
189      * is thrown if the clusters configuration does not exist (which should never
190      * happen).
191      */

192     public static Config getConfigForCluster(ConfigContext configContext, String JavaDoc clusterName)
193         throws ConfigException
194     {
195         final Cluster cluster = getClusterByName(configContext, clusterName);
196         final Domain domain = getDomainConfigBean(configContext);
197         final Config config = domain.getConfigs().getConfigByName(cluster.getConfigRef());
198         if (config == null) {
199             throw new ConfigException(_strMgr.getString("noSuchClusterConfig",
200                 cluster.getConfigRef(), clusterName));
201         }
202         return config;
203     }
204     
205     /**
206      * Return true if the cluster is standalone. A standalone cluster has a configuration
207      * named <clusterName>-config and it configuration is referenced by the cluster
208      * (and its instances) only. No other clusters or servers may refer to its configuration.
209      */

210     public static boolean isClusterStandAlone(ConfigContext configContext, String JavaDoc clusterName)
211         throws ConfigException
212     {
213         final Cluster cluster = getClusterByName(configContext, clusterName);
214         final String JavaDoc configName = cluster.getConfigRef();
215         if (isConfigurationNameStandAlone(configName, clusterName)) {
216             if (isConfigurationReferencedByClusterOnly(configContext, configName, clusterName)) {
217                 return true;
218             }
219         }
220        return false;
221     }
222         
223     /**
224      * Return true if the given server instance references the stated application.
225      */

226     public static boolean clusterReferencesApplication(ConfigContext configContext,
227         String JavaDoc clusterName, String JavaDoc appName) throws ConfigException
228     {
229         final Cluster cluster = getClusterByName(configContext, clusterName);
230         return clusterReferencesApplication(cluster, appName);
231     }
232     
233     public static boolean clusterReferencesApplication(Cluster cluster, String JavaDoc appName)
234         throws ConfigException
235     {
236         final ApplicationRef ref = cluster.getApplicationRefByRef(appName);
237         return (ref == null) ? false : true;
238     }
239
240     public static boolean clusterReferencesJdbcConPool(ConfigContext ctx,
241             String JavaDoc clusterName, String JavaDoc poolName) throws ConfigException
242     {
243
244         final Cluster cluster = getClusterByName(ctx, clusterName);
245         return clusterReferencesJdbcConPool(cluster, poolName);
246     }
247     
248     public static boolean clusterReferencesJdbcConPool(Cluster cluster,
249             String JavaDoc poolName) throws ConfigException
250     {
251         final ResourceRef ref = cluster.getResourceRefByRef(poolName);
252         return (ref == null) ? false : true;
253     }
254        
255     /**
256      * Return the server instances referencing the given configName or a zero length list.
257      */

258     public static Cluster[] getClustersReferencingApplication(ConfigContext configContext, String JavaDoc appName)
259         throws ConfigException
260     {
261         //Now find all server instances that reference the application
262
Cluster[] clusters = getClustersInDomain(configContext);
263         ArrayList JavaDoc result = new ArrayList JavaDoc();
264         for (int i = 0; i < clusters.length; i++) {
265             if (clusterReferencesApplication(clusters[i], appName)) {
266                 result.add(clusters[i]);
267             }
268         }
269         return (Cluster[])result.toArray(new Cluster[result.size()]);
270     }
271     
272     /**
273      * Return true if the given server instance references the stated resource.
274      */

275     public static boolean clusterReferencesResource(ConfigContext configContext,
276         String JavaDoc clusterName, String JavaDoc resourceName) throws ConfigException
277     {
278         final Cluster cluster = getClusterByName(configContext, clusterName);
279         return clusterReferencesResource(cluster, resourceName);
280     }
281     
282     public static boolean clusterReferencesResource(Cluster cluster,
283         String JavaDoc resourceName) throws ConfigException
284     {
285         final ResourceRef ref = cluster.getResourceRefByRef(resourceName);
286         return (ref == null) ? false : true;
287     }
288        
289     /**
290      * Return the server instances referencing the given configName or a zero length list.
291      */

292     public static Cluster[] getClustersReferencingResource(ConfigContext configContext, String JavaDoc resName)
293         throws ConfigException
294     {
295         //Now find all server instances that reference the application
296
Cluster[] clusters = getClustersInDomain(configContext);
297         ArrayList JavaDoc result = new ArrayList JavaDoc();
298         for (int i = 0; i < clusters.length; i++) {
299             if (clusterReferencesResource(clusters[i], resName)) {
300                 result.add(clusters[i]);
301             }
302         }
303         return (Cluster[])result.toArray(new Cluster[result.size()]);
304     }
305     
306     
307         /**
308      * Return all the application refs of the server
309      */

310     public static ApplicationRef[] getApplicationReferences(ConfigContext configContext,
311         String JavaDoc clusterName) throws ConfigException
312     {
313         final Cluster cluster = getClusterByName(configContext, clusterName);
314         if (cluster.getApplicationRef() == null) {
315             return new ApplicationRef[0];
316         } else {
317             return cluster.getApplicationRef();
318         }
319     }
320     
321     /**
322      * Return all the resource refs of the server
323      */

324     public static ResourceRef[] getResourceReferences(ConfigContext configContext,
325         String JavaDoc clusterName) throws ConfigException
326     {
327         final Cluster cluster = getClusterByName(configContext, clusterName);
328         if (cluster.getResourceRef() == null) {
329             return new ResourceRef[0];
330         } else {
331             return cluster.getResourceRef();
332         }
333     }
334 }
335
Popular Tags