View | Details | Raw Unified | Return to bug 147831 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/pde/internal/core/ExternalModelManager.java (-1 / +126 lines)
Lines 10-26 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.pde.internal.core;
11
package org.eclipse.pde.internal.core;
12
12
13
import java.io.File;
13
import java.io.*;
14
import java.net.MalformedURLException;
14
import java.net.MalformedURLException;
15
import java.net.URL;
15
import java.net.URL;
16
import java.util.*;
16
import java.util.*;
17
import java.util.jar.JarFile;
18
import org.eclipse.core.runtime.IPath;
17
import org.eclipse.core.runtime.Path;
19
import org.eclipse.core.runtime.Path;
20
import org.eclipse.osgi.service.resolver.BundleDescription;
21
import org.eclipse.pde.core.plugin.IPluginLibrary;
18
import org.eclipse.pde.core.plugin.IPluginModelBase;
22
import org.eclipse.pde.core.plugin.IPluginModelBase;
19
import org.eclipse.pde.internal.core.target.AbstractTargetHandle;
23
import org.eclipse.pde.internal.core.target.AbstractTargetHandle;
20
import org.eclipse.pde.internal.core.target.provisional.NameVersionDescriptor;
24
import org.eclipse.pde.internal.core.target.provisional.NameVersionDescriptor;
25
import org.eclipse.pde.internal.core.util.CoreUtility;
21
26
22
public class ExternalModelManager extends AbstractModelManager {
27
public class ExternalModelManager extends AbstractModelManager {
23
28
29
	private static final String LIB_CACHE_DIR = ".libcache"; //$NON-NLS-1$
30
24
	private IPluginModelBase[] fModels = new IPluginModelBase[0];
31
	private IPluginModelBase[] fModels = new IPluginModelBase[0];
25
32
26
	protected IPluginModelBase[] getAllModels() {
33
	protected IPluginModelBase[] getAllModels() {
Lines 63-68 Link Here
63
				}
70
				}
64
			}
71
			}
65
		}
72
		}
73
		purgeLibraryCache(fModels);
66
	}
74
	}
67
75
68
	public void setModels(IPluginModelBase[] models) {
76
	public void setModels(IPluginModelBase[] models) {
Lines 130-133 Link Here
130
		System.arraycopy(additional, 0, result, base.length, additional.length);
138
		System.arraycopy(additional, 0, result, base.length, additional.length);
131
		return result;
139
		return result;
132
	}
140
	}
141
142
	/**
143
	 * Caches the libraries contained in the given external bundle (if not already cached).
144
	 * The JAR files are stored in the local file system (in the state location
145
	 * of PDE Core). 
146
	 * 
147
	 * @param model Model of an external bundle.
148
	 * 
149
	 * @return The cached versions of the JAR files wrapped by this bundle, or
150
	 * an empty array if the given bundle is not packaged as a JAR or does not contain 
151
	 * any wrapped libraries.
152
	 */
153
	public File[] getCachedLibraryFiles(IPluginModelBase model) {
154
		File fJarFile = new File(model.getInstallLocation());
155
		if (!fJarFile.isFile())
156
			return new File[0];
157
158
		BundleDescription desc = model.getBundleDescription();
159
		IPluginLibrary[] libs = model.getPluginBase().getLibraries();
160
161
		File fCacheDir = new File(getLibraryCacheDir(), getBundleLibsCacheDirName(desc));
162
163
		List files = new ArrayList();
164
165
		for (int i = 0; i < libs.length; i++) {
166
			String libName = libs[i].getName();
167
			if (!".".equals(libName)) { //$NON-NLS-1$
168
				libName = ClasspathUtilCore.expandLibraryName(libName);
169
				File fDestFile = new File(fCacheDir, libName);
170
				// assume that an existing file is always valid
171
				if (!fDestFile.isFile()) {
172
					try {
173
						fDestFile.getParentFile().mkdirs();
174
						extractJar(fJarFile, libName, fDestFile);
175
						files.add(fDestFile);
176
					} catch (IOException ie) {
177
						// do not add file, but log error
178
						PDECore.logException(ie, "Could not extract JAR file from bundle " + desc.getSymbolicName()); //$NON-NLS-1$
179
					}
180
				} else
181
					files.add(fDestFile);
182
			}
183
		}
184
185
		return (File[]) files.toArray(new File[0]);
186
	}
