001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://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 053 /** import. */ 054 importModule("import"), 055 056 /** export. */ 057 exportModule("export"), 058 059 /** delete. */ 060 deleteModule("delete"); 061 062 /** The action name to be written to the log. */ 063 private String m_printName; 064 065 /** 066 * Creates new action type with the given print name.<p> 067 * 068 * @param printName the name to be written to the log 069 */ 070 Action(String printName) { 071 072 m_printName = printName; 073 } 074 075 /** 076 * Gets the name to be written to the log.<p> 077 * 078 * @return the name to be written to the log 079 */ 080 public String getPrintName() { 081 082 return m_printName; 083 } 084 } 085 086 /** The logger instance for this class. */ 087 private static final Log LOG = CmsLog.getLog(CmsModuleLog.class); 088 089 /** Date format for log messages. */ 090 public static final String DATE_FORMAT_STRING = "yyyy-MM-dd/HH:mm:ss"; 091 092 /** 093 * Gets the log file for the given module name.<p> 094 * 095 * @param moduleName the module 096 * 097 * @return the log file 098 */ 099 public File getLogFile(String moduleName) { 100 101 return new File( 102 OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf("packages/_modulelogs/" + moduleName + ".log")); 103 } 104 105 /** 106 * Logs a module action.<p> 107 * 108 * @param moduleName the module name 109 * @param action the action 110 * @param ok true if the action was successful 111 */ 112 public synchronized void log(String moduleName, Action action, boolean ok) { 113 114 if (moduleName == null) { 115 return; 116 } 117 SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_STRING); 118 String now = format.format(new Date()); 119 String message = now + " " + action.getPrintName() + " " + (ok ? "0" : "1"); 120 log(moduleName, message); 121 } 122 123 /** 124 * Reads the log file and returns the data.<p> 125 * 126 * @param moduleName the module name 127 * @return the log contents 128 * 129 * @throws IOException if something goes wrong 130 */ 131 public synchronized byte[] readLog(String moduleName) throws IOException { 132 133 File logFile = getLogFile(moduleName); 134 if (logFile.exists()) { 135 return CmsFileUtil.readFile(logFile); 136 } else { 137 return new byte[] {}; 138 } 139 } 140 141 /** 142 * Logs a message for the given module.<p> 143 * 144 * @param moduleName the module name 145 * @param message the message to log 146 */ 147 private synchronized void log(String moduleName, String message) { 148 149 // We re-open the file every time. This should be OK performance-wise, since modules are usually not 150 // imported/exported/deleted so frequently that this would matter. 151 File logFile = getLogFile(moduleName); 152 logFile.getParentFile().mkdirs(); 153 FileOutputStream out = null; 154 PrintStream ps = null; 155 try { 156 out = new FileOutputStream(logFile, true); // open file with append=true 157 ps = new PrintStream(out); 158 ps.println(message); 159 } catch (IOException e) { 160 LOG.error(e.getLocalizedMessage(), e); 161 } finally { 162 if (ps != null) { 163 ps.close(); 164 } 165 } 166 } 167}