1 23 24 28 29 package com.sun.enterprise.admin.verifier; 30 31 32 import java.util.Vector ; 33 import java.io.*; 34 import javax.xml.parsers.*; 35 import org.w3c.dom.*; 36 import java.util.*; 37 39 import com.sun.enterprise.tools.verifier.TestInformation; 40 import com.sun.enterprise.admin.verifier.Result; 42 import com.sun.enterprise.tools.verifier.StringManagerHelper; 43 44 import java.util.StringTokenizer ; 45 import com.sun.enterprise.config.ConfigContext; 46 47 import com.sun.enterprise.config.ConfigContextEvent; 48 import com.sun.enterprise.config.ConfigContextEventListener; 49 import com.sun.enterprise.admin.common.exception.AFRuntimeException; 50 51 import java.util.logging.Logger ; 53 import java.util.logging.Level ; 54 import com.sun.logging.LogDomains; 55 56 60 61 public class ServerMgr implements ConfigContextEventListener { 62 63 static Logger _logger = LogDomains.getLogger(LogDomains.APPVERIFY_LOGGER); 65 66 public String testFileName = "ServerTestList.xml"; 67 public static HashMap testCases = new HashMap(); 68 public static String fileUrl = null; 69 public String description = "Tests for server.xml"; 70 public boolean debug; 71 72 public com.sun.enterprise.admin.verifier.Result result=null; 73 public Vector vresult=null; 74 75 public com.sun.enterprise.util.LocalStringManagerImpl smh = StringManagerHelper.getLocalStringsManager(); 76 77 78 public ServerMgr() { 79 debug = false; 80 } 81 82 public ServerMgr(boolean verbose){ 83 debug = true; 84 } 85 86 public static void setFile(String file){ 87 fileUrl = file; 88 } 89 90 public boolean loadTestInfo() { 91 boolean allIsWell = true; 92 93 if(testCases.isEmpty()) { 94 if (debug) { 95 _logger.log(Level.INFO, "serverxmlverifier.getting_testnamefrom_propertyfile"); 97 } 98 File inputFile = getTestFile(testFileName); 99 try { 100 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 102 Document doc = db.parse(inputFile); 103 NodeList list = doc.getElementsByTagName("description"); 104 if (list.getLength()>0) { 105 Element e = (Element) list.item(0); 106 description = e.getFirstChild().getNodeValue().trim(); 107 } 108 109 list = doc.getElementsByTagName("test"); 110 for (int i=0;i<list.getLength();i++) { 111 Element e = (Element) list.item(i); 112 NodeList nl = e.getChildNodes(); 113 TestInformation ti = new TestInformation(); 114 String testName = ""; 115 for (int j=0;j<nl.getLength();j++) { 116 String nodeName = nl.item(j).getNodeName(); 117 if("test-name".equals(nodeName.trim())) { 118 Node el = (Node)nl.item(j); 119 testName = el.getFirstChild().getNodeValue().trim(); 120 } 121 if ("test-class".equals(nodeName.trim())) { 122 Node el = (Node) nl.item(j); 123 ti.setClassName(el.getFirstChild().getNodeValue().trim()); 124 } 125 if ("minimum-version".equals(nodeName.trim())) { 126 Node el = (Node) nl.item(j); 127 ti.setMinimumVersion(el.getFirstChild().getNodeValue().trim()); 128 } 129 if ("maximum-version".equals(nodeName.trim())) { 130 Node el = (Node) nl.item(j); 131 ti.setMaximumVersion(el.getFirstChild().getNodeValue().trim()); 132 } 133 } 134 testCases.put(testName,ti); 135 } 136 } catch (ParserConfigurationException e) { 137 _logger.log(Level.WARNING, "serverxmlverifier.parser_error", e); 139 allIsWell = false; 140 } catch (org.xml.sax.SAXException e) { 141 _logger.log(Level.WARNING, "serverxmlverifier.sax_error", e); 143 allIsWell = false; 144 } catch (IOException e) { 145 _logger.log(Level.WARNING, "serverxmlverifier.error_loading_xmlfile"); 147 allIsWell = false; 148 } 149 } 150 151 return allIsWell; 152 } 153 154 public HashMap getTests() { 155 return testCases; 156 } 157 158 private File getTestFile(String name) { 159 160 String iasHome = System.getProperty("s1as.home"); 161 if(iasHome!=null) { 162 File temp = new File(iasHome,"lib"); 163 temp = new File(temp,name); 164 if(temp.exists()) 165 return temp; 166 else 167 return null; 168 } 169 else 170 return getFileFromCP(name); 171 } 172 173 private File getFileFromCP(String name) { 174 File cand = null; 175 String classPath = System.getProperty("java.class.path"); 176 String classPathSep = File.pathSeparator; 177 StringTokenizer tokens = new StringTokenizer (classPath,classPathSep); 178 while(tokens.hasMoreTokens()) { 179 String fileName = tokens.nextToken(); 180 if(fileName.endsWith("appserv-rt.jar")) { 181 int slashPos = fileName.lastIndexOf('/'); 184 if (slashPos == -1) { 185 slashPos = fileName.lastIndexOf(File.separator); 186 } 187 if (slashPos != -1) { 188 String libPath = fileName.substring(0, slashPos); 189 cand = new File(libPath,name); 190 } 191 break; 192 } 193 } 194 return cand; 195 } 196 197 312 313 public boolean check(ConfigContextEvent ccce) { 315 String name = ccce.getName(); 316 Object value = ccce.getObject(); 317 ConfigContext context = ccce.getConfigContext(); 318 String choice = ccce.getChoice(); 319 String beanName = ccce.getBeanName(); 320 321 if(name == null && beanName == null) 322 return true; 323 boolean retValue = false; 324 if(testCases.isEmpty()) 325 loadTestInfo(); 326 TestInformation ti = (TestInformation)testCases.get(name); 327 if(ti == null && beanName != null) 328 ti =(TestInformation)testCases.get(beanName); 329 String testClass; 330 try { 331 testClass = ti.getClassName(); 332 } 333 catch(Exception e){ 334 return true; 335 } 336 337 try { 338 Class test = Class.forName(testClass); 339 ServerCheck tester = (ServerCheck)test.newInstance(); 340 result = tester.check(ccce); 341 if (result.getStatus() == Result.PASSED) 342 retValue = true; 343 else 344 retValue = false; 345 } 346 catch(Throwable tt) { 347 _logger.log(Level.FINE, "serverxmlverifier.error_check", tt); 349 retValue = true; 350 } 351 return retValue; 352 } 353 354 357 public void postAccessNotification(ConfigContextEvent ccce) { 358 } 359 360 363 public void postChangeNotification(ConfigContextEvent ccce) { 364 } 365 366 369 public void preAccessNotification(ConfigContextEvent ccce) { 370 } 371 372 375 public void preChangeNotification(ConfigContextEvent ccce) { 376 if(! check(ccce)) 377 throw new AFRuntimeException(result.getErrorDetails().toString()); 378 } 379 380 } 381 382 | Popular Tags |