1 23 24 package com.sun.enterprise.config.serverbeans.validation; 25 26 import javax.xml.parsers.*; 28 import org.w3c.dom.*; 29 import java.io.File ; 30 import org.xml.sax.SAXException ; 31 import org.xml.sax.InputSource ; 32 import java.io.IOException ; 33 import java.net.URL ; 34 35 import java.util.ArrayList ; 36 import java.util.StringTokenizer ; 37 import java.util.Hashtable ; 38 import java.util.Enumeration ; 39 40 import java.lang.Class ; 41 import java.lang.reflect.Constructor ; 42 43 import java.util.logging.Logger ; 45 import java.util.logging.Level ; 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 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 102 103 public class NameListMgr { 104 105 static Logger _logger = LogDomains.getLogger(LogDomains.APPVERIFY_LOGGER); 107 LocalStringManagerImpl _localStrings = StringManagerHelper.getLocalStringsManager(); 108 109 Hashtable _lists; 110 boolean _precreateAndKeepLists; 111 ConfigContext _ctx; 112 113 114 public NameListMgr(ConfigContext ctx, boolean precreateAndKeepLists) { 115 _lists = new Hashtable (); 116 _precreateAndKeepLists = precreateAndKeepLists; 117 _ctx = ctx; 118 loadDescriptors(); 119 } 121 122 public String toString() 123 { 124 String str = ""; 125 126 if(_lists!=null) 127 { 128 Enumeration keys = _lists.keys(); 129 while(keys.hasMoreElements()) 130 { 131 String key = (String )keys.nextElement(); 132 str = str + "\n" + ((NameList)_lists.get(key)).toString(); 133 } 134 } 135 return str; 136 } 137 138 private NameList getNameList(String nameDomainName) 139 { 140 NameList list = null; 141 if(_lists!=null) 142 list=(NameList)_lists.get(nameDomainName); 143 if(list!=null) 144 { 145 if(!_precreateAndKeepLists) 148 { 149 list.buildLists(null); 151 } 152 } 153 return list; 154 } 155 156 public String getDomainValueSourceXPath(String nameDomainName, Object value, String xpath) 157 { 158 NameList list = getNameList(nameDomainName); 160 if(list!=null) 161 { 162 return list.getDomainValueSourceXPath(value, xpath, false); 163 } 164 return null; 165 } 166 public String getDomainValueReferenceeXPath(String nameDomainName, Object value, String xpath) 167 { 168 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 nameDomainName, Object value, String xpath) 178 { 179 NameList list = getNameList(nameDomainName); 181 if(list!=null && list.isValueInNameDomain(value, xpath, false)) 182 { 183 String sourceXPath = list.getDomainValueSourceXPath(value, xpath, false); 184 return (xpath.equals(sourceXPath)); 185 } 186 return true; 187 } 188 189 public boolean isValueInNameDomain(String nameDomainName, Object value, String xpath) 190 { 191 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 nameDomainName, Object value, String xpath) 201 { 202 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 getDescriptionForNameDomain(String nameDomainName) 212 { 213 NameList list = getNameList(nameDomainName); 215 if(list!=null && list._fullName!=null) 216 { 217 return list._fullName; 218 } 219 return nameDomainName; 220 } 221 222 private String getDescriptorsFilePath() throws Exception { 224 URL propertiesFile = NameListMgr.class.getClassLoader().getResource( 225 "com/sun/enterprise/config/serverbeans/validation/config/name-domains.xml"); 226 return propertiesFile.toString(); 229 } 230 231 public boolean loadDescriptors() { 233 boolean allIsWell = true; 234 235 try { 236 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 237 InputSource is = new InputSource (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 name = e.getAttribute("name"); 244 String fullName = e.getAttribute("full-name"); 245 String scope = e.getAttribute("scope"); 246 NodeList nl = e.getElementsByTagName("forms-from"); 248 ArrayList xpath_arr = new ArrayList (); 249 for(int j=0; j<nl.getLength(); j++) 250 { 251 String xpath = ((Element)nl.item(j)).getAttribute("xpath"); 252 if(xpath!=null && xpath.length()>0) 253 xpath_arr.add(xpath); 254 } 255 nl = e.getElementsByTagName("referenced-by"); 257 ArrayList ref_xpath_arr = new ArrayList (); 258 for(int j=0; j<nl.getLength(); j++) 259 { 260 String xpath = ((Element)nl.item(j)).getAttribute("xpath"); 261 if(xpath!=null && xpath.length()>0) 262 ref_xpath_arr.add(xpath); 263 } 264 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 e) { 278 _logger.log(Level.WARNING, "sax_error", e); 279 allIsWell = false; 280 } catch (IOException e) { 281 _logger.log(Level.WARNING, "error_loading_xmlfile", e); 282 allIsWell = false; 283 } catch(Exception e) { 284 _logger.log(Level.WARNING, "error", e); 285 allIsWell = false; 286 } 287 return allIsWell; 288 } 289 290 291 } 292 | Popular Tags |