KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > model > SiteLocalModel


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 package org.eclipse.update.internal.model;
12 import java.io.File JavaDoc;
13 import java.net.MalformedURLException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Date JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.MissingResourceException JavaDoc;
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 /**
28  * This class manages the configurations.
29  */

30
31 public class SiteLocalModel extends ModelObject {
32     public static final String JavaDoc CONFIG_FILE = "platform.xml"; //$NON-NLS-1$
33
private long stamp;
34     private String JavaDoc label;
35     private URL JavaDoc location;
36     private String JavaDoc locationURLString;
37     private int history = UpdateCore.DEFAULT_HISTORY;
38     private List JavaDoc /* of InstallConfigurationModel */configurations;
39     private List JavaDoc /* of InstallConfigurationModel */preservedConfigurations;
40     private InstallConfigurationModel currentConfiguration;
41
42     /**
43      * Constructor for LocalSite
44      */

45     public SiteLocalModel(){
46         super();
47     }
48
49     /**
50      * @since 2.0
51      */

52     public InstallConfigurationModel getCurrentConfigurationModel() {
53         return currentConfiguration;
54     }
55
56     /**
57      * @since 2.0
58      */

59     public InstallConfigurationModel[] getConfigurationHistoryModel() {
60         if (configurations==null)
61             // initialize history
62
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     /**
71      * adds a new configuration to the LocalSite
72      * the newly added configuration is teh current one
73      */

74     public void addConfigurationModel(InstallConfigurationModel config) {
75         if (config != null) {
76             if (configurations == null)
77                 configurations = new ArrayList JavaDoc();
78             if (!configurations.contains(config))
79                 configurations.add(config);
80         }
81     }
82
83     /**
84      * adds a new configuration to the LocalSite
85      * the newly added configuration is teh current one
86      */

87     public boolean removeConfigurationModel(InstallConfigurationModel config) {
88         if (config != null) {
89             return configurations.remove(config);
90         }
91         return false;
92     }
93     /**
94      * Gets the location of the local site.
95      * @return Returns a URL
96      */

97     public URL JavaDoc getLocationURL() {
98         return location;
99     }
100
101     /**
102      * Gets the locationURLString.
103      * @return Returns a String
104      */

105     public String JavaDoc getLocationURLString() {
106         return locationURLString;
107     }
108
109
110     /**
111      * Sets the locationURLString.
112      * @param locationURLString The locationURLString to set
113      */

114     public void setLocationURLString(String JavaDoc locationURLString) {
115         assertIsWriteable();
116         this.locationURLString = locationURLString;
117         this.location=null;
118     }
119
120
121     /**
122      * @since 2.0
123      */

124     public String JavaDoc getLabel() {
125         return label;
126     }
127
128     /**
129      * Sets the label.
130      * @param label The label to set
131      */

132     public void setLabel(String JavaDoc label) {
133         assertIsWriteable();
134         this.label = label;
135     }
136
137     
138     /**
139      * @since 2.0
140      */

141     public int getMaximumHistoryCount() {
142         return history;
143     }
144
145     /**
146      * @since 2.0
147      */

148     public void setMaximumHistoryCount(int history) {
149         assertIsWriteable();
150         this.history = history;
151     }
152
153     
154     /**
155      * Adds a preserved configuration into teh collection
156      * do not save the configuration
157      * @since 2.0
158      */

159     public void addPreservedInstallConfigurationModel(InstallConfigurationModel configuration) {
160         if (preservedConfigurations == null)
161             preservedConfigurations = new ArrayList JavaDoc();
162
163         preservedConfigurations.add(configuration);
164     }
165
166     /**
167      * @since 2.0
168      */

169     public boolean removePreservedConfigurationModel(InstallConfigurationModel configuration) {
170         if (preservedConfigurations != null) {
171             return preservedConfigurations.remove(configuration);
172         }
173         return false;
174     }
175
176     /**
177      * @since 2.0
178      */

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     /**
187      * Sets the currentConfiguration.
188      * @param currentConfiguration The currentConfiguration to set
189      */

190     public void setCurrentConfigurationModel(InstallConfigurationModel currentConfiguration) {
191         assertIsWriteable();
192         this.currentConfiguration = currentConfiguration;
193         
194         //2.0.2 set the configuredSite of sites
195
ConfiguredSiteModel[] confSites = currentConfiguration.getConfigurationSitesModel();
196         for (int i = 0; i < confSites.length; i++) {
197             confSites[i].getSiteModel().setConfiguredSiteModel(confSites[i]);
198         }
199     }
200
201     /*
202      * @see ModelObject#resolve(URL)
203      */

204     public void resolve(URL JavaDoc base,URL JavaDoc bundleURL) throws MalformedURLException JavaDoc {
205         // local
206
location = resolveURL(base,bundleURL,getLocationURLString());
207         
208         // delegate
209
resolveListReference(getConfigurationHistoryModel(),base,bundleURL);
210         resolveListReference(getPreservedConfigurationsModel(),base,bundleURL);
211         resolveReference(getCurrentConfigurationModel(),base,bundleURL);
212     }
213     
214
215     /**
216      * Gets the stamp.
217      * @return Returns a long
218      */

219     public long getStamp() {
220         return stamp;
221     }
222
223     /**
224      * Sets the stamp.
225      * @param stamp The stamp to set
226      */

227     public void setStamp(long stamp) {
228         this.stamp = stamp;
229     }
230
231     /**
232      * @see org.eclipse.update.core.model.ModelObject#getPropertyName()
233      */

234     protected String JavaDoc getPropertyName() {
235         return "platform"; //$NON-NLS-1$
236
}
237
238     /*
239      * reads the configuration/history directory
240      */

241     private void processHistory() {
242         try {
243             URL JavaDoc historyURL = new URL JavaDoc(getLocationURL(), "history"); //$NON-NLS-1$
244
historyURL = FileLocator.toFileURL(historyURL);
245             File JavaDoc historyDir = new File JavaDoc(historyURL.getFile());
246             if (historyDir.exists()) {
247                 File JavaDoc[] backedConfigs = historyDir.listFiles();
248                 BaseSiteLocalFactory factory = new BaseSiteLocalFactory();
249                 for (int i=0; i<backedConfigs.length; i++) {
250                     String JavaDoc name = backedConfigs[i].getName();
251                     if (name.endsWith(".xml")) //$NON-NLS-1$
252
name = name.substring(0, name.length()-4);
253                     else
254                         continue;
255                     Date JavaDoc date = new Date JavaDoc(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                     // add the config
263
addConfigurationModel(config);
264                 }
265             }
266         } catch (Exception JavaDoc e) {
267             UpdateCore.warn("Error processing history: ", e); //$NON-NLS-1$
268
}
269     }
270
271     /**
272      * return the appropriate resource bundle for this sitelocal
273      */

274     URL JavaDoc getResourceBundleURL() throws CoreException {
275         URL JavaDoc url = null;
276         try {
277             url = UpdateManagerUtils.asDirectoryURL(getLocationURL());
278         } catch (MissingResourceException JavaDoc e) {
279             UpdateCore.warn(e.getLocalizedMessage() + ":" + url.toExternalForm()); //$NON-NLS-1$
280
} catch (MalformedURLException JavaDoc e) {
281             UpdateCore.warn(e.getLocalizedMessage());
282         }
283         return url;
284     }
285
286 }
287
Popular Tags