1 23 package com.sun.enterprise.tools.verifier; 24 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 30 import java.util.List ; 31 import java.util.ArrayList ; 32 33 import javax.xml.parsers.DocumentBuilder ; 34 import javax.xml.parsers.DocumentBuilderFactory ; 35 import javax.xml.parsers.ParserConfigurationException ; 36 37 import org.w3c.dom.Document ; 38 import org.xml.sax.EntityResolver ; 39 import org.xml.sax.InputSource ; 40 import org.xml.sax.SAXException ; 41 42 import com.sun.enterprise.deployment.deploy.shared.FileArchive; 43 import com.sun.enterprise.deployment.Descriptor; 44 import com.sun.enterprise.deployment.Application; 45 import com.sun.enterprise.deployment.BundleDescriptor; 46 import com.sun.enterprise.logging.LogDomains; 47 import com.sun.enterprise.util.io.FileUtils; 48 49 import tools.com.sun.enterprise.util.XMLValidationHandler; 50 51 57 public abstract class BaseVerifier { 58 59 protected Logger logger = LogDomains.getLogger(LogDomains.AVK_VERIFIER_LOGGER); 60 protected FrameworkContext frameworkContext = null; 61 protected Context context = null; 62 63 public abstract void verify() throws Exception ; 64 65 public abstract Descriptor getDescriptor(); 66 67 protected abstract ClassLoader createClassLoader() 68 throws IOException ; 69 70 protected abstract String getArchiveUri(); 71 72 protected abstract String getClassPath() throws IOException ; 73 74 protected void createDocumentObject(Descriptor descriptor) 75 throws IOException , ParserConfigurationException , SAXException { 76 InputStream is = null; 77 InputSource source = null; 78 79 String archBase = context.getAbstractArchive().getArchiveUri(); 80 String uri = null; 81 if(descriptor instanceof Application) { 82 uri = archBase; 83 } else { 84 BundleDescriptor bundleDesc = 85 BundleDescriptor.class.cast(descriptor); 86 if(bundleDesc.getModuleDescriptor().isStandalone()) { 87 uri = archBase; 88 } else { 89 uri = archBase + File.separator + getArchiveUri(); 90 } 91 } 92 String dd[] = getDDString(); 93 for (int i = 0; i < dd.length; i++) { 94 try{ 95 is = getInputStreamFromAbstractArchive(uri, dd[i]); 96 if (is != null) { 97 source = new InputSource (is); 98 createDOMObject(source, dd[i]); 99 is.close(); 100 } 101 } finally { 102 try { 103 if(is != null) { 104 is.close(); 105 } 106 } catch(Exception e) {} 107 } 108 } 109 } 110 111 protected boolean areTestsNotRequired( 112 boolean isModuleGivenInPartiotioningArgs) { 113 return (frameworkContext.isPartition() && 114 !isModuleGivenInPartiotioningArgs); 115 } 116 117 protected void preVerification() throws Exception { 118 logger.log(Level.INFO, "Verifying: [ " + getArchiveUri() + " ]"); 119 ClassLoader loader = createClassLoader(); 120 context = new Context(loader); 121 context.setAppserverMode(!frameworkContext.isPortabilityMode()); 122 context.setAbstractArchive(frameworkContext.getAbstractArchive()); 123 context.setClassPath(getClassPath()); 124 logger.log(Level.FINE, "Using CLASSPATH: " + getClassPath()); 125 } 126 127 protected void verify(Descriptor descriptor, CheckMgr checkMgrImpl) 128 throws Exception { 129 createDocumentObject(descriptor); 131 checkMgrImpl.setVerifierContext(context); 133 checkMgrImpl.check(descriptor); 134 logger.log(Level.FINE, 135 getClass().getName() + ".debug.endStaticVerification"); } 137 138 protected void createDOMObject(InputSource source, String dd) 139 throws ParserConfigurationException , SAXException , IOException { 140 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 141 factory.setNamespaceAware(true); 142 DocumentBuilder builder = factory.newDocumentBuilder(); 143 EntityResolver dh = new XMLValidationHandler(false); 144 builder.setEntityResolver(dh); 145 Document document = builder.parse(source); 146 147 if ((dd.indexOf("sun-")) < 0) { if ((dd.indexOf("webservices")) < 0) { context.setDocument(document); 150 } else { 151 context.setWebServiceDocument(document); 152 } 153 } else 154 context.setRuntimeDocument(document); 155 } 156 157 protected abstract String [] getDDString(); 158 159 protected InputStream getInputStreamFromAbstractArchive(String uri, 160 String ddName) 161 throws IOException { 162 FileArchive arch = new FileArchive(); 163 arch.open(uri); 164 InputStream deploymentEntry = arch.getEntry(ddName); 165 return deploymentEntry; 166 } 167 168 176 protected String getLibdirClasspath(String appRoot, String libdirPath) throws IOException { 177 StringBuilder classpath=new StringBuilder (); 178 libdirPath = libdirPath.replace('/', File.separatorChar); 179 List <String > jars = getAllJars(new File (appRoot, libdirPath)); 180 for (String s : jars) { 181 classpath.append(s); 182 classpath.append(File.pathSeparator); 183 } 184 return classpath.toString(); 185 } 186 187 193 private List <String > getAllJars(File file) throws IOException { 194 List <String > list = new ArrayList <String >(); 195 if (file.isDirectory() || file.canRead()) { 196 File [] files = file.listFiles(); 197 for (int i=0; i<files.length; i++) { 198 File jar = files[i]; 199 if ( FileUtils.isJar(jar)) { 200 list.add( jar.getCanonicalPath() ); 201 } 202 } 203 } 204 return list; 205 } 206 207 208 protected String getClassPath(List <String > classPath) { 209 StringBuilder cp = new StringBuilder (""); 210 for (int i = 0; i < classPath.size(); i++) { 211 cp.append(classPath.get(i)); 212 cp.append(File.pathSeparator); 213 } 214 return cp.toString(); 215 } 216 } 217 | Popular Tags |