KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > verifier > ServerXMLVerifier


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
24 /*
25  * ServerXmlVerifier.java
26  *
27 */

28
29 package com.sun.enterprise.admin.verifier;
30
31 import java.util.*;
32 import java.io.*;
33
34 import java.io.File JavaDoc;
35
36 import com.sun.enterprise.util.LocalStringManager;
37 import com.sun.enterprise.util.LocalStringManagerImpl;
38 import com.sun.enterprise.tools.verifier.StringManagerHelper;
39 //import com.sun.enterprise.tools.verifier.Result;
40
//import com.sun.enterprise.tools.verifier.ResultMgr;
41
//import com.sun.enterprise.tools.verifier.ResultsReport;
42
import com.sun.enterprise.config.ConfigBeansFactory;
43 import com.sun.enterprise.config.ConfigFactory;
44 import com.sun.enterprise.config.ConfigContext;
45 import com.sun.enterprise.config.ConfigException;
46
47 // Logging
48
import java.util.logging.Logger JavaDoc;
49 import java.util.logging.Level JavaDoc;
50 import com.sun.logging.LogDomains;
51
52
53 /* ServerXML Verifier is the main program which is called from command line to verify all attributes in server.xml
54 */

55
56 public class ServerXMLVerifier {
57     
58     // Logging
59
static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.APPVERIFY_LOGGER);
60     
61     public boolean debug = false;
62     public String JavaDoc outputFileStr = null;
63     public final static int FAIL = 0;
64     public final static int WARN = 1;
65     public final static int ALL = 2;
66     
67     private static int reportLevel = ALL;
68     
69     //public ResultMgr resultMgr = null;
70
//private ResultsReport resultReport = null;
71

72     public String JavaDoc xmlFileName;
73     public ConfigContext configContext = null;
74     
75     public com.sun.enterprise.util.LocalStringManagerImpl smh;
76     public static ServerMgr serverMgr;
77     
78     public ServerXMLVerifier() {
79         try {
80             //resultMgr = new ResultMgr();
81
//resultReport = new ResultsReport();
82
StringManagerHelper.setLocalStringsManager(Class.forName(getClass().getName()));
83             smh = StringManagerHelper.getLocalStringsManager();
84         }
85         catch (ClassNotFoundException JavaDoc e) {
86             // Logging
87
_logger.log(Level.FINE, "serverxmlverifier.error_getting_localstringsmanager", e);
88         }
89         init();
90     }
91     
92     /*public boolean verify(String name, Object value, ConfigContext context) {
93         boolean retValue = false;
94         configContext = context;
95         //System.out.println("Name : " + name);
96         //System.out.println("Value : " + value.getClass().getName());
97         if(serverMgr==null)
98             serverMgr = new ServerMgr(true);
99         retValue = serverMgr.check(name,value,configContext);
100         return retValue;
101     }*/

102     
103     /*public boolean verify() {
104         boolean retValue = false;
105         try {
106             configContext = ConfigFactory.createConfigContext(xmlFileName);
107             configContext.refresh();
108             if(serverMgr==null)
109                 serverMgr = new ServerMgr(true);
110             serverMgr.check(configContext);
111             retValue = true;
112         }
113         catch(ConfigException cex){
114             // Logging
115             _logger.log(Level.FINE, "serverxmlverifier.error_creating_object", cex);
116             retValue = false;
117         }
118         return retValue;
119     }
120     
121     // <addition> srini@sun.com
122     
123     // Function added to check whether all tests has been passed
124     // return true when all tests passed
125     // return false when one test failed
126     // throws ConfigException if give xml file is not exist or invalid
127     
128     public boolean isPassed(String xmlFileName) throws ConfigException {
129         boolean retValue = false;
130         configContext = ConfigFactory.createConfigContext(xmlFileName);
131         configContext.refresh();
132         if(serverMgr == null)
133                 serverMgr = new ServerMgr(debug);
134         retValue = serverMgr.testStatus(configContext);
135         return retValue;
136     }*/

137     
138     // </addition>
139

140     
141     public void init() {
142         File JavaDoc testNames = new File JavaDoc("lib/TestNamesMBean.xml");
143         if(testNames.exists())
144             // Logging
145
_logger.log(Level.INFO, "serverxmlverifier.looking_file", testNames.getAbsolutePath());
146     }
147     
148     /*
149      * Standalone verification of Server.xml
150      */