187
188
	/**
189
	 * Delete all the cached JARs of libraries which are currently not contained 
190
	 * or enabled in the target platform. Will ignore any errors when trying to
191
	 * delete a directory.
192
	 * 
193
	 * @param targetModels The current contents of the target platform.
194
	 */
195
	private void purgeLibraryCache(IPluginModelBase[] targetModels) {
196
		File fCacheDir = getLibraryCacheDir();
197
		if (!fCacheDir.isDirectory())
198
			return;
199
200
		// build a list with all potential directory names for quick check
201
		Set bundleKeys = new HashSet();
202
203
		for (int i = 0; i < targetModels.length; i++) {
204
			if (targetModels[i].isEnabled()) {
205
				BundleDescription desc = targetModels[i].getBundleDescription();
206
				bundleKeys.add(getBundleLibsCacheDirName(desc));
207
			}
208
		}
209
210
		File[] fDirs = fCacheDir.listFiles();
211
		for (int i = 0; i < fDirs.length; i++) {
212
			if (fDirs[i].isDirectory() && !bundleKeys.contains(fDirs[i].getName()))
213
				CoreUtility.deleteContent(fDirs[i]);
214
		}
215
	}
216
217
	/**
218
	 * @return The directory in the PDE Core's state location where wrapped JARs
219
	 * from external bundles are stored.
220
	 */
221
	private static File getLibraryCacheDir() {
222
		IPath path = PDECore.getDefault().getStateLocation();
223
		return new File(path.toFile(), LIB_CACHE_DIR);
224
	}
225
226
	/**
227
	 * Returns the name of the library cache directory for the given bundle.
228
	 * 
229
	 * @param desc Bundle descriptor. 
230
	 * 
231
	 * @return <code>[bundle ID]_[bundle version]</code>
232
	 */
233
	private static String getBundleLibsCacheDirName(BundleDescription desc) {
234
		return desc.getSymbolicName() + "_" + desc.getVersion(); //$NON-NLS-1$
235
	}
236
237
	private static void extractJar(File fJarFile, String libName, File fTargetFile) throws IOException {
238
		JarFile f = new JarFile(fJarFile);
239
		InputStream in = null;
240
		try {
241
			in = f.getInputStream(f.getEntry(libName));
242
			if (in == null)
243
				throw new IOException();
244
245
			CoreUtility.readFile(in, fTargetFile);
246
		} finally {
247
			try {
248
				f.close();
249
			} catch (Exception e) {
250
			}
251
			try {
252
				in.close();
253
			} catch (Exception e) {
254
			}
255
		}
256
	}
257
133
}
258
}
(-)src/org/eclipse/pde/internal/core/PDEClasspathContainer.java (+5 lines)
Lines 67-72 Link Here
67
			if (srcPath == null)
67
			if (srcPath == null)
68
				srcPath = new Path(model.getInstallLocation());
68
				srcPath = new Path(model.getInstallLocation());
69
			addLibraryEntry(new Path(model.getInstallLocation()), srcPath, rules, getClasspathAttributes(model), entries);
69
			addLibraryEntry(new Path(model.getInstallLocation()), srcPath, rules, getClasspathAttributes(model), entries);
70
			File[] jars = PDECore.getDefault().getModelManager().getExternalModelManager().getCachedLibraryFiles(model);
71
			for (int i = 0; i < jars.length; i++) {
72
				Path path = new Path(jars[i].getAbsolutePath());
73
				addLibraryEntry(path, path, rules, getClasspathAttributes(model), entries);
74
			}
70
		} else {
75
		} else {
71
			IPluginLibrary[] libraries = model.getPluginBase().getLibraries();
76
			IPluginLibrary[] libraries = model.getPluginBase().getLibraries();
72
			for (int i = 0; i < libraries.length; i++) {
77
			for (int i = 0; i < libraries.length; i++) {

Return to bug 147831