KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.update.internal.core;
13
14 import java.io.*;
15 import java.net.*;
16 import java.text.ParseException JavaDoc;
17 import java.text.SimpleDateFormat JavaDoc;
18 import java.util.Arrays JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Comparator JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24
25 import org.eclipse.core.runtime.*;
26 import org.eclipse.update.configuration.*;
27 import org.eclipse.update.configurator.*;
28 import org.eclipse.update.core.*;
29 import org.eclipse.update.internal.operations.*;
30
31
32 /**
33  * Parses the installation log and creates installation configuration objects
34  */

35 public class InstallLogParser {
36     private IPath logPath;
37     private BufferedReader buffRead;
38     private InstallConfiguration currentConfiguration;
39     private HashMap JavaDoc installConfigMap;
40     private Comparator JavaDoc comparator;
41     
42     private static final String JavaDoc FEATURE_INSTALL = "feature-install"; //$NON-NLS-1$
43
private static final String JavaDoc FEATURE_REMOVE = "feature-remove"; //$NON-NLS-1$
44
private static final String JavaDoc SITE_INSTALL = "site-install"; //$NON-NLS-1$
45
private static final String JavaDoc SITE_REMOVE = "site-remove"; //$NON-NLS-1$
46
private static final String JavaDoc UNCONFIGURE = "feature-disable"; //$NON-NLS-1$
47
private static final String JavaDoc CONFIGURE = "feature-enable"; //$NON-NLS-1$
48
private static final String JavaDoc REVERT = "revert"; //$NON-NLS-1$
49
private static final String JavaDoc RECONCILIATION = "reconciliation"; //$NON-NLS-1$
50
private static final String JavaDoc PRESERVED = "preserve-configuration"; //$NON-NLS-1$
51

52     private static final String JavaDoc ACTIVITY = "!ACTIVITY"; //$NON-NLS-1$
53

54     public static final String JavaDoc SUCCESS = "success"; //$NON-NLS-1$
55
public static final String JavaDoc FAILURE = "failure"; //$NON-NLS-1$
56

57     
58     public InstallLogParser(){
59         String JavaDoc loc = ConfiguratorUtils.getCurrentPlatformConfiguration().getConfigurationLocation().getFile();
60         logPath = new Path(loc).removeLastSegments(1).append("install.log"); //$NON-NLS-1$
61
installConfigMap = new HashMap JavaDoc();
62         try {
63             InstallConfiguration[] configs = (InstallConfiguration[])SiteManager.getLocalSite().getConfigurationHistory();
64             for (int i=0;i<configs.length; i++){
65                 if (!configs[i].isCurrent())
66                     installConfigMap.put(new Long JavaDoc(configs[i].getCreationDate().getTime()), configs[i]);
67             }
68             // Need to make a copy of the current config instead
69
InstallConfiguration config = getConfigCopy((InstallConfiguration)SiteManager.getLocalSite().getCurrentConfiguration());
70             installConfigMap.put(new Long JavaDoc(config.getCreationDate().getTime()), config);
71             
72         } catch (CoreException e) {
73             UpdateCore.log(e);
74         } catch (MalformedURLException e){
75             UpdateCore.log(e);
76         }
77         comparator = new Comparator JavaDoc(){
78             public int compare(Object JavaDoc e1, Object JavaDoc e2) {
79                 Date JavaDoc date1 = ((InstallConfiguration)e1).getCreationDate();
80                 Date JavaDoc date2 = ((InstallConfiguration)e2).getCreationDate();
81                 return date1.before(date2) ? 1 : -1;
82             }
83         };
84     }
85     private InstallConfiguration getConfigCopy(InstallConfiguration origConfig) throws CoreException, MalformedURLException{
86         InstallConfiguration config = new InstallConfiguration(origConfig, origConfig.getURL(), origConfig.getLabel() );
87         config.setCreationDate(origConfig.getCreationDate());
88         return config;
89     }
90     public void parseInstallationLog(){
91         try {
92             openLog();
93             parseLog();
94         } catch (CoreException e) {
95             UpdateUtils.logException(e);
96         } finally {
97             closeLog();
98         }
99     }
100     
101     private void openLog() throws CoreException {
102         try {
103             // throws FileNotFoundException, IOException
104
InputStream is = new FileInputStream(logPath.toOSString());
105             // throws UnsupportedEncodingException
106
InputStreamReader isr = new InputStreamReader(is,"UTF-8"); //$NON-NLS-1$
107
buffRead = new BufferedReader(isr);
108         } catch (Exception JavaDoc e) {
109             throwCoreException(e);
110         }
111     }
112     
113     private void throwCoreException(Throwable JavaDoc e) throws CoreException {
114         throw new CoreException(
115             new Status(
116                 IStatus.ERROR,
117                 UpdateUtils.getPluginId(),
118                 IStatus.ERROR,
119                 Messages.InstallLogParser_errors,
120                 e));
121     }
122     
123     private void parseLog() throws CoreException {
124         // .install-log template
125
// !CONFIGURATION <configuration-date>
126
// !ACTIVITY <date> <target> <action> <status>
127

128         try {
129             String JavaDoc type, status, action;
130             StringTokenizer JavaDoc htmlCode;
131
132             while (buffRead.ready()) {
133
134                 htmlCode = new StringTokenizer JavaDoc(buffRead.readLine());
135                 while (!(htmlCode.hasMoreElements())) {
136                     if (!buffRead.ready())
137                         return;
138                     htmlCode = new StringTokenizer JavaDoc(buffRead.readLine());
139                 }
140
141                 type = htmlCode.nextToken().trim();
142
143                 if (type.equals(ACTIVITY)) {
144                     String JavaDoc time = htmlCode.nextToken();
145                     String JavaDoc date;
146                     StringBuffer JavaDoc target = new StringBuffer JavaDoc();
147                     date = htmlCode.nextToken("."); //$NON-NLS-1$
148
htmlCode.nextToken(" "); //$NON-NLS-1$
149
while (htmlCode.countTokens() > 2){
150                         target.append(" "); //$NON-NLS-1$
151
target.append(htmlCode.nextToken());
152                     }
153                     
154                     action = htmlCode.nextToken();
155                     status = htmlCode.nextToken();
156                     createActivity(action, time, date, status, target.toString(), currentConfiguration);
157                 } else {
158                     String JavaDoc time = htmlCode.nextToken();
159                     StringBuffer JavaDoc date;
160                     date = new StringBuffer JavaDoc();
161                     while (htmlCode.countTokens() > 0){
162                         if (date.length() != 0)
163                             date.append(" "); //$NON-NLS-1$
164
date.append(htmlCode.nextToken());
165                     }
166                     currentConfiguration = (InstallConfiguration)installConfigMap.get(new Long JavaDoc(time));
167                 }
168             }
169         } catch (Exception JavaDoc e) {
170             throwCoreException(e);
171         }
172     }
173     
174     private void closeLog() {
175         try {
176             if (buffRead != null)
177                 buffRead.close();
178         } catch (IOException e) {
179         } finally {
180             buffRead = null;
181         }
182     }
183     private IActivity createActivity(String JavaDoc action, String JavaDoc time, String JavaDoc date, String JavaDoc status, String JavaDoc target, InstallConfiguration config){
184         ConfigurationActivity a = new ConfigurationActivity();
185
186         int code = 0;
187         if (FEATURE_INSTALL.equals(action))
188             code = IActivity.ACTION_FEATURE_INSTALL;
189         else if (FEATURE_REMOVE.equals(action))
190             code = IActivity.ACTION_FEATURE_REMOVE;
191         else if (SITE_INSTALL.equals(action))
192             code = IActivity.ACTION_SITE_INSTALL;
193         else if (SITE_REMOVE.equals(action))
194             code = IActivity.ACTION_SITE_REMOVE;
195         else if (UNCONFIGURE.equals(action))
196             code = IActivity.ACTION_UNCONFIGURE;
197         else if (CONFIGURE.equals(action))
198             code = IActivity.ACTION_CONFIGURE;
199         else if (REVERT.equals(action))
200             code = IActivity.ACTION_REVERT;
201         else if (RECONCILIATION.equals(action))
202             code = IActivity.ACTION_RECONCILIATION;
203         else if (PRESERVED.equals(action))
204             code = IActivity.ACTION_ADD_PRESERVED;
205         
206         a.setAction(code);
207         try {
208             long activityTime = Long.parseLong(time);
209             a.setDate(new Date JavaDoc(activityTime));
210         } catch (NumberFormatException JavaDoc e) {
211             //PAL foundation
212
//a.setDate(new Date(date));
213
try {
214                 a.setDate(new SimpleDateFormat JavaDoc().parse(date));
215             } catch (ParseException JavaDoc e1) {
216                 //ignore
217
}
218         }
219         a.setStatus(SUCCESS.equals(status) ? IActivity.STATUS_OK : IActivity.STATUS_NOK);
220         a.setLabel(target);
221         a.setInstallConfigurationModel(config);
222         
223         if (config != null && !configContainsActivity(config, a)){
224             config.addActivity(a);
225         }
226         
227         return a;
228     }
229     
230     private boolean configContainsActivity(InstallConfiguration c, IActivity a){
231         IActivity[] activities = c.getActivities();
232         for (int i = 0 ; i<activities.length; i++){
233             if (a.equals(activities[i]))
234                 return true;
235         }
236         return false;
237     }
238
239     public InstallConfiguration[] getConfigurations(){
240         Collection JavaDoc configSet = installConfigMap.values();
241         InstallConfiguration[] configs = (InstallConfiguration[]) configSet.toArray(new InstallConfiguration[configSet.size()]);
242         Arrays.sort(configs, comparator);
243         return configs;
244     }
245 }
246
Popular Tags