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, 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.module; 029 030import org.opencms.main.CmsLog; 031import org.opencms.main.OpenCms; 032import org.opencms.util.CmsFileUtil; 033 034import java.io.File; 035import java.io.FileOutputStream; 036import java.io.IOException; 037import java.io.PrintStream; 038import java.text.SimpleDateFormat; 039import java.util.Date; 040 041import org.apache.commons.logging.Log; 042 043/** 044 * This class is responsible for reading and writing module import/export log messages, to be used by CmsResourceWrapperModules. 045 */ 046public class CmsModuleLog { 047 048 /** 049 * Action type.<p> 050 */ 051 public enum Action { 052 /** import. */ 053 importModule("import"), 054 055 /** export. */ 056 exportModule("export"), 057 058 /** delete. */ 059 deleteModule("delete"); 060 061 /** The action name to be written to the log. */ 062 private String m_printName; 063 064 /** 065 * Creates new action type with the given print name.<p> 066 * 067 * @param printName the name to be written to the log 068 */ 069 Action(String printName) { 070 m_printName = printName; 071 } 072 073 /** 074 * Gets the name to be written to the log.<p> 075 * 076 * @return the name to be written to the log 077 */ 078 public String getPrintName() { 079 080 return m_printName; 081 } 082 } 083 084 /** The logger instance for this class. */ 085 private static final Log LOG = CmsLog.getLog(CmsModuleLog.class); 086 087 /** Date format for log messages. */ 088 public static final String DATE_FORMAT_STRING = "yyyy-MM-dd/HH:mm:ss"; 089 090 /** 091 * Gets the log file for the given module name.<p> 092 * 093 * @param moduleName the module 094 * 095 * @return the log file 096 */ 097 public File getLogFile(String moduleName) { 098 099 return new File( 100 OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf("packages/_modulelogs/" + moduleName + ".log")); 101 } 102 103 /** 104 * Logs a module action.<p> 105 * 106 * @param moduleName the module name 107 * @param action the action 108 * @param ok true if the action was successful 109 */ 110 public synchronized void log(String moduleName, Action action, boolean ok) { 111 112 if (moduleName == null) { 113 return; 114 } 115 SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_STRING); 116 String now = format.format(new Date()); 117 String message = now + " " + action.getPrintName() + " " + (ok ? "0" : "1"); 118 log(moduleName, message); 119 } 120 121 /** 122 * Reads the log file and returns the data.<p> 123 * 124 * @param moduleName the module name 125 * @return the log contents 126 * 127 * @throws IOException if something goes wrong 128 */ 129 public synchronized byte[] readLog(String moduleName) throws IOException { 130 131 File logFile = getLogFile(moduleName); 132 if (logFile.exists()) { 133 return CmsFileUtil.readFile(logFile); 134 } else { 135 return new byte[] {}; 136 } 137 } 138 139 /** 140 * Logs a message for the given module.<p> 141 * 142 * @param moduleName the module name 143 * @param message the message to log 144 */ 145 private synchronized void log(String moduleName, String message) { 146 147 // We re-open the file every time. This should be OK performance-wise, since modules are usually not 148 // imported/exported/deleted so frequently that this would matter. 149 File logFile = getLogFile(moduleName); 150 logFile.getParentFile().mkdirs(); 151 FileOutputStream out = null; 152 PrintStream ps = null; 153 try { 154 out = new FileOutputStream(logFile, true); // open file with append=true 155 ps = new PrintStream(out); 156 ps.println(message); 157 } catch (IOException e) { 158 LOG.error(e.getLocalizedMessage(), e); 159 } finally { 160 if (ps != null) { 161 ps.close(); 162 } 163 } 164 } 165}