KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > configurator > Configuration


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  * James D Miles (IBM Corp.) - bug 176250, Configurator needs to handle more platform urls
11  *******************************************************************************/

12 package org.eclipse.update.internal.configurator;
13
14 import java.io.IOException JavaDoc;
15 import java.net.*;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Date JavaDoc;
18 import java.util.HashMap JavaDoc;
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 JavaDoc sites = new HashMap JavaDoc();
27     private HashMap JavaDoc platformURLs = new HashMap JavaDoc();
28     private Date JavaDoc date;
29     private long lastModified; // needed to account for file system limitations
30
private URL url;
31     private boolean transientConfig;
32     private boolean isDirty;
33     private Configuration linkedConfig; // shared configuration
34

35     public Configuration() {
36         this(new Date JavaDoc());
37         // the config is created now or out of a platform.xml without a date
38
isDirty = true;
39     }
40     public Configuration(Date JavaDoc 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         // make all the sites read-only
55
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     /**
65      * @return true if the config needs to be saved
66      */

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 JavaDoc url, SiteEntry site) {
76         url = Utils.canonicalizeURL(url);
77         // only add the same site once
78
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:")){//$NON-NLS-1$
82
URL pURL;
83                 try {
84                     pURL = new URL(url);
85                     URL rURL = PlatformConfiguration.resolvePlatformURL(pURL);
86                     String JavaDoc resolvedURL = rURL.toExternalForm();
87                     platformURLs.put(resolvedURL, pURL);
88                 } catch (IOException JavaDoc e) {
89                     // can't resolve so can't have look up.
90
}
91             }
92         }
93     }
94     
95     public void removeSiteEntry(String JavaDoc url) {
96         url =Utils.canonicalizeURL(url);
97         sites.remove(url);
98         if(url.startsWith("platform:")){ //$NON-NLS-1$
99
URL pURL;
100             try {
101                 pURL = new URL(url);
102                 URL rURL = PlatformConfiguration.resolvePlatformURL(pURL);
103                 String JavaDoc resolvedURL = rURL.toExternalForm();
104                 platformURLs.remove(resolvedURL);
105             } catch (IOException JavaDoc e) {
106                 // can't resolve so can't have look up.
107
}
108         }
109     }
110     
111     public SiteEntry getSiteEntry(String JavaDoc 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 JavaDoc combinedSites = new ArrayList JavaDoc(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 JavaDoc transitory = isTransient() ? "true" : "false"; //$NON-NLS-1$ //$NON-NLS-2$
134
configElement.setAttribute(CFG_TRANSIENT, transitory);
135                         
136             if (linkedConfig != null) {
137                 // make externalized URL install relative
138
configElement.setAttribute(CFG_SHARED_URL, Utils.makeRelative(Utils.getInstallURL(), linkedConfig.getURL()).toExternalForm());
139             }
140
141             // collect site entries
142
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 JavaDoc e) {
153             throw Utils.newCoreException("", e); //$NON-NLS-1$
154
}
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 JavaDoc getDate() {
166         return date;
167     }
168     
169     public void setDate(Date JavaDoc 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     /**
190      * Returns the url as a platform:/ url, if possible, else leaves it unchanged
191      * @param url
192      * @return
193      */

194     public URL asPlatformURL(URL url) {
195         try {
196             if (url.getProtocol().equals("file")) {//$NON-NLS-1$
197
String JavaDoc 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 JavaDoc e) {
205             return url;
206         }
207     }
208     
209 }
210
Popular Tags