KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > servermgmt > pe > PEDomainConfigValidator


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 package com.sun.enterprise.admin.servermgmt.pe;
25
26
27
28
29 import com.sun.enterprise.admin.servermgmt.DomainConfig;
30 import com.sun.enterprise.admin.servermgmt.DomainConfigValidator;
31 import com.sun.enterprise.admin.servermgmt.FileValidator;
32 import com.sun.enterprise.admin.servermgmt.InvalidConfigException;
33 import com.sun.enterprise.admin.servermgmt.PortValidator;
34 import com.sun.enterprise.admin.servermgmt.StringValidator;
35 import com.sun.enterprise.util.i18n.StringManager;
36 import java.lang.StringBuffer JavaDoc;
37 import java.util.HashSet JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.Map.Entry;
40 import java.util.Map JavaDoc;
41 import java.util.Set JavaDoc;
42 import java.util.TreeMap JavaDoc;
43 import java.util.TreeSet JavaDoc;
44
45 /**
46  * This class defines the domain config entries that are required to create a
47  * PE Tomcat domain.
48  */

49 class PEDomainConfigValidator extends DomainConfigValidator
50 {
51     /**
52      * i18n strings manager object
53      */

54     private static final StringManager strMgr =
55         StringManager.getManager(PEDomainConfigValidator.class);
56
57     private static final String JavaDoc lInstallRoot = strMgr.getString("installRoot");
58     private static final String JavaDoc lDomainsRoot = strMgr.getString("domainsRoot");
59     private static final String JavaDoc lJavaHome = strMgr.getString("javaHome");
60     private static final String JavaDoc lAdminPort = strMgr.getString("adminPort");
61     private static final String JavaDoc lInstancePort = strMgr.getString("instancePort");
62     private static final String JavaDoc lHostName = strMgr.getString("hostName");
63     private static final String JavaDoc lJmsUser = strMgr.getString("jmsUser");
64     private static final String JavaDoc lJmsPasswd = strMgr.getString("jmsPassword");
65     private static final String JavaDoc lJmsPort = strMgr.getString("jmsPort");
66     private static final String JavaDoc lOrbPort = strMgr.getString("orbPort");
67
68     static DomainConfigEntryInfo[] entries = new DomainConfigEntryInfo[]
69     {
70         new DomainConfigEntryInfo(DomainConfig.K_INSTALL_ROOT,
71                                   "java.lang.String", lInstallRoot,
72                                   new FileValidator(lInstallRoot, "dr")),
73         new DomainConfigEntryInfo(DomainConfig.K_DOMAINS_ROOT,
74                                   "java.lang.String", lDomainsRoot,
75                                   new FileValidator(lDomainsRoot, "drw")),
76         new DomainConfigEntryInfo(DomainConfig.K_ADMIN_PORT,
77                                   "java.lang.Integer", lAdminPort,
78                                   new PortValidator(lAdminPort)),
79         new DomainConfigEntryInfo(DomainConfig.K_INSTANCE_PORT,
80                                   "java.lang.Integer", lInstancePort,
81                                   new PortValidator(lInstancePort)),
82         new DomainConfigEntryInfo(DomainConfig.K_HOST_NAME,
83                                   "java.lang.String", lHostName,
84                                   new StringValidator(lHostName)),
85         new DomainConfigEntryInfo(DomainConfig.K_JMS_USER,
86                                   "java.lang.String", lJmsUser,
87                                   new StringValidator(lJmsUser)),
88         new DomainConfigEntryInfo(DomainConfig.K_JMS_PASSWORD,
89                                   "java.lang.String", lJmsPasswd,
90                                   new StringValidator(lJmsPasswd)),
91         new DomainConfigEntryInfo(DomainConfig.K_ORB_LISTENER_PORT,
92                                   "java.lang.Integer", lOrbPort,
93                                   new PortValidator(lOrbPort)),
94         new DomainConfigEntryInfo(DomainConfig.K_JMS_PORT,
95                                   "java.lang.Integer", lJmsPort,
96                                   new PortValidator(lJmsPort))
97     };
98
99     /** Creates a new instance of PEDomainConfigValidator */
100     PEDomainConfigValidator()
101     {
102         super(entries);
103     }
104
105     public void validate(Object JavaDoc domainConfig) throws InvalidConfigException{
106         super.validate(domainConfig);
107         uniquePorts((DomainConfig) domainConfig);
108     }
109
110     protected boolean isValidate(String JavaDoc name, Object JavaDoc domainConfig)
111     {
112         boolean isPortEntry = DomainConfig.K_ADMIN_PORT.equals(name) ||
113                                 DomainConfig.K_INSTANCE_PORT.equals(name) ||
114                                 DomainConfig.K_ORB_LISTENER_PORT.equals(name) ||
115                                 DomainConfig.K_JMS_PORT.equals(name);
116         return (isPortEntry) ? isValidatePorts((Map JavaDoc)domainConfig) : true;
117     }
118
119     private boolean isValidatePorts(Map JavaDoc domainConfig)
120     {
121         Boolean JavaDoc isValidatePorts =
122             (Boolean JavaDoc)domainConfig.get(DomainConfig.K_VALIDATE_PORTS);
123         return (null != isValidatePorts) ?
124                     isValidatePorts.booleanValue() : true;
125     }
126
127     final void uniquePorts(final DomainConfig dc) throws InvalidConfigException{
128         final Map JavaDoc ports = dc.getPorts();
129         final Set JavaDoc portValues = new HashSet JavaDoc(ports.values());
130         if (ports.keySet().size() != portValues.size()){
131             throw new InvalidConfigException(getMessage(ports));
132         }
133     }
134
135     private final String JavaDoc getMessage(final Map JavaDoc ports){
136         return getLocalizedString("duplicatePorts", getDuplicatePorts(ports));
137     }
138
139     private final String JavaDoc getLocalizedString(final String JavaDoc key, final Object JavaDoc o){
140         return strMgr.getString(key, o);
141     }
142
143     final String JavaDoc getDuplicatePorts(final Map JavaDoc ports){
144         return printDuplicatesFromMap(reverseMap(ports));
145     }
146
147
148         /**
149          * Reverse the given map. i.e. a keys in the input map are
150          * values in the output map, and values in the input map are
151          * kays in the output map. Note that the values in the output
152          * map must be sets, to allow for many to 1 relations in the input
153          * map.
154          */

155     private final Map JavaDoc reverseMap(final Map JavaDoc inputMap){
156         final Map JavaDoc outputMap = new TreeMap JavaDoc();
157         for (Iterator JavaDoc entries = inputMap.entrySet().iterator(); entries.hasNext(); ){
158             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) entries.next();
159             addEntryToMap(entry.getKey(), entry.getValue(), outputMap);
160         }
161         return outputMap;
162     }
163
164         /**
165          * Add the given key/value pair, but reversing it, in the given
166          * map. reversal means that the values in the map must be sets.
167          */

