KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > web > WebVerifier


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.web;
24
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.Random JavaDoc;
28
29 import com.sun.enterprise.deployment.Descriptor;
30 import com.sun.enterprise.deployment.WebBundleDescriptor;
31 import com.sun.enterprise.deployment.util.ModuleDescriptor;
32 import com.sun.enterprise.tools.verifier.BaseVerifier;
33 import com.sun.enterprise.tools.verifier.FrameworkContext;
34 import com.sun.enterprise.tools.verifier.SpecVersionMapper;
35 import com.sun.enterprise.tools.verifier.apiscan.classfile.ClassFileLoaderFactory;
36 import com.sun.enterprise.tools.verifier.apiscan.packaging.ClassPathBuilder;
37 import com.sun.enterprise.tools.verifier.apiscan.stdapis.WebClosureCompiler;
38 import com.sun.enterprise.util.FileUtil;
39 import com.sun.enterprise.util.io.FileUtils;
40 import com.sun.enterprise.loader.EJBClassLoader;
41
42 /**
43  * Responsible for verfying the j2ee war archive.
44  *
45  * @author Vikas Awasthi
46  */

47 public class WebVerifier extends BaseVerifier {
48
49     private WebBundleDescriptor webd = null;
50     private String JavaDoc classPath;//this is lazily populated in getClassPath()
51
private boolean isASMode = false;
52     private File JavaDoc jspOutDir = null;
53
54     public WebVerifier(FrameworkContext frameworkContext,
55                        WebBundleDescriptor webd) {
56         this.frameworkContext = frameworkContext;
57         this.webd = webd;
58         this.isASMode = !frameworkContext.isPortabilityMode();
59     }
60
61     /**
62      * Responsible for running web based verifier tests on the the web archive.
63      * Called from runVerifier in {@link BaseVerifier} class.
64      *
65      * @throws Exception
66      */

67     public void verify() throws Exception JavaDoc {
68         if (areTestsNotRequired(frameworkContext.isWeb()) &&
69                 areTestsNotRequired(frameworkContext.isWebServices()) &&
70                 areTestsNotRequired(frameworkContext.isWebServicesClient()) &&
71                 areTestsNotRequired(frameworkContext.isPersistenceUnits()))
72             return;
73
74         jspOutDir = getJspOutDir();
75         try {
76             preVerification();
77             context.setOutDir(jspOutDir);
78             createClosureCompiler();
79             verify(webd, new WebCheckMgrImpl(frameworkContext));
80         } finally {
81             // frameworkContext.getJspOutDir() will be non-null only when the
82
// call is from deployment backend and precompilejsp is set
83
if(frameworkContext.getJspOutDir()==null)
84                 FileUtil.deleteDir(jspOutDir);
85         }
86     }
87
88     /**
89      *
90      * @return web bundle descriptor
91      */

92     public Descriptor getDescriptor() {
93         return webd;
94     }
95
96     /**
97      * Creates the ClassLoader for the war archive.
98      *
99      * @return ClassLoader
100      * @throws IOException
101      */

102     protected ClassLoader JavaDoc createClassLoader()
103             throws IOException JavaDoc {
104         EJBClassLoader ejbClassLoader = new EJBClassLoader(webd.getClassLoader());
105         ejbClassLoader.appendURL(jspOutDir);
106         return ejbClassLoader;
107     }
108
109     /**
110      *
111      * @return name of the war archive
112      */

113     protected String JavaDoc getArchiveUri() {
114         return FileUtils.makeFriendlyFileName(webd.getModuleDescriptor().getArchiveUri());
115     }
116
117     /**
118      * @return the array of deployment descriptor names
119      */

120     protected String JavaDoc[] getDDString() {
121         String JavaDoc dd[] = {"WEB-INF/sun-web.xml", "WEB-INF/web.xml", // NOI18N
122
"WEB-INF/webservices.xml"}; // NOI18N
123
return dd;
124     }
125
126     /**
127      * Creates and returns the class path associated with the web archive.
128      * Uses the exploded location of the archive for generating the classpath.
129      *
130      * @return entire classpath string
131      * @throws IOException
132      */

133     protected String JavaDoc getClassPath() throws IOException JavaDoc {
134         if (classPath != null) return classPath;
135
136         if(isASMode)
137             return (classPath = getClassPath(frameworkContext.getClassPath()) +
138                                 File.pathSeparator +
139                                 jspOutDir.getAbsolutePath());
140
141         String JavaDoc cp;
142         if (!webd.getModuleDescriptor().isStandalone()) {
143             //take the cp from the enclosing ear file
144
String JavaDoc ear_uri = frameworkContext.getExplodedArchivePath();
145             File JavaDoc ear = new File JavaDoc(ear_uri);
146             assert(ear.isDirectory());
147             String JavaDoc earCP = ClassPathBuilder.buildClassPathForEar(ear);
148             String JavaDoc libdir = webd.getApplication().getLibraryDirectory();
149             if (libdir!=null) {
150                 earCP = getLibdirClasspath(ear_uri, libdir) + earCP;
151             }
152             String JavaDoc module_uri = webd.getModuleDescriptor().getArchiveUri();//this is a relative path
153
File JavaDoc module = new File JavaDoc(module_uri);
154             assert(module.isFile() && !module.isAbsolute());
155             // exploder creates the directory replacing all dots by '_'
156
File JavaDoc explodedModuleDir = new File JavaDoc(ear_uri,
157                     FileUtils.makeFriendlyFileName(module_uri));
158             String JavaDoc moduleCP = ClassPathBuilder.buildClassPathForWar(
159                     explodedModuleDir);
160             cp = moduleCP + File.pathSeparator + earCP;
161         } else {
162             String JavaDoc module_uri = frameworkContext.getExplodedArchivePath();//this is an absolute path
163
File JavaDoc module = new File JavaDoc(module_uri);
164             assert(module.isDirectory() && module.isAbsolute());
165             cp = ClassPathBuilder.buildClassPathForWar(module);
166         }
167         String JavaDoc as_lib_root=System.getProperty("com.sun.aas.installRoot")+File.separator+"lib"+File.separator;
168         if (frameworkContext.getJavaEEVersion().compareTo("5") >= 0) { // NOI18N
169
cp += File.pathSeparator+as_lib_root+"jsf-impl.jar"+File.pathSeparator+ // NOI18N
170
as_lib_root+"appserv-jstl.jar"+File.pathSeparator; // NOI18N
171
}
172         cp = cp + File.pathSeparator + jspOutDir.getAbsolutePath();
173         return (classPath = cp);
174     }
175
176     /**
177      * creates the ClosureCompiler for the web module and sets it to the
178      * verifier context. This is used to compute the closure on the classes used
179      * in the web archive.
180      *
181      * @throws IOException
182      */

183     protected void createClosureCompiler() throws IOException JavaDoc {
184         String JavaDoc specVer = SpecVersionMapper.getWebAppVersion(
185                 frameworkContext.getJavaEEVersion());
186         Object JavaDoc arg = (isASMode)?context.getClassLoader():(Object JavaDoc)getClassPath();
187         WebClosureCompiler cc = new WebClosureCompiler(specVer,
188                 ClassFileLoaderFactory.newInstance(new Object JavaDoc[]{arg}));
189         context.setClosureCompiler(cc);
190     }
191
192     /**
193      * If precompilejsp is set in the backend then return the jspOutput
194      * directory set in the frameworkContext. Otherwise create a new unique
195      * directory and return it.
196      * @return the output directory where compiled JSPs will be put.
197      */

198     private File JavaDoc getJspOutDir(){
199         // frameworkContext.getJspOutDir() will be non-null only when the
200
// call is from deployment backend and precompilejsp is set
201
File JavaDoc jspOutDir = frameworkContext.getJspOutDir();
202         if(jspOutDir != null) {
203             ModuleDescriptor moduleDescriptor = webd.getModuleDescriptor();
204             if(moduleDescriptor.isStandalone())
205                 return jspOutDir;
206             return new File JavaDoc(jspOutDir, FileUtils.makeFriendlyFilename(moduleDescriptor.getArchiveUri()));
207         }
208         Random JavaDoc random=new Random JavaDoc();
209         String JavaDoc prefix=System.getProperty("java.io.tmpdir")+File.separator+".jspc";
210         do{
211             float f=random.nextFloat();
212             String JavaDoc outDirPath=new String JavaDoc(prefix+f);
213             File JavaDoc out=new File JavaDoc(outDirPath);
214             if(out.mkdirs())
215                 return out;
216         }while(true);
217     }
218 }
219
Popular Tags