001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer(s): Florent BENOIT & Ludovic BERT
022: * --------------------------------------------------------------------------
023: * $Id: DeployerLog.java 6673 2005-04-28 16:53:00Z benoitf $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas_lib.deployment.work;
026:
027: //import java
028: import java.io.BufferedReader;
029: import java.io.BufferedWriter;
030: import java.io.File;
031: import java.io.FileNotFoundException;
032: import java.io.FileReader;
033: import java.io.FileWriter;
034: import java.io.IOException;
035: import java.io.PrintWriter;
036: import java.util.Enumeration;
037: import java.util.StringTokenizer;
038: import java.util.Vector;
039:
040: import org.objectweb.util.monolog.api.Logger;
041: import org.objectweb.util.monolog.api.BasicLevel;
042:
043: import org.objectweb.jonas.common.Log;
044:
045: /**
046: * Class which permits to store or load the association between the name of an
047: * package and the timestamped work copy associated.
048: * @author Florent Benoit
049: * @author Ludovic Bert
050: */
051: public class DeployerLog {
052:
053: /**
054: * The logger used in JOnAS
055: */
056: private static Logger logger = Log
057: .getLogger(Log.JONAS_DEPLOY_WORK_PREFIX);
058:
059: /**
060: * separator char of a entry in the log file
061: */
062: private static final String SEPARATOR_ENTRY = ";";
063:
064: /**
065: * File for logging
066: */
067: private File logFile;
068:
069: /**
070: * the current entries of the logfile
071: */
072: private Vector logEntries = null;
073:
074: /**
075: * Constructor for the deployerLog.
076: * @param logFile the file which is used for read/write entries
077: * @throws DeployerLogException if the loadentries failed.
078: */
079: public DeployerLog(File logFile) throws DeployerLogException {
080: if (getLogger().isLoggable(BasicLevel.DEBUG)) {
081: getLogger().log(BasicLevel.DEBUG,
082: "logfile=" + logFile.getName());
083: }
084:
085: this .logFile = logFile;
086: logEntries = new Vector();
087: loadEntries();
088: }
089:
090: /**
091: * @return the logger
092: */
093: protected static Logger getLogger() {
094: return logger;
095: }
096:
097: /**
098: * load the entries of the log file.
099: * @throws DeployerLogException if the load failed.
100: */
101: private synchronized void loadEntries() throws DeployerLogException {
102:
103: BufferedReader br = null;
104: try {
105: br = new BufferedReader(new FileReader(logFile));
106: } catch (FileNotFoundException e) {
107: throw new DeployerLogException("Can not read the "
108: + logFile + " file");
109: }
110: String line = null;
111:
112: String field = null;
113: File originalField = null;
114: File copyField = null;
115: StringTokenizer st = null;
116:
117: try {
118: //Read the text file
119: while ((line = br.readLine()) != null) {
120:
121: //parse the String
122: st = new StringTokenizer(line, SEPARATOR_ENTRY);
123: field = st.nextToken();
124: if (field == null) {
125: throw new DeployerLogException(
126: "Inconsistent line in the file " + logFile);
127: }
128: originalField = new File(field);
129:
130: field = st.nextToken();
131: if (field == null) {
132: throw new DeployerLogException(
133: "Inconsistent line in the file " + logFile);
134: }
135:
136: copyField = new File(field);
137:
138: getLogger().log(
139: BasicLevel.DEBUG,
140: "Entry[originalField=" + originalField
141: + ",copyField=" + copyField + "]");
142: logEntries.add(new LogEntry(originalField, copyField));
143: }
144: // Close the input stream
145: br.close();
146: } catch (IOException ioe) {
147: throw new DeployerLogException(
148: "Error while reading the log file " + logFile
149: + " :" + ioe.getMessage());
150: }
151: }
152:
153: /**
154: * Dump(save) the entries to the log file.
155: * @throws DeployerLogException if the save failed.
156: */
157: private synchronized void saveEntries() throws DeployerLogException {
158:
159: PrintWriter pw = null;
160: try {
161: pw = new PrintWriter(new BufferedWriter(new FileWriter(
162: logFile)));
163: } catch (IOException e) {
164: throw new DeployerLogException(
165: "Problem while trying to get an output stream for the "
166: + logFile + " file");
167: }
168:
169: LogEntry logEntry = null;
170: String original = null;
171: String copy = null;
172: String line = null;
173: for (Enumeration e = logEntries.elements(); e.hasMoreElements();) {
174: logEntry = (LogEntry) e.nextElement();
175:
176: //get the infos
177:
178: try {
179: original = logEntry.getOriginal().getCanonicalPath();
180: copy = logEntry.getCopy().getCanonicalPath();
181: } catch (IOException ioe) {
182: throw new DeployerLogException(
183: "Problem while trying to get files names ");
184: }
185:
186: //create the line
187: line = original + SEPARATOR_ENTRY + copy;
188:
189: //dump the line
190: pw.println(line);
191: }
192: // Close the stream
193: pw.close();
194: }
195:
196: /**
197: * Return the entries of the file.
198: * @return a vector of LogEntry item.
199: */
200: public synchronized Vector getEntries() {
201: return logEntries;
202: }
203:
204: /**
205: * Remove the given entry and return the entries of the file.
206: * @param entry the LogEntry which must be remove.
207: * @return the new vector of LogEntry item.
208: * @throws DeployerLogException if the remove can't be done
209: */
210: public synchronized Vector removeEntry(LogEntry entry)
211: throws DeployerLogException {
212: if (logEntries == null) {
213: throw new DeployerLogException(
214: "Can not remove a entry, the vector is null");
215: }
216:
217: if (!logEntries.contains(entry)) {
218: throw new DeployerLogException("Can not remove entry "
219: + entry + ". There is no such entry");
220: }
221:
222: //remove can be done
223: logEntries.remove(entry);
224:
225: //write to the file.
226: saveEntries();
227:
228: //return the new vector
229: return logEntries;
230: }
231:
232: /**
233: * Add the entry and return the new entries
234: * @param original the name of the file
235: * @param copy the copy of the file
236: * @return the new vector of LogEntry item.
237: * @throws DeployerLogException if the add can't be done
238: */
239: public synchronized Vector addEntry(File original, File copy)
240: throws DeployerLogException {
241: if (logEntries == null) {
242: throw new DeployerLogException(
243: "Can not add an entry, the vector is null");
244: }
245:
246: //add only if it's not already present
247: LogEntry logEntry = null;
248: File originalEntry = null;
249: File copyEntry = null;
250:
251: boolean found = false;
252: Enumeration e = logEntries.elements();
253:
254: //add only if the entry is not found
255: while (e.hasMoreElements() && !found) {
256: logEntry = (LogEntry) e.nextElement();
257:
258: originalEntry = logEntry.getOriginal();
259: copyEntry = logEntry.getCopy();
260:
261: if (originalEntry.getPath().equals(original.getPath())
262: && copyEntry.getName().equals(copy.getName())) {
263: found = true;
264: }
265: }
266: if (found) {
267: return logEntries;
268: }
269:
270: //add entry
271: logEntry = new LogEntry(original, copy);
272:
273: //add can be done
274: logEntries.add(logEntry);
275:
276: //write to the file.
277: saveEntries();
278:
279: //return the new vector
280: return logEntries;
281: }
282:
283: }
|