1 11 package org.eclipse.update.internal.core; 12 13 import java.io.BufferedWriter ; 14 import java.io.File ; 15 import java.io.FileOutputStream ; 16 import java.io.IOException ; 17 import java.io.OutputStream ; 18 import java.io.OutputStreamWriter ; 19 import java.io.UnsupportedEncodingException ; 20 import java.io.Writer ; 21 import java.text.DateFormat ; 22 import java.text.SimpleDateFormat ; 23 import java.util.Date ; 24 25 import org.eclipse.core.runtime.FileLocator; 26 import org.eclipse.update.configuration.IActivity; 27 import org.eclipse.update.configuration.IInstallConfiguration; 28 import org.eclipse.update.configurator.ConfiguratorUtils; 29 import org.eclipse.update.configurator.IPlatformConfiguration; 30 31 32 35 public class UpdateManagerLogWriter { 36 private File logFile = null; 37 private Writer log = null; 38 39 private static final String CONFIGURATION = "!CONFIGURATION"; private static final String ACTIVITY = "!ACTIVITY"; 42 private static final String SUCCESS = "success"; private static final String FAILURE = "failure"; 45 private static final String FEATURE_INSTALL = "feature-install"; private static final String FEATURE_REMOVE = "feature-remove"; private static final String SITE_INSTALL = "site-install"; private static final String SITE_REMOVE = "site-remove"; private static final String UNCONFIGURE = "feature-disable"; private static final String CONFIGURE = "feature-enable"; private static final String REVERT = "revert"; private static final String RECONCILIATION = "reconciliation"; private static final String PRESERVED = "preserve-configuration"; private static final String UNKNOWN = "unknown"; 56 private static final String LINE_SEPARATOR; 57 58 static { 59 String s = System.getProperty("line.separator"); LINE_SEPARATOR = s == null ? "\n" : s; } 62 63 66 public UpdateManagerLogWriter(File file) { 67 this.logFile = file; 68 UpdateCore.warn("UPDATE MANAGER LOG Location: "+file.getAbsolutePath()); 70 if (!file.exists()) 72 initLog(); 73 } 74 75 78 private void initLog() { 79 try { 80 IPlatformConfiguration runtimeConfig = ConfiguratorUtils.getCurrentPlatformConfiguration(); 81 IPlatformConfiguration.ISiteEntry[] sites = runtimeConfig.getConfiguredSites(); 82 ConfigurationActivity[] activities = new ConfigurationActivity[sites.length]; 83 for (int i=0; i<sites.length; i++) { 84 activities[i] = new ConfigurationActivity(IActivity.ACTION_SITE_INSTALL); 85 activities[i].setLabel(FileLocator.toFileURL(sites[i].getURL()).toExternalForm()); 86 activities[i].setDate(new Date ()); 87 activities[i].setStatus(IActivity.STATUS_OK); 88 } 89 Date date = new Date (runtimeConfig.getChangeStamp()); 90 safeWriteConfiguration(date, activities); 91 } catch (Exception e) { 92 } 94 } 95 96 99 private void closeLogFile() throws IOException { 100 try { 101 if (log != null) { 102 log.flush(); 103 log.close(); 104 } 105 } finally { 106 log = null; 107 } 108 } 109 110 113 public void log(IInstallConfiguration installConfig) { 114 safeWriteConfiguration(installConfig.getCreationDate(), installConfig.getActivities()); 115 } 116 117 118 121 private void openLogFile() { 122 try { 123 log = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (logFile.getAbsolutePath(), true), "UTF-8")); } catch (IOException e) { 125 log = logForStream(System.err); 127 } 128 } 129 130 133 private String getFormattedDate(Date date) { 134 try { 135 DateFormat formatter = new SimpleDateFormat ("MMM dd, yyyy kk:mm:ss.SS"); return formatter.format(date); 137 } catch (Exception e) { 138 } 142 return Long.toString(System.currentTimeMillis()); 143 } 144 145 148 private Writer logForStream(OutputStream output) { 149 try { 150 return new BufferedWriter (new OutputStreamWriter (output, "UTF-8")); } catch (UnsupportedEncodingException e) { 152 return new BufferedWriter (new OutputStreamWriter (output)); 153 } 154 } 155 156 157 160 public synchronized void shutdown() { 161 try { 162 if (logFile != null) { 163 closeLogFile(); 164 logFile = null; 165 } else { 166 if (log != null) { 167 Writer old = log; 168 log = null; 169 old.flush(); 170 old.close(); 171 } 172 } 173 } catch (IOException e) { 174 e.printStackTrace(); 176 } 177 } 178 179 182 private synchronized void safeWriteConfiguration(Date date, IActivity[] activities) { 183 if (logFile != null) 185 openLogFile(); 186 if (log == null) 187 log = logForStream(System.err); 188 try { 189 try { 190 write(date, activities); 191 } finally { 192 if (logFile != null) 193 closeLogFile(); 194 else 195 log.flush(); 196 } 197 } catch (Exception e) { 198 System.err.println("An exception occurred while writing to the update manager log:"); e.printStackTrace(System.err); 200 System.err.println("Logging to the console instead."); try { 203 log = logForStream(System.err); 204 write(date, activities); 205 log.flush(); 206 } catch (Exception e2) { 207 System.err.println("An exception occurred while logging to the console:"); e2.printStackTrace(System.err); 209 } 210 } finally { 211 log = null; 212 } 213 } 214 215 216 219 private void write(Date date, IActivity[] activities) throws IOException { 220 writeln(); 221 write(CONFIGURATION); 222 writeSpace(); 223 write(String.valueOf(date.getTime())); 224 writeSpace(); 225 write(date.toString()); 226 writeln(); 227 for (int i = 0; i < activities.length; i++) { 228 write(activities[i]); 229 } 230 } 231 232 235 private void write(IActivity activity) throws IOException { 236 write(ACTIVITY); 237 writeSpace(); 238 write(String.valueOf(activity.getDate().getTime())); 239 writeSpace(); 240 write(getFormattedDate(activity.getDate())); 241 writeSpace(); 242 write(activity.getLabel()); 243 writeSpace(); 244 write(getAction(activity.getAction())); 245 writeSpace(); 246 write((activity.getStatus()==IActivity.STATUS_OK)?SUCCESS:FAILURE); 247 writeln(); 248 } 249 250 253 private String getAction(int i) { 254 switch (i) { 255 case IActivity.ACTION_FEATURE_INSTALL : 256 return FEATURE_INSTALL; 257 case IActivity.ACTION_FEATURE_REMOVE : 258 return FEATURE_REMOVE; 259 case IActivity.ACTION_SITE_INSTALL : 260 return SITE_INSTALL; 261 case IActivity.ACTION_SITE_REMOVE : 262 return SITE_REMOVE; 263 case IActivity.ACTION_UNCONFIGURE : 264 return UNCONFIGURE; 265 case IActivity.ACTION_CONFIGURE : 266 return CONFIGURE; 267 case IActivity.ACTION_REVERT : 268 return REVERT; 269 case IActivity.ACTION_RECONCILIATION : 270 return RECONCILIATION; 271 case IActivity.ACTION_ADD_PRESERVED : 272 return PRESERVED; 273 274 default : 275 return UNKNOWN+" ["+i+"]"; } 277 } 278 279 280 283 private void writeln() throws IOException { 284 write(LINE_SEPARATOR); 285 } 286 287 290 private void write(String message) throws IOException { 291 if (message != null) 292 log.write(message); 293 } 294 295 298 private void writeSpace() throws IOException { 299 write(" "); } 301 } 302 | Popular Tags |