KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > plugin > NewLibraryPluginCreationOperation


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  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.wizards.plugin;
12
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.FileNotFoundException JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.InputStream JavaDoc;
18 import java.lang.reflect.InvocationTargetException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Set JavaDoc;
22 import java.util.Stack JavaDoc;
23 import java.util.zip.ZipFile JavaDoc;
24
25 import org.eclipse.core.resources.IContainer;
26 import org.eclipse.core.resources.IFile;
27 import org.eclipse.core.resources.IFolder;
28 import org.eclipse.core.resources.IProject;
29 import org.eclipse.core.resources.IResource;
30 import org.eclipse.core.runtime.CoreException;
31 import org.eclipse.core.runtime.IPath;
32 import org.eclipse.core.runtime.IProgressMonitor;
33 import org.eclipse.core.runtime.IStatus;
34 import org.eclipse.core.runtime.Status;
35 import org.eclipse.core.runtime.SubProgressMonitor;
36 import org.eclipse.jdt.core.IClasspathEntry;
37 import org.eclipse.jdt.core.JavaCore;
38 import org.eclipse.jdt.core.JavaModelException;
39 import org.eclipse.osgi.service.resolver.BundleDescription;
40 import org.eclipse.osgi.util.ManifestElement;
41 import org.eclipse.osgi.util.NLS;
42 import org.eclipse.pde.core.build.IBuildEntry;
43 import org.eclipse.pde.core.build.IBuildModelFactory;
44 import org.eclipse.pde.core.plugin.IPluginBase;
45 import org.eclipse.pde.core.plugin.IPluginLibrary;
46 import org.eclipse.pde.core.plugin.IPluginModelBase;
47 import org.eclipse.pde.core.plugin.ISharedPluginModel;
48 import org.eclipse.pde.core.plugin.PluginRegistry;
49 import org.eclipse.pde.internal.core.build.WorkspaceBuildModel;
50 import org.eclipse.pde.internal.core.bundle.BundlePluginBase;
51 import org.eclipse.pde.internal.core.converter.PluginConverter;
52 import org.eclipse.pde.internal.core.ibundle.IBundle;
53 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
54 import org.eclipse.pde.internal.core.plugin.WorkspacePluginModelBase;
55 import org.eclipse.pde.internal.ui.IPDEUIConstants;
56 import org.eclipse.pde.internal.ui.PDEUIMessages;
57 import org.eclipse.pde.internal.ui.search.dependencies.AddNewBinaryDependenciesOperation;
58 import org.eclipse.pde.internal.ui.wizards.IProjectProvider;
59 import org.eclipse.pde.ui.IFieldData;
60 import org.eclipse.pde.ui.IPluginContentWizard;
61 import org.eclipse.ui.dialogs.IOverwriteQuery;
62 import org.eclipse.ui.wizards.datatransfer.ImportOperation;
63 import org.eclipse.ui.wizards.datatransfer.ZipFileStructureProvider;
64 import org.osgi.framework.BundleException;
65 import org.osgi.framework.Constants;
66
67 public class NewLibraryPluginCreationOperation extends
68         NewProjectCreationOperation {
69
70     private LibraryPluginFieldData fData;
71
72     public NewLibraryPluginCreationOperation(LibraryPluginFieldData data,
73             IProjectProvider provider, IPluginContentWizard contentWizard) {
74         super(data, provider, contentWizard);
75         fData = data;
76     }
77
78     private void addJar(File JavaDoc jarFile, IProject project, IProgressMonitor monitor)
79             throws CoreException {
80         String JavaDoc jarName = jarFile.getName();
81         IFile file = project.getFile(jarName);
82         monitor.subTask(NLS.bind(
83                 PDEUIMessages.NewProjectCreationOperation_copyingJar, jarName));
84         InputStream JavaDoc in = null;
85         try {
86             in = new FileInputStream JavaDoc(jarFile);
87             file.create(in, true, monitor);
88         } catch (FileNotFoundException JavaDoc fnfe) {
89         } finally {
90             if (in != null) {
91                 try {
92                     in.close();
93                 } catch (IOException JavaDoc ioe) {
94                 }
95             }
96         }
97     }
98
99     private void adjustExportRoot(IProject project, IBundle bundle)
100             throws CoreException {
101         IResource[] resources = project.members(false);
102         for (int j = 0; j < resources.length; j++) {
103             if (resources[j] instanceof IFile) {
104                 if (".project".equals(resources[j].getName()) //$NON-NLS-1$
105
|| ".classpath".equals(resources[j] //$NON-NLS-1$
106
.getName()) || "plugin.xml".equals(resources[j] //$NON-NLS-1$
107
.getName())
108                         || "build.properties".equals(resources[j] //$NON-NLS-1$
109
.getName())) {
110                     continue;
111                 }
112                 // resource at the root, export root
113
return;
114             }
115         }
116         removeExportRoot(bundle);
117     }
118
119     protected void adjustManifests(IProgressMonitor monitor, IProject project, IPluginBase base)
120             throws CoreException {
121         super.adjustManifests(monitor, project, base);
122         monitor.beginTask(new String JavaDoc(), fData.doFindDependencies() ? 4 : 2);
123         IBundle bundle = (base instanceof BundlePluginBase) ? ((BundlePluginBase)base).getBundle() : null;
124         if (bundle != null) {
125             adjustExportRoot(project, bundle);
126             monitor.worked(1);
127             addExportedPackages(project, bundle);
128             monitor.worked(1);
129             if (fData.doFindDependencies())
130                 addDependencies(project, base.getModel(), new SubProgressMonitor(monitor, 2));
131         }
132         monitor.done();
133     }
134
135     protected void createContents(IProgressMonitor monitor, IProject project)
136             throws CoreException, JavaModelException,
137             InvocationTargetException JavaDoc, InterruptedException JavaDoc {
138         // copy jars
139
String JavaDoc[] paths = fData.getLibraryPaths();
140         for (int i = paths.length - 1; i >= 0; i--) {
141             File JavaDoc jarFile = new File JavaDoc(paths[i]);
142             if (fData.isUnzipLibraries()) {
143                 importJar(jarFile, project, monitor);
144             } else {
145                 addJar(jarFile, project, monitor);
146             }
147             monitor.worked(1);
148         }
149
150         // delete manifest.mf imported from libraries
151
IFile importedManifest = project.getFile("META-INF/MANIFEST.MF"); //$NON-NLS-1$
152
if (importedManifest.exists()) {
153             importedManifest.delete(true, false, monitor);
154             if (!fData.hasBundleStructure()) {
155                 IFolder meta_inf = project.getFolder("META-INF"); //$NON-NLS-1$
156
if (meta_inf.members().length == 0) {
157                     meta_inf.delete(true, false, monitor);
158                 }
159             }
160         }
161     }
162
163     protected void fillBinIncludes(IProject project, IBuildEntry binEntry)
164             throws CoreException {
165         if (fData.hasBundleStructure())
166             binEntry.addToken("META-INF/"); //$NON-NLS-1$
167
else
168             binEntry.addToken("plugin.xml"); //$NON-NLS-1$
169

170         if (fData.isUnzipLibraries()) {
171             IResource[] resources = project.members(false);
172             for (int j = 0; j < resources.length; j++) {
173                 if (resources[j] instanceof IFolder) {
174                     if (!binEntry.contains(resources[j].getName() + "/")) //$NON-NLS-1$
175
binEntry.addToken(resources[j].getName() + "/"); //$NON-NLS-1$
176
} else {
177                     if (".project".equals(resources[j].getName()) //$NON-NLS-1$
178
|| ".classpath".equals(resources[j] //$NON-NLS-1$
179
.getName())
180                             || "build.properties".equals(resources[j] //$NON-NLS-1$
181
.getName())) {
182                         continue;
183                     }
184                     if (!binEntry.contains(resources[j].getName()))
185                         binEntry.addToken(resources[j].getName());
186                 }
187             }
188         } else {
189             String JavaDoc[] libraryPaths = fData.getLibraryPaths();
190             for (int j = 0; j < libraryPaths.length; j++) {
191                 File JavaDoc jarFile = new File JavaDoc(libraryPaths[j]);
192                 String JavaDoc name = jarFile.getName();
193                 if (!binEntry.contains(name))
194                     binEntry.addToken(name);
195             }
196         }
197     }
198
199     protected IClasspathEntry[] getInternalClassPathEntries(IProject project,
200             IFieldData data) {
201         String JavaDoc[] libraryPaths;
202         if (fData.isUnzipLibraries()) {
203             libraryPaths = new String JavaDoc[] { "" }; //$NON-NLS-1$
204
} else {
205             libraryPaths = fData.getLibraryPaths();
206         }
207         IClasspathEntry[] entries = new IClasspathEntry[libraryPaths.length];
208         for (int j = 0; j < libraryPaths.length; j++) {
209             File JavaDoc jarFile = new File JavaDoc(libraryPaths[j]);
210             String JavaDoc jarName = jarFile.getName();
211             IPath path = project.getFullPath().append(jarName);
212             entries[j] = JavaCore.newLibraryEntry(path, null, null, true);
213         }
214         return entries;
215     }
216
217     protected int getNumberOfWorkUnits() {
218         int numUnits = super.getNumberOfWorkUnits();
219         numUnits += fData.getLibraryPaths().length;
220         return numUnits;
221     }
222
223     private void importJar(File JavaDoc jar, IResource destination,
224             IProgressMonitor monitor) throws CoreException,
225             InvocationTargetException JavaDoc, InterruptedException JavaDoc {
226         ZipFile JavaDoc input = null;
227         try {
228             try {
229                 input = new ZipFile JavaDoc(jar);
230                 ZipFileStructureProvider provider = new ZipFileStructureProvider(
231                         input);
232                 ImportOperation op = new ImportOperation(destination
233                         .getFullPath(), provider.getRoot(), provider,
234                         new IOverwriteQuery() {
235                             public String JavaDoc queryOverwrite(String JavaDoc pathString) {
236                                 return IOverwriteQuery.ALL;
237                             }
238                         });
239                 op.run(monitor);
240             } finally {
241                 if (input != null)
242                     input.close();
243             }
244         } catch (IOException JavaDoc e) {
245             throw new CoreException(
246                     new Status(
247                             IStatus.ERROR,
248                             IPDEUIConstants.PLUGIN_ID,
249                             IStatus.OK,
250                             NLS.bind(PDEUIMessages.NewProjectCreationOperation_errorImportingJar,jar), e));
251         }
252     }
253
254     private void removeExportRoot(IBundle bundle) {
255         String JavaDoc value = bundle.getHeader(Constants.BUNDLE_CLASSPATH);
256         if (value == null)
257             value = "."; //$NON-NLS-1$
258
try {
259             ManifestElement [] elems = ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH, value);
260             StringBuffer JavaDoc buff = new StringBuffer JavaDoc(value.length());
261             for (int i = 0; i < elems.length; i++) {
262                 if (!elems[i].getValue().equals(".")) //$NON-NLS-1$
263
buff.append(elems[i].getValue());
264             }
265             bundle.setHeader(Constants.BUNDLE_CLASSPATH, buff.toString());
266         } catch (BundleException e) {
267         }
268     }
269
270     protected void setPluginLibraries(WorkspacePluginModelBase model)
271             throws CoreException {
272         IPluginBase pluginBase = model.getPluginBase();
273         if (fData.isUnzipLibraries()) {
274             IPluginLibrary library = model.getPluginFactory().createLibrary();
275             library.setName("."); //$NON-NLS-1$
276
library.setExported(true);
277             pluginBase.add(library);
278         } else {
279             String JavaDoc[] paths = fData.getLibraryPaths();
280             for (int i = 0; i < paths.length; i++) {
281                 File JavaDoc jarFile = new File JavaDoc(paths[i]);
282                 IPluginLibrary library = model.getPluginFactory().createLibrary();
283                 library.setName(jarFile.getName());
284                 library.setExported(true);
285                 pluginBase.add(library);
286             }
287         }
288     }
289
290     protected void createSourceOutputBuildEntries(WorkspaceBuildModel model,
291             IBuildModelFactory factory) throws CoreException {
292         if (fData.isUnzipLibraries()) {
293             // SOURCE.<LIBRARY_NAME>
294
IBuildEntry entry = factory.createEntry(IBuildEntry.JAR_PREFIX + "."); //$NON-NLS-1$
295
entry.addToken("."); //$NON-NLS-1$
296
model.getBuild().add(entry);
297
298             // OUTPUT.<LIBRARY_NAME>
299
entry = factory.createEntry(IBuildEntry.OUTPUT_PREFIX + "."); //$NON-NLS-1$
300
entry.addToken("."); //$NON-NLS-1$
301
model.getBuild().add(entry);
302         }
303     }
304     
305     private void addExportedPackages(IProject project, IBundle bundle) {
306         String JavaDoc value = bundle.getHeader(Constants.BUNDLE_CLASSPATH);
307         if (value == null)
308             value = "."; //$NON-NLS-1$
309
try {
310             ManifestElement[] elems = ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH, value);
311             HashMap JavaDoc map = new HashMap JavaDoc();
312             for (int i = 0; i < elems.length; i++) {
313                 ArrayList JavaDoc filter = new ArrayList JavaDoc();
314                 filter.add("*"); //$NON-NLS-1$
315
map.put(elems[i].getValue(), filter);
316             }
317             Set JavaDoc packages = PluginConverter.getDefault().getExports(project, map);
318             String JavaDoc pkgValue = getCommaValueFromSet(packages);
319             bundle.setHeader(Constants.EXPORT_PACKAGE, pkgValue);
320         } catch (BundleException e) {
321         }
322     }
323     
324     private void addDependencies(IProject project, ISharedPluginModel model, IProgressMonitor monitor) {
325         if (!(model instanceof IBundlePluginModelBase)) {
326             monitor.done();
327             return;
328         }
329         final boolean unzip = fData.isUnzipLibraries();
330         try {
331             new AddNewBinaryDependenciesOperation(project, (IBundlePluginModelBase)model){
332                 // Need to override this function to include every bundle in the target platform as a possible dependency
333
protected String JavaDoc[] findSecondaryBundles(IBundle bundle, Set JavaDoc ignorePkgs) {
334                     IPluginModelBase[] bases = PluginRegistry.getActiveModels();
335                     String JavaDoc[] ids = new String JavaDoc[bases.length];
336                     for (int i = 0; i < bases.length; i++) {
337                         BundleDescription desc = bases[i].getBundleDescription();
338                         if (desc == null)
339                             ids[i] = bases[i].getPluginBase().getId();
340                         else
341                             ids[i] = desc.getSymbolicName();
342                     }
343                     return ids;
344                 }
345                 
346                 // Need to override this function because when jar is unzipped, build.properties does not contain entry for '.'.
347
// Therefore, the super.addProjectPackages will not find the project packages(it includes only things in bin.includes)
348
protected void addProjectPackages(IBundle bundle, Set JavaDoc ignorePkgs) {
349                     if (!unzip)
350                         super.addProjectPackages(bundle, ignorePkgs);
351                     Stack JavaDoc stack = new Stack JavaDoc();
352                     stack.push(fProject);
353                     try {
354                         while (!stack.isEmpty()) {
355                             IContainer folder = (IContainer)stack.pop();
356                             IResource[] children = folder.members();
357                             for (int i = 0; i < children.length; i++) {
358                                 if (children[i] instanceof IContainer)
359                                     stack.push(children[i]);
360                                 else if ("class".equals(((IFile)children[i]).getFileExtension())) { //$NON-NLS-1$
361
String JavaDoc path = folder.getProjectRelativePath().toString();
362                                     ignorePkgs.add(path.replace('/', '.'));
363                                 }
364                             }
365                         }
366                     } catch(CoreException e) {
367                     }
368                     
369                 }
370             }.run(monitor);
371         } catch (InvocationTargetException JavaDoc e) {
372         } catch (InterruptedException JavaDoc e) {
373         }
374     }
375
376 }
377
Popular Tags