KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > deployment > work > DeployerLog


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Florent BENOIT & Ludovic BERT
22  * --------------------------------------------------------------------------
23  * $Id: DeployerLog.java,v 1.3 2005/04/28 16:53:00 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas_lib.deployment.work;
28
29 //import java
30
import java.io.BufferedReader JavaDoc;
31 import java.io.BufferedWriter JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.FileNotFoundException JavaDoc;
34 import java.io.FileReader JavaDoc;
35 import java.io.FileWriter JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.PrintWriter JavaDoc;
38 import java.util.Enumeration JavaDoc;
39 import java.util.StringTokenizer JavaDoc;
40 import java.util.Vector JavaDoc;
41
42 import org.objectweb.util.monolog.api.Logger;
43 import org.objectweb.util.monolog.api.BasicLevel;
44
45 import org.objectweb.jonas.common.Log;
46
47 /**
48  * Class which permits to store or load the association between the name of an
49  * package and the timestamped work copy associated.
50  * @author Florent Benoit
51  * @author Ludovic Bert
52  */

53 public class DeployerLog {
54
55     /**
56      * The logger used in JOnAS
57      */

58     private static Logger logger = Log.getLogger(Log.JONAS_DEPLOY_WORK_PREFIX);
59
60     /**
61      * separator char of a entry in the log file
62      */

63     private static final String JavaDoc SEPARATOR_ENTRY = ";";
64
65     /**
66      * File for logging
67      */

68     private File JavaDoc logFile;
69
70     /**
71      * the current entries of the logfile
72      */

73     private Vector JavaDoc logEntries = null;
74
75     /**
76      * Constructor for the deployerLog.
77      * @param logFile the file which is used for read/write entries
78      * @throws DeployerLogException if the loadentries failed.
79      */

80     public DeployerLog(File JavaDoc logFile) throws DeployerLogException {
81         if (getLogger().isLoggable(BasicLevel.DEBUG)) {
82             getLogger().log(BasicLevel.DEBUG, "logfile=" + logFile.getName());
83         }
84
85         this.logFile = logFile;
86         logEntries = new Vector JavaDoc();
87         loadEntries();
88     }
89
90     /**
91      * @return the logger
92      */

93     protected static Logger getLogger() {
94         return logger;
95     }
96
97     /**
98      * load the entries of the log file.
99      * @throws DeployerLogException if the load failed.
100      */

101     private synchronized void loadEntries() throws DeployerLogException {
102
103         BufferedReader JavaDoc br = null;
104         try {
105             br = new BufferedReader JavaDoc(new FileReader JavaDoc(logFile));
106         } catch (FileNotFoundException JavaDoc e) {
107             throw new DeployerLogException("Can not read the " + logFile + " file");
108         }
109         String JavaDoc line = null;
110
111         String JavaDoc field = null;
112         File JavaDoc originalField = null;
113         File JavaDoc copyField = null;
114         StringTokenizer JavaDoc st = null;
115
116         try {
117             //Read the text file
118
while ((line = br.readLine()) != null) {
119
120                 //parse the String
121
st = new StringTokenizer JavaDoc(line, SEPARATOR_ENTRY);
122                 field = st.nextToken();
123                 if (field == null) {
124                     throw new DeployerLogException("Inconsistent line in the file " + logFile);
125                 }
126                 originalField = new File JavaDoc(field);
127
128                 field = st.nextToken();
129                 if (field == null) {
130                     throw new DeployerLogException("Inconsistent line in the file " + logFile);
131                 }
132
133                 copyField = new File JavaDoc(field);
134
135                 getLogger().log(BasicLevel.DEBUG,
136                         "Entry[originalField=" + originalField + ",copyField=" + copyField + "]");
137                 logEntries.add(new LogEntry(originalField, copyField));
138             }
139             // Close the input stream
140
br.close();
141         } catch (IOException JavaDoc ioe) {
142             throw new DeployerLogException("Error while reading the log file " + logFile + " :" + ioe.getMessage());
143         }
144     }
145
146     /**
147      * Dump(save) the entries to the log file.
148      * @throws DeployerLogException if the save failed.
149      */

150     private synchronized void saveEntries() throws DeployerLogException {
151
152         PrintWriter JavaDoc pw = null;
153         try {
154             pw = new PrintWriter JavaDoc(new BufferedWriter JavaDoc(new FileWriter JavaDoc(logFile)));
155         } catch (IOException JavaDoc e) {
156             throw new DeployerLogException("Problem while trying to get an output stream for the " + logFile + " file");
157         }
158
159         LogEntry logEntry = null;
160         String JavaDoc original = null;
161         String JavaDoc copy = null;
162         String JavaDoc line = null;
163         for (Enumeration JavaDoc e = logEntries.elements(); e.hasMoreElements();) {
164             logEntry = (LogEntry) e.nextElement();
165
166             //get the infos
167

168             try {
169                 original = logEntry.getOriginal().getCanonicalPath();
170                 copy = logEntry.getCopy().getCanonicalPath();
171             } catch (IOException JavaDoc ioe) {
172                 throw new DeployerLogException("Problem while trying to get files names ");
173             }
174
175             //create the line
176
line = original + SEPARATOR_ENTRY + copy;
177
178             //dump the line
179
pw.println(line);
180         }
181         // Close the stream
182
pw.close();
183     }
184
185     /**
186      * Return the entries of the file.
187      * @return a vector of LogEntry item.
188      */

189     public synchronized Vector JavaDoc getEntries() {
190         return logEntries;
191     }
192
193     /**
194      * Remove the given entry and return the entries of the file.
195      * @param entry the LogEntry which must be remove.
196      * @return the new vector of LogEntry item.
197      * @throws DeployerLogException if the remove can't be done
198      */

199     public synchronized Vector JavaDoc removeEntry(LogEntry entry) throws DeployerLogException {
200         if (logEntries == null) {
201             throw new DeployerLogException("Can not remove a entry, the vector is null");
202         }
203
204         if (!logEntries.contains(entry)) {
205             throw new DeployerLogException("Can not remove entry " + entry + ". There is no such entry");
206         }
207
208         //remove can be done
209
logEntries.remove(entry);
210
211         //write to the file.
212
saveEntries();
213
214         //return the new vector
215
return logEntries;
216     }
217
218     /**
219      * Add the entry and return the new entries
220      * @param original the name of the file
221      * @param copy the copy of the file
222      * @return the new vector of LogEntry item.
223      * @throws DeployerLogException if the add can't be done
224      */

225     public synchronized Vector JavaDoc addEntry(File JavaDoc original, File JavaDoc copy) throws DeployerLogException {
226         if (logEntries == null) {
227             throw new DeployerLogException("Can not add an entry, the vector is null");
228         }
229
230         //add only if it's not already present
231
LogEntry logEntry = null;
232         File JavaDoc originalEntry = null;
233         File JavaDoc copyEntry = null;
234
235         boolean found = false;
236         Enumeration JavaDoc e = logEntries.elements();
237
238         //add only if the entry is not found
239
while (e.hasMoreElements() && !found) {
240             logEntry = (LogEntry) e.nextElement();
241
242             originalEntry = logEntry.getOriginal();
243             copyEntry = logEntry.getCopy();
244
245             if (originalEntry.getPath().equals(original.getPath()) && copyEntry.getName().equals(copy.getName())) {
246                 found = true;
247             }
248         }
249         if (found) {
250             return logEntries;
251         }
252
253         //add entry
254
logEntry = new LogEntry(original, copy);
255
256         //add can be done
257
logEntries.add(logEntry);
258
259         //write to the file.
260
saveEntries();
261
262         //return the new vector
263
return logEntries;
264     }
265
266 }
Popular Tags