KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > core > Site


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 191783, NullPointerException in FeatureDownloader
11  *******************************************************************************/

12 package org.eclipse.update.core;
13
14 import java.net.*;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import org.eclipse.core.runtime.*;
24 import org.eclipse.osgi.util.NLS;
25 import org.eclipse.update.configuration.*;
26 import org.eclipse.update.core.model.*;
27 import org.eclipse.update.internal.core.*;
28
29 /**
30  * Convenience implementation of a site.
31  * <p>
32  * This class may be instantiated or subclassed by clients.
33  * </p>
34  * <p>
35  * <b>Note:</b> This class/interface is part of an interim API that is still under development and expected to
36  * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
37  * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
38  * (repeatedly) as the API evolves.
39  * </p>
40  * @see org.eclipse.update.core.ISite
41  * @see org.eclipse.update.core.model.SiteModel
42  * @since 2.0
43  */

44 public class Site extends SiteModel implements ISiteWithMirrors {
45
46     /**
47      * Default installation path for features
48      *
49      * @since 2.0
50      */

51     public static final String JavaDoc DEFAULT_INSTALLED_FEATURE_PATH = "features/"; //$NON-NLS-1$
52

53     /**
54      * Default installation path for plug-ins and plug-in fragments
55      *
56      * @since 2.0
57      */

58     public static final String JavaDoc DEFAULT_PLUGIN_PATH = "plugins/"; //$NON-NLS-1$
59

60     /**
61      * Default path on a site where packaged features are located
62      *
63      * @since 2.0
64      */

65     public static final String JavaDoc DEFAULT_FEATURE_PATH = "features/"; //$NON-NLS-1$
66

67     /**
68      * Default site manifest file name
69      *
70      * @since 2.0
71      */

72     public static final String JavaDoc SITE_FILE = "site"; //$NON-NLS-1$
73

74     /**
75      * Default site manifest extension
76      *
77      * @since 2.0
78      */

79     public static final String JavaDoc SITE_XML = SITE_FILE + ".xml"; //$NON-NLS-1$
80

81     private ISiteContentProvider siteContentProvider;
82     
83     private Map JavaDoc featureCache = Collections.synchronizedMap(new HashMap JavaDoc()); // key=URLKey value=IFeature
84

85     /**
86      * Constructor for Site
87      */

88     public Site() {
89         super();
90     }
91
92     /**
93      * Compares two sites for equality
94      *
95      * @param obj site object to compare with
96      * @return <code>true</code> if the two sites are equal,
97      * <code>false</code> otherwise
98      * @since 2.0
99      */

100     public boolean equals(Object JavaDoc obj) {
101         if (!(obj instanceof ISite))
102             return false;
103         if (getURL() == null)
104             return false;
105         ISite otherSite = (ISite) obj;
106
107         return UpdateManagerUtils.sameURL(getURL(), otherSite.getURL());
108     }
109
110     /**
111      * Returns the site URL
112      *
113      * @see ISite#getURL()
114      * @since 2.0
115      */

116     public URL getURL() {
117         URL url = null;
118         try {
119             url = getSiteContentProvider().getURL();
120         } catch (CoreException e) {
121             UpdateCore.warn(null, e);
122         }
123         return url;
124     }
125
126     /**
127      * Returns the site description.
128      *
129      * @see ISite#getDescription()
130      * @since 2.0
131      */

132     public IURLEntry getDescription() {
133         return (IURLEntry) getDescriptionModel();
134     }
135
136     /**
137      * Returns an array of categories defined by the site.
138      *
139      * @see ISite#getCategories()
140      * @since 2.0
141      */

142     public ICategory[] getCategories() {
143         CategoryModel[] result = getCategoryModels();
144         if (result.length == 0)
145             return new ICategory[0];
146         else
147             return (ICategory[]) result;
148     }
149
150     /**
151      * Returns the named site category.
152      *
153      * @see ISite#getCategory(String)
154      * @since 2.0
155      */

156     public ICategory getCategory(String JavaDoc key) {
157         ICategory result = null;
158         boolean found = false;
159         int length = getCategoryModels().length;
160
161         for (int i = 0; i < length; i++) {
162             if (getCategoryModels()[i].getName().equals(key)) {
163                 result = (ICategory) getCategoryModels()[i];
164                 found = true;
165                 break;
166             }
167         }
168
169         //DEBUG:
170
if (!found) {
171             String JavaDoc URLString = (this.getURL() != null) ? this.getURL().toExternalForm() : "<no site url>"; //$NON-NLS-1$
172
UpdateCore.warn(NLS.bind(Messages.Site_CannotFindCategory, (new String JavaDoc[] { key, URLString })));
173             if (getCategoryModels().length <= 0)
174                 UpdateCore.warn(Messages.Site_NoCategories);
175         }
176
177         return result;
178     }
179
180     /**
181      * Returns an array of references to features on this site.
182      *
183      * @see ISite#getFeatureReferences()
184      * @since 2.0
185      */

186     public ISiteFeatureReference[] getRawFeatureReferences() {
187         FeatureReferenceModel[] result = getFeatureReferenceModels();
188         if (result.length == 0)
189             return new ISiteFeatureReference[0];
190         else
191             return (ISiteFeatureReference[]) result;
192     }
193
194     /**
195      * @see org.eclipse.update.core.ISite#getFeatureReferences()
196      */

197     public ISiteFeatureReference[] getFeatureReferences() {
198         // only filter local site
199
if (getCurrentConfiguredSite()!=null)
200             return filterFeatures(getRawFeatureReferences());
201         else
202             return getRawFeatureReferences();
203         
204     }
205
206     /*
207      * Method filterFeatures.
208      * Also implemented in Feature
209      *
210      * @param list
211      * @return List
212      */

213     private ISiteFeatureReference[] filterFeatures(ISiteFeatureReference[] allIncluded) {
214         List JavaDoc list = new ArrayList JavaDoc();
215         if (allIncluded!=null){
216             for (int i = 0; i < allIncluded.length; i++) {
217                 ISiteFeatureReference included = allIncluded[i];
218                 if (UpdateManagerUtils.isValidEnvironment(included))
219                     list.add(included);
220                 else{
221                     if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_WARNINGS){
222                         UpdateCore.warn("Filtered out feature reference:"+included); //$NON-NLS-1$
223
}
224                 }
225             }
226         }
227         
228         ISiteFeatureReference[] result = new ISiteFeatureReference[list.size()];
229         if (!list.isEmpty()){
230             list.toArray(result);
231         }
232         
233         return result;
234     }
235
236     /**
237      * Returns a reference to the specified feature on this site.
238      *
239      * @see ISite#getFeatureReference(IFeature)
240      * @since 2.0
241      */

