KickJava   Java API By Example, From Geeks To Geeks.

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


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.ServerBeansFactory;
28 import com.sun.enterprise.config.serverbeans.Domain;
29 import com.sun.enterprise.config.serverbeans.NodeAgent;
30 import com.sun.enterprise.config.serverbeans.Servers;
31 import com.sun.enterprise.config.serverbeans.Cluster;
32 import com.sun.enterprise.config.serverbeans.Server;
33 import com.sun.enterprise.config.serverbeans.Config;
34
35 import com.sun.enterprise.util.i18n.StringManager;
36
37 import com.sun.enterprise.admin.util.IAdminConstants;
38
39 import java.util.ArrayList JavaDoc;
40
41 public class ConfigAPIHelper extends ReferenceHelperBase implements IAdminConstants {
42         
43     protected static final StringManager _strMgr=StringManager.getManager(ConfigAPIHelper.class);
44     private static ConfigAPIHelper _theInstance;
45     private final static String JavaDoc[] ILLEGAL_NAME_STRINGS = {"--"};
46
47     
48     public ConfigAPIHelper() {
49         super();
50     }
51     
52     protected Server[] getReferencingServers(ConfigContext configContext, String JavaDoc name)
53         throws ConfigException
54     {
55         return ServerHelper.getServersReferencingConfig(configContext, name);
56     }
57     
58     protected Cluster[] getReferencingClusters(ConfigContext configContext, String JavaDoc name)
59         throws ConfigException
60     {
61         return ClusterHelper.getClustersReferencingConfig(configContext, name);
62     }
63         
64     private synchronized static ConfigAPIHelper getInstance()
65     {
66         if (_theInstance == null) {
67             _theInstance = new ConfigAPIHelper();
68         }
69         return _theInstance;
70     }
71     
72     public static Domain getDomainConfigBean(ConfigContext configCtxt)
73         throws ConfigException
74     {
75         return ServerBeansFactory.getDomainBean(configCtxt);
76     }
77
78     /**
79      * Return all the configurations in the domain. We dont check for null here
80      * because we assume there is always at least one configuration in the domain.
81      */

82     public static Config[] getConfigsInDomain(ConfigContext configContext)
83         throws ConfigException
84     {
85         final Domain domain = getDomainConfigBean(configContext);
86         return domain.getConfigs().getConfig();
87     }
88     
89     /**
90      * Return true if the given configName is a configuration.
91      */

92     public static boolean isAConfig(ConfigContext configContext, String JavaDoc configName)
93         throws ConfigException
94     {
95         final Domain domain = getDomainConfigBean(configContext);
96         final Config config = domain.getConfigs().getConfigByName(configName);
97         return (config != null ? true : false);
98     }
99         
100     /**
101      * Return the configuration associated with the given configName. An exception
102      * is thrown if the configuration does not exist.
103      */

104     public static Config getConfigByName(ConfigContext configContext, String JavaDoc configName)
105         throws ConfigException
106     {
107         final Domain domain = getDomainConfigBean(configContext);
108         final Config config = domain.getConfigs().getConfigByName(configName);
109         if (config == null) {
110             throw new ConfigException(_strMgr.getString("noSuchConfig", configName));
111         }
112         return config;
113     }
114
115     /**
116      * Returns true if the name is unique across a namespace including:
117      * server instances, configurations, clusters, node agents.
118      */

119     public static boolean isNameUnique(ConfigContext configContext, String JavaDoc name)
120         throws ConfigException
121     {
122         final Server[] servers = ServerHelper.getServersInDomain(configContext);
123         for (int i = 0; i < servers.length; i++) {
124             if (servers[i].getName().equals(name)) {
125                 return false;
126             }
127         }
128         
129         final Config[] configs = getConfigsInDomain(configContext);
130         for (int i = 0; i < configs.length; i++) {
131             if (configs[i].getName().equals(name)) {
132                 return false;
133             }
134         }
135         
136         final Cluster[] clusters = ClusterHelper.getClustersInDomain(configContext);
137         for (int i = 0; i < clusters.length; i++) {
138             if (clusters[i].getName().equals(name)) {
139                 return false;
140             }
141         }
142         
143         final NodeAgent[] controllers = NodeAgentHelper.getNodeAgentsInDomain(configContext);
144         for (int i = 0; i < controllers.length; i++) {
145             if (controllers[i].getName().equals(name)) {
146                 return false;
147             }
148         }
149         return true;
150     }
151     
152     /**
153      * Given a server or cluster name, checks if the name is legal according
154      * to our defined specs.
155      */

156     public static void checkLegalName(String JavaDoc name)
157         throws ConfigException
158     {
159         // we don't want an instance with, say, "--" in the name.
160
// this will cause havoc with CLI
161
// we throw the Exception so that we can document *what* the
162
// exact problem with the name is
163

164         for(int i = 0; i < ILLEGAL_NAME_STRINGS.length; i++)
165         {
166             if(name.indexOf(ILLEGAL_NAME_STRINGS[i]) >= 0)
167             {
168                 String JavaDoc s = _strMgr.getString("illegalName", ILLEGAL_NAME_STRINGS[i]);
169                 throw new ConfigException(s);
170             }
171         }
172     }
173     
174     public static String JavaDoc getStandAloneConfigurationName(String JavaDoc name)
175     {
176         return name + STANDALONE_CONFIGURATION_SUFFIX;
177     }
178     
179     /**
180      * Returns true if the given configuration and cluster or instance name represents
181      * a standalone configuration.
182      */

183     public static boolean isConfigurationNameStandAlone(String JavaDoc configName, String JavaDoc name)
184     {
185         return configName.equals(getStandAloneConfigurationName(
186             name)) ? true : false;
187     }
188     
189     /**
190      * Is the configuration referenced by anyone (i.e. any server instance or cluster
191      */

192     public static boolean isConfigurationReferenced(ConfigContext configContext, String JavaDoc configName)
193         throws ConfigException
194     {
195         return getInstance().isReferenced(configContext, configName);
196     }
197     
198     /**
199      * Return true if the configuration is referenced by no-one other than the given
200      * server instance.
201      */

202     public static boolean isConfigurationReferencedByServerOnly(ConfigContext configContext,
203         String JavaDoc configName, String JavaDoc serverName) throws ConfigException
204     {
205         return getInstance().isReferencedByServerOnly(configContext, configName, serverName);
206     }
207     
208     /**
209      * Return true if the configuration is referenced by no-one other than the given
210      * cluster.
211      */

212     public static boolean isConfigurationReferencedByClusterOnly(ConfigContext configContext,
213         String JavaDoc configName, String JavaDoc clusterName) throws ConfigException
214     {
215         return getInstance().isReferencedByClusterOnly(configContext, configName, clusterName);
216     }
217     
218     /**
219      * Find all the servers or clusters associated with the given configuration and return them
220      * as a comma separated list.
221      */

222     public static String JavaDoc getConfigurationReferenceesAsString(ConfigContext configContext, String JavaDoc configName)
223         throws ConfigException
224     {
225        return getInstance().getReferenceesAsString(configContext, configName);
226     }
227 }
228
Popular Tags