KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > serverbeans > validation > NameListMgr


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.config.serverbeans.validation;
25
26 //jdk imports
27
import javax.xml.parsers.*;
28 import org.w3c.dom.*;
29 import java.io.File JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31 import org.xml.sax.InputSource JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.net.URL JavaDoc;
34
35 import java.util.ArrayList JavaDoc;
36 import java.util.StringTokenizer JavaDoc;
37 import java.util.Hashtable JavaDoc;
38 import java.util.Enumeration JavaDoc;
39
40 import java.lang.Class JavaDoc;
41 import java.lang.reflect.Constructor JavaDoc;
42
43 // Logging
44
import java.util.logging.Logger JavaDoc;
45 import java.util.logging.Level JavaDoc;
46 import com.sun.logging.LogDomains;
47
48 import com.sun.enterprise.util.LocalStringManagerImpl;
49 import com.sun.enterprise.config.ConfigContext;
50 import com.sun.enterprise.config.ConfigContextEvent;
51 import com.sun.enterprise.config.ConfigContextEventListener;
52
53 // config imports
54
import com.sun.enterprise.config.ConfigFactory;
55 import com.sun.enterprise.config.ConfigException;
56 import com.sun.enterprise.config.ConfigBean;
57 import com.sun.enterprise.config.serverbeans.*;
58
59 /**
60  * This class provides access to NameLists defined for validations
61  * Its constructor parses nameLists.xml file to read lists descriptors.
62  *
63  * $Id: NameListMgr.java,v 1.4 2005/12/25 03:44:23 tcfujii Exp $
64  * @author alexkrav
65  *
66  * $Log: NameListMgr.java,v $
67  * Revision 1.4 2005/12/25 03:44:23 tcfujii
68  * Updated copyright text and year.
69  *
70  * Revision 1.3 2005/08/18 17:10:08 kravtch
71  * M3: Admin Config Validator changes:
72  * 1. referencees support in name domains;
73  * 2. "virtual servers" name domain is added;
74  * 3. required elements existance validation (for ADD/DELETE element);
75  * 4. Clean up of custom validators (removed duplication testy, covered by generic validation;
76  * 5. Validation messages are prefixed by message code (for SQE negative tests)
77  * Submitted by: kravtch
78  * Reviewed by: Shreedhar
79  * Affected modules admin/validator; admin-core/config-api
80  * Tests passed: SQE, QLT/EE + devtests
81  *
82  * Revision 1.2 2005/06/27 21:18:16 tcfujii
83  * Issue number: CDDL header updates.
84  *
85  * Revision 1.1 2005/06/17 17:09:58 kravtch
86  * MS3: Config Validator's infrastructure changes:
87  * - new "VALIDATE" ConfigContextEvent type introduced for "static" validation;
88  * - host "static" validation as ConfigContext listener for "VALIDATE" events;
89  * - new namespace "http://www.sun.com/ias/validation" support for RelaxNG schemas extension:
90  * - "name-domains" metadata support for test uniquiness/references;
91  * - new "belongs-to"/"references-to"/"type"/"**-than" schema attributes;
92  * - most rng element definitions and xslt scripts changed;
93  * - new base validation classes to perform generic validation;
94  * - custom validation test classes cleaned from performing generic validation cases;
95  * - ConfigBeans and domain dtd re-generated;
96  * - Schematron(scripts and validation) and Relax NG (validator only) not used for validation any more;
97  * Submitted by: kravtch
98  * Tests passed QLT/EE + devtests
99  * Modules affected: appserver-commons; admin/validator; admin-core/config-api
100  *
101 */

