KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > BaseVerifier


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 package com.sun.enterprise.tools.verifier;
24
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.ArrayList JavaDoc;
32
33 import javax.xml.parsers.DocumentBuilder JavaDoc;
34 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
35 import javax.xml.parsers.ParserConfigurationException JavaDoc;
36
37 import org.w3c.dom.Document JavaDoc;
38 import org.xml.sax.EntityResolver JavaDoc;
39 import org.xml.sax.InputSource JavaDoc;
40 import org.xml.sax.SAXException JavaDoc;
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 /**
52  * The base class of all the verifiers. It has all the common
53  * logic required in the verification process.
54  *
55  * @author Vikas Awasthi
56  */

57 public abstract class BaseVerifier {
58
59     protected Logger JavaDoc 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 JavaDoc;
64
65     public abstract Descriptor getDescriptor();
66
67     protected abstract ClassLoader JavaDoc createClassLoader()
68             throws IOException JavaDoc;
69
70     protected abstract String JavaDoc getArchiveUri();
71
72     protected abstract String JavaDoc getClassPath() throws IOException JavaDoc;
73
74     protected void createDocumentObject(Descriptor descriptor)
75             throws IOException JavaDoc, ParserConfigurationException JavaDoc, SAXException JavaDoc {
76         InputStream JavaDoc is = null;
77         InputSource JavaDoc source = null;
78
79         String JavaDoc archBase = context.getAbstractArchive().getArchiveUri();
80         String JavaDoc 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 JavaDoc 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 JavaDoc(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 JavaDoc 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 JavaDoc {
118         logger.log(Level.INFO, "Verifying: [ " + getArchiveUri() + " ]");
119         ClassLoader JavaDoc 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 JavaDoc {
129         //hs for creating DOM object for runtime tests
130
createDocumentObject(descriptor);
131         // now start calling each individual test, as per spec
132
checkMgrImpl.setVerifierContext(context);
133         checkMgrImpl.check(descriptor);
134         logger.log(Level.FINE,
135                 getClass().getName() + ".debug.endStaticVerification"); // NOI18N
136
}
137
138     protected void createDOMObject(InputSource JavaDoc source, String JavaDoc dd)
139             throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
140         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
141         factory.setNamespaceAware(true);
142         DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
143         EntityResolver JavaDoc dh = new XMLValidationHandler(false);
144         builder.setEntityResolver(dh);
145         Document JavaDoc document = builder.parse(source);
146
147         if ((dd.indexOf("sun-")) < 0) { // NOI18N
148
if ((dd.indexOf("webservices")) < 0) { // NOI18N
149
context.setDocument(document);
150             } else {
151                 context.setWebServiceDocument(document);
152             }
153         } else
154             context.setRuntimeDocument(document);
155     }
156
157     protected abstract String JavaDoc[] getDDString();
158
159     protected InputStream JavaDoc getInputStreamFromAbstractArchive(String JavaDoc uri,
160                                                             String JavaDoc ddName)
161             throws IOException JavaDoc {
162         FileArchive arch = new FileArchive();
163         arch.open(uri);
164         InputStream JavaDoc deploymentEntry = arch.getEntry(ddName);
165         return deploymentEntry;
166     }
167
168     /**
169      *
170      * @param appRoot The location of the exploded archive
171      * @param libdirPath library-directory location as specified in aplication.xml
172      * @return Classpath string containing list of jar files in the library directory
173      * separated by File.separator char
174      * @throws IOException
175      */

176     protected String JavaDoc getLibdirClasspath(String JavaDoc appRoot, String JavaDoc libdirPath) throws IOException JavaDoc{
177         StringBuilder JavaDoc classpath=new StringBuilder JavaDoc();
178         libdirPath = libdirPath.replace('/', File.separatorChar);
179         List JavaDoc<String JavaDoc> jars = getAllJars(new File JavaDoc(appRoot, libdirPath));
180         for (String JavaDoc s : jars) {
181             classpath.append(s);
182             classpath.append(File.pathSeparator);
183         }
184         return classpath.toString();
185     }
186
187     /**
188      *
189      * @param file
190      * @return returns a list of jars in the a directory
191      * @throws IOException
192      */

193     private List JavaDoc<String JavaDoc> getAllJars(File JavaDoc file) throws IOException JavaDoc{
194         List JavaDoc<String JavaDoc> list = new ArrayList JavaDoc<String JavaDoc>();
195         if (file.isDirectory() || file.canRead()) {
196             File JavaDoc[] files = file.listFiles();
197             for (int i=0; i<files.length; i++) {
198                 File JavaDoc jar = files[i];
199                 if ( FileUtils.isJar(jar)) {
200                     list.add( jar.getCanonicalPath() );
201                 }
202             }
203         }
204         return list;
205     }
206
207     /** converts list of paths to a string of paths separated by pathSeparator*/
208     protected String JavaDoc getClassPath(List JavaDoc<String JavaDoc> classPath) {
209         StringBuilder JavaDoc cp = new StringBuilder JavaDoc("");
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