KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ServerCheck.java
26  *
27  */

28
29 package com.sun.enterprise.admin.verifier;
30
31
32 import java.util.Vector JavaDoc;
33 import java.io.*;
34 import javax.xml.parsers.*;
35 import org.w3c.dom.*;
36 import java.util.*;
37 //import com.sun.enterprise.tools.verifier.*;
38

39 import com.sun.enterprise.tools.verifier.TestInformation;
40 //import com.sun.enterprise.tools.verifier.Result;
41
import com.sun.enterprise.admin.verifier.Result;
42 import com.sun.enterprise.tools.verifier.StringManagerHelper;
43
44 import java.util.StringTokenizer JavaDoc;
45 import com.sun.enterprise.config.ConfigContext;
46
47 import com.sun.enterprise.config.ConfigContextEvent;
48 import com.sun.enterprise.config.ConfigContextEventListener;
49 import com.sun.enterprise.admin.common.exception.AFRuntimeException;
50
51 // Logging
52
import java.util.logging.Logger JavaDoc;
53 import java.util.logging.Level JavaDoc;
54 import com.sun.logging.LogDomains;
55
56 /**
57  * ServerMgr reads all TestCases and verifies it with server.xml file
58  *
59  */

60
61 public class ServerMgr implements ConfigContextEventListener {
62     
63     // Logging
64
static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.APPVERIFY_LOGGER);
65
66     public String JavaDoc testFileName = "ServerTestList.xml";
67     public static HashMap testCases = new HashMap();
68     public static String JavaDoc fileUrl = null;
69     public String JavaDoc description = "Tests for server.xml";
70     public boolean debug;
71     
72     public com.sun.enterprise.admin.verifier.Result result=null;
73     public Vector JavaDoc vresult=null;
74     
75     public com.sun.enterprise.util.LocalStringManagerImpl smh = StringManagerHelper.getLocalStringsManager();
76     
77     /** Creates a new instance of ServerCheck */
78     public ServerMgr() {
79         debug = false;
80     }
81     
82     public ServerMgr(boolean verbose){
83         debug = true;
84     }
85     
86     public static void setFile(String JavaDoc file){
87         fileUrl = file;
88     }
89     
90     public boolean loadTestInfo() {
91         boolean allIsWell = true;
92         
93         if(testCases.isEmpty()) {
94             if (debug) {
95                 // Logging
96
_logger.log(Level.INFO, "serverxmlverifier.getting_testnamefrom_propertyfile");
97             }
98             File inputFile = getTestFile(testFileName);
99             try {
100                 // parse the xml file
101
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
102                 Document doc = db.parse(inputFile);
103                 NodeList list = doc.getElementsByTagName("description");
104                 if (list.getLength()>0) {
105                     Element e = (Element) list.item(0);
106                     description = e.getFirstChild().getNodeValue().trim();
107                 }
108
109                 list = doc.getElementsByTagName("test");
110                 for (int i=0;i<list.getLength();i++) {
111                     Element e = (Element) list.item(i);
112                     NodeList nl = e.getChildNodes();
113                     TestInformation ti = new TestInformation();
114                     String JavaDoc testName = "";
115                     for (int j=0;j<nl.getLength();j++) {
116                         String JavaDoc nodeName = nl.item(j).getNodeName();
117                         if("test-name".equals(nodeName.trim())) {
118                             Node el = (Node)nl.item(j);
119                             testName = el.getFirstChild().getNodeValue().trim();
120                         }
121                         if ("test-class".equals(nodeName.trim())) {
122                             Node el = (Node) nl.item(j);
123                             ti.setClassName(el.getFirstChild().getNodeValue().trim());
124                         }
125                         if ("minimum-version".equals(nodeName.trim())) {
126                             Node el = (Node) nl.item(j);
127                             ti.setMinimumVersion(el.getFirstChild().getNodeValue().trim());
128                         }
129                         if ("maximum-version".equals(nodeName.trim())) {
130                             Node el = (Node) nl.item(j);
131                             ti.setMaximumVersion(el.getFirstChild().getNodeValue().trim());
132                         }
133                     }
134                     testCases.put(testName,ti);
135                 }
136             } catch (ParserConfigurationException e) {
137                 // Logging
138
_logger.log(Level.WARNING, "serverxmlverifier.parser_error", e);
139                 allIsWell = false;
140             } catch (org.xml.sax.SAXException JavaDoc e) {
141                 // Logging
142
_logger.log(Level.WARNING, "serverxmlverifier.sax_error", e);
143                 allIsWell = false;
144             } catch (IOException e) {
145                 // Logging
146
_logger.log(Level.WARNING, "serverxmlverifier.error_loading_xmlfile");
147                 allIsWell = false;
148             }
149         }
150         
151         return allIsWell;
152     }
153     
154     public HashMap getTests() {
155         return testCases;
156     }
157     
158     private File getTestFile(String JavaDoc name) {
159         
160         String JavaDoc iasHome = System.getProperty("s1as.home");
161         if(iasHome!=null) {
162             File temp = new File(iasHome,"lib");
163             temp = new File(temp,name);
164             if(temp.exists())
165                 return temp;
166             else
167                 return null;
168         }
169         else
170             return getFileFromCP(name);
171     }
172     
173     private File getFileFromCP(String JavaDoc name) {
174         File cand = null;
175         String JavaDoc classPath = System.getProperty("java.class.path");
176         String JavaDoc classPathSep = File.pathSeparator;
177         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(classPath,classPathSep);
178         while(tokens.hasMoreTokens()) {
179             String JavaDoc fileName = tokens.nextToken();
180             if(fileName.endsWith("appserv-rt.jar")) {
181                 // File Path separator in classpath set in server.xml is
182
// always forward-slash (/), so trying forward slash first
183
int slashPos = fileName.lastIndexOf('/');
184                 if (slashPos == -1) {
185                     slashPos = fileName.lastIndexOf(File.separator);
186                 }
187                 if (slashPos != -1) {
188                     String JavaDoc libPath = fileName.substring(0, slashPos);
189                     cand = new File(libPath,name);
190                 }
191                 break;
192             }
193         }
194         return cand;
195     }
196     
197     /*public boolean check(ConfigContext context) {
198         if(testCases.isEmpty())
199             loadTestInfo();
200         Iterator testClasses = testCases.values().iterator();
201         ResultMgr resultMgr = new ResultMgr();
202         while(testClasses.hasNext()) {
203             TestInformation ti = (TestInformation)testClasses.next();
204             String testClass = ti.getClassName();
205             try {
206                 Result r = null;
207                 Class test = Class.forName(testClass);
208                 ServerCheck tester = (ServerCheck)test.newInstance();
209                 r = tester.check(context);
210                 r.setStatus(0);
211                 resultMgr.addResults(r);
212                 r.setStatus(1);
213                 resultMgr.addResults(r);
214             }
215             catch (Throwable tt) {
216                 // Logging
217                 _logger.log(Level.FINE, "serverxmlverifier.error_check", tt);
218             }
219         }
220         writeToFile(resultMgr);
221         return true;
222     }
223     
224     // <addition> srini@sun.com
225     // Function added to return a boolean after verification of all tests
226     // true if all tests passed
227     // false if one test failed
228     
229     public boolean testStatus(ConfigContext context) {
230         boolean retValue = true;
231         if(testCases.isEmpty())
232               loadTestInfo();
233         Iterator testClasses = testCases.values().iterator();
234         ResultMgr resultMgr = new ResultMgr();
235         while(testClasses.hasNext()) {
236             TestInformation ti = (TestInformation)testClasses.next();
237             String testClass = ti.getClassName();
238             try {
239                 Result r = null;
240                 Class test = Class.forName(testClass);
241                 ServerCheck tester = (ServerCheck)test.newInstance();
242                 r = tester.check(context);
243                 r.setStatus(0);
244                 resultMgr.addResults(r);
245                 r.setStatus(1);
246                 resultMgr.addResults(r);
247                 Vector vobj = resultMgr.getFailedResults();
248                 for(int i=0;i<vobj.size();i++) {
249                         result=(Result)vobj.elementAt(i);
250                         vresult=result.getErrorDetails();
251                         if(vresult.size() > 0)
252                             retValue = false;
253                 }
254             }
255             catch (Throwable tt) {
256                 // Logging
257                 _logger.log(Level.FINE, "serverxmlverifier.error_check", tt);
258                 return false;
259             }
260         }
261         writeToFile(resultMgr);
262         return retValue;
263      }
264      
265      // </addition>
266     
267      public void writeToFile(ResultMgr resultMgr) {
268         String tmpDir = System.getProperty("java.io.tmpdir");
269         Calendar calendar = Calendar.getInstance();
270         String timestamp = new Integer(calendar.get(Calendar.HOUR_OF_DAY)).toString() +
271                            new Integer(calendar.get(Calendar.MINUTE)).toString() +
272                            new Integer(calendar.get(Calendar.MILLISECOND)).toString();
273         if(fileUrl == null)
274             fileUrl = tmpDir + "/TestResults" + timestamp + ".txt";
275         // Print the file it is writing the output
276         // Logging
277         _logger.log(Level.INFO, "serverxmlverifier.test_output", fileUrl);
278         try {
279             FileOutputStream fout = new FileOutputStream(fileUrl);
280             PrintWriter pout = new PrintWriter(fout);
281             pout.println("------------------------------------");
282             pout.println("SERVER.XML VERIFICATION TEST RESULTS");
283             pout.println("------------------------------------");
284             pout.println();
285             printFailedDetails(resultMgr.getFailedResults(), pout);
286             pout.close();
287         } catch(IOException e) {
288             // Logging
289             _logger.log(Level.WARNING, "serverxmlverifier.error_writing_file");
290         }
291     }
292
293     public void printFailedDetails(Vector vobj, PrintWriter pout){
294         pout.println(" FAILED TESTS ");
295         pout.println(" ------------ ");
296         pout.println();
297         boolean tests = true;
298         for(int i=0;i<vobj.size();i++) {
299                 result=(Result)vobj.elementAt(i);
300                 vresult=result.getErrorDetails();
301                 if(vresult.size() > 0) {
302                     tests =false;
303                     pout.println(result.getTestName() + ":");
304                 }
305                 for(int j=0;j<vresult.size();j++)
306                    pout.println(" " + (String)vresult.elementAt(j));
307                 pout.println();
308         }
309         if(tests)
310             pout.println(" ***** None *****");
311     }*/

