KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > core > SiteFileFactory


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.core;
12
13
14 import java.io.File JavaDoc;
15 import java.io.FileFilter JavaDoc;
16 import java.io.FileInputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.InputStream JavaDoc;
19 import java.net.MalformedURLException JavaDoc;
20 import java.net.URL JavaDoc;
21
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.Status;
25 import org.eclipse.osgi.util.NLS;
26 import org.eclipse.update.core.BaseSiteFactory;
27 import org.eclipse.update.core.ContentReference;
28 import org.eclipse.update.core.Feature;
29 import org.eclipse.update.core.ISite;
30 import org.eclipse.update.core.JarContentReference;
31 import org.eclipse.update.core.PluginEntry;
32 import org.eclipse.update.core.Site;
33 import org.eclipse.update.core.SiteContentProvider;
34 import org.eclipse.update.core.SiteFeatureReferenceModel;
35 import org.eclipse.update.core.Utilities;
36 import org.eclipse.update.core.model.ArchiveReferenceModel;
37 import org.eclipse.update.core.model.InvalidSiteTypeException;
38 import org.eclipse.update.core.model.SiteModel;
39 import org.eclipse.update.core.model.SiteModelFactory;
40 import org.eclipse.update.internal.model.BundleManifest;
41 import org.eclipse.update.internal.model.DefaultPluginParser;
42 import org.xml.sax.SAXException JavaDoc;
43
44 public class SiteFileFactory extends BaseSiteFactory {
45
46     // private when parsing file system
47
private SiteFile site;
48
49
50     /*
51      * @see ISiteFactory#createSite(URL,boolean)
52      */

53     public ISite createSite(URL JavaDoc url) throws CoreException, InvalidSiteTypeException {
54     
55         Site site = null;
56         InputStream JavaDoc siteStream = null;
57         SiteModelFactory factory = this;
58     
59         try {
60             // if url points to a directory
61
// attempt to parse site.xml
62
String JavaDoc path = url.getFile();
63             File JavaDoc siteLocation = new File JavaDoc(path);
64             if (siteLocation.isDirectory()) {
65                 url = siteLocation.toURL();
66                 File JavaDoc siteXMLFile = new File JavaDoc(siteLocation, Site.SITE_XML);
67                 if (siteXMLFile.exists()) {
68                     siteStream = new FileInputStream JavaDoc(siteXMLFile);
69                     site = (Site) factory.parseSite(siteStream);
70                 } else {
71                     // parse siteLocation
72
site = parseSite(siteLocation);
73                 }
74             } else {
75                 // we are not pointing to a directory
76
// attempt to parse the file
77
try {
78                     URL JavaDoc resolvedURL = URLEncoder.encode(url);
79                     siteStream = openStream(resolvedURL);
80                     site = (Site) factory.parseSite(siteStream);
81                 } catch (IOException JavaDoc e) {
82     
83                     // attempt to parse parent directory
84
File JavaDoc file = new File JavaDoc(url.getFile());
85                     File JavaDoc parentDirectory = file.getParentFile();
86     
87                     // do not create directory if it doesn't exist [18318]
88
// instead hrow error
89
if (parentDirectory != null && !parentDirectory.exists())
90                         throw Utilities.newCoreException(NLS.bind(Messages.SiteFileFactory_DirectoryDoesNotExist, (new String JavaDoc[] { file.getAbsolutePath() })), null);
91     
92                     if (parentDirectory == null || !parentDirectory.isDirectory())
93                         throw Utilities.newCoreException(NLS.bind(Messages.SiteFileFactory_UnableToObtainParentDirectory, (new String JavaDoc[] { file.getAbsolutePath() })), null);
94     
95                     site = parseSite(parentDirectory);
96     
97                 }
98             }
99     
100             SiteContentProvider contentProvider = new SiteFileContentProvider(url);
101             site.setSiteContentProvider(contentProvider);
102             contentProvider.setSite(site);
103             site.resolve(url, url);
104     
105             // Do not set read only as may install in it
106
//site.markReadOnly();
107
} catch (MalformedURLException JavaDoc e) {
108             throw Utilities.newCoreException(NLS.bind(Messages.SiteFileFactory_UnableToCreateURL, (new String JavaDoc[] { url == null ? "" : url.toExternalForm() })), e); //$NON-NLS-1$
109
} catch (IOException JavaDoc e) {
110             throw Utilities.newCoreException(Messages.SiteFileFactory_UnableToAccessSite,ISite.SITE_ACCESS_EXCEPTION, e);
111         } finally {
112             try {
113                 if (siteStream != null)
114                     siteStream.close();
115             } catch (IOException JavaDoc e) {
116             }
117         }
118         return site;
119     }
120     /**
121      * Method parseSite.
122      */

123     private Site parseSite(File JavaDoc directory) throws CoreException {
124
125         this.site = (SiteFile) createSiteMapModel();
126
127         if (!directory.exists())
128             throw Utilities.newCoreException(NLS.bind(Messages.SiteFileFactory_FileDoesNotExist, (new String JavaDoc[] { directory.getAbsolutePath() })), null);
129
130         File JavaDoc pluginPath = new File JavaDoc(directory, Site.DEFAULT_PLUGIN_PATH);
131
132         //PACKAGED
133
try {
134             parsePackagedFeature(directory); // in case it contains JAR files
135
} catch (EmptyDirectoryException ede) {
136             UpdateCore.log(ede.getStatus());
137         }
138
139         try {
140             parsePackagedPlugins(pluginPath);
141         } catch (EmptyDirectoryException ede) {
142             UpdateCore.log(ede.getStatus());
143         }
144
145         // INSTALLED
146
try {
147             parseInstalledFeature(directory);
148         } catch (EmptyDirectoryException ede) {
149             UpdateCore.log(ede.getStatus());
150         }
151
152         try {
153             parseInstalledPlugins(pluginPath);
154         } catch (EmptyDirectoryException ede) {
155             UpdateCore.log(ede.getStatus());
156         }
157
158         return site;
159
160     }
161
162     /**
163      * Method parseFeature.
164      * @throws CoreException
165      */

166     private void parseInstalledFeature(File JavaDoc directory) throws CoreException {
167
168         File JavaDoc featureDir = new File JavaDoc(directory, Site.DEFAULT_INSTALLED_FEATURE_PATH);
169         if (featureDir.exists()) {
170             String JavaDoc[] dir;
171             SiteFeatureReferenceModel featureRef;
172             URL JavaDoc featureURL;
173             File JavaDoc currentFeatureDir;
174             String JavaDoc newFilePath = null;
175
176             try {
177                 // handle the installed featuresConfigured under featuresConfigured subdirectory
178
dir = featureDir.list();
179                 if (dir == null) {
180                     throw new EmptyDirectoryException( new Status(IStatus.WARNING, UpdateCore.getPlugin().getBundle().getSymbolicName(), IStatus.OK, directory.getName() + File.separator + directory.getName() + "directory is empty", null)); //$NON-NLS-1$
181
}
182                 for (int index = 0; index < dir.length; index++) {
183
184                     // the URL must ends with '/' for the bundle to be resolved
185
newFilePath = dir[index] + (dir[index].endsWith("/") ? "/" : ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
186
currentFeatureDir = new File JavaDoc(featureDir, newFilePath);
187                     // check if feature.xml exists
188
File JavaDoc featureXMLFile = new File JavaDoc(currentFeatureDir, Feature.FEATURE_XML);
189                     if (!featureXMLFile.exists()) {
190                         UpdateCore.warn("Unable to find feature.xml in directory:" + currentFeatureDir); //$NON-NLS-1$
191
} else {
192                         // PERF: remove code
193
//SiteFileFactory archiveFactory = new SiteFileFactory();
194
featureURL = currentFeatureDir.toURL();
195                         featureRef = createFeatureReferenceModel();
196                         featureRef.setSiteModel(site);
197                         featureRef.setURLString(featureURL.toExternalForm());
198                         featureRef.setType(ISite.DEFAULT_INSTALLED_FEATURE_TYPE);
199                         ((Site) site).addFeatureReferenceModel(featureRef);
200                     }
201                 }
202             } catch (MalformedURLException JavaDoc e) {
203                 throw Utilities.newCoreException(NLS.bind(Messages.SiteFileFactory_UnableToCreateURLForFile, (new String JavaDoc[] { newFilePath })), e);
204             }
205         }
206     }
207
208     /**
209     * Method parseFeature.
210     * @throws CoreException
211     */

212     private void parsePackagedFeature(File JavaDoc directory) throws CoreException {
213
214         // FEATURES
215
File JavaDoc featureDir = new File JavaDoc(directory, Site.DEFAULT_FEATURE_PATH);
216         if (featureDir.exists()) {
217             String JavaDoc[] dir;
218             SiteFeatureReferenceModel featureRef;
219             URL JavaDoc featureURL;
220             File JavaDoc currentFeatureFile;
221             String JavaDoc newFilePath = null;
222
223             try {
224                 // only list JAR files
225
dir = featureDir.list(FeaturePackagedContentProvider.filter);
226                 if (dir == null) {
227                     throw new EmptyDirectoryException( new Status(IStatus.WARNING, UpdateCore.getPlugin().getBundle().getSymbolicName(), IStatus.OK, directory.getName() + File.separator + directory.getName() + "directory is empty", null)); //$NON-NLS-1$
228
}
229                 
230                 for (int index = 0; index < dir.length; index++) {
231
232                     // check if the JAR file contains a feature.xml
233
currentFeatureFile = new File JavaDoc(featureDir, dir[index]);
234                     JarContentReference ref = new JarContentReference("", currentFeatureFile); //$NON-NLS-1$
235
ContentReference result = null;
236                     try {
237                         result = ref.peek(Feature.FEATURE_XML, null, null);
238                     } catch (IOException JavaDoc e) {
239                         UpdateCore.warn("Exception retrieving feature.xml in file:" + currentFeatureFile, e); //$NON-NLS-1$
240
}
241                     if (result == null) {
242                         UpdateCore.warn("Unable to find feature.xml in file:" + currentFeatureFile); //$NON-NLS-1$
243
} else {
244                         featureURL = currentFeatureFile.toURL();
245                         // PERF: remove code
246
//SiteFileFactory archiveFactory = new SiteFileFactory();
247
featureRef = createFeatureReferenceModel();
248                         featureRef.setSiteModel(site);
249                         featureRef.setURLString(featureURL.toExternalForm());
250                         featureRef.setType(ISite.DEFAULT_PACKAGED_FEATURE_TYPE);
251                         site.addFeatureReferenceModel(featureRef);
252                     }
253                 }
254             } catch (MalformedURLException JavaDoc e) {
255                 throw Utilities.newCoreException(NLS.bind(Messages.SiteFileFactory_UnableToCreateURLForFile, (new String JavaDoc[] { newFilePath })), e);
256             }
257         }
258     }
259
260     /**
261      * Method parsePlugins.
262      *
263      * look into each plugin/fragment directory, crack the plugin.xml open (or
264      * fragment.xml ???) get id and version, calculate URL...
265      *
266      * @throws CoreException
267      */

268     private void parseInstalledPlugins(File JavaDoc pluginsDir) throws CoreException {
269         if (!pluginsDir.exists() || !pluginsDir.isDirectory()) {
270             return;
271         }
272         File JavaDoc[] dirs = pluginsDir.listFiles(new FileFilter JavaDoc() {
273             public boolean accept(File JavaDoc f) {
274                 return f.isDirectory();
275             }
276         });
277         DefaultPluginParser parser = new DefaultPluginParser();
278         
279         if (dirs == null) {
280             throw new EmptyDirectoryException( new Status(IStatus.WARNING, UpdateCore.getPlugin().getBundle().getSymbolicName(), IStatus.OK, pluginsDir.getName() + File.separator + pluginsDir.getName() + "directory is empty", null)); //$NON-NLS-1$
281
}
282         for (int i = 0; i < dirs.length; i++) {
283             File JavaDoc pluginFile = new File JavaDoc(dirs[i], "META-INF/MANIFEST.MF"); //$NON-NLS-1$
284
InputStream JavaDoc in = null;
285             try {
286                 BundleManifest bundleManifest = new BundleManifest(pluginFile);
287                 if (bundleManifest.exists()) {
288                     PluginEntry entry = bundleManifest.getPluginEntry();
289                     addParsedPlugin(entry, dirs[i]);
290                 } else {
291                     if (!(pluginFile = new File JavaDoc(dirs[i], "plugin.xml")) //$NON-NLS-1$
292
.exists()) {
293                         pluginFile = new File JavaDoc(dirs[i], "fragment.xml"); //$NON-NLS-1$
294
}
295                     if (pluginFile != null && pluginFile.exists()
296                             && !pluginFile.isDirectory()) {
297                         in = new FileInputStream JavaDoc(pluginFile);
298                         PluginEntry entry = parser.parse(in);
299                         addParsedPlugin(entry, dirs[i]);
300                     }
301                 }
302             } catch (IOException JavaDoc e) {
303                 String JavaDoc pluginFileString = (pluginFile == null)
304                         ? null
305                         : pluginFile.getAbsolutePath();
306                 throw Utilities.newCoreException(NLS.bind(Messages.SiteFileFactory_ErrorAccessing, (new String JavaDoc[] { pluginFileString })), e);
307             } catch (SAXException JavaDoc e) {
308                 String JavaDoc pluginFileString = (pluginFile == null)
309                         ? null
310                         : pluginFile.getAbsolutePath();
311                 throw Utilities.newCoreException(NLS.bind(Messages.SiteFileFactory_ErrorParsingFile, (new String JavaDoc[] { pluginFileString })),
312                         e);
313             } finally {
314                 if (in != null){
315                     try{
316                         in.close();
317                     } catch(IOException JavaDoc e){
318                     }
319                 }
320             }
321         }
322     }
323
324     /**
325      * tranform each Plugin and Fragment into an ArchiveReferenceModel
326      * and a PluginEntry for the Site
327      */

328     // PERF: removed intermediate Plugin object
329
private void addParsedPlugin(PluginEntry entry,File JavaDoc file) throws CoreException {
330
331         String JavaDoc location = null;
332         try {
333             if (entry != null) {
334
335                 // create the plugin Entry
336
((Site) site).addPluginEntry(entry);
337
338                 // Create the Site mapping ArchiveRef->PluginEntry
339
// the id of the archiveRef is plugins\<pluginid>_<ver>.jar as per the specs
340
// PERF: remove code
341
//SiteFileFactory archiveFactory = new SiteFileFactory();
342
ArchiveReferenceModel archive = createArchiveReferenceModel();
343                 String JavaDoc id = (entry.getVersionedIdentifier().toString());
344                 String JavaDoc pluginID = Site.DEFAULT_PLUGIN_PATH + id + FeaturePackagedContentProvider.JAR_EXTENSION;
345                 archive.setPath(pluginID);
346                 location = file.toURL().toExternalForm();
347                 archive.setURLString(location);
348                 ((Site) site).addArchiveReferenceModel(archive);
349
350                 // TRACE
351
if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_PARSING) {
352                     UpdateCore.debug("Added archive to site:" + pluginID + " pointing to: " + location); //$NON-NLS-1$ //$NON-NLS-2$
353
}
354             }
355         } catch (MalformedURLException JavaDoc e) {
356             throw Utilities.newCoreException(NLS.bind(Messages.SiteFileFactory_UnableToCreateURLForFile, (new String JavaDoc[] { location })), e);
357         }
358     }
359
360     /**
361      *
362      */

363     private void parsePackagedPlugins(File JavaDoc pluginDir) throws CoreException {
364         if (!pluginDir.exists()) {
365             return;
366         }
367         String JavaDoc[] dir = pluginDir.list(FeaturePackagedContentProvider.filter);
368         
369         if (dir == null) {
370             throw new EmptyDirectoryException( new Status(IStatus.WARNING, UpdateCore.getPlugin().getBundle().getSymbolicName(), IStatus.OK, pluginDir.getName() + File.separator + pluginDir.getName() + "directory is empty", null)); //$NON-NLS-1$
371
}
372         for (int i = 0; i < dir.length; i++) {
373             ContentReference ref = null;
374             String JavaDoc refString = null;
375             InputStream JavaDoc in = null;
376             JarContentReference jarReference = null;
377             try {
378                 File JavaDoc file = new File JavaDoc(pluginDir, dir[i]);
379                 jarReference = new JarContentReference(
380                         null, file);
381                 ref = jarReference.peek("META-INF/MANIFEST.MF", null, null); //$NON-NLS-1$
382
if (ref != null) {
383                     in = ref.getInputStream();
384                     BundleManifest manifest = new BundleManifest(in);
385                     if (manifest.exists()) {
386                         addParsedPlugin(manifest.getPluginEntry(), file);
387                         continue;
388                     }
389                 }
390                 ref = jarReference.peek("plugin.xml", null, null); //$NON-NLS-1$
391
if (ref == null) {
392                     ref = jarReference.peek("fragment.xml", null, null); //$NON-NLS-1$
393
}
394                 if (ref != null) {
395                     in = ref.getInputStream();
396                     PluginEntry entry = new DefaultPluginParser().parse(in);
397                     addParsedPlugin(entry, file);
398                 }
399             } catch (IOException JavaDoc e) {
400                 try {
401                     refString = (ref == null) ? null : ref.asURL()
402                             .toExternalForm();
403                 } catch (IOException JavaDoc ioe) {
404                 }
405                 throw Utilities.newCoreException(NLS.bind(Messages.SiteFileFactory_ErrorAccessing, (new String JavaDoc[] { refString })), e);
406             } catch (SAXException JavaDoc e) {
407                 try {
408                     refString = (ref == null) ? null : ref.asURL()
409                             .toExternalForm();
410                 } catch (IOException JavaDoc ioe) {
411                 }
412                 throw Utilities.newCoreException(NLS.bind(Messages.SiteFileFactory_ErrorParsingFile, (new String JavaDoc[] { refString })), e);
413             } finally {
414                 if(in != null){
415                     try{
416                         in.close();
417                     }catch(IOException JavaDoc ce){
418                     }
419                 }
420                 if (jarReference != null) {
421                     try {
422                         jarReference.closeArchive();
423                     } catch (IOException JavaDoc e) {
424                         // TODO Auto-generated catch block
425
e.printStackTrace();
426                     }
427                 }
428             }
429         }
430     }
431
432     /*
433      * @see SiteModelFactory#createSiteMapModel()
434      */

435     public SiteModel createSiteMapModel() {
436         return new SiteFile();
437     }
438
439     /*
440      * @see SiteModelFactory#canParseSiteType(String)
441      */

442     public boolean canParseSiteType(String JavaDoc type) {
443         return (super.canParseSiteType(type) || SiteFileContentProvider.SITE_TYPE.equalsIgnoreCase(type));
444     }
445
446 }
447
Popular Tags