242     public ISiteFeatureReference getFeatureReference(IFeature feature) {
243
244         if (feature == null) {
245             UpdateCore.warn("Site:getFeatureReference: The feature is null"); //$NON-NLS-1$
246
return null;
247         }
248
249         ISiteFeatureReference[] references = getFeatureReferences();
250         ISiteFeatureReference currentReference = null;
251         for (int i = 0; i < references.length; i++) {
252             currentReference = references[i];
253             //if (UpdateManagerUtils.sameURL(feature.getURL(), currentReference.getURL()))
254
// return currentReference;
255
try {
256                 if (feature.getVersionedIdentifier().equals(currentReference.getVersionedIdentifier()))
257                     return currentReference;
258             } catch (CoreException e) {
259                 // TODO Auto-generated catch block
260
e.printStackTrace();
261             }
262         }
263
264         UpdateCore.warn("Feature " + feature + " not found on site" + this.getURL()); //$NON-NLS-1$ //$NON-NLS-2$
265
return null;
266     }
267
268     /**
269      * Returns an array of plug-in and non-plug-in archives located
270      * on this site
271      *
272      * @see ISite#getArchives()
273      * @since 2.0
274      */

275     public IArchiveReference[] getArchives() {
276         ArchiveReferenceModel[] result = getArchiveReferenceModels();
277         if (result.length == 0)
278             return new IArchiveReference[0];
279         else
280             return (IArchiveReference[]) result;
281     }
282
283     /**
284      * Returns the content provider for this site.
285      *
286      * @see ISite#getSiteContentProvider()
287      * @since 2.0
288      */

289     public ISiteContentProvider getSiteContentProvider() throws CoreException {
290         if (siteContentProvider == null) {
291             throw Utilities.newCoreException(Messages.Site_NoContentProvider, null);
292         }
293         return siteContentProvider;
294     }
295
296     /**
297      * Returns the default type for a packaged feature supported by this site
298      *
299      * @see ISite#getDefaultPackagedFeatureType()
300      * @since 2.0
301      */

302     public String JavaDoc getDefaultPackagedFeatureType() {
303         return DEFAULT_PACKAGED_FEATURE_TYPE;
304     }
305
306     /**
307      * Returns an array of entries corresponding to plug-ins installed
308      * on this site.
309      *
310      * @see ISite#getPluginEntries()
311      * @since 2.0
312      */

