1 26 27 package org.objectweb.jonas_lib.deployment.work; 28 29 import java.io.BufferedReader ; 31 import java.io.BufferedWriter ; 32 import java.io.File ; 33 import java.io.FileNotFoundException ; 34 import java.io.FileReader ; 35 import java.io.FileWriter ; 36 import java.io.IOException ; 37 import java.io.PrintWriter ; 38 import java.util.Enumeration ; 39 import java.util.StringTokenizer ; 40 import java.util.Vector ; 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 53 public class DeployerLog { 54 55 58 private static Logger logger = Log.getLogger(Log.JONAS_DEPLOY_WORK_PREFIX); 59 60 63 private static final String SEPARATOR_ENTRY = ";"; 64 65 68 private File logFile; 69 70 73 private Vector logEntries = null; 74 75 80 public DeployerLog(File 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 (); 87 loadEntries(); 88 } 89 90 93 protected static Logger getLogger() { 94 return logger; 95 } 96 97 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 " + logFile + " file"); 108 } 109 String line = null; 110 111 String field = null; 112 File originalField = null; 113 File copyField = null; 114 StringTokenizer st = null; 115 116 try { 117 while ((line = br.readLine()) != null) { 119 120 st = new StringTokenizer (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 (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 (field); 134 135 getLogger().log(BasicLevel.DEBUG, 136 "Entry[originalField=" + originalField + ",copyField=" + copyField + "]"); 137 logEntries.add(new LogEntry(originalField, copyField)); 138 } 139 br.close(); 141 } catch (IOException ioe) { 142 throw new DeployerLogException("Error while reading the log file " + logFile + " :" + ioe.getMessage()); 143 } 144 } 145 146 150 private synchronized void saveEntries() throws DeployerLogException { 151 152 PrintWriter pw = null; 153 try { 154 pw = new PrintWriter (new BufferedWriter (new FileWriter (logFile))); 155 } catch (IOException 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 original = null; 161 String copy = null; 162 String line = null; 163 for (Enumeration e = logEntries.elements(); e.hasMoreElements();) { 164 logEntry = (LogEntry) e.nextElement(); 165 166 168 try { 169 original = logEntry.getOriginal().getCanonicalPath(); 170 copy = logEntry.getCopy().getCanonicalPath(); 171 } catch (IOException ioe) { 172 throw new DeployerLogException("Problem while trying to get files names "); 173 } 174 175 line = original + SEPARATOR_ENTRY + copy; 177 178 pw.println(line); 180 } 181 pw.close(); 183 } 184 185 189 public synchronized Vector getEntries() { 190 return logEntries; 191 } 192 193 199 public synchronized Vector 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 logEntries.remove(entry); 210 211 saveEntries(); 213 214 return logEntries; 216 } 217 218 225 public synchronized Vector addEntry(File original, File copy) throws DeployerLogException { 226 if (logEntries == null) { 227 throw new DeployerLogException("Can not add an entry, the vector is null"); 228 } 229 230 LogEntry logEntry = null; 232 File originalEntry = null; 233 File copyEntry = null; 234 235 boolean found = false; 236 Enumeration e = logEntries.elements(); 237 238 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 logEntry = new LogEntry(original, copy); 255 256 logEntries.add(logEntry); 258 259 saveEntries(); 261 262 return logEntries; 264 } 265 266 } | Popular Tags |