00001
00027 package org.objectweb.jonas_lib.deployment.work;
00028
00029
00030 import java.io.BufferedReader;
00031 import java.io.BufferedWriter;
00032 import java.io.File;
00033 import java.io.FileNotFoundException;
00034 import java.io.FileReader;
00035 import java.io.FileWriter;
00036 import java.io.IOException;
00037 import java.io.PrintWriter;
00038 import java.util.Enumeration;
00039 import java.util.StringTokenizer;
00040 import java.util.Vector;
00041
00042 import org.objectweb.util.monolog.api.Logger;
00043 import org.objectweb.util.monolog.api.BasicLevel;
00044
00045 import org.objectweb.jonas.common.Log;
00046
00053 public class DeployerLog {
00054
00058 private static Logger logger = Log.getLogger(Log.JONAS_DEPLOY_WORK_PREFIX);
00059
00063 private static final String SEPARATOR_ENTRY = ";";
00064
00068 private File logFile;
00069
00073 private Vector logEntries = null;
00074
00080 public DeployerLog(File logFile) throws DeployerLogException {
00081 getLogger().log(BasicLevel.DEBUG, "logfile=" + logFile.getName());
00082
00083 this.logFile = logFile;
00084 logEntries = new Vector();
00085 loadEntries();
00086 }
00087
00091 protected static Logger getLogger() {
00092 return logger;
00093 }
00094
00099 private synchronized void loadEntries() throws DeployerLogException {
00100
00101 BufferedReader br = null;
00102 try {
00103 br = new BufferedReader(new FileReader(logFile));
00104 } catch (FileNotFoundException e) {
00105 throw new DeployerLogException("Can not read the " + logFile + " file");
00106 }
00107 String line = null;
00108
00109 String field = null;
00110 File originalField = null;
00111 File copyField = null;
00112 StringTokenizer st = null;
00113
00114 try {
00115
00116 while ((line = br.readLine()) != null) {
00117
00118
00119 st = new StringTokenizer(line, SEPARATOR_ENTRY);
00120 field = st.nextToken();
00121 if (field == null) {
00122 throw new DeployerLogException("Inconsistent line in the file " + logFile);
00123 }
00124 originalField = new File(field);
00125
00126 field = st.nextToken();
00127 if (field == null) {
00128 throw new DeployerLogException("Inconsistent line in the file " + logFile);
00129 }
00130
00131 copyField = new File(field);
00132
00133 getLogger().log(BasicLevel.DEBUG,
00134 "Entry[originalField=" + originalField + ",copyField=" + copyField + "]");
00135 logEntries.add(new LogEntry(originalField, copyField));
00136 }
00137
00138 br.close();
00139 } catch (IOException ioe) {
00140 throw new DeployerLogException("Error while reading the log file " + logFile + " :" + ioe.getMessage());
00141 }
00142 }
00143
00148 private synchronized void saveEntries() throws DeployerLogException {
00149
00150 PrintWriter pw = null;
00151 try {
00152 pw = new PrintWriter(new BufferedWriter(new FileWriter(logFile)));
00153 } catch (IOException e) {
00154 throw new DeployerLogException("Problem while trying to get an output stream for the " + logFile + " file");
00155 }
00156
00157 LogEntry logEntry = null;
00158 String original = null;
00159 String copy = null;
00160 String line = null;
00161 for (Enumeration e = logEntries.elements(); e.hasMoreElements();) {
00162 logEntry = (LogEntry) e.nextElement();
00163
00164
00165
00166 try {
00167 original = logEntry.getOriginal().getCanonicalPath();
00168 copy = logEntry.getCopy().getCanonicalPath();
00169 } catch (IOException ioe) {
00170 throw new DeployerLogException("Problem while trying to get files names ");
00171 }
00172
00173
00174 line = original + SEPARATOR_ENTRY + copy;
00175
00176
00177 pw.println(line);
00178 }
00179
00180 pw.close();
00181 }
00182
00187 public synchronized Vector getEntries() {
00188 return logEntries;
00189 }
00190
00197 public synchronized Vector removeEntry(LogEntry entry) throws DeployerLogException {
00198 if (logEntries == null) {
00199 throw new DeployerLogException("Can not remove a entry, the vector is null");
00200 }
00201
00202 if (!logEntries.contains(entry)) {
00203 throw new DeployerLogException("Can not remove entry " + entry + ". There is no such entry");
00204 }
00205
00206
00207 logEntries.remove(entry);
00208
00209
00210 saveEntries();
00211
00212
00213 return logEntries;
00214 }
00215
00223 public synchronized Vector addEntry(File original, File copy) throws DeployerLogException {
00224 if (logEntries == null) {
00225 throw new DeployerLogException("Can not add an entry, the vector is null");
00226 }
00227
00228
00229 LogEntry logEntry = null;
00230 File originalEntry = null;
00231 File copyEntry = null;
00232
00233 boolean found = false;
00234 Enumeration e = logEntries.elements();
00235
00236
00237 while (e.hasMoreElements() && !found) {
00238 logEntry = (LogEntry) e.nextElement();
00239
00240 originalEntry = logEntry.getOriginal();
00241 copyEntry = logEntry.getCopy();
00242
00243 if (originalEntry.getPath().equals(original.getPath()) && copyEntry.getName().equals(copy.getName())) {
00244 found = true;
00245 }
00246 }
00247 if (found) {
00248 return logEntries;
00249 }
00250
00251
00252 logEntry = new LogEntry(original, copy);
00253
00254
00255 logEntries.add(logEntry);
00256
00257
00258 saveEntries();
00259
00260
00261 return logEntries;
00262 }
00263
00264 }