313     public IPluginEntry[] getPluginEntries() {
314         throw new UnsupportedOperationException JavaDoc();
315     }
316
317     /**
318      * Returns the number of plug-ins installed on this site
319      *
320      * @see ISite#getPluginEntryCount()
321      * @since 2.0
322      */

323     public int getPluginEntryCount() {
324         throw new UnsupportedOperationException JavaDoc();
325     }
326
327     /**
328      * Returns an array of entries corresponding to plug-ins that are
329      * installed on this site and are referenced only by the specified
330      * feature.
331      *
332      * @see ISite#getPluginEntriesOnlyReferencedBy(IFeature) *
333      * @since 2.0
334      */

335     public IPluginEntry[] getPluginEntriesOnlyReferencedBy(IFeature feature) throws CoreException {
336
337         IPluginEntry[] pluginsToRemove = new IPluginEntry[0];
338         if (feature == null)
339             return pluginsToRemove;
340
341         // get the plugins from the feature
342
IPluginEntry[] entries = feature.getPluginEntries();
343         if (entries != null) {
344             // get all the other plugins from all the other features
345
Set JavaDoc allPluginID = new HashSet JavaDoc();
346             ISiteFeatureReference[] features = getFeatureReferences();
347             if (features != null) {
348                 for (int indexFeatures = 0; indexFeatures < features.length; indexFeatures++) {
349                     IFeature featureToCompare = null;
350                     try {
351                         featureToCompare = features[indexFeatures].getFeature(null);
352                     } catch (CoreException e) {
353                         UpdateCore.warn(null, e);
354                     }
355                     if (!feature.equals(featureToCompare)) {
356                         IPluginEntry[] pluginEntries = features[indexFeatures].getFeature(null).getPluginEntries();
357                         if (pluginEntries != null) {
358                             for (int indexEntries = 0; indexEntries < pluginEntries.length; indexEntries++) {
359                                 allPluginID.add(pluginEntries[indexEntries].getVersionedIdentifier());
360                             }
361                         }
362                     }
363                 }
364             }
365
366             // create the delta with the plugins that may be still used by other configured or unconfigured feature
367
List JavaDoc plugins = new ArrayList JavaDoc();
368             for (int indexPlugins = 0; indexPlugins < entries.length; indexPlugins++) {
369                 if (!allPluginID.contains(entries[indexPlugins].getVersionedIdentifier())) {
370                     plugins.add(entries[indexPlugins]);
371                 }
372             }
373
374             // move List into Array
375
if (!plugins.isEmpty()) {
376                 pluginsToRemove = new IPluginEntry[plugins.size()];
377                 plugins.toArray(pluginsToRemove);
378             }
379         }
380
381         return pluginsToRemove;
382     }
383
384     /**
385      * Adds a new plug-in entry to this site.
386      * This implementation always throws UnsupportedOperationException
387      * because this implementation does not support the install action.
388      *
389      * @see ISite#addPluginEntry(IPluginEntry)
390      * @exception java.lang.UnsupportedOperationException
391      * @since 2.0
392      */

393     public void addPluginEntry(IPluginEntry pluginEntry) {
394         throw new UnsupportedOperationException JavaDoc();
395     }
396
397     /**
398      * Get download size for the specified feature on this site.
399      * This implementation always throws UnsupportedOperationException
400      * because this implementation does not support the install action.
401      *
402      * @see ISite#getDownloadSizeFor(IFeature)
403      * @exception java.lang.UnsupportedOperationException
404      * @since 2.0
405      */

406     public long getDownloadSizeFor(IFeature feature) {
407         throw new UnsupportedOperationException JavaDoc();
408     }
409
410     /**
411      * Get install size for the specified feature on this site.
412      * This implementation always throws UnsupportedOperationException
413      * because this implementation does not support the install action.
414      *
415      * @see ISite#getInstallSizeFor(IFeature)
416      * @exception java.lang.UnsupportedOperationException
417      * @since 2.0
418      */

419     public long getInstallSizeFor(IFeature feature) {
420         throw new UnsupportedOperationException JavaDoc();
421     }
422
423     /**
424      * Install the specified feature and all optional features on this site.
425      * This implementation always throws UnsupportedOperationException
426      * because this implementation does not support the install action.
427      *
428      * @see ISite#install(IFeature, IVerificationListener, IProgressMonitor)
429      * @exception InstallAbortedException when the user cancels the install
430      * @exception CoreException
431      * @exception java.lang.UnsupportedOperationException
432      * @since 2.0
433      */