102
103 public class NameListMgr {
104     
105     // Logging
106
static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.APPVERIFY_LOGGER);
107     LocalStringManagerImpl _localStrings = StringManagerHelper.getLocalStringsManager();
108
109     Hashtable JavaDoc _lists;
110     boolean _precreateAndKeepLists;
111     ConfigContext _ctx;
112     
113     
114     public NameListMgr(ConfigContext ctx, boolean precreateAndKeepLists) {
115         _lists = new Hashtable JavaDoc();
116         _precreateAndKeepLists = precreateAndKeepLists;
117         _ctx = ctx;
118         loadDescriptors();
119 //System.out.println(this.toString());
120
}
121
122     public String JavaDoc toString()
123     {
124         String JavaDoc str = "";
125         
126         if(_lists!=null)
127         {
128             Enumeration JavaDoc keys = _lists.keys();
129             while(keys.hasMoreElements())
130             {
131                 String JavaDoc key = (String JavaDoc)keys.nextElement();
132                 str = str + "\n" + ((NameList)_lists.get(key)).toString();
133             }
134         }
135         return str;
136     }
137     
138     private NameList getNameList(String JavaDoc nameDomainName)
139     {
140         NameList list = null;
141         if(_lists!=null)
142             list=(NameList)_lists.get(nameDomainName);
143         if(list!=null)
144         {
145             //FIXME: 1. should no recreate all sublists
146
//FIXME: 2. should recreate only if ADD/DELETE happened
147
if(!_precreateAndKeepLists)
148             {
149                 //XPathHelper.getXPathPrefix(xpath
150
list.buildLists(null);
151             }
152         }
153         return list;
154     }
155             
156     public String JavaDoc getDomainValueSourceXPath(String JavaDoc nameDomainName, Object JavaDoc value, String JavaDoc xpath)
157     {
158         //returns true even if value not in doamin at all
159
NameList list = getNameList(nameDomainName);
160         if(list!=null)
161         {
162             return list.getDomainValueSourceXPath(value, xpath, false);
163         }
164         return null;
165     }
166     public String JavaDoc getDomainValueReferenceeXPath(String JavaDoc nameDomainName, Object JavaDoc value, String JavaDoc xpath)
167     {
168         //returns true even if value not in doamin at all
169
NameList list = getNameList(nameDomainName);
170         if(list!=null)
171         {
172             return list.getDomainValueSourceXPath(value, xpath, true);
173         }
174         return null;
175     }
176     
177     public boolean isUniqueValueInNameDomain(String JavaDoc nameDomainName, Object JavaDoc value, String JavaDoc xpath)
178     {
179         //returns true even if value not in doamin at all
180
NameList list = getNameList(nameDomainName);
181         if(list!=null && list.isValueInNameDomain(value, xpath, false))
182         {
183             String JavaDoc sourceXPath = list.getDomainValueSourceXPath(value, xpath, false);
184             return (xpath.equals(sourceXPath));
185         }
186         return true;
187     }
188
189     public boolean isValueInNameDomain(String JavaDoc nameDomainName, Object JavaDoc value, String JavaDoc xpath)
190     {
191         //returns true only if value is in correspondent name domain
192
NameList list = getNameList(nameDomainName);
193         if(list!=null)
194         {
195             return list.isValueInNameDomain(value, xpath, false);
196         }
197         return false;
198     }
199
200     public boolean isValueInNameDomainReferenced(String JavaDoc nameDomainName, Object JavaDoc value, String JavaDoc xpath)
201     {
202         //returns true only if value is in correspondent name domain
203
NameList list = getNameList(nameDomainName);
204         if(list!=null)
205         {
206             return list.isValueInNameDomain(value, xpath, true);
207         }
208         return false;
209     }
210     
211     public String JavaDoc getDescriptionForNameDomain(String JavaDoc nameDomainName)
212     {
213         //returns true only if value is in correspondent name domain
214
NameList list = getNameList(nameDomainName);
215         if(list!=null && list._fullName!=null)
216         {
217             return list._fullName;
218         }
219         return nameDomainName;
220     }
221     
222     // Get the path of namelist descriptors xml file from jar
223
private String JavaDoc getDescriptorsFilePath() throws Exception JavaDoc {
224         URL JavaDoc propertiesFile = NameListMgr.class.getClassLoader().getResource(
225              "com/sun/enterprise/config/serverbeans/validation/config/name-domains.xml");
226 //TODO: for test only
227
// URL propertiesFile = (new File("/ias/admin/validator/NameLists.xml")).toURL();
228
return propertiesFile.toString();
229     }
230
231     // Loads all namelist descriptors and instantiate correspondent name lists
232
public boolean loadDescriptors() {
233         boolean allIsWell = true;
234        
235         try {
236             DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
237             InputSource JavaDoc is = new InputSource JavaDoc(getDescriptorsFilePath());
238             Document doc = db.parse(is);
239             NodeList list = doc.getElementsByTagName("name-list");
240             for(int i=0; i<list.getLength(); i++)
241             {
242                 Element e = (Element) list.item(i);
243                 String JavaDoc name = e.getAttribute("name");
244                 String JavaDoc fullName = e.getAttribute("full-name");
245                 String JavaDoc scope = e.getAttribute("scope");
246                 //sources list
247
NodeList nl = e.getElementsByTagName("forms-from");
248                 ArrayList JavaDoc xpath_arr = new ArrayList JavaDoc();
249                 for(int j=0; j<nl.getLength(); j++)
250                 {
251                     String JavaDoc xpath = ((Element)nl.item(j)).getAttribute("xpath");
252                     if(xpath!=null && xpath.length()>0)
253                         xpath_arr.add(xpath);
254                 }
255                 //referencees list
256
nl = e.getElementsByTagName("referenced-by");
257                 ArrayList JavaDoc ref_xpath_arr = new ArrayList JavaDoc();
258                 for(int j=0; j<nl.getLength(); j++)
259                 {
260                     String JavaDoc xpath = ((Element)nl.item(j)).getAttribute("xpath");
261                     if(xpath!=null && xpath.length()>0)
262                         ref_xpath_arr.add(xpath);
263                 }
264 /*
265 System.out.println("name="+name);
266 System.out.println(" fullname="+fullName);
267 System.out.println(" scope="+scope);
268 for(int j=0; j<xpath_arr.size(); j++)
269     System.out.println(" xpath="+xpath_arr.get(j));
270 */

271                 
272                 _lists.put(name, new NameList(name, fullName, scope, xpath_arr, ref_xpath_arr, _ctx, _precreateAndKeepLists));
273             }
274         } catch (ParserConfigurationException e) {
275             _logger.log(Level.WARNING, "parser_error", e);
276             allIsWell = false;
277         } catch (SAXException JavaDoc e) {
278             _logger.log(Level.WARNING, "sax_error", e);
279             allIsWell = false;
280         } catch (IOException JavaDoc e) {
281             _logger.log(Level.WARNING, "error_loading_xmlfile", e);
282             allIsWell = false;
283         } catch(Exception JavaDoc e) {
284             _logger.log(Level.WARNING, "error", e);
285             allIsWell = false;
286         }
287         return allIsWell;
288     }
289
290     
291 }
292
Popular Tags