KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > setup > comptest > CmsSetupTests


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/setup/comptest/CmsSetupTests.java,v $
3  * Date : $Date: 2006/03/27 14:52:42 $
4  * Version: $Revision: 1.2 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.setup.comptest;
33
34 import org.opencms.setup.CmsSetupBean;
35 import org.opencms.setup.CmsSetupDb;
36
37 import java.io.File JavaDoc;
38 import java.io.FileWriter JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.io.PrintWriter JavaDoc;
41 import java.text.DateFormat JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.List JavaDoc;
45
46 /**
47  * Runs various tests to give users infos about whether their system is compatible to OpenCms.<p>
48  *
49  * @author Thomas Weckert
50  * @author Michael Moossen
51  *
52  * @version $Revision: 1.2 $
53  *
54  * @since 6.0.0
55  */

56 public class CmsSetupTests {
57
58     private boolean m_green;
59     private boolean m_red;
60     private List JavaDoc m_testResults;
61     private boolean m_yellow;
62
63     /**
64      * Creates a new setup test suite.<p>
65      */

66     public CmsSetupTests() {
67
68         super();
69     }
70
71     /**
72      * Returns a list of all available tests.<p>
73      *
74      * @return a list of all available tests
75      */

76     public List JavaDoc getAllTests() {
77
78         List JavaDoc tests = new ArrayList JavaDoc();
79         tests.add(new CmsSetupTestFolderPermissions());
80         tests.add(new CmsSetupTestJdkVersion());
81         tests.add(new CmsSetupTestOperatingSystem());
82         tests.add(new CmsSetupTestServletEngine());
83         tests.add(new CmsSetupTestSimapi());
84         tests.add(new CmsSetupTestWarFileUnpacked());
85         tests.add(new CmsSetupTestXercesVersion());
86         return tests;
87     }
88
89     /**
90      * Returns the test results.<p>
91      *
92      * @return the test results
93      */

94     public List JavaDoc getTestResults() {
95
96         return m_testResults;
97     }
98
99     /**
100      * Returns true, if the conditions in all testes were fulfilled.<p>
101      *
102      * @return true, if the conditions in all testes were fulfilled
103      */

104     public boolean isGreen() {
105
106         return m_green;
107     }
108
109     /**
110      * Returns true if one of the tests found a violated condition.
111      * It is assumed that it will be impossible to run OpenCms.<p>
112      *
113      * @return true if one of the tests violates a condition
114      */

115     public boolean isRed() {
116
117         return m_red;
118     }
119
120     /**
121      * Returns true if one of the tests found a questionable condition.
122      * It is possible that OpenCms will not run.<p>
123      *
124      * @return true if one of the tests found a questionable condition
125      */

126     public boolean isYellow() {
127
128         return m_yellow;
129     }
130
131     /**
132      * Runs all tests.<p>
133      *
134      * @param setupBean the CmsSetup bean of the setup wizard
135      */

136     public void runTests(CmsSetupBean setupBean) {
137
138         boolean hasRed = false;
139         boolean hasYellow = false;
140
141         // reset everything back to an initial state
142
m_testResults = new ArrayList JavaDoc();
143         setGreen();
144
145         Iterator JavaDoc it = getAllTests().iterator();
146         while (it.hasNext()) {
147             I_CmsSetupTest test = (I_CmsSetupTest)it.next();
148             CmsSetupTestResult testResult = null;
149             try {
150                 testResult = test.execute(setupBean);
151                 m_testResults.add(testResult);
152             } catch (Throwable JavaDoc e) {
153                 if (testResult != null) {
154                     testResult.setRed();
155                     testResult.setResult(I_CmsSetupTest.RESULT_FAILED);
156                     testResult.setHelp("Unable to test " + test.getName());
157                     testResult.setInfo(e.toString());
158                 }
159             }
160         }
161
162         // check whether a test found violated or questionable conditions
163
for (int i = 0; i < m_testResults.size(); i++) {
164             CmsSetupTestResult testResult = (CmsSetupTestResult)m_testResults.get(i);
165             if (testResult.isRed()) {
166                 hasRed = true;
167             } else if (testResult.isYellow()) {
168                 hasYellow = true;
169             }
170         }
171
172         // set the global result of all tests
173
if (hasRed) {
174             setRed();
175         } else if (!hasRed && hasYellow) {
176             setYellow();
177         } else {
178             setGreen();
179         }
180
181         // save the detected software component versions in a text file
182
writeVersionInfo(
183             setupBean.getServletConfig().getServletContext().getServerInfo(),
184             System.getProperty("java.version"),
185             setupBean.getWebAppRfsPath());
186     }
187
188     /**
189      * Sets if the conditions in all testes were fulfilled.<p>
190      */

191     protected void setGreen() {
192
193         m_green = true;
194         m_red = false;
195         m_yellow = false;
196     }
197
198     /**
199      * Sets if one of the tests found a violated condition.<p>
200      */

201     protected void setRed() {
202
203         m_green = false;
204         m_red = true;
205         m_yellow = false;
206     }
207
208     /**
209      * Sets if one of the tests found a questionable condition.<p>
210      */

211     protected void setYellow() {
212
213         m_green = false;
214         m_red = false;
215         m_yellow = true;
216     }
217
218     /**
219      * Writes the version info of the used servlet engine and the used JDK
220      * to the version.txt.<p>
221      *
222      * @param thisEngine The servlet engine in use
223      * @param usedJDK The JDK version in use
224      * @param basePath the OpenCms base path
225      */

226     protected void writeVersionInfo(String JavaDoc thisEngine, String JavaDoc usedJDK, String JavaDoc basePath) {
227
228         FileWriter JavaDoc fOut = null;
229         PrintWriter JavaDoc dOut = null;
230         String JavaDoc filename = basePath + CmsSetupDb.SETUP_FOLDER + "versions.txt";
231         try {
232             File JavaDoc file = new File JavaDoc(filename);
233             if (file.exists()) {
234                 // new FileOutputStream of the existing file with parameter append=true
235
fOut = new FileWriter JavaDoc(filename, true);
236             } else {
237                 fOut = new FileWriter JavaDoc(file);
238             }
239             // write the content to the file in server filesystem
240
dOut = new PrintWriter JavaDoc(fOut);
241             dOut.println();
242             dOut.println("############### currently used configuration ################");
243             dOut.println("Date: "
244                 + DateFormat.getDateTimeInstance().format(new java.util.Date JavaDoc(System.currentTimeMillis())));
245             dOut.println("Used JDK: " + usedJDK);
246             dOut.println("Used Servlet Engine: " + thisEngine);
247             dOut.close();
248         } catch (IOException JavaDoc e) {
249             // nothing we can do
250
} finally {
251             try {
252                 if (fOut != null) {
253                     fOut.close();
254                 }
255             } catch (IOException JavaDoc e) {
256                 // nothing we can do
257
}
258         }
259     }
260
261 }
262
Popular Tags