KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > servermgmt > DomainConfigValidator


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;
25
26 import com.sun.enterprise.util.i18n.StringManager;
27 import com.sun.enterprise.admin.servermgmt.InvalidConfigException;
28
29 import java.util.HashMap JavaDoc;
30
31 /**
32  * This class validates the domain config Map object. It does this by invoking
33  * the validator of each required entry. Subclasses must specify the required
34  * set of DomainConfigEntryInfo objects.
35  */

36 public abstract class DomainConfigValidator extends Validator
37 {
38     /**
39      * i18n strings manager object
40      */

41     private static final StringManager strMgr =
42         StringManager.getManager(DomainConfigValidator.class);
43
44     /**
45      * Holder class for domain config entry meta info. The meta info of a
46      * domainConfig entry is the key, dataType, displayText and an optional
47      * validator object. The key must be defined in the DomainConfig class.
48      */

49     protected static class DomainConfigEntryInfo
50     {
51         final String JavaDoc key;
52         final String JavaDoc dataType;
53         final String JavaDoc displayText;
54         final Validator validator;
55
56         /** Creates a new DomainConfigEntryInfo object */
57         public DomainConfigEntryInfo(String JavaDoc key,
58                                      String JavaDoc dataType,
59                                      String JavaDoc displayText,
60                                      Validator validator)
61         {
62             this.key = key;
63             this.dataType = dataType;
64             this.displayText = displayText;
65             this.validator = validator;
66         }
67
68         /**
69          * Returns true if a non-null Validator object was specified during
70          * construction.
71          */

72         public boolean hasValidator()
73         {
74             return (validator != null);
75         }
76     }
77
78     /**
79      * An array of DomainConfigEntryInfo objects that must be initialized by
80      * subclasses.
81      */

82     private DomainConfigEntryInfo[] entries;
83
84     /**
85      * Constructs a new DomainConfigValidator object.
86      * @param entries An array of required DomainConfigEntryInfo objects.
87      * Must be supplied by subclasses.
88      */

89     protected DomainConfigValidator(DomainConfigEntryInfo[] entries)
90     {
91         super(strMgr.getString("domainConfig"), DomainConfig.class);
92         this.entries = entries;
93     }
94     
95     protected DomainConfigValidator(String JavaDoc name, Class JavaDoc type, DomainConfigEntryInfo[] entries)
96     {
97         super(name, type);
98         this.entries = entries;
99     }
100
101     /**
102      * Validates the domainConfig. For each required domain config entry in the
103      * entries, gets the value from the domainConfig object and invokes the
104      * validator of that entry. Skips the validation of an entry if no validator
105      * is specified for that entry.
106      * @param domainConfig The domainConfig object that needs to be validated.
107      * A domainConfig object is valid if it
108      * <ul>
109      * is of type DomainConfig
110      * contains the required set of DomainConfig keys
111      * the value for each required key is valid.
112      * </ul>
113      * @throws InvalidConfigException If invalid domainConfig is supplied.
114      */

115     public void validate(Object JavaDoc domainConfig)
116         throws InvalidConfigException
117     {
118         super.validate(domainConfig);
119         for (int i = 0; i < entries.length; i++)
120         {
121             if (isValidate(entries[i].key, domainConfig))
122             {
123                 final Object JavaDoc value = ((HashMap JavaDoc)domainConfig).get(entries[i].key);
124                 if (entries[i].hasValidator())
125                 {
126                     entries[i].validator.validate(value);
127                 }
128             }
129         }
130     }
131
132     /**
133      * @param key
134      * @return Returns true if the key is valid and required.
135      */

136     public boolean isKeyAllowed(Object JavaDoc key)
137     {
138         return (get(key) != null);
139     }
140
141     /**
142      * @param key
143      * @param value
144      * @return Returns true if the key is valid and required and the value for
145      * that key is valid.
146      */

147     public boolean isValueValid(Object JavaDoc key, Object JavaDoc value)
148     {
149         boolean isValid = false;
150         final DomainConfigEntryInfo info = get(key);
151         if (info != null)
152         {
153             if (info.hasValidator())
154             {
155                 try
156                 {
157                     info.validator.validate(value);
158                 }
159                 catch (InvalidConfigException idce)
160                 {
161                     isValid = false;
162                 }
163             }
164             else
165             {
166                 isValid = true;
167             }
168         }
169         return isValid;
170     }
171
172     /**
173      * @return Returns the accepted datatype for the key. The returned value is
174      * the fully qualified class name of the datatype. If the key is invalid or
175      * doesnot belong to the valid domain config key set, "" is returned.
176      */

177     public String JavaDoc getDataType(Object JavaDoc key)
178     {
179         final DomainConfigEntryInfo info = get(key);
180         if (info != null)
181         {
182             return info.dataType;
183         }
184         return "";
185     }
186
187     /**
188      * This method allows subclasses to say if an entry should be validated at
189      * all. This is an attempt to add some flexibility to the otherwise static
190      * validation. (Eg:- If we donot want to validate the ports during domain
191      * creation)
192      */

193     protected abstract boolean isValidate(String JavaDoc name, Object JavaDoc domainConfig);
194
195     /**
196      * @return Returns the DomainConfigEntryInfo corresponding to the key.
197      * Returns null if no DomainConfigEntryInfo exists in the entries
198      * for the given key.
199      */

200     private DomainConfigEntryInfo get(Object JavaDoc key)
201     {
202         for (int i = 0; i < entries.length; i++)
203         {
204             if (entries[i].key.equals(key)) { return entries[i]; }
205         }
206         return null;
207     }
208 }
209
Popular Tags