1 11 package org.eclipse.update.internal.model; 12 import java.io.File ; 13 import java.net.MalformedURLException ; 14 import java.net.URL ; 15 import java.util.ArrayList ; 16 import java.util.Date ; 17 import java.util.List ; 18 import java.util.MissingResourceException ; 19 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.FileLocator; 22 import org.eclipse.update.core.model.ModelObject; 23 import org.eclipse.update.internal.core.BaseSiteLocalFactory; 24 import org.eclipse.update.internal.core.UpdateCore; 25 import org.eclipse.update.internal.core.UpdateManagerUtils; 26 27 30 31 public class SiteLocalModel extends ModelObject { 32 public static final String CONFIG_FILE = "platform.xml"; private long stamp; 34 private String label; 35 private URL location; 36 private String locationURLString; 37 private int history = UpdateCore.DEFAULT_HISTORY; 38 private List configurations; 39 private List preservedConfigurations; 40 private InstallConfigurationModel currentConfiguration; 41 42 45 public SiteLocalModel(){ 46 super(); 47 } 48 49 52 public InstallConfigurationModel getCurrentConfigurationModel() { 53 return currentConfiguration; 54 } 55 56 59 public InstallConfigurationModel[] getConfigurationHistoryModel() { 60 if (configurations==null) 61 processHistory(); 63 64 if (configurations == null || configurations.size() == 0) 65 return new InstallConfigurationModel[0]; 66 else 67 return (InstallConfigurationModel[])configurations.toArray(arrayTypeFor(configurations)); 68 } 69 70 74 public void addConfigurationModel(InstallConfigurationModel config) { 75 if (config != null) { 76 if (configurations == null) 77 configurations = new ArrayList (); 78 if (!configurations.contains(config)) 79 configurations.add(config); 80 } 81 } 82 83 87 public boolean removeConfigurationModel(InstallConfigurationModel config) { 88 if (config != null) { 89 return configurations.remove(config); 90 } 91 return false; 92 } 93 97 public URL getLocationURL() { 98 return location; 99 } 100 101 105 public String getLocationURLString() { 106 return locationURLString; 107 } 108 109 110 114 public void setLocationURLString(String locationURLString) { 115 assertIsWriteable(); 116 this.locationURLString = locationURLString; 117 this.location=null; 118 } 119 120 121 124 public String getLabel() { 125 return label; 126 } 127 128 132 public void setLabel(String label) { 133 assertIsWriteable(); 134 this.label = label; 135 } 136 137 138 141 public int getMaximumHistoryCount() { 142 return history; 143 } 144 145 148 public void setMaximumHistoryCount(int history) { 149 assertIsWriteable(); 150 this.history = history; 151 } 152 153 154 159 public void addPreservedInstallConfigurationModel(InstallConfigurationModel configuration) { 160 if (preservedConfigurations == null) 161 preservedConfigurations = new ArrayList (); 162 163 preservedConfigurations.add(configuration); 164 } 165 166 169 public boolean removePreservedConfigurationModel(InstallConfigurationModel configuration) { 170 if (preservedConfigurations != null) { 171 return preservedConfigurations.remove(configuration); 172 } 173 return false; 174 } 175 176 179 public InstallConfigurationModel[] getPreservedConfigurationsModel() { 180 if (preservedConfigurations==null || preservedConfigurations.isEmpty()) 181 return new InstallConfigurationModel[0]; 182 return (InstallConfigurationModel[])preservedConfigurations.toArray(arrayTypeFor(preservedConfigurations)); 183 } 184 185 186 190 public void setCurrentConfigurationModel(InstallConfigurationModel currentConfiguration) { 191 assertIsWriteable(); 192 this.currentConfiguration = currentConfiguration; 193 194 ConfiguredSiteModel[] confSites = currentConfiguration.getConfigurationSitesModel(); 196 for (int i = 0; i < confSites.length; i++) { 197 confSites[i].getSiteModel().setConfiguredSiteModel(confSites[i]); 198 } 199 } 200 201 204 public void resolve(URL base,URL bundleURL) throws MalformedURLException { 205 location = resolveURL(base,bundleURL,getLocationURLString()); 207 208 resolveListReference(getConfigurationHistoryModel(),base,bundleURL); 210 resolveListReference(getPreservedConfigurationsModel(),base,bundleURL); 211 resolveReference(getCurrentConfigurationModel(),base,bundleURL); 212 } 213 214 215 219 public long getStamp() { 220 return stamp; 221 } 222 223 227 public void setStamp(long stamp) { 228 this.stamp = stamp; 229 } 230 231 234 protected String getPropertyName() { 235 return "platform"; } 237 238 241 private void processHistory() { 242 try { 243 URL historyURL = new URL (getLocationURL(), "history"); historyURL = FileLocator.toFileURL(historyURL); 245 File historyDir = new File (historyURL.getFile()); 246 if (historyDir.exists()) { 247 File [] backedConfigs = historyDir.listFiles(); 248 BaseSiteLocalFactory factory = new BaseSiteLocalFactory(); 249 for (int i=0; i<backedConfigs.length; i++) { 250 String name = backedConfigs[i].getName(); 251 if (name.endsWith(".xml")) name = name.substring(0, name.length()-4); 253 else 254 continue; 255 Date date = new Date (Long.parseLong(name)); 256 InstallConfigurationModel config = factory.createInstallConfigurationModel(); 257 config.setLocationURLString(backedConfigs[i].getAbsolutePath().replace('\\', '/')); 258 config.setLabel(date.toString()); 259 config.setCreationDate(date); 260 config.resolve(backedConfigs[i].toURL(), getResourceBundleURL()); 261 262 addConfigurationModel(config); 264 } 265 } 266 } catch (Exception e) { 267 UpdateCore.warn("Error processing history: ", e); } 269 } 270 271 274 URL getResourceBundleURL() throws CoreException { 275 URL url = null; 276 try { 277 url = UpdateManagerUtils.asDirectoryURL(getLocationURL()); 278 } catch (MissingResourceException e) { 279 UpdateCore.warn(e.getLocalizedMessage() + ":" + url.toExternalForm()); } catch (MalformedURLException e) { 281 UpdateCore.warn(e.getLocalizedMessage()); 282 } 283 return url; 284 } 285 286 } 287 | Popular Tags |