KickJava   Java API By Example, From Geeks To Geeks.

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


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;
12
13 import java.net.MalformedURLException JavaDoc;
14 import java.net.URL JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.osgi.util.NLS;
19 import org.eclipse.update.core.model.FeatureReferenceModel;
20 import org.eclipse.update.core.model.SiteModel;
21 import org.eclipse.update.internal.core.FeatureTypeFactory;
22 import org.eclipse.update.internal.core.Messages;
23 import org.eclipse.update.internal.core.UpdateCore;
24
25 /**
26  * Convenience implementation of a feature reference.
27  * <p>
28  * This class may be instantiated or subclassed by clients.
29  * </p>
30  * <p>
31  * <b>Note:</b> This class/interface is part of an interim API that is still under development and expected to
32  * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
33  * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
34  * (repeatedly) as the API evolves.
35  * </p>
36  * @see org.eclipse.update.core.IFeatureReference
37  * @see org.eclipse.update.core.model.FeatureReferenceModel
38  * @since 2.0
39  */

40 public class FeatureReference extends FeatureReferenceModel implements IFeatureReference, IPlatformEnvironment {
41
42     private VersionedIdentifier versionId;
43
44     //PERF: new instance variable
45
private IFeature exactFeature;
46
47     /**
48      * Feature reference default constructor
49      */

50     public FeatureReference() {
51         super();
52     }
53
54     /**
55      * Constructor FeatureReference.
56      * @param ref the reference to copy
57      */

58     public FeatureReference(IFeatureReference ref) {
59         super((FeatureReferenceModel) ref);
60         try {
61             setURL(ref.getURL());
62         } catch (CoreException e) {
63             UpdateCore.warn("", e); //$NON-NLS-1$
64
}
65     }
66
67     /**
68      * Constructor FeatureReference.
69      * @param ref the reference to copy
70      */

71     public FeatureReference(FeatureReferenceModel ref) {
72         super(ref);
73         try {
74             setURL(ref.getURL());
75         } catch (CoreException e) {
76             UpdateCore.warn("", e); //$NON-NLS-1$
77
}
78     }
79
80     /**
81      * Returns the feature this reference points to
82      * @return the feature on the Site
83      * @deprecated use getFeaure(IProgressMonitor)
84      */

85     public IFeature getFeature() throws CoreException {
86         return getFeature(null);
87     }
88
89     /**
90      * Returns the feature this reference points to
91      * @return the feature on the Site
92      */

93     public IFeature getFeature(IProgressMonitor monitor) throws CoreException {
94
95         if (exactFeature != null)
96             return exactFeature;
97         exactFeature = getFeature(this,monitor);
98         return exactFeature;
99     }
100
101     /**
102      * Returns the feature the reference points to
103      * @param ref the feature reference
104      * @return the feature on the Site
105      */

106     protected IFeature getFeature(IFeatureReference ref,IProgressMonitor monitor) throws CoreException {
107
108         IFeature feature = null;
109         URL JavaDoc refURL = ref.getURL();
110         feature = createFeature(refURL,monitor);
111         return feature;
112     }
113
114     /*
115      * create an instance of a concrete feature corresponding to this reference
116      */

117     private IFeature createFeature(URL JavaDoc url,IProgressMonitor monitor) throws CoreException {
118         String JavaDoc type = getType();
119         ISite site = getSite();
120         // if the site exists, use the site factory
121
if (site != null) {
122             return site.createFeature(type, url, monitor);
123         }
124         
125         IFeatureFactory factory = FeatureTypeFactory.getInstance().getFactory(type);
126         return factory.createFeature(url, site, monitor);
127     }
128
129     /**
130      * Returns the update site for the referenced feature
131      *
132      * @see IFeatureReference#getSite()
133      * @since 2.0
134      */

135     public ISite getSite() {
136         return (ISite) getSiteModel();
137     }
138
139     /**
140      * Sets the feature reference URL.
141      * This is typically performed as part of the feature reference creation
142      * operation. Once set, the url should not be reset.
143      *
144      * @see IFeatureReference#setURL(URL)
145      * @since 2.0
146      */

147     public void setURL(URL JavaDoc url) throws CoreException {
148         if (url != null) {
149             setURLString(url.toExternalForm());
150             try {
151                 resolve(url, null);
152             } catch (MalformedURLException JavaDoc e) {
153                 throw Utilities.newCoreException(NLS.bind(Messages.FeatureReference_UnableToResolveURL, (new String JavaDoc[] { url.toExternalForm() })), e);
154             }
155         }
156     }
157
158     /**
159      * Associates a site with the feature reference.
160      * This is typically performed as part of the feature reference creation
161      * operation. Once set, the site should not be reset.
162      *
163      * @see IFeatureReference#setSite(ISite)
164      * @since 2.0
165      */

166     public void setSite(ISite site) {
167         setSiteModel((SiteModel) site);
168     }
169
170     /**
171     * Returns the feature identifier.
172     *
173     * @see IFeatureReference#getVersionedIdentifier()
174     * @since 2.0
175     */

176     public VersionedIdentifier getVersionedIdentifier() {
177
178         if (versionId != null)
179             return versionId;
180
181         String JavaDoc id = getFeatureIdentifier();
182         String JavaDoc ver = getFeatureVersion();
183         if (id != null && ver != null) {
184             try {
185                 versionId = new VersionedIdentifier(id, ver);
186                 return versionId;
187             } catch (Exception JavaDoc e) {
188                 UpdateCore.warn("Unable to create versioned identifier:" + id + ":" + ver); //$NON-NLS-1$ //$NON-NLS-2$
189
}
190         }
191
192         // we need the exact match or we may have an infinite loop
193
versionId = new VersionedIdentifier(getURL().toExternalForm(), null);
194         try {
195             versionId = getFeature(null).getVersionedIdentifier();
196         } catch (CoreException e) {
197             UpdateCore.warn("", e); //$NON-NLS-1$
198
}
199         return versionId;
200     }
201
202     /**
203      * @see org.eclipse.update.core.IFeatureReference#getName()
204      */

205     public String JavaDoc getName() {
206         if (super.getLabel() != null)
207             return super.getLabel();
208         try {
209             return getFeature(null).getLabel();
210         } catch (CoreException e) {
211             return getVersionedIdentifier().toString();
212         }
213     }
214
215     /**
216      * Get optional operating system specification as a comma-separated string.
217      *
218      * @return the operating system specification string, or <code>null</code>.
219      * @since 2.1
220      */

221     public String JavaDoc getOS() {
222         if (super.getOS() == null && getURL()!=null)
223             try {
224                 return getFeature(null).getOS();
225             } catch (CoreException e) {
226                 return null;
227             }
228         return super.getOS();
229     }
230
231     /**
232      * Get optional windowing system specification as a comma-separated string.
233      *
234      * @return the windowing system specification string, or <code>null</code>.
235      * @since 2.1
236      */

237     public String JavaDoc getWS() {
238         if (super.getWS() == null && getURL()!=null)
239             try {
240                 return getFeature(null).getWS();
241             } catch (CoreException e) {
242                 return null;
243             }
244         return super.getWS();
245     }
246
247     /**
248      * Get optional system architecture specification as a comma-separated string.
249      *
250      * @return the system architecture specification string, or <code>null</code>.
251      * @since 2.1
252      */

253     public String JavaDoc getOSArch() {
254         if (super.getOSArch() == null && getURL()!=null)
255             try {
256                 return getFeature(null).getOSArch();
257             } catch (CoreException e) {
258                 return null;
259             }
260         return super.getOSArch();
261     }
262
263     /**
264      * Get optional locale specification as a comma-separated string.
265      *
266      * @return the locale specification string, or <code>null</code>.
267      * @since 2.1
268      */

269     public String JavaDoc getNL() {
270         if (super.getNL() == null && getURL()!=null)
271             try {
272                 return getFeature(null).getNL();
273             } catch (CoreException e) {
274                 return null;
275             }
276         return super.getNL();
277     }
278
279     /**
280      * Returns <code>true</code> if this feature is patching another feature,
281      * <code>false</code> otherwise
282      * @return boolean
283      */

284     public boolean isPatch() {
285         if (super.getPatch() == null)
286             try {
287                 return getFeature(null).isPatch();
288             } catch (CoreException e) {
289                 return false;
290             }
291         return "true".equalsIgnoreCase(super.getPatch()); //$NON-NLS-1$
292
}
293
294 }
295
Popular Tags