001: /*---------------------------------------------------------------------------*\
002: $Id: ZipOutputPlugIn.java 7041 2007-09-09 01:04:47Z bmc $
003: ---------------------------------------------------------------------------
004: This software is released under a BSD-style license:
005:
006: Copyright (c) 2004-2007 Brian M. Clapper. All rights reserved.
007:
008: Redistribution and use in source and binary forms, with or without
009: modification, are permitted provided that the following conditions are
010: met:
011:
012: 1. Redistributions of source code must retain the above copyright notice,
013: this list of conditions and the following disclaimer.
014:
015: 2. The end-user documentation included with the redistribution, if any,
016: must include the following acknowlegement:
017:
018: "This product includes software developed by Brian M. Clapper
019: (bmc@clapper.org, http://www.clapper.org/bmc/). That software is
020: copyright (c) 2004-2007 Brian M. Clapper."
021:
022: Alternately, this acknowlegement may appear in the software itself,
023: if wherever such third-party acknowlegements normally appear.
024:
025: 3. Neither the names "clapper.org", "curn", nor any of the names of the
026: project contributors may be used to endorse or promote products
027: derived from this software without prior written permission. For
028: written permission, please contact bmc@clapper.org.
029:
030: 4. Products derived from this software may not be called "curn", nor may
031: "clapper.org" appear in their names without prior written permission
032: of Brian M. Clapper.
033:
034: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
036: MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
037: NO EVENT SHALL BRIAN M. CLAPPER BE LIABLE FOR ANY DIRECT, INDIRECT,
038: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
039: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
040: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
041: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
042: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
043: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
044: \*---------------------------------------------------------------------------*/
045:
046: package org.clapper.curn.plugins;
047:
048: import org.clapper.curn.CurnConfig;
049: import org.clapper.curn.CurnException;
050: import org.clapper.curn.MainConfigItemPlugIn;
051: import org.clapper.curn.OutputHandler;
052: import org.clapper.curn.PostOutputPlugIn;
053:
054: import org.clapper.util.classutil.ClassUtil;
055: import org.clapper.util.config.ConfigurationException;
056: import org.clapper.util.logging.Logger;
057:
058: import org.clapper.util.io.Zipper;
059:
060: import java.io.File;
061: import java.io.IOException;
062: import java.util.Collection;
063: import org.clapper.curn.CurnUtil;
064:
065: /**
066: * The <tt>ZipOutputPlugIn</tt> handles zipping up the output from a
067: * <i>curn</i> run, if one or more email addresses are specified in the
068: * configuration file. It intercepts the following main (<tt>[curn]</tt>)
069: * section configuration parameter:
070: *
071: * <table border="1">
072: * <tr valign="top">
073: * <td><tt>ZipOutputTo</tt></td>
074: * <td>The path to the zip file to receive the output files. The zip
075: * file is overwritten if it exists.</td>
076: * </table>
077: *
078: * @version <tt>$Revision: 7041 $</tt>
079: */
080: public class ZipOutputPlugIn implements MainConfigItemPlugIn,
081: PostOutputPlugIn {
082: /*----------------------------------------------------------------------*\
083: Private Constants
084: \*----------------------------------------------------------------------*/
085:
086: private static final String VAR_ZIP_FILE = "ZipOutputTo";
087:
088: /*----------------------------------------------------------------------*\
089: Private Data Items
090: \*----------------------------------------------------------------------*/
091:
092: /**
093: * The zip file
094: */
095: private File zipFile = null;
096:
097: /**
098: * For log messages
099: */
100: private static final Logger log = new Logger(ZipOutputPlugIn.class);
101:
102: /*----------------------------------------------------------------------*\
103: Constructor
104: \*----------------------------------------------------------------------*/
105:
106: /**
107: * Default constructor (required).
108: */
109: public ZipOutputPlugIn() {
110: // Nothing to do
111: }
112:
113: /*----------------------------------------------------------------------*\
114: Public Methods Required by *PlugIn Interfaces
115: \*----------------------------------------------------------------------*/
116:
117: /**
118: * Get a displayable name for the plug-in.
119: *
120: * @return the name
121: */
122: public String getPlugInName() {
123: return "Zip Output";
124: }
125:
126: /**
127: * Get the sort key for this plug-in.
128: *
129: * @return the sort key string.
130: */
131: public String getPlugInSortKey() {
132: return ClassUtil.getShortClassName(getClass().getName());
133: }
134:
135: /**
136: * Initialize the plug-in. This method is called before any of the
137: * plug-in methods are called.
138: *
139: * @throws CurnException on error
140: */
141: public void initPlugIn() throws CurnException {
142: }
143:
144: /**
145: * Called immediately after <i>curn</i> has read and processed a
146: * configuration item in the main [curn] configuration section. All
147: * configuration items are passed, one by one, to each loaded plug-in.
148: * If a plug-in class is not interested in a particular configuration
149: * item, this method should simply return without doing anything. Note
150: * that some configuration items may simply be variable assignment;
151: * there's no real way to distinguish a variable assignment from a
152: * blessed configuration item.
153: *
154: * @param sectionName the name of the configuration section where
155: * the item was found
156: * @param paramName the name of the parameter
157: * @param config the {@link CurnConfig} object
158: *
159: * @throws CurnException on error
160: *
161: * @see CurnConfig
162: */
163: public void runMainConfigItemPlugIn(String sectionName,
164: String paramName, CurnConfig config) throws CurnException {
165: try {
166: if (paramName.equals(VAR_ZIP_FILE)) {
167: String zipFilePath = config.getConfigurationValue(
168: sectionName, paramName);
169: this .zipFile = CurnUtil
170: .mapConfiguredPathName(zipFilePath);
171: }
172: }
173:
174: catch (ConfigurationException ex) {
175: throw new CurnException(ex);
176: }
177: }
178:
179: /**
180: * Called after <i>curn</i> has flushed <i>all</i> output handlers. A
181: * post-output plug-in is a useful place to consolidate the output from
182: * all output handlers. For instance, such a plug-in might pack all the
183: * output into a zip file, or email it.
184: *
185: * @param outputHandlers a <tt>Collection</tt> of the
186: * {@link OutputHandler} objects (useful for
187: * obtaining the output files, for instance).
188: *
189: * @throws CurnException on error
190: *
191: * @see OutputHandler
192: */
193: public void runPostOutputPlugIn(
194: Collection<OutputHandler> outputHandlers)
195: throws CurnException {
196: if (zipFile != null) {
197: log
198: .debug("Zipping output to \"" + zipFile.getPath()
199: + "\"");
200: zipOutput(outputHandlers);
201: }
202: }
203:
204: /*----------------------------------------------------------------------*\
205: Private Methods
206: \*----------------------------------------------------------------------*/
207:
208: /**
209: * Zip the output from all output handlers.
210: *
211: * @param outputHandlers the output handlers
212: *
213: * @throws CurnException on error
214: */
215: private void zipOutput(Collection<OutputHandler> outputHandlers)
216: throws CurnException {
217: try {
218: boolean haveFiles = false;
219:
220: // First, figure out whether we have any output or not.
221:
222: for (OutputHandler handler : outputHandlers) {
223: if (handler.hasGeneratedOutput()) {
224: haveFiles = true;
225: break;
226: }
227: }
228:
229: if (!haveFiles) {
230: // None of the handlers produced any output.
231:
232: log.error("Warning: None of the output handlers "
233: + "produced any zippable output.");
234: }
235:
236: else {
237: // Create the zip file.
238:
239: Zipper zipper = new Zipper(zipFile, /* flatten */true);
240:
241: for (OutputHandler handler : outputHandlers) {
242: File file = handler.getGeneratedOutput();
243: if (file != null) {
244: log.debug("Zipping \"" + file.getPath() + "\"");
245: zipper.put(file);
246: }
247: }
248:
249: zipper.close();
250: }
251: }
252:
253: catch (IOException ex) {
254: throw new CurnException(ex);
255: }
256: }
257: }
|