1 11 package org.eclipse.pde.internal.ui.wizards.plugin; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.FileNotFoundException ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.lang.reflect.InvocationTargetException ; 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.Set ; 22 import java.util.Stack ; 23 import java.util.zip.ZipFile ; 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 jarFile, IProject project, IProgressMonitor monitor) 79 throws CoreException { 80 String jarName = jarFile.getName(); 81 IFile file = project.getFile(jarName); 82 monitor.subTask(NLS.bind( 83 PDEUIMessages.NewProjectCreationOperation_copyingJar, jarName)); 84 InputStream in = null; 85 try { 86 in = new FileInputStream (jarFile); 87 file.create(in, true, monitor); 88 } catch (FileNotFoundException fnfe) { 89 } finally { 90 if (in != null) { 91 try { 92 in.close(); 93 } catch (IOException 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()) || ".classpath".equals(resources[j] .getName()) || "plugin.xml".equals(resources[j] .getName()) 108 || "build.properties".equals(resources[j] .getName())) { 110 continue; 111 } 112 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 (), 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 , InterruptedException { 138 String [] paths = fData.getLibraryPaths(); 140 for (int i = paths.length - 1; i >= 0; i--) { 141 File jarFile = new File (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 IFile importedManifest = project.getFile("META-INF/MANIFEST.MF"); if (importedManifest.exists()) { 153 importedManifest.delete(true, false, monitor); 154 if (!fData.hasBundleStructure()) { 155 IFolder meta_inf = project.getFolder("META-INF"); 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/"); else 168 binEntry.addToken("plugin.xml"); 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() + "/")) binEntry.addToken(resources[j].getName() + "/"); } else { 177 if (".project".equals(resources[j].getName()) || ".classpath".equals(resources[j] .getName()) 180 || "build.properties".equals(resources[j] .getName())) { 182 continue; 183 } 184 if (!binEntry.contains(resources[j].getName())) 185 binEntry.addToken(resources[j].getName()); 186 } 187 } 188 } else { 189 String [] libraryPaths = fData.getLibraryPaths(); 190 for (int j = 0; j < libraryPaths.length; j++) { 191 File jarFile = new File (libraryPaths[j]); 192 String 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 [] libraryPaths; 202 if (fData.isUnzipLibraries()) { 203 libraryPaths = new String [] { "" }; } else { 205 libraryPaths = fData.getLibraryPaths(); 206 } 207 IClasspathEntry[] entries = new IClasspathEntry[libraryPaths.length]; 208 for (int j = 0; j < libraryPaths.length; j++) { 209 File jarFile = new File (libraryPaths[j]); 210 String 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 jar, IResource destination, 224 IProgressMonitor monitor) throws CoreException, 225 InvocationTargetException , InterruptedException { 226 ZipFile input = null; 227 try { 228 try { 229 input = new ZipFile (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 queryOverwrite(String pathString) { 236 return IOverwriteQuery.ALL; 237 } 238 }); 239 op.run(monitor); 240 } finally { 241 if (input != null) 242 input.close(); 243 } 244 } catch (IOException 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 value = bundle.getHeader(Constants.BUNDLE_CLASSPATH); 256 if (value == null) 257 value = "."; try { 259 ManifestElement [] elems = ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH, value); 260 StringBuffer buff = new StringBuffer (value.length()); 261 for (int i = 0; i < elems.length; i++) { 262 if (!elems[i].getValue().equals(".")) 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("."); library.setExported(true); 277 pluginBase.add(library); 278 } else { 279 String [] paths = fData.getLibraryPaths(); 280 for (int i = 0; i < paths.length; i++) { 281 File jarFile = new File (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 IBuildEntry entry = factory.createEntry(IBuildEntry.JAR_PREFIX + "."); entry.addToken("."); model.getBuild().add(entry); 297 298 entry = factory.createEntry(IBuildEntry.OUTPUT_PREFIX + "."); entry.addToken("."); model.getBuild().add(entry); 302 } 303 } 304 305 private void addExportedPackages(IProject project, IBundle bundle) { 306 String value = bundle.getHeader(Constants.BUNDLE_CLASSPATH); 307 if (value == null) 308 value = "."; try { 310 ManifestElement[] elems = ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH, value); 311 HashMap map = new HashMap (); 312 for (int i = 0; i < elems.length; i++) { 313 ArrayList filter = new ArrayList (); 314 filter.add("*"); map.put(elems[i].getValue(), filter); 316 } 317 Set packages = PluginConverter.getDefault().getExports(project, map); 318 String 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 protected String [] findSecondaryBundles(IBundle bundle, Set ignorePkgs) { 334 IPluginModelBase[] bases = PluginRegistry.getActiveModels(); 335 String [] ids = new String [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 protected void addProjectPackages(IBundle bundle, Set ignorePkgs) { 349 if (!unzip) 350 super.addProjectPackages(bundle, ignorePkgs); 351 Stack stack = new Stack (); 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())) { String 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 e) { 372 } catch (InterruptedException e) { 373 } 374 } 375 376 } 377 | Popular Tags |