1 11 12 package org.eclipse.update.internal.core; 13 14 import java.io.*; 15 import java.net.*; 16 import java.text.ParseException ; 17 import java.text.SimpleDateFormat ; 18 import java.util.Arrays ; 19 import java.util.Collection ; 20 import java.util.Comparator ; 21 import java.util.Date ; 22 import java.util.HashMap ; 23 import java.util.StringTokenizer ; 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 35 public class InstallLogParser { 36 private IPath logPath; 37 private BufferedReader buffRead; 38 private InstallConfiguration currentConfiguration; 39 private HashMap installConfigMap; 40 private Comparator comparator; 41 42 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"; 52 private static final String ACTIVITY = "!ACTIVITY"; 54 public static final String SUCCESS = "success"; public static final String FAILURE = "failure"; 57 58 public InstallLogParser(){ 59 String loc = ConfiguratorUtils.getCurrentPlatformConfiguration().getConfigurationLocation().getFile(); 60 logPath = new Path(loc).removeLastSegments(1).append("install.log"); installConfigMap = new HashMap (); 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 (configs[i].getCreationDate().getTime()), configs[i]); 67 } 68 InstallConfiguration config = getConfigCopy((InstallConfiguration)SiteManager.getLocalSite().getCurrentConfiguration()); 70 installConfigMap.put(new Long (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 (){ 78 public int compare(Object e1, Object e2) { 79 Date date1 = ((InstallConfiguration)e1).getCreationDate(); 80 Date 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 InputStream is = new FileInputStream(logPath.toOSString()); 105 InputStreamReader isr = new InputStreamReader(is,"UTF-8"); buffRead = new BufferedReader(isr); 108 } catch (Exception e) { 109 throwCoreException(e); 110 } 111 } 112 113 private void throwCoreException(Throwable 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 128 try { 129 String type, status, action; 130 StringTokenizer htmlCode; 131 132 while (buffRead.ready()) { 133 134 htmlCode = new StringTokenizer (buffRead.readLine()); 135 while (!(htmlCode.hasMoreElements())) { 136 if (!buffRead.ready()) 137 return; 138 htmlCode = new StringTokenizer (buffRead.readLine()); 139 } 140 141 type = htmlCode.nextToken().trim(); 142 143 if (type.equals(ACTIVITY)) { 144 String time = htmlCode.nextToken(); 145 String date; 146 StringBuffer target = new StringBuffer (); 147 date = htmlCode.nextToken("."); htmlCode.nextToken(" "); while (htmlCode.countTokens() > 2){ 150 target.append(" "); 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 time = htmlCode.nextToken(); 159 StringBuffer date; 160 date = new StringBuffer (); 161 while (htmlCode.countTokens() > 0){ 162 if (date.length() != 0) 163 date.append(" "); date.append(htmlCode.nextToken()); 165 } 166 currentConfiguration = (InstallConfiguration)installConfigMap.get(new Long (time)); 167 } 168 } 169 } catch (Exception 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 action, String time, String date, String status, String 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 (activityTime)); 210 } catch (NumberFormatException e) { 211 try { 214 a.setDate(new SimpleDateFormat ().parse(date)); 215 } catch (ParseException e1) { 216 } 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 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 |