1 11 package org.eclipse.pde.internal.core; 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.util.Dictionary ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Properties ; 22 import java.util.jar.Attributes ; 23 import java.util.jar.JarFile ; 24 import java.util.jar.Manifest ; 25 26 import org.eclipse.core.resources.IFile; 27 import org.eclipse.core.resources.IProject; 28 import org.eclipse.core.resources.IResource; 29 import org.eclipse.core.runtime.CoreException; 30 import org.eclipse.core.runtime.IProgressMonitor; 31 import org.eclipse.osgi.service.pluginconversion.PluginConversionException; 32 import org.eclipse.osgi.service.pluginconversion.PluginConverter; 33 import org.eclipse.osgi.util.ManifestElement; 34 import org.eclipse.pde.core.plugin.IPluginModelBase; 35 import org.osgi.framework.BundleException; 36 import org.osgi.framework.Constants; 37 import org.osgi.util.tracker.ServiceTracker; 38 39 public class PDEPluginConverter { 40 41 public static void convertToOSGIFormat(IProject project, String target, Dictionary dictionary, IProgressMonitor monitor) throws CoreException { 42 convertToOSGIFormat(project, target, dictionary, null, monitor); 43 } 44 45 public static void convertToOSGIFormat(IProject project, String target, Dictionary dictionary, HashMap newProps, IProgressMonitor monitor) throws CoreException { 46 try { 47 File outputFile = new File (project.getLocation().append( 48 "META-INF/MANIFEST.MF").toOSString()); File inputFile = new File (project.getLocation().toOSString()); 50 ServiceTracker tracker = new ServiceTracker(PDECore.getDefault() 51 .getBundleContext(), PluginConverter.class.getName(), null); 52 tracker.open(); 53 PluginConverter converter = (PluginConverter) tracker.getService(); 54 converter.convertManifest(inputFile, outputFile, false, target, true, dictionary); 55 56 if (newProps != null && newProps.size() > 0) 57 converter.writeManifest(outputFile, getProperties(outputFile, newProps), false); 58 59 project.refreshLocal(IResource.DEPTH_INFINITE, null); 60 tracker.close(); 61 } catch (PluginConversionException e) { 62 } finally { 63 monitor.done(); 64 } 65 } 66 67 public static void createBundleForFramework(IProject project, HashMap newProps, IProgressMonitor monitor) throws CoreException { 68 try { 69 File outputFile = new File (project.getLocation().append( 70 "META-INF/MANIFEST.MF").toOSString()); File inputFile = new File (project.getLocation().toOSString()); 72 ServiceTracker tracker = new ServiceTracker(PDECore.getDefault() 73 .getBundleContext(), PluginConverter.class.getName(), null); 74 tracker.open(); 75 PluginConverter converter = (PluginConverter) tracker.getService(); 76 double version = TargetPlatform.getTargetVersion(); 77 String versionString = version <= 3.1 ? ICoreConstants.TARGET31 : TargetPlatform.getTargetVersionString(); 78 converter.convertManifest(inputFile, outputFile, false, versionString, true, null); 79 80 Properties prop = getProperties(outputFile, newProps); 81 prop.remove(ICoreConstants.ECLIPSE_AUTOSTART); 82 prop.remove(ICoreConstants.ECLIPSE_LAZYSTART); 83 converter.writeManifest(outputFile, prop, false); 84 project.refreshLocal(IResource.DEPTH_INFINITE, null); 85 tracker.close(); 86 } catch (PluginConversionException e) { 87 } catch (CoreException e) { 88 } finally { 89 monitor.done(); 90 } 91 } 92 93 private static Properties getProperties(File file, HashMap newProps) { 94 InputStream manifestStream = null; 95 try { 96 manifestStream = new FileInputStream (file); 97 Manifest manifest = new Manifest (manifestStream); 98 Properties prop = manifestToProperties(manifest.getMainAttributes()); 99 if (newProps != null && newProps.size() > 0) { 100 Iterator iter = newProps.keySet().iterator(); 101 while (iter.hasNext()) { 102 String key = iter.next().toString(); 103 prop.put(key, newProps.get(key)); 104 } 105 } 106 return prop; 107 } catch (FileNotFoundException e) { 108 } catch (IOException e) { 109 } finally { 110 try { 111 if (manifestStream != null) 112 manifestStream.close(); 113 } catch (IOException e) { 114 } 115 } 116 return new Properties (); 117 } 118 119 public static void modifyBundleClasspathHeader(IProject project, IPluginModelBase model) { 120 IFile file = project.getFile(JarFile.MANIFEST_NAME); 121 if (file.exists()) { 122 InputStream manifestStream = null; 123 try { 124 manifestStream = new FileInputStream (file.getLocation().toFile()); 125 Manifest manifest = new Manifest (manifestStream); 126 Properties prop = manifestToProperties(manifest.getMainAttributes()); 127 String classpath = prop.getProperty(Constants.BUNDLE_CLASSPATH); 128 if (classpath == null) { 129 prop.put(Constants.BUNDLE_CLASSPATH, 130 ClasspathUtilCore.getFilename(model)); 131 } else { 132 ManifestElement[] elements = ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH, classpath); 133 StringBuffer buffer = new StringBuffer (); 134 for (int i = 0; i < elements.length; i++) { 135 if (buffer.length() > 0) { 136 buffer.append(","); buffer.append(System.getProperty("line.separator")); buffer.append(" "); } 140 if (elements[i].getValue().equals(".")) buffer.append(ClasspathUtilCore.getFilename(model)); 142 else 143 buffer.append(elements[i].getValue()); 144 } 145 prop.put(Constants.BUNDLE_CLASSPATH, buffer.toString()); 146 } 147 ServiceTracker tracker = new ServiceTracker(PDECore.getDefault() 148 .getBundleContext(), PluginConverter.class.getName(), null); 149 tracker.open(); 150 PluginConverter converter = (PluginConverter) tracker.getService(); 151 converter.writeManifest(new File (file.getLocation().toOSString()), prop, false); 152 file.refreshLocal(1, null); 153 tracker.close(); 154 } catch (FileNotFoundException e) { 155 } catch (IOException e) { 156 } catch (BundleException e) { 157 } catch (PluginConversionException e) { 158 } catch (CoreException e) { 159 } finally { 160 try { 161 if (manifestStream != null) 162 manifestStream.close(); 163 } catch (IOException e) { 164 } 165 } 166 } 167 } 168 169 private static Properties manifestToProperties(Attributes d) { 170 Iterator iter = d.keySet().iterator(); 171 Properties result = new Properties (); 172 while (iter.hasNext()) { 173 Attributes.Name key = (Attributes.Name ) iter.next(); 174 result.put(key.toString(), d.get(key)); 175 } 176 return result; 177 } 178 179 } 180 | Popular Tags |