KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
26 import java.io.File JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.LogRecord JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30 import java.util.List JavaDoc;
31
32 import com.sun.enterprise.deployment.Application;
33 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive;
34 import com.sun.enterprise.deployment.Descriptor;
35 import com.sun.enterprise.logging.LogDomains;
36 import com.sun.enterprise.tools.verifier.gui.MainFrame;
37 import com.sun.enterprise.util.LocalStringManagerImpl;
38
39 /**
40  * This class is the main class to invoke the verification process. It
41  * is directly called by the scripts in AVK and verifier in appserver.
42  * The deployment backend invokes verifier in a separate process.
43  * Deploytool GUI invokes verifier by calling the verify() and
44  * generateReports() APIs.
45  */

46 public class Verifier {
47
48     private static boolean debug = false;
49     private static Logger JavaDoc logger = LogDomains.getLogger(
50             LogDomains.AVK_VERIFIER_LOGGER);
51     /**
52      * contains arguments data. It is used throughout the verification framework
53      */

54     private FrameworkContext frameworkContext = null;
55
56
57     /**
58      * Constructor that does the initialization. It parses and validates
59      * the arguments and creates the frameworkContext that is used
60      * throughout the verification framework.
61      *
62      * @param args
63      */

64     public Verifier(String JavaDoc[] args) {
65         StringManagerHelper.setLocalStringsManager(this.getClass());
66         frameworkContext = new Initializer(args).getFrameworkContext();
67     }
68
69     /**
70      * This constructor is called by the deployment backend. The invocation
71      * of this method is in the server's process.
72      */

73     public Verifier() {
74         StringManagerHelper.setLocalStringsManager(this.getClass());
75         frameworkContext = new FrameworkContext();
76         frameworkContext.setUseTimeStamp(true);
77         frameworkContext.setOutputDirName(System.getProperty("com.sun.aas.instanceRoot") + // NOI18N
78
File.separator +
79                                                             "logs" + // NOI18N
80
File.separator +
81                                                             "verifier-results"); // NOI18N
82
}
83
84     /**
85      * Main verifier method
86      *
87      * @param args Arguments to pass to verifier
88      * returns 0 if successfully verified with ZERO failures & ZERO errors.
89      * returns failure_count+error_count otherwise.
90      */

91     public static void main(String JavaDoc[] args) throws IOException JavaDoc {
92         Verifier verifier = new Verifier(args);
93         if (verifier.frameworkContext.isUsingGui()) {
94             MainFrame mf = new MainFrame(
95                     verifier.frameworkContext.getJarFileName(), true, verifier);
96             mf.setSize(800, 600);
97             mf.setVisible(true);
98         } else {
99             LocalStringManagerImpl smh = StringManagerHelper.getLocalStringsManager();
100             try {
101                 verifier.verify();
102             } catch (Exception JavaDoc e) {
103                 LogRecord JavaDoc logRecord = new LogRecord JavaDoc(Level.SEVERE,
104                         smh.getLocalString(
105                                 verifier.getClass().getName() +
106                                 ".verifyFailed", // NOI18N
107
"Could not verify successfully.")); // NOI18N
108
logRecord.setThrown(e);
109                 verifier.frameworkContext.getResultManager().log(logRecord);
110             }
111             verifier.generateReports();
112             int failedCount = verifier.frameworkContext.getResultManager()
113                     .getFailedCount() +
114                     verifier.frameworkContext.getResultManager().getErrorCount();
115             if (failedCount != 0)
116                 System.exit(failedCount);
117         }
118     }
119
120     /**
121      * This method does the verification by running all the verifier tests
122      *
123      * @return ResultManager that contains all the test results.
124      * @throws IOException
125      */

126     private ResultManager verify() throws IOException JavaDoc {
127         VerificationHandler verificationHandler =
128                         new VerificationHandler(frameworkContext);
129         ResultManager resultManager;
130         try {
131             resultManager = verificationHandler.verifyArchive();
132         } finally {
133             verificationHandler.cleanup();
134         }
135         return resultManager;
136     }
137
138     /**
139      * @param jarFile This method is called from gui MainPanel to run verifier
140      * on selected archive
141      * @return ResultManager Object containing all test results
142      * @throws IOException
143      */

144     public ResultManager verify(String JavaDoc jarFile) throws IOException JavaDoc {
145         frameworkContext.setJarFileName(jarFile);
146         return verify();
147     }
148
149     /**
150      * Call from deployment backend. This call is in the appserver process.
151      * Verifier will run in appserver mode for this invocation.
152      * If parameter application is null then this api is equivalent to
153      * invoking a standalone verifier.
154      * Parameter abstractArchive must not be null.
155      *
156      * @return status of the invocation. A non zero value will denote a failure.
157      * @throws IOException
158      */

159     public int verify(Application application,
160                       AbstractArchive abstractArchive,
161                       List JavaDoc<String JavaDoc> classPath,
162                       File JavaDoc jspOutDir)
163             throws IOException JavaDoc {
164         boolean originalBoundsChecking = Descriptor.isBoundsChecking();
165         Descriptor.setBoundsChecking(false);
166         ResultManager rmanager=null;
167         frameworkContext.setJspOutDir(jspOutDir);
168         frameworkContext.setIsBackend(true);
169         VerificationHandler verificationHandler = null;
170         try {
171             if(application == null) { //can be a standalone connector deployment
172
frameworkContext.setJarFileName(abstractArchive.getArchiveUri());
173                 verificationHandler = new VerificationHandler(frameworkContext);
174             } else
175                 verificationHandler = new VerificationHandler(frameworkContext,
176                                                               application,
177                                                               abstractArchive,
178                                                               classPath);
179             rmanager = verificationHandler.verifyArchive();
180         } catch(Exception JavaDoc e) {
181             LocalStringManagerImpl smh = StringManagerHelper.getLocalStringsManager();
182             LogRecord JavaDoc logRecord =
183                     new LogRecord JavaDoc(Level.SEVERE,
184                                  smh.getLocalString(getClass().getName() +
185                                                    ".verifyFailed", // NOI18N
186
"Could not verify successfully.")); // NOI18N
187
logRecord.setThrown(e);
188             frameworkContext.getResultManager().log(logRecord);
189         } finally { // restore the original values
190
Descriptor.setBoundsChecking(originalBoundsChecking);
191             if(verificationHandler!=null)
192                 verificationHandler.cleanup();
193         }
194         generateReports();
195         return rmanager.getErrorCount() + rmanager.getFailedCount();
196     }
197
198     /**
199      * It generates the reports using the ResultManager
200      *
201      * @throws IOException
202      */

203     public void generateReports() throws IOException JavaDoc {
204         new ReportHandler(frameworkContext).generateAllReports();
205     }
206
207     /**
208      * checks if verifier is running in debug mode
209      * @return debug status
210      */

211     public static boolean isDebug() {
212         return debug;
213     }
214
215     /**
216      * debug messages are logged here.
217      * @param t
218      */

219     public static void debug(Throwable JavaDoc t) {
220         logger.log(Level.FINEST, "Exception occurred", t);
221     }
222 }
223
Popular Tags