001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.loggers;
028    
029    import java.io.File;
030    import java.io.FileInputStream;
031    import java.io.FileOutputStream;
032    import java.io.IOException;
033    import java.util.zip.ZipEntry;
034    import java.util.zip.ZipOutputStream;
035    
036    import static org.opends.server.loggers.debug.DebugLogger.*;
037    import org.opends.server.loggers.debug.DebugTracer;
038    import org.opends.server.types.DebugLogLevel;
039    
040    /**
041     * This class implements a post rotation action that compresses
042     * the file using ZIP compression.
043     */
044    public class ZIPAction implements PostRotationAction
045    {
046      /**
047       * The tracer object for the debug logger.
048       */
049      private static final DebugTracer TRACER = getTracer();
050    
051    
052      private File originalFile;
053      private File newFile;
054      private boolean deleteOriginal;
055    
056      /**
057       * Create the action based on the original file, the new file after
058       * compression and whether the original file should be deleted.
059       *
060       * @param origFile    The source file name to compress.
061       * @param newFile     The compressed file name.
062       * @param deleteOrig  Whether the source file should be deleted after
063       *                    compression or not.
064       */
065      public ZIPAction(String origFile, String newFile, boolean deleteOrig)
066      {
067        this.originalFile = new File(origFile);
068        this.newFile = new File(newFile);
069        this.deleteOriginal = deleteOrig;
070      }
071    
072      /**
073       * The compression action that is executed. Returns true if the
074       * compression succeeded and false otherwise.
075       *
076       * @return  <CODE>true</CODE> if the compression was successful, or
077       *          <CODE>false</CODE> if it was not.
078       */
079      public boolean execute()
080      {
081        FileInputStream fis = null;
082        ZipOutputStream zip = null;
083        boolean inputStreamOpen = false;
084        boolean outputStreamOpen = false;
085    
086        try
087        {
088          if(!originalFile.exists())
089          {
090            System.err.println("Source file does not exist:" + originalFile);
091            return false;
092          }
093    
094          fis = new FileInputStream(originalFile);
095          inputStreamOpen = true;
096          FileOutputStream fos = new FileOutputStream(newFile);
097          zip = new ZipOutputStream(fos);
098          outputStreamOpen = true;
099    
100          ZipEntry zipEntry = new ZipEntry(originalFile.getName());
101          zip.putNextEntry(zipEntry);
102    
103          byte[] buf = new byte[8192];
104          int n;
105    
106          while((n = fis.read(buf)) != -1)
107          {
108            zip.write(buf, 0, n);
109          }
110    
111          zip.close();
112          outputStreamOpen = false;
113          fis.close();
114          inputStreamOpen = false;
115    
116          if(deleteOriginal)
117          {
118            if(!originalFile.delete())
119            {
120              System.err.println("Cannot delete original file:" + originalFile);
121              return false;
122            }
123          }
124    
125          return true;
126        } catch(IOException ioe)
127        {
128          if (debugEnabled())
129          {
130            TRACER.debugCaught(DebugLogLevel.ERROR, ioe);
131          }
132          if (inputStreamOpen)
133          {
134            try
135            {
136              fis.close();
137            }
138            catch (Exception fe)
139            {
140              if (debugEnabled())
141              {
142                TRACER.debugCaught(DebugLogLevel.ERROR, fe);
143              }
144              // Cannot do much. Ignore.
145            }
146          }
147          if (outputStreamOpen)
148          {
149            try
150            {
151              zip.close();
152            }
153            catch (Exception ze)
154            {
155              if (debugEnabled())
156              {
157                TRACER.debugCaught(DebugLogLevel.ERROR, ze);
158              }
159              // Cannot do much. Ignore.
160            }
161          }
162          return false;
163        }
164      }
165    
166    
167    }
168