KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > core > model > SiteModel


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.core.model;
12
13 import java.net.MalformedURLException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Set JavaDoc;
19 import java.util.TreeSet JavaDoc;
20
21 import org.eclipse.update.core.Site;
22 import org.eclipse.update.core.SiteFeatureReferenceModel;
23 import org.eclipse.update.internal.core.ExtendedSite;
24 import org.eclipse.update.internal.core.SiteURLFactory;
25 import org.eclipse.update.internal.core.UpdateManagerUtils;
26 import org.eclipse.update.internal.model.ConfiguredSiteModel;
27
28 /**
29  * Site model object.
30  * <p>
31  * This class may be instantiated or subclassed by clients. However, in most
32  * cases clients should instead instantiate or subclass the provided
33  * concrete implementation of this model.
34  * </p>
35  * <p>
36  * <b>Note:</b> This class/interface is part of an interim API that is still under development and expected to
37  * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
38  * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
39  * (repeatedly) as the API evolves.
40  * </p>
41  * @see org.eclipse.update.core.Site
42  * @since 2.0
43  */

44 public class SiteModel extends ModelObject {
45
46     private String JavaDoc type;
47     private URLEntryModel description;
48     private List JavaDoc /*of FeatureReferenceModel*/ featureReferences;
49     private List JavaDoc /*of ArchiveReferenceModel*/ archiveReferences;
50     private Set JavaDoc /*of CategoryModel*/ categories;
51     private List JavaDoc /* of URLEntryModel */ mirrors;
52     private String JavaDoc locationURLString;
53     private URL JavaDoc locationURL;
54     private String JavaDoc mirrorsURLString;
55     private ConfiguredSiteModel configuredSiteModel;
56
57     /**
58      * Creates an uninitialized site model object.
59      *
60      * @since 2.0
61      */

62     public SiteModel() {
63         super();
64     }
65
66     /**
67      * Returns the site type.
68      *
69      * @return site type, or <code>null</code>.
70      * @since 2.0
71      */

72     public String JavaDoc getType() {
73         return type;
74     }
75
76     /**
77      * Returns the site description.
78      *
79      * @return site description, or <code>null</code>.
80      * @since 2.0
81      */

82     public URLEntryModel getDescriptionModel() {
83         return description;
84     }
85
86     /**
87      * Returns an array of feature reference models on this site.
88      *
89      * @return an array of feature reference models, or an empty array.
90      * @since 2.0
91      */

92     public SiteFeatureReferenceModel[] getFeatureReferenceModels() {
93         if (featureReferences == null || featureReferences.size() == 0)
94             return new SiteFeatureReferenceModel[0];
95
96         return (SiteFeatureReferenceModel[]) featureReferences.toArray(arrayTypeFor(featureReferences));
97     }
98
99     /**
100      * Returns an array of plug-in and non-plug-in archive reference models
101      * on this site
102      *
103      * @return an array of archive reference models, or an empty array if there are
104      * no archives known to this site.
105      * @since 2.0
106      */

107     public ArchiveReferenceModel[] getArchiveReferenceModels() {
108         if (archiveReferences == null || archiveReferences.size() == 0)
109             return new ArchiveReferenceModel[0];
110
111         return (ArchiveReferenceModel[]) archiveReferences.toArray(arrayTypeFor(archiveReferences));
112     }
113
114     /**
115      * Returns an array of category models for this site.
116      *
117      * @return array of site category models, or an empty array.
118      * @since 2.0
119      */

120     public CategoryModel[] getCategoryModels() {
121         if (categories == null || categories.size()==0)
122             return new CategoryModel[0];
123
124         return (CategoryModel[]) categories.toArray(arrayTypeFor(categories));
125     }
126
127     /**
128      * Returns the unresolved URL string for the site.
129      *
130      * @return url string, or <code>null</code>
131      * @since 2.0
132      */

133     public String JavaDoc getLocationURLString() {
134         return locationURLString;
135     }
136
137     /**
138      * Returns the resolved URL for the site.
139      *
140      * @return url, or <code>null</code>
141      * @since 2.0
142      */

143     public URL JavaDoc getLocationURL() {
144         return locationURL;
145     }
146
147     /**
148      * Sets the site type.
149      * Throws a runtime exception if this object is marked read-only.
150      *
151      * @param type site type
152      * @since 2.0
153      */

154     public void setType(String JavaDoc type) {
155         assertIsWriteable();
156         this.type = type;
157     }
158
159     /**
160      * Sets the site description.
161      * Throws a runtime exception if this object is marked read-only.
162      *
163      * @param description site description
164      * @since 2.0
165      */

166     public void setDescriptionModel(URLEntryModel description) {
167         assertIsWriteable();
168         this.description = description;
169     }
170
171     /**
172      * Sets the feature references for this site.
173      * Throws a runtime exception if this object is marked read-only.
174      *
175      * @param featureReferences an array of feature reference models
176      * @since 2.0
177      */

178     public void setFeatureReferenceModels(FeatureReferenceModel[] featureReferences) {
179         assertIsWriteable();
180         if (featureReferences == null)
181             this.featureReferences = null;
182         else
183             this.featureReferences = new ArrayList JavaDoc(Arrays.asList(featureReferences));
184     }
185
186     /**
187      * Sets the archive references for this site.
188      * Throws a runtime exception if this object is marked read-only.
189      *
190      * @param archiveReferences an array of archive reference models
191      * @since 2.0
192      */

193     public void setArchiveReferenceModels(ArchiveReferenceModel[] archiveReferences) {
194         assertIsWriteable();
195         if (archiveReferences == null)
196             this.archiveReferences = null;
197         else
198             this.archiveReferences = new ArrayList JavaDoc(Arrays.asList(archiveReferences));
199     }
200
201     /**
202      * Sets the site categories.
203      * Throws a runtime exception if this object is marked read-only.
204      *
205      * @param categories an array of category models
206      * @since 2.0
207      */

208     public void setCategoryModels(CategoryModel[] categories) {
209         assertIsWriteable();
210         if (categories == null)
211             this.categories = null;
212         else {
213             this.categories = new TreeSet JavaDoc(CategoryModel.getComparator());
214             this.categories.addAll(Arrays.asList(categories));
215         }
216     }
217
218     /**
219      * Sets the unresolved URL for the site.
220      * Throws a runtime exception if this object is marked read-only.
221      *
222      * @param locationURLString url for the site (as a string)
223      * @since 2.0
224      */

225     public void setLocationURLString(String JavaDoc locationURLString) {
226         assertIsWriteable();
227         this.locationURLString = locationURLString;
228     }
229
230     /**
231      * Adds a feature reference model to site.
232      * Throws a runtime exception if this object is marked read-only.
233      *
234      * @param featureReference feature reference model
235      * @since 2.0
236      */

237     public void addFeatureReferenceModel(SiteFeatureReferenceModel featureReference) {
238         assertIsWriteable();
239         if (this.featureReferences == null)
240             this.featureReferences = new ArrayList JavaDoc();
241         // PERF: do not check if already present
242
//if (!this.featureReferences.contains(featureReference))
243
this.featureReferences.add(featureReference);
244     }
245
246     /**
247      * Adds an archive reference model to site.
248      * Throws a runtime exception if this object is marked read-only.
249      *
250      * @param archiveReference archive reference model
251      * @since 2.0
252      */

253     public void addArchiveReferenceModel(ArchiveReferenceModel archiveReference) {
254         assertIsWriteable();
255         if (this.archiveReferences == null)
256             this.archiveReferences = new ArrayList JavaDoc();
257         if (!this.archiveReferences.contains(archiveReference))
258             this.archiveReferences.add(archiveReference);
259     }
260
261     /**
262      * Adds a category model to site.
263      * Throws a runtime exception if this object is marked read-only.
264      *
265      * @param category category model
266      * @since 2.0
267      */

268     public void addCategoryModel(CategoryModel category) {
269         assertIsWriteable();
270         if (this.categories == null)
271             this.categories = new TreeSet JavaDoc(CategoryModel.getComparator());
272         if (!this.categories.contains(category))
273             this.categories.add(category);
274     }
275     
276     /**
277      * Adds a mirror site.
278      * Throws a runtime exception if this object is marked read-only.
279      *
280      * @param mirror mirror model
281      * @since 3.1
282      */

283     public void addMirrorModel(URLEntryModel mirror) {
284         assertIsWriteable();
285         if (this.mirrors == null)
286             this.mirrors = new ArrayList JavaDoc();
287         if (!this.mirrors.contains(mirror))
288             this.mirrors.add(mirror);
289     }
290
291     /**
292      * Removes a feature reference model from site.
293      * Throws a runtime exception if this object is marked read-only.
294      *
295      * @param featureReference feature reference model
296      * @since 2.0
297      */

298     public void removeFeatureReferenceModel(FeatureReferenceModel featureReference) {
299         assertIsWriteable();
300         if (this.featureReferences != null)
301             this.featureReferences.remove(featureReference);
302     }
303
304     /**
305      * Removes an archive reference model from site.
306      * Throws a runtime exception if this object is marked read-only.
307      *
308      * @param archiveReference archive reference model
309      * @since 2.0
310      */

311     public void removeArchiveReferenceModel(ArchiveReferenceModel archiveReference) {
312         assertIsWriteable();
313         if (this.archiveReferences != null)
314             this.archiveReferences.remove(archiveReference);
315     }
316
317     /**
318      * Removes a category model from site.
319      * Throws a runtime exception if this object is marked read-only.
320      *
321      * @param category category model
322      * @since 2.0
323      */

324     public void removeCategoryModel(CategoryModel category) {
325         assertIsWriteable();
326         if (this.categories != null)
327             this.categories.remove(category);
328     }
329
330     /**
331      * Removes a mirror from site.
332      * Throws a runtime exception if this object is marked read-only.
333      *
334      * @param mirror mirror to remove
335      * @since 3.1
336      */

337     public void removeMirror(URLEntryModel mirror) {
338         assertIsWriteable();
339         if (this.mirrors != null)
340             this.mirrors.remove(mirror);
341     }
342     
343     /**
344      * Marks the model object as read-only.
345      *
346      * @since 2.0
347      */

348     public void markReadOnly() {
349         super.markReadOnly();
350         markReferenceReadOnly(getDescriptionModel());
351         markListReferenceReadOnly(getFeatureReferenceModels());
352         markListReferenceReadOnly(getArchiveReferenceModels());
353         markListReferenceReadOnly(getCategoryModels());
354     }
355
356     /**
357      * Resolve the model object.
358      * Any URL strings in the model are resolved relative to the
359      * base URL argument. Any translatable strings in the model that are
360      * specified as translation keys are localized using the supplied
361      * resource bundle.
362      *
363      * @param base URL
364      * @param bundleURL resource bundle URL
365      * @exception MalformedURLException
366      * @since 2.0
367      */

368     public void resolve(URL JavaDoc base, URL JavaDoc bundleURL) throws MalformedURLException JavaDoc {
369
370         // Archives and feature are relative to location URL
371
// if the Site element has a URL tag: see spec
372
locationURL = resolveURL(base, bundleURL, getLocationURLString());
373         if (locationURL == null)
374             locationURL = base;
375         resolveListReference(getFeatureReferenceModels(), locationURL, bundleURL);
376         resolveListReference(getArchiveReferenceModels(), locationURL, bundleURL);
377
378         resolveReference(getDescriptionModel(), base, bundleURL);
379         resolveListReference(getCategoryModels(), base, bundleURL);
380         
381         URL JavaDoc url = resolveURL(base, bundleURL, mirrorsURLString);
382         if (url != null)
383             mirrorsURLString = url.toString();
384         
385         if ( (this instanceof ExtendedSite) && ((ExtendedSite)this).isDigestExist()) {
386             ExtendedSite extendedSite = (ExtendedSite)this;
387             extendedSite.setLiteFeatures(UpdateManagerUtils.getLightFeatures(extendedSite));
388         }
389     }
390
391     /**
392      *
393      */

394     public ConfiguredSiteModel getConfiguredSiteModel() {
395         return this.configuredSiteModel;
396     }
397
398     /**
399      *
400      */

401     public void setConfiguredSiteModel(ConfiguredSiteModel configuredSiteModel) {
402         this.configuredSiteModel = configuredSiteModel;
403     }
404
405     /**
406      * @see org.eclipse.update.core.model.ModelObject#getPropertyName()
407      */

408     protected String JavaDoc getPropertyName() {
409         return Site.SITE_FILE;
410     }
411
412     /**
413      * Return an array of updat site mirrors
414      *
415      * @return an array of mirror entries, or an empty array.
416      * @since 3.1
417      */

418     public URLEntryModel[] getMirrorSiteEntryModels() {
419         //delayedResolve(); no delay;
420
if ( mirrors == null || mirrors.size() == 0)
421             // see if we can get mirrors from the provided url
422
if (mirrorsURLString != null)
423                 doSetMirrorSiteEntryModels(DefaultSiteParser.getMirrors(mirrorsURLString, new SiteURLFactory()));
424                 
425         if (mirrors == null || mirrors.size() == 0)
426             return new URLEntryModel[0];
427         else
428             return (URLEntryModel[]) mirrors.toArray(arrayTypeFor(mirrors));
429     }
430     
431     /**
432      * Sets additional mirror sites
433      * Throws a runtime exception if this object is marked read-only.
434      *
435      * @param mirrors additional update site mirrors
436      * @since 3.1
437      */

438     public void setMirrorSiteEntryModels(URLEntryModel[] mirrors) {
439         assertIsWriteable();
440         doSetMirrorSiteEntryModels(mirrors);
441     }
442     
443     private void doSetMirrorSiteEntryModels(URLEntryModel[] mirrors) {
444         if (mirrors == null || mirrors.length == 0)
445             this.mirrors = null;
446         else
447             this.mirrors = new ArrayList JavaDoc(Arrays.asList(mirrors));
448     }
449     
450     /**
451      * Sets the mirrors url. Mirror sites will then be obtained from this mirror url later.
452      * This method is complementary to setMirrorsiteEntryModels(), and only one of these
453      * methods should be called.
454      * Throws a runtime exception if this object is marked read-only.
455      *
456      * @param mirrorsURL additional update site mirrors
457      * @since 3.1
458      */

459     public void setMirrorsURLString(String JavaDoc mirrorsURL) {
460         assertIsWriteable();
461         this.mirrorsURLString = mirrorsURL;
462     }
463 }
464
Popular Tags