434     public IFeatureReference install(IFeature sourceFeature, IVerificationListener verificationListener, IProgressMonitor progress) throws InstallAbortedException, CoreException {
435         throw new UnsupportedOperationException JavaDoc();
436     }
437
438     /**
439      * Install the specified feature and listed optional features on this site.
440      * This implementation always throws UnsupportedOperationException
441      * because this implementation does not support the install action.
442      *
443      * @see ISite#install(IFeature, IVerificationListener, IProgressMonitor)
444      * @exception InstallAbortedException when the user cancels the install
445      * @exception CoreException
446      * @exception java.lang.UnsupportedOperationException
447      * @since 2.0
448      */

449     public IFeatureReference install(IFeature sourceFeature, IFeatureReference[] optionalFeatures, IVerificationListener verificationListener, IProgressMonitor progress) throws InstallAbortedException, CoreException {
450         throw new UnsupportedOperationException JavaDoc();
451     }
452
453     /**
454      * Install the specified feature on this site using the content consumer as
455      * a context to install the feature in.
456      * This implementation always throws UnsupportedOperationException
457      * because this implementation does not support the install action.
458      *
459      * @param sourceFeature feature to install
460      * @param parentContentConsumer content consumer of the parent feature
461      * @param parentVerifier verifier of the parent feature
462      * @param verificationListener install verification listener
463      * @param progress install monitor, can be <code>null</code>
464      * @exception InstallAbortedException when the user cancels the install
465      * @exception CoreException
466      * @exception java.lang.UnsupportedOperationException
467      * @since 2.0
468      */

469     public IFeatureReference install(IFeature sourceFeature, IFeatureReference[] optionalFeatures, IFeatureContentConsumer parentContentConsumer, IVerifier parentVerifier, IVerificationListener verificationListener, IProgressMonitor progress)
470         throws CoreException {
471         throw new UnsupportedOperationException JavaDoc();
472     }
473
474     /**
475      * Remove (uninstall) the specified feature from this site.
476      * This implementation always throws UnsupportedOperationException
477      * because this implementation does not support the remove action.
478      *
479      * @see ISite#remove(IFeature, IProgressMonitor)
480      * @exception java.lang.UnsupportedOperationException
481      * @since 2.0
482      */

483     public void remove(IFeature feature, IProgressMonitor progress) throws CoreException {
484         throw new UnsupportedOperationException JavaDoc();
485     }
486
487     /**
488      * Sets the site content provider.
489      *
490      * @see ISite#setSiteContentProvider(ISiteContentProvider)
491      * @since 2.0
492      */

493     public void setSiteContentProvider(ISiteContentProvider siteContentProvider) {
494         this.siteContentProvider = siteContentProvider;
495     }
496     /**
497      * @see ISite#getCurrentConfiguredSite()
498      */

499     public IConfiguredSite getCurrentConfiguredSite() {
500         return (IConfiguredSite) getConfiguredSiteModel();
501     }
502
503     /**
504      * @see org.eclipse.update.core.ISite#createFeature(String, URL)
505      * @deprecated
506      */

507     public IFeature createFeature(String JavaDoc type, URL url) throws CoreException {
508         return createFeature(type,url,null);
509     }
510
511     /**
512      * @see org.eclipse.update.core.ISite#createFeature(String, URL,
513      * IProgressMonitor)
514      */

515     public IFeature createFeature(String JavaDoc type, URL url, IProgressMonitor monitor) throws CoreException {
516
517         if(url == null) {
518             UpdateCore.warn("The feature URL passed is null");
519             return null;
520         }
521         
522         // First check the cache
523
URLKey key = new URLKey(url);
524         IFeature feature = (IFeature) featureCache.get(key);
525         if (feature != null) return feature;
526
527         // Create a new one
528
if (type == null || type.equals("")) { //$NON-NLS-1$
529
// ask the Site for the default type
530
type = getDefaultPackagedFeatureType();
531         }
532
533         IFeatureFactory factory = FeatureTypeFactory.getInstance().getFactory(type);
534         feature = factory.createFeature(url, this, monitor);
535         if (feature != null) {
536             // Add the feature to the cache
537
featureCache.put(key, feature);
538         }
539         return feature;
540     }
541
542     protected void removeFeatureFromCache(URL featureURL) {
543         URLKey key = new URLKey(featureURL);
544         featureCache.remove(key);
545     }
546
547     /**
548      * Return an array of mirror update sites.
549      *
550      * @return an array of mirror update sites
551      * @since 2.0
552      */

553     public IURLEntry[] getMirrorSiteEntries() {
554         URLEntryModel[] result = getMirrorSiteEntryModels();
555         if (result.length == 0)
556             return new IURLEntry[0];
557         else
558             return (IURLEntry[]) result;
559     }
560 }
561
Popular Tags