001: /*
002: * File : $Source: /usr/local/cvs/opencms/src-setup/org/opencms/setup/comptest/CmsSetupTests.java,v $
003: * Date : $Date: 2008-02-27 12:05:32 $
004: * Version: $Revision: 1.4 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.setup.comptest;
033:
034: import org.opencms.setup.CmsSetupBean;
035: import org.opencms.setup.CmsSetupDb;
036:
037: import java.io.File;
038: import java.io.FileWriter;
039: import java.io.IOException;
040: import java.io.PrintWriter;
041: import java.text.DateFormat;
042: import java.util.ArrayList;
043: import java.util.Iterator;
044: import java.util.List;
045:
046: /**
047: * Runs various tests to give users infos about whether their system is compatible to OpenCms.<p>
048: *
049: * @author Thomas Weckert
050: * @author Michael Moossen
051: *
052: * @version $Revision: 1.4 $
053: *
054: * @since 6.0.0
055: */
056: public class CmsSetupTests {
057:
058: private boolean m_green;
059: private boolean m_red;
060: private List m_testResults;
061: private boolean m_yellow;
062:
063: /**
064: * Creates a new setup test suite.<p>
065: */
066: public CmsSetupTests() {
067:
068: super ();
069: }
070:
071: /**
072: * Returns a list of all available tests.<p>
073: *
074: * @return a list of all available tests
075: */
076: public List getAllTests() {
077:
078: List tests = new ArrayList();
079: tests.add(new CmsSetupTestFolderPermissions());
080: tests.add(new CmsSetupTestJdkVersion());
081: tests.add(new CmsSetupTestJavaTempDir());
082: tests.add(new CmsSetupTestOperatingSystem());
083: tests.add(new CmsSetupTestServletContainer());
084: tests.add(new CmsSetupTestSimapi());
085: tests.add(new CmsSetupTestWarFileUnpacked());
086: tests.add(new CmsSetupTestXercesVersion());
087: return tests;
088: }
089:
090: /**
091: * Returns the test results.<p>
092: *
093: * @return the test results
094: */
095: public List getTestResults() {
096:
097: return m_testResults;
098: }
099:
100: /**
101: * Returns true, if the conditions in all tests were fulfilled.<p>
102: *
103: * @return true, if the conditions in all tests were fulfilled
104: */
105: public boolean isGreen() {
106:
107: return m_green;
108: }
109:
110: /**
111: * Returns true if one of the tests found a violated condition.
112: * It is assumed that it will be impossible to run OpenCms.<p>
113: *
114: * @return true if one of the tests violates a condition
115: */
116: public boolean isRed() {
117:
118: return m_red;
119: }
120:
121: /**
122: * Returns true if one of the tests found a questionable condition.
123: * It is possible that OpenCms will not run.<p>
124: *
125: * @return true if one of the tests found a questionable condition
126: */
127: public boolean isYellow() {
128:
129: return m_yellow;
130: }
131:
132: /**
133: * Runs all tests.<p>
134: *
135: * @param setupBean the CmsSetup bean of the setup wizard
136: */
137: public void runTests(CmsSetupBean setupBean) {
138:
139: boolean hasRed = false;
140: boolean hasYellow = false;
141:
142: // reset everything back to an initial state
143: m_testResults = new ArrayList();
144: setGreen();
145:
146: Iterator it = getAllTests().iterator();
147: while (it.hasNext()) {
148: I_CmsSetupTest test = (I_CmsSetupTest) it.next();
149: CmsSetupTestResult testResult = null;
150: try {
151: testResult = test.execute(setupBean);
152: m_testResults.add(testResult);
153: } catch (Throwable e) {
154: testResult = new CmsSetupTestResult(test);
155: testResult.setRed();
156: testResult.setResult(I_CmsSetupTest.RESULT_FAILED);
157: testResult.setHelp("Unable to test " + test.getName());
158: testResult.setInfo(e.toString());
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
165: .get(i);
166: if (testResult.isRed()) {
167: hasRed = true;
168: } else if (testResult.isYellow()) {
169: hasYellow = true;
170: }
171: }
172:
173: // set the global result of all tests
174: if (hasRed) {
175: setRed();
176: } else if (!hasRed && hasYellow) {
177: setYellow();
178: } else {
179: setGreen();
180: }
181:
182: // save the detected software component versions in a text file
183: writeVersionInfo(setupBean.getServletConfig()
184: .getServletContext().getServerInfo(), System
185: .getProperty("java.version"), setupBean
186: .getWebAppRfsPath());
187: }
188:
189: /**
190: * Sets if the conditions in all testes were fulfilled.<p>
191: */
192: protected void setGreen() {
193:
194: m_green = true;
195: m_red = false;
196: m_yellow = false;
197: }
198:
199: /**
200: * Sets if one of the tests found a violated condition.<p>
201: */
202: protected void setRed() {
203:
204: m_green = false;
205: m_red = true;
206: m_yellow = false;
207: }
208:
209: /**
210: * Sets if one of the tests found a questionable condition.<p>
211: */
212: protected void setYellow() {
213:
214: m_green = false;
215: m_red = false;
216: m_yellow = true;
217: }
218:
219: /**
220: * Writes the version info of the used servlet engine and the used JDK
221: * to the version.txt.<p>
222: *
223: * @param thisEngine The servlet engine in use
224: * @param usedJDK The JDK version in use
225: * @param basePath the OpenCms base path
226: */
227: protected void writeVersionInfo(String this Engine, String usedJDK,
228: String basePath) {
229:
230: FileWriter fOut = null;
231: PrintWriter dOut = null;
232: String filename = basePath + CmsSetupDb.SETUP_FOLDER
233: + "versions.txt";
234: try {
235: File file = new File(filename);
236: if (file.exists()) {
237: // new FileOutputStream of the existing file with parameter append=true
238: fOut = new FileWriter(filename, true);
239: } else {
240: fOut = new FileWriter(file);
241: }
242: // write the content to the file in server filesystem
243: dOut = new PrintWriter(fOut);
244: dOut.println();
245: dOut
246: .println("############### currently used configuration ################");
247: dOut.println("Date: "
248: + DateFormat.getDateTimeInstance().format(
249: new java.util.Date(System
250: .currentTimeMillis())));
251: dOut.println("Used JDK: " + usedJDK);
252: dOut.println("Used Servlet Engine: " + this Engine);
253: dOut.close();
254: } catch (IOException e) {
255: // nothing we can do
256: } finally {
257: try {
258: if (fOut != null) {
259: fOut.close();
260: }
261: } catch (IOException e) {
262: // nothing we can do
263: }
264: }
265: }
266:
267: }
|