1 12 package org.eclipse.update.internal.configurator; 13 14 import java.io.IOException ; 15 import java.net.*; 16 import java.util.ArrayList ; 17 import java.util.Date ; 18 import java.util.HashMap ; 19 20 import org.eclipse.core.runtime.*; 21 import org.eclipse.update.configurator.*; 22 import org.w3c.dom.*; 23 24 public class Configuration implements IConfigurationConstants { 25 26 private HashMap sites = new HashMap (); 27 private HashMap platformURLs = new HashMap (); 28 private Date date; 29 private long lastModified; private URL url; 31 private boolean transientConfig; 32 private boolean isDirty; 33 private Configuration linkedConfig; 35 public Configuration() { 36 this(new Date ()); 37 isDirty = true; 39 } 40 public Configuration(Date date) { 41 this.date = date; 42 } 43 44 public void setURL(URL url) { 45 this.url = url; 46 } 47 48 public URL getURL() { 49 return url; 50 } 51 52 public void setLinkedConfig(Configuration linkedConfig) { 53 this.linkedConfig = linkedConfig; 54 SiteEntry[] linkedSites = linkedConfig.getSites(); 56 for (int i=0; i<linkedSites.length; i++) 57 linkedSites[i].setUpdateable(false); 58 } 59 60 public Configuration getLinkedConfig() { 61 return linkedConfig; 62 } 63 64 67 public boolean isDirty() { 68 return isDirty; 69 } 70 71 public void setDirty(boolean dirty) { 72 isDirty = dirty; 73 } 74 75 public void addSiteEntry(String url, SiteEntry site) { 76 url = Utils.canonicalizeURL(url); 77 if (sites.get(url) == null && (linkedConfig == null || linkedConfig.sites.get(url) == null)) { 79 site.setConfig(this); 80 sites.put(url, site); 81 if(url.startsWith("platform:")){ URL pURL; 83 try { 84 pURL = new URL(url); 85 URL rURL = PlatformConfiguration.resolvePlatformURL(pURL); 86 String resolvedURL = rURL.toExternalForm(); 87 platformURLs.put(resolvedURL, pURL); 88 } catch (IOException e) { 89 } 91 } 92 } 93 } 94 95 public void removeSiteEntry(String url) { 96 url =Utils.canonicalizeURL(url); 97 sites.remove(url); 98 if(url.startsWith("platform:")){ URL pURL; 100 try { 101 pURL = new URL(url); 102 URL rURL = PlatformConfiguration.resolvePlatformURL(pURL); 103 String resolvedURL = rURL.toExternalForm(); 104 platformURLs.remove(resolvedURL); 105 } catch (IOException e) { 106 } 108 } 109 } 110 111 public SiteEntry getSiteEntry(String url) { 112 url = Utils.canonicalizeURL(url); 113 SiteEntry site = (SiteEntry)sites.get(url); 114 if (site == null && linkedConfig != null) 115 site = linkedConfig.getSiteEntry(url); 116 return site; 117 } 118 119 public SiteEntry[] getSites() { 120 if (linkedConfig == null) 121 return (SiteEntry[]) sites.values().toArray(new SiteEntry[sites.size()]); 122 ArrayList combinedSites = new ArrayList (sites.values()); 123 combinedSites.addAll(linkedConfig.sites.values()); 124 return (SiteEntry[]) combinedSites.toArray(new SiteEntry[combinedSites.size()]); 125 } 126 127 public Element toXML(Document doc) throws CoreException { 128 try { 129 Element configElement = doc.createElement(CFG); 130 131 configElement.setAttribute(CFG_VERSION, VERSION); 132 configElement.setAttribute(CFG_DATE, String.valueOf(date.getTime())); 133 String transitory = isTransient() ? "true" : "false"; configElement.setAttribute(CFG_TRANSIENT, transitory); 135 136 if (linkedConfig != null) { 137 configElement.setAttribute(CFG_SHARED_URL, Utils.makeRelative(Utils.getInstallURL(), linkedConfig.getURL()).toExternalForm()); 139 } 140 141 SiteEntry[] list = (SiteEntry[]) sites.values().toArray(new SiteEntry[0]); 143 for (int i = 0; i < list.length; i++) { 144 if (linkedConfig != null && linkedConfig.getSiteEntry(list[i].getURL().toExternalForm()) != null) 145 continue; 146 Element siteElement = list[i].toXML(doc); 147 configElement.appendChild(siteElement); 148 } 149 150 return configElement; 151 152 } catch (Exception e) { 153 throw Utils.newCoreException("", e); } 155 } 156 157 public boolean isTransient() { 158 return transientConfig; 159 } 160 161 public void setTransient(boolean isTransient) { 162 this.transientConfig = isTransient; 163 } 164 165 public Date getDate() { 166 return date; 167 } 168 169 public void setDate(Date date) { 170 this.date = date; 171 } 172 173 public boolean unconfigureFeatureEntry(IPlatformConfiguration.IFeatureEntry feature) { 174 SiteEntry[] sites = getSites(); 175 for (int i=0; i<sites.length; i++) 176 if (sites[i].unconfigureFeatureEntry(feature)) 177 return true; 178 return false; 179 } 180 181 public void setLastModified(long lastModified) { 182 this.lastModified = lastModified; 183 } 184 185 public long lastModified() { 186 return (lastModified != 0) ? lastModified : date.getTime(); 187 } 188 189 194 public URL asPlatformURL(URL url) { 195 try { 196 if (url.getProtocol().equals("file")) { String rUrl = url.toExternalForm(); 198 URL pUrl = (URL)platformURLs.get(rUrl); 199 if(pUrl == null) 200 return url; 201 return pUrl; 202 } 203 return url; 204 } catch (Exception e) { 205 return url; 206 } 207 } 208 209 } 210 | Popular Tags |