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