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.GZIPOutputStream;
034
035 import static org.opends.server.loggers.debug.DebugLogger.*;
036 import org.opends.server.loggers.debug.DebugTracer;
037 import org.opends.server.types.DebugLogLevel;
038
039 /**
040 * This class implements a post rotation action that compresses
041 * the file using GZIP compression.
042 */
043 public class GZIPAction implements PostRotationAction
044 {
045 /**
046 * The tracer object for the debug logger.
047 */
048 private static final DebugTracer TRACER = getTracer();
049
050
051 private File originalFile;
052 private File newFile;
053 private boolean deleteOriginal;
054
055 /**
056 * Create the action based on the original file, the new file after
057 * compression and whether the original file should be deleted.
058 *
059 * @param origFile The source file name to compress.
060 * @param newFile The compressed file name.
061 * @param deleteOrig Whether the source file should be deleted after
062 * compression or not.
063 */
064 public GZIPAction(String origFile, String newFile, boolean deleteOrig)
065 {
066 this.originalFile = new File(origFile);
067 this.newFile = new File(newFile);
068 this.deleteOriginal = deleteOrig;
069 }
070
071 /**
072 * The compression action that is executed. Returns true if the
073 * compression succeeded and false otherwise.
074 *
075 * @return <CODE>true</CODE> if the compression succeeded, or
076 * <CODE>false</CODE> if it did not.
077 */
078 public boolean execute()
079 {
080 FileInputStream fis = null;
081 GZIPOutputStream gzip = null;
082 boolean inputStreamOpen = false;
083 boolean outputStreamOpen = false;
084
085 try
086 {
087 if(!originalFile.exists())
088 {
089 System.err.println("Source file does not exist:" + originalFile);
090 return false;
091 }
092
093 fis = new FileInputStream(originalFile);
094 inputStreamOpen = true;
095 FileOutputStream fos = new FileOutputStream(newFile);
096 gzip = new GZIPOutputStream(fos);
097 outputStreamOpen = true;
098
099 byte[] buf = new byte[8192];
100 int n;
101
102 while((n = fis.read(buf)) != -1)
103 {
104 gzip.write(buf, 0, n);
105 }
106
107 gzip.close();
108 outputStreamOpen = false;
109 fis.close();
110 inputStreamOpen = false;
111
112 if(deleteOriginal)
113 {
114 if(!originalFile.delete())
115 {
116 System.err.println("Cannot delete original file:" + originalFile);
117 return false;
118 }
119 }
120
121 return true;
122 } catch(IOException ioe)
123 {
124 if (debugEnabled())
125 {
126 TRACER.debugCaught(DebugLogLevel.ERROR, ioe);
127 }
128 if (inputStreamOpen)
129 {
130 try
131 {
132 fis.close();
133 }
134 catch (Exception fe)
135 {
136 if (debugEnabled())
137 {
138 TRACER.debugCaught(DebugLogLevel.ERROR, fe);
139 }
140 // Cannot do much. Ignore.
141 }
142 }
143 if (outputStreamOpen)
144 {
145 try
146 {
147 gzip.close();
148 }
149 catch (Exception ge)
150 {
151 if (debugEnabled())
152 {
153 TRACER.debugCaught(DebugLogLevel.ERROR, ge);
154 }
155 // Cannot do much. Ignore.
156 }
157 }
158 return false;
159 }
160 }
161
162
163 }
164