312
313     //public boolean check(String name, Object value, ConfigContext context, String choice) {
314
public boolean check(ConfigContextEvent ccce) {
315         String JavaDoc name = ccce.getName();
316         Object JavaDoc value = ccce.getObject();
317         ConfigContext context = ccce.getConfigContext();
318         String JavaDoc choice = ccce.getChoice();
319         String JavaDoc beanName = ccce.getBeanName();
320         
321         if(name == null && beanName == null)
322                 return true;
323         boolean retValue = false;
324         if(testCases.isEmpty())
325             loadTestInfo();
326         TestInformation ti = (TestInformation)testCases.get(name);
327         if(ti == null && beanName != null)
328                     ti =(TestInformation)testCases.get(beanName);
329         String JavaDoc testClass;
330         try {
331             testClass = ti.getClassName();
332         }
333         catch(Exception JavaDoc e){
334                return true;
335         }
336
337         try {
338             Class JavaDoc test = Class.forName(testClass);
339             ServerCheck tester = (ServerCheck)test.newInstance();
340             result = tester.check(ccce);
341             if (result.getStatus() == Result.PASSED)
342                 retValue = true;
343             else
344                 retValue = false;
345         }
346         catch(Throwable JavaDoc tt) {
347             // Logger
348
_logger.log(Level.FINE, "serverxmlverifier.error_check", tt);
349             retValue = true;
350         }
351         return retValue;
352     }
353     
354     /**
355      * after config add, delete, set, update or flush. type is in ccce
356      */

357     public void postAccessNotification(ConfigContextEvent ccce) {
358     }
359     
360     /**
361      * after config add, delete, set, update or flush. type is in ccce
362      */

363     public void postChangeNotification(ConfigContextEvent ccce) {
364     }
365     
366     /**
367      * before config add, delete, set, update or flush. type is in ccce
368      */

369     public void preAccessNotification(ConfigContextEvent ccce) {
370     }
371     
372     /**
373      * before config add, delete, set, update or flush. type is in ccce
374      */

375     public void preChangeNotification(ConfigContextEvent ccce) {
376          if(! check(ccce))
377                 throw new AFRuntimeException(result.getErrorDetails().toString());
378     }
379     
380 }
381    
382
Popular Tags