KickJava   Java API By Example, From Geeks To Geeks.

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


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
13 import java.io.*;
14 import java.net.*;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Date JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.update.configurator.*;
22 import org.eclipse.update.core.*;
23 import org.eclipse.update.core.model.*;
24 import org.eclipse.update.internal.core.*;
25
26 /**
27  * An InstallConfigurationModel is
28  *
29  */

30
31 public class InstallConfigurationModel extends ModelObject {
32
33     // performance
34
private URL bundleURL;
35     private URL base;
36     private boolean resolved = false;
37
38     private boolean isCurrent = false;
39     private URL locationURL;
40     private String JavaDoc locationURLString;
41     protected Date JavaDoc date;
42     private String JavaDoc label;
43     private List JavaDoc /* of ConfiguretionActivityModel */ activities;
44     private List JavaDoc /* of configurationSiteModel */ configurationSites;
45
46     protected boolean initialized = false;
47     protected boolean lightlyInitialized = false;
48
49     /**
50      * default constructor. Create
51      */

52     public InstallConfigurationModel() {
53     }
54
55     /**
56      * @since 2.0
57      */

58     public ConfiguredSiteModel[] getConfigurationSitesModel() {
59         if (!initialized)
60             initialize();
61         if (configurationSites == null || configurationSites.size() == 0)
62             return new ConfiguredSiteModel[0];
63     
64         return (ConfiguredSiteModel[]) configurationSites.toArray(arrayTypeFor(configurationSites));
65     }
66
67     /**
68      * Adds the configuration to the list
69      * is called when adding a Site or parsing the XML file
70      * in this case we do not want to create a new activity, so we do not want t call
71      * addConfigurationSite()
72      */

73     public void addConfigurationSiteModel(ConfiguredSiteModel site) {
74         if (configurationSites == null) {
75             configurationSites = new ArrayList JavaDoc();
76         }
77         if (!configurationSites.contains(site)) {
78             configurationSites.add(site);
79         }
80     }
81
82     public void setConfigurationSiteModel(ConfiguredSiteModel[] sites) {
83         configurationSites = null;
84         for (int i = 0; i < sites.length; i++) {
85             addConfigurationSiteModel(sites[i]);
86         }
87     }
88
89     /**
90      * @since 2.0
91      */

92     public boolean removeConfigurationSiteModel(ConfiguredSiteModel site) {
93         if (!initialized) initialize();
94
95         if (configurationSites != null) {
96             return configurationSites.remove(site);
97         }
98
99         return false;
100     }
101
102     /**
103      * @since 2.0
104      */

105     public boolean isCurrent() {
106         if (!lightlyInitialized && !initialized )
107             doLightInitialization();
108         
109         return isCurrent;
110     }
111
112     /**
113      * @since 2.0
114      */

115     public void setCurrent(boolean isCurrent) {
116         // do not check if writable as we may
117
// set an install config as Not current
118
this.isCurrent = isCurrent;
119     }
120
121     /**
122      * @since 2.0
123      */

124     public ConfigurationActivityModel[] getActivityModel() {
125         if (activities == null && !initialized)
126             initialize();
127         if (activities == null || activities.size() == 0)
128             return new ConfigurationActivityModel[0];
129         return (ConfigurationActivityModel[]) activities.toArray(arrayTypeFor(activities));
130     }
131
132     /**
133      * @since 2.0
134      */

135     public void addActivityModel(ConfigurationActivityModel activity) {
136         if (activities == null)
137             activities = new ArrayList JavaDoc();
138         if (!activities.contains(activity)) {
139             activities.add(activity);
140             activity.setInstallConfigurationModel(this);
141         }
142     }
143     /**
144      *
145      */

146     public Date JavaDoc getCreationDate() {
147 // if (!initialized) initialize();
148
if (date == null)
149             doLightInitialization();
150         return date;
151     }
152     /**
153      * Sets the date.
154      * @param date The date to set
155      */

156     public void setCreationDate(Date JavaDoc date) {
157         assertIsWriteable();
158         this.date = date;
159     }
160     /**
161      * @since 2.0
162      */

163     public URL getURL() {
164         //if (!initialized) initialize();
165
//no need to initialize, always set
166
delayedResolve();
167         return locationURL;
168     }
169
170     /**
171      * @since 2.0
172      */

173     public String JavaDoc getLabel() {
174 // if (!initialized) initialize();
175
if (label == null)
176             doLightInitialization();
177         return label;
178     }
179
180     /**
181      * @since 2.0.2
182      */

183
184     public String JavaDoc toString() {
185         return getLabel();
186     }
187
188     /**
189      * Sets the label.
190      * @param label The label to set
191      */

192     public void setLabel(String JavaDoc label) {
193         assertIsWriteable();
194         this.label = label;
195     }
196
197     /**
198      * Gets the locationURLString.
199      * @return Returns a String
200      */

201     public String JavaDoc getLocationURLString() {
202         if (!initialized) delayedResolve();
203         return locationURLString;
204     }
205
206     /**
207      * Sets the locationURLString.
208      * @param locationURLString The locationURLString to set
209      */

210     public void setLocationURLString(String JavaDoc locationURLString) {
211         assertIsWriteable();
212         this.locationURLString = locationURLString;
213         this.locationURL = null;
214     }
215
216     /*
217      * @see ModelObject#resolve(URL, ResourceBundle)
218      */

219     public void resolve(URL base, URL bundleURL) throws MalformedURLException {
220
221         this.base = base;
222         this.bundleURL = bundleURL;
223
224     }
225
226     /**
227      * Returns the timeline.
228      * @return long
229      */

230     public long getTimeline() {
231         return 0;
232 // if (!initialized) initialize();
233
// return timeline;
234
}
235
236
237     /*
238      * initialize the configurations from the persistent model.
239      */

240     private void initialize() {
241         
242         try {
243             try {
244                 IPlatformConfiguration platformConfig = getPlatformConfiguration();
245                 
246                 new InstallConfigurationParser(platformConfig, this, false);
247             } catch (FileNotFoundException exception) {
248                 UpdateCore.warn(locationURLString + " does not exist, The local site is not in synch with the file system and is pointing to a file that doesn't exist.", exception); //$NON-NLS-1$
249
throw Utilities.newCoreException(NLS.bind(Messages.InstallConfiguration_ErrorDuringFileAccess, (new String JavaDoc[] { locationURLString })), exception);
250             } catch (IOException exception) {
251                 throw Utilities.newCoreException(NLS.bind(Messages.InstallConfiguration_ErrorDuringFileAccess, (new String JavaDoc[] { locationURLString })), exception);
252             }
253             
254         } catch (CoreException e) {
255             UpdateCore.warn("Error processing configuration history:" + locationURL.toExternalForm(), e); //$NON-NLS-1$
256
} finally {
257             initialized = true;
258         }
259         
260         //finish resolve
261
// PERF:
262
try {
263             // delegate
264
resolveListReference(getActivityModel(), base, bundleURL);
265             resolveListReference(getConfigurationSitesModel(), base, bundleURL);
266         } catch (MalformedURLException e){}
267     }
268
269     private IPlatformConfiguration getPlatformConfiguration() throws IOException {
270         IPlatformConfiguration platformConfig;
271         if (UpdateManagerUtils.sameURL(getURL(), ConfiguratorUtils.getCurrentPlatformConfiguration().getConfigurationLocation()))
272             platformConfig = ConfiguratorUtils.getCurrentPlatformConfiguration();
273         else
274             platformConfig = ConfiguratorUtils.getPlatformConfiguration(getURL());
275         return platformConfig;
276     }
277     
278     private void doLightInitialization() {
279         try {
280             try {
281                 IPlatformConfiguration platformConfig = getPlatformConfiguration();
282             
283                 new InstallConfigurationParser(platformConfig, this, true);
284             } catch (FileNotFoundException exception) {
285                 UpdateCore.warn(locationURLString + " does not exist, The local site is not in synch with the file system and is pointing to a file that doesn't exist.", exception); //$NON-NLS-1$
286
throw Utilities.newCoreException(NLS.bind(Messages.InstallConfiguration_ErrorDuringFileAccess, (new String JavaDoc[] { locationURLString })), exception);
287             } catch (IOException exception) {
288                 throw Utilities.newCoreException(NLS.bind(Messages.InstallConfiguration_ErrorDuringFileAccess, (new String JavaDoc[] { locationURLString })), exception);
289             }
290         } catch (CoreException e) {
291             UpdateCore.warn("Error processing configuration history:" + locationURL.toExternalForm(), e); //$NON-NLS-1$
292
} finally {
293             lightlyInitialized = true;
294         }
295     }
296
297     /*
298      *
299      */

300     private void delayedResolve() {
301
302         // PERF: delay resolution
303
if (resolved)
304             return;
305
306         resolved = true;
307         // resolve local elements
308
try {
309             //locationURL = resolveURL(base, bundleURL, locationURLString);
310
locationURL = new URL(locationURLString);
311         } catch (MalformedURLException e){
312             File f = new File(locationURLString);
313             try {
314                 if (f.exists())
315                     locationURL = f.toURL();
316                 else
317                     locationURL = base;
318             } catch (MalformedURLException e1) {
319                 locationURL = base;
320             }
321         }
322     }
323     
324     public void resetActivities() {
325         activities = null;
326     }
327 }
328
Popular Tags