KickJava   Java API By Example, From Geeks To Geeks.

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


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

99
100 public class NameList {
101     
102     // Logging
103
static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.APPVERIFY_LOGGER);
104     LocalStringManagerImpl _localStrings = StringManagerHelper.getLocalStringsManager();
105
106     ConfigContext _ctx;
107
108     ArrayList JavaDoc _srcXPathes;
109     ArrayList JavaDoc _refXPathes;
110     
111     Hashtable JavaDoc _srcLists = null;
112     Hashtable JavaDoc _refLists = null;
113     
114     boolean _bKeepList = false;
115     String JavaDoc _name;
116     String JavaDoc _fullName;
117     String JavaDoc _scope;
118     int _scope_depth;
119     
120     
121     public NameList(String JavaDoc name, String JavaDoc fullName, String JavaDoc scope, ArrayList JavaDoc srcXPathes, ArrayList JavaDoc refXPathes, ConfigContext ctx, boolean bPreCreateAndKeepList) {
122         _name = name;
123         _fullName = fullName;
124         _srcXPathes = srcXPathes;
125         _refXPathes = refXPathes;
126         if(scope!=null && !scope.equals("/"))
127         {
128             _scope = scope.trim();
129             _scope_depth = XPathHelper.getNumberOfElemTokens(_scope);
130 //System.out.println("######################### _scope = " +_scope + " _scope_depth="+_scope_depth);
131
}
132         _ctx = ctx;
133         _bKeepList = bPreCreateAndKeepList;
134         _srcLists = new Hashtable JavaDoc();
135         _refLists = new Hashtable JavaDoc();
136         if(bPreCreateAndKeepList)
137             buildLists(null);
138     }
139
140     public String JavaDoc toString()
141     {
142         String JavaDoc str = "domain name: " + _name;
143         
144         if(_srcLists!=null)
145         {
146             Iterator JavaDoc lists = _srcLists.keySet().iterator();
147             while(lists.hasNext())
148             {
149                 String JavaDoc key = (String JavaDoc)lists.next();
150                 str = str + "\n " + "list name: " + key;
151                 Hashtable JavaDoc list = (Hashtable JavaDoc)_srcLists.get(key);
152                 if(list!=null)
153                 {
154                     Object JavaDoc[] keys = (Object JavaDoc[])list.keySet().toArray();
155                     for(int i=0; i<keys.length; i++)
156                         str = str + "\n " + keys[i]; // + " xpath=" + list.get(keys[i]);
157
}
158             }
159         }
160         if(_refLists!=null)
161         {
162             Iterator JavaDoc lists = _refLists.keySet().iterator();
163             while(lists.hasNext())
164             {
165                 String JavaDoc key = (String JavaDoc)lists.next();
166                 str = str + "\n " + "Referencees list name: " + key;
167                 Hashtable JavaDoc list = (Hashtable JavaDoc)_refLists.get(key);
168                 if(list!=null)
169                 {
170                     Object JavaDoc[] keys = (Object JavaDoc[])list.keySet().toArray();
171                     for(int i=0; i<keys.length; i++)
172                         str = str + "\n " + keys[i]; // + " xpath=" + list.get(keys[i]);
173
}
174             }
175         }
176         return str;
177     }
178
179     public String JavaDoc getDomainValueSourceXPath(Object JavaDoc value, String JavaDoc xpath, boolean bRef)
180     {
181         String JavaDoc listName;
182         if(xpath==null || value==null || (listName = getListNameForXpath(xpath))==null)
183             return null;
184         Hashtable JavaDoc list = getNamedList(listName, bRef, false);
185         if(list!=null)
186             return (String JavaDoc)list.get(value);
187         return null;
188     }
189     
190     public boolean isValueInNameDomain(Object JavaDoc value, String JavaDoc xpath, boolean bRef)
191     {
192         return (getDomainValueSourceXPath(value, xpath, bRef)!=null);
193     }
194
195     protected void buildLists(String JavaDoc onlyPrefix)
196     {
197         //FIXME: should clear only poroper sublist
198
_srcLists.clear();
199         _refLists.clear();
200         
201         //source lists
202
buildList(_srcXPathes, onlyPrefix, false);
203         //referencees lists
204
buildList(_refXPathes, onlyPrefix, true);
205     }
206     
207     private void buildList(ArrayList JavaDoc xpathes, String JavaDoc onlyPrefix, boolean bRef)
208     {
209         //build list
210
AttributeList JavaDoc arr = XPathHelper.resolve(_ctx, xpathes, onlyPrefix);
211         for(int i=0; i<arr.size(); i++)
212         {
213             Attribute JavaDoc attr = (Attribute JavaDoc)arr.get(i);
214             String JavaDoc[] values = ((String JavaDoc)attr.getValue()).split(",");
215             for(int j=0; j<values.length; j++)
216             {
217                 addValueToProperList(attr.getName(), values[j], bRef);
218             }
219         }
220     }
221     
222     private String JavaDoc addValueToProperList(String JavaDoc xpath, Object JavaDoc value, boolean bRef)
223     {
224         String JavaDoc listName;
225         if(xpath==null || value==null || (listName = getListNameForXpath(xpath))==null)
226             return null;
227 //System.out.println("+++addValueToProperList() domain-name=" + _name + " sublistName=" + listName + " value=" + value);
228
addValueToNamedList(listName, xpath, value, bRef);
229         return listName;
230     }
231     
232     private void addValueToNamedList(String JavaDoc listName, String JavaDoc sourceXPath, Object JavaDoc value, boolean bRef)
233     {
234         if(listName!=null && value!=null)
235         {
236             Hashtable JavaDoc list = getNamedList(listName, bRef, true);
237             if(list.get(value)==null)
238                 list.put(value, sourceXPath);
239         }
240     }
241     
242     private Hashtable JavaDoc getNamedList(String JavaDoc listName, boolean bRef, boolean bCreateIfNotFound)
243     {
244         Hashtable JavaDoc lists = bRef?_refLists:_srcLists;
245         Hashtable JavaDoc list = (Hashtable JavaDoc)lists.get(listName);
246         if(list==null && bCreateIfNotFound)
247         {
248             list = new Hashtable JavaDoc();
249             lists.put(listName, list);
250         }
251         return list;
252     }
253     
254     private String JavaDoc getListNameForXpath(String JavaDoc xpath)
255     {
256         return XPathHelper.getXPathPrefix(xpath, _scope_depth);
257     }
258     
259 }
260  
Popular Tags