1 31 32 package org.opencms.configuration; 33 34 import org.opencms.i18n.CmsEncoder; 35 import org.opencms.main.CmsLog; 36 import org.opencms.util.CmsFileUtil; 37 import org.opencms.xml.CmsXmlEntityResolver; 38 import org.opencms.xml.CmsXmlErrorHandler; 39 40 import java.io.File ; 41 import java.io.FileOutputStream ; 42 import java.io.IOException ; 43 import java.io.OutputStream ; 44 import java.net.URL ; 45 import java.text.SimpleDateFormat ; 46 import java.util.ArrayList ; 47 import java.util.Date ; 48 import java.util.Iterator ; 49 import java.util.List ; 50 import java.util.Map ; 51 52 import org.apache.commons.digester.Digester; 53 import org.apache.commons.logging.Log; 54 55 import org.dom4j.Document; 56 import org.dom4j.DocumentHelper; 57 import org.dom4j.Element; 58 import org.dom4j.dom.DOMDocumentType; 59 import org.dom4j.io.OutputFormat; 60 import org.dom4j.io.XMLWriter; 61 import org.xml.sax.SAXException ; 62 63 75 public class CmsConfigurationManager implements I_CmsXmlConfiguration { 76 77 78 public static final String DEFAULT_DTD_LOCATION = "org/opencms/configuration/"; 79 80 81 public static final String DEFAULT_DTD_PREFIX = "http://www.opencms.org/dtd/6.0/"; 82 83 84 public static final String DEFAULT_XML_FILE_NAME = "opencms.xml"; 85 86 87 public static final String DTD_FILE_NAME = "opencms-configuration.dtd"; 88 89 90 public static final String N_ROOT = "opencms"; 91 92 93 public static final String POSTFIX_ORI = ".ori"; 94 95 96 protected static final String N_CONFIG = "config"; 97 98 99 protected static final String N_CONFIGURATION = "configuration"; 100 101 102 private static final SimpleDateFormat BACKUP_DATE_FORMAT = new SimpleDateFormat ("yyyy-MM-dd_HH-mm-ss_"); 103 104 105 private static final Log LOG = CmsLog.getLog(CmsConfigurationManager.class); 106 107 108 private static final long MAX_BACKUP_DAYS = 15; 109 110 111 private File m_backupFolder; 112 113 114 private File m_baseFolder; 115 116 117 private List m_configurations; 118 119 120 private Digester m_digester; 121 122 123 private Map m_legacyConfiguration; 124 125 130 public CmsConfigurationManager(String baseFolder) { 131 132 m_baseFolder = new File (baseFolder); 133 if (!m_baseFolder.exists()) { 134 if (LOG.isErrorEnabled()) { 135 LOG.error(Messages.get().getBundle().key( 136 Messages.LOG_INVALID_CONFIG_BASE_FOLDER_1, 137 m_baseFolder.getAbsolutePath())); 138 } 139 } 140 m_backupFolder = new File (m_baseFolder.getAbsolutePath() + File.separatorChar + "backup"); 141 if (!m_backupFolder.exists()) { 142 if (LOG.isDebugEnabled()) { 143 LOG.debug(Messages.get().getBundle().key( 144 Messages.LOG_CREATE_CONFIG_BKP_FOLDER_1, 145 m_backupFolder.getAbsolutePath())); 146 } 147 m_backupFolder.mkdirs(); 148 } 149 if (LOG.isDebugEnabled()) { 150 LOG.debug(Messages.get().getBundle().key(Messages.LOG_CONFIG_BASE_FOLDER_1, m_baseFolder.getAbsolutePath())); 151 LOG.debug(Messages.get().getBundle().key(Messages.LOG_CONFIG_BKP_FOLDER_1, m_backupFolder.getAbsolutePath())); 152 } 153 cacheDtdSystemId(this); 154 m_configurations = new ArrayList (); 155 } 156 157 162 public void addConfiguration(I_CmsXmlConfiguration configuration) { 163 164 if (LOG.isDebugEnabled()) { 165 LOG.debug(Messages.get().getBundle().key(Messages.LOG_ADD_CONFIG_1, configuration)); 166 } 167 m_configurations.add(configuration); 168 cacheDtdSystemId(configuration); 169 } 170 171 174 public void addConfigurationParameter(String paramName, String paramValue) { 175 176 } 178 179 182 public void addXmlDigesterRules(Digester digester) { 183 184 digester.addObjectCreate( 186 "*/" + N_CONFIGURATION + "/" + N_CONFIG, 187 I_CmsXmlConfiguration.A_CLASS, 188 CmsConfigurationException.class); 189 digester.addSetNext("*/" + N_CONFIGURATION + "/" + N_CONFIG, "addConfiguration"); 190 } 191 192 195 public Element generateXml(Element parent) { 196 197 Element configurationElement = parent.addElement(N_CONFIGURATION); 199 for (int i = 0; i < m_configurations.size(); i++) { 200 I_CmsXmlConfiguration configuration = (I_CmsXmlConfiguration)m_configurations.get(i); 202 configurationElement.addElement(N_CONFIG).addAttribute( 203 I_CmsXmlConfiguration.A_CLASS, 204 configuration.getClass().getName()); 205 } 206 return parent; 207 } 208 209 215 public Document generateXml(I_CmsXmlConfiguration configuration) { 216 217 Document result = DocumentHelper.createDocument(); 219 220 DOMDocumentType docType = new DOMDocumentType(); 222 docType.setElementName(N_ROOT); 223 docType.setSystemID(configuration.getDtdUrlPrefix() + configuration.getDtdFilename()); 224 result.setDocType(docType); 225 226 Element root = result.addElement(N_ROOT); 227 configuration.generateXml(root); 229 230 return result; 232 } 233 234 239 public File getBackupFolder() { 240 241 return m_backupFolder; 242 } 243 244 247 public Map getConfiguration() { 248 249 return m_legacyConfiguration; 250 } 251 252 258 public I_CmsXmlConfiguration getConfiguration(Class clazz) { 259 260 for (int i = 0; i < m_configurations.size(); i++) { 261 I_CmsXmlConfiguration configuration = (I_CmsXmlConfiguration)m_configurations.get(i); 262 if (clazz.equals(configuration.getClass())) { 263 return configuration; 264 } 265 } 266 return null; 267 } 268 269 274 public List getConfigurations() { 275 276 return m_configurations; 277 } 278 279 282 public String getDtdFilename() { 283 284 return DTD_FILE_NAME; 285 } 286 287 290 public String getDtdSystemLocation() { 291 292 return DEFAULT_DTD_LOCATION; 293 } 294 295 298 public String getDtdUrlPrefix() { 299 300 return DEFAULT_DTD_PREFIX; 301 } 302 303 306 public String getXmlFileName() { 307 308 return DEFAULT_XML_FILE_NAME; 309 } 310 311 314 public void initConfiguration() { 315 316 if (LOG.isDebugEnabled()) { 318 LOG.debug(Messages.get().getBundle().key(Messages.LOG_INIT_CONFIGURATION_1, this)); 319 } 320 } 321 322 328 public void loadXmlConfiguration() throws SAXException , IOException { 329 330 URL baseUrl = m_baseFolder.toURL(); 331 if (LOG.isDebugEnabled()) { 332 LOG.debug(Messages.get().getBundle().key(Messages.LOG_BASE_URL_1, baseUrl)); 333 } 334 335 loadXmlConfiguration(baseUrl, this); 337 338 Iterator i = m_configurations.iterator(); 340 while (i.hasNext()) { 341 I_CmsXmlConfiguration config = (I_CmsXmlConfiguration)i.next(); 342 loadXmlConfiguration(baseUrl, config); 343 } 344 345 removeOldBackups(MAX_BACKUP_DAYS); 347 } 348 349 354 public void setConfiguration(Map legacyConfiguration) { 355 356 m_legacyConfiguration = legacyConfiguration; 357 } 358 359 366 public void writeConfiguration(Class clazz) throws IOException , CmsConfigurationException { 367 368 I_CmsXmlConfiguration configuration = getConfiguration(clazz); 369 if (configuration == null) { 370 throw new CmsConfigurationException(Messages.get().container( 371 Messages.ERR_CONFIG_WITH_UNKNOWN_CLASS_1, 372 clazz.getName())); 373 } 374 375 File file = new File (m_baseFolder, configuration.getXmlFileName()); 377 if (LOG.isDebugEnabled()) { 378 LOG.debug(Messages.get().getBundle().key(Messages.LOG_WRITE_CONFIG_XMLFILE_1, file.getAbsolutePath())); 379 } 380 381 Document config = generateXml(configuration); 383 384 XMLWriter writer = null; 386 OutputFormat format = OutputFormat.createPrettyPrint(); 387 format.setIndentSize(4); 388 format.setTrimText(false); 389 format.setEncoding(CmsEncoder.ENCODING_UTF_8); 390 391 try { 392 OutputStream out = new FileOutputStream (file); 393 writer = new XMLWriter(out, format); 394 writer.write(config); 395 writer.flush(); 396 } finally { 397 if (writer != null) { 398 writer.close(); 399 } 400 } 401 402 if (LOG.isInfoEnabled()) { 403 LOG.info(Messages.get().getBundle().key( 404 Messages.LOG_WRITE_CONFIG_SUCCESS_2, 405 file.getAbsolutePath(), 406 configuration.getClass().getName())); 407 } 408 } 409 410 415 private void backupXmlConfiguration(I_CmsXmlConfiguration configuration) { 416 417 String fromName = m_baseFolder.getAbsolutePath() + File.separatorChar + configuration.getXmlFileName(); 418 String toDatePrefix = BACKUP_DATE_FORMAT.format(new Date ()); 419 String toName = m_backupFolder.getAbsolutePath() 420 + File.separatorChar 421 + toDatePrefix 422 + configuration.getXmlFileName(); 423 424 if (LOG.isDebugEnabled()) { 425 LOG.debug(Messages.get().getBundle().key(Messages.LOG_CREATE_CONFIG_BKP_2, fromName, toName)); 426 } 427 428 try { 429 CmsFileUtil.copy(fromName, toName); 430 } catch (IOException e) { 431 LOG.error(Messages.get().getBundle().key(Messages.LOG_CREATE_CONFIG_BKP_FAILURE_1, toName), e); 432 } 433 } 434 435 440 private void cacheDtdSystemId(I_CmsXmlConfiguration configuration) { 441 442 if (configuration.getDtdSystemLocation() != null) { 443 try { 444 String file = CmsFileUtil.readFile(configuration.getDtdSystemLocation() 445 + configuration.getDtdFilename(), CmsEncoder.ENCODING_UTF_8); 446 CmsXmlEntityResolver.cacheSystemId( 447 configuration.getDtdUrlPrefix() + configuration.getDtdFilename(), 448 file.getBytes(CmsEncoder.ENCODING_UTF_8)); 449 if (LOG.isDebugEnabled()) { 450 LOG.debug(Messages.get().getBundle().key( 451 Messages.LOG_CACHE_DTD_SYSTEM_ID_1, 452 configuration.getDtdUrlPrefix() 453 + configuration.getDtdFilename() 454 + " --> " 455 + configuration.getDtdSystemLocation() 456 + configuration.getDtdFilename())); 457 } 458 } catch (IOException e) { 459 LOG.error(Messages.get().getBundle().key( 460 Messages.LOG_CACHE_DTD_SYSTEM_ID_FAILURE_1, 461 configuration.getDtdSystemLocation() + configuration.getDtdFilename()), e); 462 } 463 } 464 } 465 466 474 private void loadXmlConfiguration(URL url, I_CmsXmlConfiguration configuration) throws SAXException , IOException { 475 476 URL fileUrl = new URL (url, configuration.getXmlFileName()); 478 if (LOG.isDebugEnabled()) { 479 LOG.debug(Messages.get().getBundle().key(Messages.LOG_LOAD_CONFIG_XMLFILE_1, fileUrl)); 480 } 481 482 backupXmlConfiguration(configuration); 484 485 m_digester = new Digester(); 487 m_digester.setUseContextClassLoader(true); 488 m_digester.setValidating(true); 489 m_digester.setEntityResolver(new CmsXmlEntityResolver(null)); 490 m_digester.setRuleNamespaceURI(null); 491 m_digester.setErrorHandler(new CmsXmlErrorHandler()); 492 493 m_digester.push(configuration); 495 496 configuration.addXmlDigesterRules(m_digester); 497 498 m_digester.parse(fileUrl.openStream()); 500 } 501 502 507 private void removeOldBackups(long daysToKeep) { 508 509 long maxAge = (System.currentTimeMillis() - (daysToKeep * 24 * 60 * 60 * 1000)); 510 File [] files = m_backupFolder.listFiles(); 511 for (int i = 0; i < files.length; i++) { 512 File file = files[i]; 513 long lastMod = file.lastModified(); 514 if ((lastMod < maxAge) & (!file.getAbsolutePath().endsWith(CmsConfigurationManager.POSTFIX_ORI))) { 515 file.delete(); 516 if (LOG.isDebugEnabled()) { 517 LOG.debug(Messages.get().getBundle().key(Messages.LOG_REMOVE_CONFIG_FILE_1, file.getAbsolutePath())); 518 } 519 } 520 } 521 } 522 } | Popular Tags |