168     private final void addEntryToMap(final Object JavaDoc key, final Object JavaDoc value, final Map JavaDoc map){
169         if (!map.containsKey(value)){
170             map.put(value, new TreeSet JavaDoc());
171         }
172         ((Set JavaDoc)map.get(value)).add(key);
173     }
174
175         /**
176          * Return a string representation of the given map, but only for
177          * those entries where the value has a size greater than 1
178          * @param map a map of key to Set of value
179          */

180     private final String JavaDoc printDuplicatesFromMap(final Map JavaDoc map){
181         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
182         final Iterator JavaDoc it = map.entrySet().iterator();
183         Map.Entry JavaDoc entry = getNextDuplicate(it);
184         if (entry != null){
185             printEntry(sb, entry);
186             while ((entry = getNextDuplicate(it)) != null){
187                 sb.append(", ");
188                 printEntry(sb, entry);
189             }
190         }
191         return sb.toString();
192     }
193   
194
195         /**
196          * Get next entry from iterator whose value is a set of
197          * cardinality greater than 1
198          */

199     private final Map.Entry JavaDoc getNextDuplicate(final Iterator JavaDoc it){
200         while (it.hasNext()){
201             Map.Entry JavaDoc result = (Map.Entry JavaDoc) it.next();
202             if (((Set JavaDoc)result.getValue()).size() > 1){
203                 return result;
204             }
205         }
206         return null;
207     }
208
209     private final void printEntry(final StringBuffer JavaDoc sb, final Map.Entry JavaDoc entry){
210         printEntry(sb, (Object JavaDoc) entry.getKey(), (Set JavaDoc) entry.getValue());
211     }
212
213         /**
214          * Print the given key and set onto the given string
215          * buffer. Note that the set is considered to contain more than
216          * one object
217          */

218     private final void printEntry(final StringBuffer JavaDoc sb, final Object JavaDoc key, final Set JavaDoc dups){
219         sb.append(key).append(" -> ");
220         printSet(sb, dups);
221     }
222
223         /**
224          * Print the given set on the given string buffer
225          */

226     private final void printSet(final StringBuffer JavaDoc sb, final Set JavaDoc set){
227         sb.append("{");
228         String JavaDoc separator = "";
229         for (Iterator JavaDoc it = set.iterator(); it.hasNext(); ){
230             sb.append(separator).append(it.next());
231             separator = ", ";
232         }
233         sb.append("}");
234     }
235 }
236
Popular Tags