KickJava   Java API By Example, From Geeks To Geeks.

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


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.config.serverbeans;
24
25 import com.sun.enterprise.config.ConfigContext;
26 import com.sun.enterprise.config.ConfigException;
27 import com.sun.enterprise.config.serverbeans.Server;
28 import com.sun.enterprise.config.serverbeans.Cluster;
29
30 import com.sun.enterprise.util.i18n.StringManager;
31
32 import java.util.ArrayList JavaDoc;
33
34 public abstract class ReferenceHelperBase {
35     
36     protected static final StringManager _strMgr=StringManager.getManager(ReferenceHelperBase.class);
37        
38     public ReferenceHelperBase() {
39     }
40     
41     protected abstract Server[] getReferencingServers(ConfigContext configContext, String JavaDoc name)
42         throws ConfigException;
43     protected abstract Cluster[] getReferencingClusters(ConfigContext configContext, String JavaDoc name)
44         throws ConfigException;
45     
46     /**
47      * Is the configuration referenced by anyone (i.e. any server instance or cluster
48      */

49     public boolean isReferenced(ConfigContext configContext, String JavaDoc name)
50         throws ConfigException
51     {
52         //See if any servers are referencing the resource
53
Server[] servers = getReferencingServers(configContext, name);
54         if (servers.length > 0) {
55             return true;
56         }
57         //See if any clusters are referencing the resource
58
Cluster[] clusters = getReferencingClusters(configContext, name);
59         if (clusters.length > 0) {
60             return true;
61         }
62         return false;
63     }
64     
65     /**
66      * Return true if the configuration is referenced by no-one other than the given
67      * server instance.
68      */

69     public boolean isReferencedByServerOnly(ConfigContext configContext,
70         String JavaDoc name, String JavaDoc serverName) throws ConfigException
71     {
72         Server[] servers = getReferencingServers(configContext, name);
73         if (servers.length == 1 && servers[0].getName().equals(serverName)) {
74             //The stated server is the only one referencing the config so see if any
75
//clusters reference the config
76
Cluster[] clusters = getReferencingClusters(configContext, name);
77             if (clusters.length == 0) {
78                 return true;
79             }
80         }
81         return false;
82     }
83     
84     /**
85      * Return true if the configuration is referenced by no-one other than the given
86      * cluster.
87      */

88     public boolean isReferencedByClusterOnly(ConfigContext configContext,
89         String JavaDoc name, String JavaDoc clusterName) throws ConfigException
90     {
91         //See if any clusters reference the config
92
Cluster[] clusters = getReferencingClusters(configContext, name);
93         if (clusters.length == 1 && clusters[0].getName().equals(clusterName)) {
94             Server[] servers = getReferencingServers(configContext, name);
95             if (servers.length == 0) {
96                 return true;
97             } else {
98                 //Check to see that the only servers referencing the configuration are those that
99
//are also part of the cluster.
100
Server[] clusterServers = ServerHelper.getServersInCluster(configContext, clusterName);
101                 if (clusterServers.length == servers.length) {
102                     boolean found = false;
103                     for (int i = 0; i < clusterServers.length; i++) {
104                         found = false;
105                         for (int j = 0; j < servers.length; j++) {
106                             if (clusterServers[i].getName().equals(servers[j].getName())) {
107                                 found = true;
108                                 break;
109                             }
110                         }
111                         if (!found) {
112                             return false;
113                         }
114                     }
115                     if (found) {
116                         return true;
117                     }
118                 }
119             }
120         }
121         return false;
122     }
123     
124     /**
125      * Find all the servers or clusters associated with the given configuration and return them
126      * as a comma separated list.
127      */

128     public String JavaDoc getReferenceesAsString(ConfigContext configContext, String JavaDoc name)
129         throws ConfigException
130     {
131         Server[] servers = getReferencingServers(configContext, name);
132         Cluster[] clusters = getReferencingClusters(configContext, name);
133         String JavaDoc serverList = ServerHelper.getServersAsString(servers);
134         String JavaDoc clusterList = ClusterHelper.getClustersAsString(clusters);
135         if (serverList.length() > 0) {
136             if (clusterList.length() > 0) {
137                 return serverList + "," + clusterList;
138             }
139             return serverList;
140         } else {
141             return clusterList;
142         }
143     }
144 }
145
Popular Tags