151     
152     public static void main(String JavaDoc args[]) {
153         ServerXMLVerifier serverVerifier = new ServerXMLVerifier();
154         serverVerifier.parseArgs(args);
155     }
156     
157     protected void parseArgs(String JavaDoc args[]) {
158         if (args.length < 1)
159             usage();
160         else {
161             //default report level is set to failures and warnings
162
for (int i = 0; i < args.length; i++) {
163                 String JavaDoc arg = args[i];
164                 if (arg.startsWith("-")) {
165                     try {
166                         switch (arg.charAt(1)) {
167                             case 'v': //verbose
168
debug = true;
169                                 debug(smh.getLocalString(getClass().getName() + ".verboseFlag",
170                                 "Setting verbose flag to TRUE."));
171                                 break;
172                             case 'h': // Help Message
173
debug
174                                 (smh.getLocalString(getClass().getName() + ".helpMessage",
175                                 "Displaying help message."));
176                                 usage();
177                                 break;
178                             case 'o': // output file name
179
debug
180                                 (smh.getLocalString(getClass().getName() + ".outputFileName",
181                                 "Retrieving results output filename."));
182                                 char vhstr[] = new char[arg.length()-2];
183                                 arg.getChars(2, arg.length(), vhstr, 0);
184                                 outputFileStr = new String JavaDoc(vhstr);
185                                 debug("Using this output file = " + outputFileStr);
186                                 ServerMgr.setFile(outputFileStr);
187                                 //resultReport.setUserSpecifiedOutputFile(true);
188
break;
189                             default:
190                                 usage();
191                         }
192                     } catch (StringIndexOutOfBoundsException JavaDoc e) {
193                         debug(e);
194                         usage();
195                     }
196                 }
197                 else
198                 {
199                     if (xmlFileName == null){
200                         xmlFileName = (new File JavaDoc(arg)).getAbsolutePath();
201                         debug(smh.getLocalString(getClass().getName() + ".xmlFileName",
202                         "XML filename: {0}",
203                         new Object JavaDoc[]
204                         {xmlFileName}));
205                     } else {
206                         debug(smh.getLocalString(getClass().getName() + "invalidArg",
207                         "invalid argument \"{0}\"",
208                         new Object JavaDoc[]
209                         {arg}));
210                         usage();
211                         return;
212                     }
213                 }
214             }
215             if(xmlFileName == null ){
216                 usage();
217                 return;
218             }
219             //verify();
220
}
221     }
222     
223     /** Display usage message to user upon encountering invalid option
224      *
225      */

226     public void usage() {
227         System.err.println
228         ("\n" +
229         (smh.getLocalString(getClass().getName() + ".usageLine1",
230         "usage: server-verifier [optional_params] <server-xml-filename>"))
231         + "\n\n" +
232         (smh.getLocalString(getClass().getName() + ".usageLine2",
233         "where :"))
234         + "\n\n" +
235         (smh.getLocalString(getClass().getName() + ".usageLine3",
236         " [optional_params]: Must be: "))
237         + "\n\n" +
238         (smh.getLocalString(getClass().getName() + ".usageLine4",
239         " -v : verbose debug turned on "))
240         + "\n\n" +
241         (smh.getLocalString(getClass().getName() + ".usageLine5",
242         " -o<output file> : test results written to this file (.xml file preferred)"))
243         + "\n" +
244         (smh.getLocalString(getClass().getName() + ".usageLine6",
245         " (Overrides default file - Results.xml)"))
246         + "\n" +
247         (smh.getLocalString(getClass().getName() + ".usageLine6a",
248         " which is created in system defined <tmp> directory"))
249         + "\n" +
250         (smh.getLocalString(getClass().getName() + ".usageLine17",
251         "<server-xml-filename>: Jar file to perform static verification on "))
252         + "\n\n" );
253     }
254     
255     
256     public void debug(String JavaDoc s) {
257         if(debug)
258             // Logging
259
_logger.log(Level.INFO, s);
260     }
261     
262     public void debug(Exception JavaDoc e) {
263         if(debug)
264             // Logging
265
_logger.log(Level.INFO, "serverxmlverifier.error_check", e);
266     }
267 }
268
Popular Tags