KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > core > UpdateManagerLogWriter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.update.internal.core;
12
13 import java.io.BufferedWriter JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.FileOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.OutputStream JavaDoc;
18 import java.io.OutputStreamWriter JavaDoc;
19 import java.io.UnsupportedEncodingException JavaDoc;
20 import java.io.Writer JavaDoc;
21 import java.text.DateFormat JavaDoc;
22 import java.text.SimpleDateFormat JavaDoc;
23 import java.util.Date JavaDoc;
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 /**
33  * A log writer that writes update manager log entries.
34  */

35 public class UpdateManagerLogWriter {
36     private File JavaDoc logFile = null;
37     private Writer JavaDoc log = null;
38
39     private static final String JavaDoc CONFIGURATION = "!CONFIGURATION"; //$NON-NLS-1$
40
private static final String JavaDoc ACTIVITY = "!ACTIVITY"; //$NON-NLS-1$
41

42     private static final String JavaDoc SUCCESS = "success"; //$NON-NLS-1$
43
private static final String JavaDoc FAILURE = "failure"; //$NON-NLS-1$
44

45     private static final String JavaDoc FEATURE_INSTALL = "feature-install"; //$NON-NLS-1$
46
private static final String JavaDoc FEATURE_REMOVE = "feature-remove"; //$NON-NLS-1$
47
private static final String JavaDoc SITE_INSTALL = "site-install"; //$NON-NLS-1$
48
private static final String JavaDoc SITE_REMOVE = "site-remove"; //$NON-NLS-1$
49
private static final String JavaDoc UNCONFIGURE = "feature-disable"; //$NON-NLS-1$
50
private static final String JavaDoc CONFIGURE = "feature-enable"; //$NON-NLS-1$
51
private static final String JavaDoc REVERT = "revert"; //$NON-NLS-1$
52
private static final String JavaDoc RECONCILIATION = "reconciliation"; //$NON-NLS-1$
53
private static final String JavaDoc PRESERVED = "preserve-configuration"; //$NON-NLS-1$
54
private static final String JavaDoc UNKNOWN = "unknown"; //$NON-NLS-1$
55

56     private static final String JavaDoc LINE_SEPARATOR;
57
58     static {
59         String JavaDoc s = System.getProperty("line.separator"); //$NON-NLS-1$
60
LINE_SEPARATOR = s == null ? "\n" : s; //$NON-NLS-1$
61
}
62
63     /*
64      *
65      */

66     public UpdateManagerLogWriter(File JavaDoc file) {
67         this.logFile = file;
68         UpdateCore.warn("UPDATE MANAGER LOG Location: "+file.getAbsolutePath()); //$NON-NLS-1$
69

70         // If the file does not exist, prime it with the sites in the exisiting config
71
if (!file.exists())
72             initLog();
73     }
74     
75     /*
76      * Initializes the log with the original configuration
77      */

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 JavaDoc());
87                 activities[i].setStatus(IActivity.STATUS_OK);
88             }
89             Date JavaDoc date = new Date JavaDoc(runtimeConfig.getChangeStamp());
90             safeWriteConfiguration(date, activities);
91         } catch (Exception JavaDoc e) {
92             // silently ignore errors
93
}
94     }
95
96     /*
97      *
98      */

99     private void closeLogFile() throws IOException JavaDoc {
100         try {
101             if (log != null) {
102                 log.flush();
103                 log.close();
104             }
105         } finally {
106             log = null;
107         }
108     }
109     
110     /*
111      *
112      */

113     public void log(IInstallConfiguration installConfig) {
114         safeWriteConfiguration(installConfig.getCreationDate(), installConfig.getActivities());
115     }
116     
117     
118     /*
119      *
120      */

121     private void openLogFile() {
122         try {
123             log = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(logFile.getAbsolutePath(), true), "UTF-8")); //$NON-NLS-1$
124
} catch (IOException JavaDoc e) {
125             // there was a problem opening the log file so log to the console
126
log = logForStream(System.err);
127         }
128     }
129     
130     /*
131      *
132      */

133     private String JavaDoc getFormattedDate(Date JavaDoc date) {
134         try {
135             DateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc("MMM dd, yyyy kk:mm:ss.SS"); //$NON-NLS-1$
136
return formatter.format(date);
137         } catch (Exception JavaDoc e) {
138             // If there were problems writing out the date, ignore and
139
// continue since that shouldn't stop us from losing the rest
140
// of the information
141
}
142         return Long.toString(System.currentTimeMillis());
143     }
144     
145     /*
146      *
147      */

148     private Writer JavaDoc logForStream(OutputStream JavaDoc output) {
149         try {
150             return new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(output, "UTF-8")); //$NON-NLS-1$
151
} catch (UnsupportedEncodingException JavaDoc e) {
152             return new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(output));
153         }
154     }
155     
156     
157     /*
158      * Shuts down the update manager log.
159      */

160     public synchronized void shutdown() {
161         try {
162             if (logFile != null) {
163                 closeLogFile();
164                 logFile = null;
165             } else {
166                 if (log != null) {
167                     Writer JavaDoc old = log;
168                     log = null;
169                     old.flush();
170                     old.close();
171                 }
172             }
173         } catch (IOException JavaDoc e) {
174             //we've shutdown the log, so not much else we can do!
175
e.printStackTrace();
176         }
177     }
178
179     /*
180      *
181      */

182     private synchronized void safeWriteConfiguration(Date JavaDoc date, IActivity[] activities) {
183         // thread safety: (Concurrency003)
184
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 JavaDoc e) {
198             System.err.println("An exception occurred while writing to the update manager log:"); //$NON-NLS-1$
199
e.printStackTrace(System.err);
200             System.err.println("Logging to the console instead."); //$NON-NLS-1$
201
//we failed to write, so dump log entry to console instead
202
try {
203                 log = logForStream(System.err);
204                 write(date, activities);
205                 log.flush();
206             } catch (Exception JavaDoc e2) {
207                 System.err.println("An exception occurred while logging to the console:"); //$NON-NLS-1$
208
e2.printStackTrace(System.err);
209             }
210         } finally {
211             log = null;
212         }
213     }
214     
215
216     /*
217      * !CONFIGURATION <label>
218      */

219     private void write(Date JavaDoc date, IActivity[] activities) throws IOException JavaDoc {
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     /*
233      * !ACTIVITY <date> <target> <action> <status>
234      */

235     private void write(IActivity activity) throws IOException JavaDoc {
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     /*
251      *
252      */

253     private String JavaDoc 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+"]"; //$NON-NLS-1$ //$NON-NLS-2$
276
}
277     }
278
279
280     /*
281      *
282      */

283     private void writeln() throws IOException JavaDoc {
284         write(LINE_SEPARATOR);
285     }
286     
287     /*
288      *
289      */

290     private void write(String JavaDoc message) throws IOException JavaDoc {
291         if (message != null)
292             log.write(message);
293     }
294     
295     /*
296      *
297      */

298     private void writeSpace() throws IOException JavaDoc {
299         write(" "); //$NON-NLS-1$
300
}
301 }
302
Popular Tags