KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
15 import java.net.*;
16 import java.util.ArrayList JavaDoc;
17 import java.util.List JavaDoc;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.osgi.util.NLS;
20 import org.eclipse.update.core.*;
21
22 /**
23  * Default implementation of a Executable Feature Content Provider
24  */

25
26 public class FeatureExecutableContentProvider extends FeatureContentProvider {
27
28     /*
29      * Constructor
30      */

31     public FeatureExecutableContentProvider(URL url) {
32         super(url);
33     }
34
35     /*
36      * Return the path for a pluginEntry
37      */

38     private String JavaDoc getPath(IPluginEntry pluginEntry)
39         throws IOException, CoreException {
40
41         // get the URL of the Archive file that contains the plugin entry
42
ISiteContentProvider provider = getFeature().getSite().getSiteContentProvider();
43         URL fileURL = provider.getArchiveReference(getPathID(pluginEntry));
44         String JavaDoc result = fileURL.getFile();
45
46         if (!result.endsWith(".jar") && !result.endsWith("/") && !result.endsWith(File.separator)) //$NON-NLS-1$ //$NON-NLS-2$
47
result += File.separator;
48         File pluginPath = new File(result);
49         if (!pluginPath.exists())
50             throw new IOException(
51                 NLS.bind(Messages.FeatureExecutableContentProvider_FileDoesNotExist, (new String JavaDoc[] { result })));
52
53         return result;
54     }
55
56     /*
57      * Returns the path for the Feature
58      */

59     private String JavaDoc getFeaturePath() throws IOException {
60         String JavaDoc result = getFeature().getURL().getFile();
61
62         // return the list of all subdirectories
63
if (!(result.endsWith(File.separator) || result.endsWith("/"))) //$NON-NLS-1$
64
result += File.separator;
65         File pluginDir = new File(result);
66         if (!pluginDir.exists())
67             throw new IOException(
68                 NLS.bind(Messages.FeatureExecutableContentProvider_FileDoesNotExist, (new String JavaDoc[] { result })));
69
70         return result;
71     }
72
73     /*
74      * Returns all the files under the directory
75      * Recursive call
76      */

77     private List JavaDoc getFiles(File dir) throws IOException {
78         List JavaDoc result = new ArrayList JavaDoc();
79
80         if (!dir.isDirectory()) {
81             String JavaDoc msg =
82                 NLS.bind(Messages.FeatureExecutableContentProvider_InvalidDirectory, (new String JavaDoc[] { dir.getAbsolutePath() }));
83
84             throw new IOException(msg);
85
86         }
87
88         File[] files = dir.listFiles();
89         if (files != null)
90             for (int i = 0; i < files.length; ++i) {
91                 if (files[i].isDirectory()) {
92                     result.addAll(getFiles(files[i]));
93                 } else {
94                     result.add(files[i]);
95                 }
96             }
97
98         return result;
99     }
100
101     /*
102      * @see IFeatureContentProvider#getVerifier()
103      */

104     public IVerifier getVerifier() throws CoreException {
105         return null;
106     }
107
108     /*
109      * @see IFeatureContentProvider#getFeatureManifestReference()
110      */

111     public ContentReference getFeatureManifestReference(InstallMonitor monitor)
112         throws CoreException {
113         ContentReference result = null;
114         try {
115             result = new ContentReference(Feature.FEATURE_XML, new URL(getURL(), Feature.FEATURE_XML));
116
117         } catch (MalformedURLException e) {
118             throw Utilities.newCoreException(
119                 NLS.bind(Messages.FeatureExecutableContentProvider_UnableToCreateURLFor, (new String JavaDoc[] { getURL().toExternalForm() + " " + Feature.FEATURE_XML })), //$NON-NLS-1$
120
e);
121         }
122         return result;
123     }
124
125     /*
126      * @see IFeatureContentProvider#getArchiveReferences()
127      */

128     public ContentReference[] getArchiveReferences(InstallMonitor monitor)
129         throws CoreException {
130         // executable feature does not contain archives
131
return new ContentReference[0];
132     }
133
134     /*
135      * @see IFeatureContentProvider#getPluginEntryArchiveReferences(IPluginEntry)
136      */

137     public ContentReference[] getPluginEntryArchiveReferences(
138         IPluginEntry pluginEntry,
139         InstallMonitor monitor)
140         throws CoreException {
141         ContentReference[] result = new ContentReference[1];
142         String JavaDoc archiveID = getPathID(pluginEntry);
143         try {
144             File archiveFile = new File(getPath(pluginEntry));
145             if(!archiveFile.isDirectory() && archiveFile.getName().endsWith(".jar")){ //$NON-NLS-1$
146
result[0] = new JarContentReference(archiveID, archiveFile);
147             } else {
148                 result[0] =
149                     new ContentReference(archiveID, archiveFile);
150             }
151         } catch (IOException e) {
152             throw Utilities.newCoreException(
153                     NLS.bind(Messages.FeatureExecutableContentProvider_UnableToRetrievePluginEntry, (new String JavaDoc[] { pluginEntry.getVersionedIdentifier().toString() })),
154                     e);
155         }
156         return result;
157     }
158
159     /*
160      * @see IFeatureContentProvider#getNonPluginEntryArchiveReferences(INonPluginEntry)
161      */

162     public ContentReference[] getNonPluginEntryArchiveReferences(
163         INonPluginEntry nonPluginEntry,
164         InstallMonitor monitor)
165         throws CoreException {
166
167         ContentReference[] result = new ContentReference[1];
168         URL fileURL;
169
170         //try {
171
// get the URL of the Archive file that contains the plugin entry
172
ISiteContentProvider provider = getFeature().getSite().getSiteContentProvider();
173         fileURL = provider.getArchiveReference(getPathID(nonPluginEntry));
174
175         String JavaDoc fileString = fileURL.getFile();
176         File nonPluginData = new File(fileString);
177         if (!nonPluginData.exists())
178             throw Utilities.newCoreException(
179                 NLS.bind(Messages.FeatureExecutableContentProvider_FileDoesNotExist, (new String JavaDoc[] { fileString })),
180                 null);
181
182         try {
183             result[0] =
184                 new ContentReference(nonPluginEntry.getIdentifier(), nonPluginData.toURL());
185         } catch (MalformedURLException e) {
186             throw Utilities.newCoreException(
187                 NLS.bind(Messages.FeatureExecutableContentProvider_UnableToRetrieveNonPluginEntry, (new String JavaDoc[] { nonPluginEntry.getIdentifier().toString() })),
188                 e);
189         }
190         return result;
191     }
192
193     /*
194      * @see IFeatureContentProvider#getFeatureEntryArchiveReferences()
195      */

196     public ContentReference[] getFeatureEntryArchiveReferences(InstallMonitor monitor)
197         throws CoreException {
198         ContentReference[] contentReferences = new ContentReference[1];
199         contentReferences[0] = new ContentReference(null, getURL());
200         return contentReferences;
201     }
202
203     /*
204      * @see IFeatureContentProvider#getFeatureEntryArchivesContentReferences()
205      */

206     public ContentReference[] getFeatureEntryContentReferences(InstallMonitor monitor)
207         throws CoreException {
208         ContentReference[] result = new ContentReference[0];
209         try {
210             File featureDir = new File(getFeaturePath());
211             List JavaDoc files = getFiles(featureDir);
212             result = new ContentReference[files.size()];
213             for (int i = 0; i < result.length; i++) {
214                 File currentFile = (File) files.get(i);
215                 result[i] = new ContentReference(currentFile.getName(), currentFile.toURL());
216             }
217         } catch (IOException e) {
218             throw Utilities.newCoreException(
219                 NLS.bind(Messages.FeatureExecutableContentProvider_UnableToRetrieveFeatureEntry, (new String JavaDoc[] { getFeature().getVersionedIdentifier().toString() })),
220                 e);
221         }
222         return result;
223     }
224
225     /*
226      * @see IFeatureContentProvider#getPluginEntryContentReferences(IPluginEntry)
227      */

228     public ContentReference[] getPluginEntryContentReferences(
229         IPluginEntry pluginEntry,
230         InstallMonitor monitor)
231         throws CoreException {
232
233         ContentReference[] references = getPluginEntryArchiveReferences(pluginEntry, monitor);
234         ContentReference[] result = new ContentReference[0];
235
236         try {
237             if (references[0] instanceof JarContentReference) {
238                 result = ((JarContentReference)references[0]).peek(null, monitor);
239             } else {
240                 // return the list of all subdirectories
241
File pluginDir = new File(getPath(pluginEntry));
242                 URL pluginURL = pluginDir.toURL();
243                 List JavaDoc files = getFiles(pluginDir);
244                 result = new ContentReference[files.size()];
245                 for (int i = 0; i < result.length; i++) {
246                     File currentFile = (File) files.get(i);
247                     String JavaDoc relativeString = UpdateManagerUtils.getURLAsString(pluginURL, currentFile.toURL());
248                     result[i] = new ContentReference(relativeString, currentFile.toURL());
249                 }
250             }
251         } catch (IOException e) {
252             throw Utilities.newCoreException(
253                 Messages.FeatureExecutableContentProvider_UnableToRetriveArchiveContentRef
254                     + pluginEntry.getVersionedIdentifier().toString(),
255                 e);
256         }
257         
258         //[20866] we did not preserve executable bit
259
validatePermissions(result);
260         
261         return result;
262     }
263 }
264
Popular Tags