KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > PDEPluginConverter


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.pde.internal.core;
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.util.Dictionary JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Properties JavaDoc;
22 import java.util.jar.Attributes JavaDoc;
23 import java.util.jar.JarFile JavaDoc;
24 import java.util.jar.Manifest JavaDoc;
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 JavaDoc target, Dictionary JavaDoc dictionary, IProgressMonitor monitor) throws CoreException {
42         convertToOSGIFormat(project, target, dictionary, null, monitor);
43     }
44     
45     public static void convertToOSGIFormat(IProject project, String JavaDoc target, Dictionary JavaDoc dictionary, HashMap JavaDoc newProps, IProgressMonitor monitor) throws CoreException {
46         try {
47             File JavaDoc outputFile = new File JavaDoc(project.getLocation().append(
48                     "META-INF/MANIFEST.MF").toOSString()); //$NON-NLS-1$
49
File JavaDoc inputFile = new File JavaDoc(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 JavaDoc newProps, IProgressMonitor monitor) throws CoreException {
68         try {
69             File JavaDoc outputFile = new File JavaDoc(project.getLocation().append(
70                     "META-INF/MANIFEST.MF").toOSString()); //$NON-NLS-1$
71
File JavaDoc inputFile = new File JavaDoc(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 JavaDoc versionString = version <= 3.1 ? ICoreConstants.TARGET31 : TargetPlatform.getTargetVersionString();
78             converter.convertManifest(inputFile, outputFile, false, versionString, true, null);
79             
80             Properties JavaDoc 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 JavaDoc getProperties(File JavaDoc file, HashMap JavaDoc newProps) {
94         InputStream JavaDoc manifestStream = null;
95         try {
96             manifestStream = new FileInputStream JavaDoc(file);
97             Manifest JavaDoc manifest = new Manifest JavaDoc(manifestStream);
98             Properties JavaDoc prop = manifestToProperties(manifest.getMainAttributes());
99             if (newProps != null && newProps.size() > 0) {
100                 Iterator JavaDoc iter = newProps.keySet().iterator();
101                 while (iter.hasNext()) {
102                     String JavaDoc key = iter.next().toString();
103                     prop.put(key, newProps.get(key));
104                 }
105             }
106             return prop;
107         } catch (FileNotFoundException JavaDoc e) {
108         } catch (IOException JavaDoc e) {
109         } finally {
110             try {
111                 if (manifestStream != null)
112                     manifestStream.close();
113             } catch (IOException JavaDoc e) {
114             }
115         }
116         return new Properties JavaDoc();
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 JavaDoc manifestStream = null;
123             try {
124                 manifestStream = new FileInputStream JavaDoc(file.getLocation().toFile());
125                 Manifest JavaDoc manifest = new Manifest JavaDoc(manifestStream);
126                 Properties JavaDoc prop = manifestToProperties(manifest.getMainAttributes());
127                 String JavaDoc 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 JavaDoc buffer = new StringBuffer JavaDoc();
134                     for (int i = 0; i < elements.length; i++) {
135                         if (buffer.length() > 0) {
136                             buffer.append(","); //$NON-NLS-1$
137
buffer.append(System.getProperty("line.separator")); //$NON-NLS-1$
138
buffer.append(" "); //$NON-NLS-1$
139
}
140                         if (elements[i].getValue().equals(".")) //$NON-NLS-1$
141
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 JavaDoc(file.getLocation().toOSString()), prop, false);
152                 file.refreshLocal(1, null);
153                 tracker.close();
154             } catch (FileNotFoundException JavaDoc e) {
155             } catch (IOException JavaDoc 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 JavaDoc e) {
164                 }
165             }
166         }
167     }
168     
169     private static Properties JavaDoc manifestToProperties(Attributes JavaDoc d) {
170         Iterator JavaDoc iter = d.keySet().iterator();
171         Properties JavaDoc result = new Properties JavaDoc();
172         while (iter.hasNext()) {
173             Attributes.Name JavaDoc key = (Attributes.Name JavaDoc) iter.next();
174             result.put(key.toString(), d.get(key));
175         }
176         return result;
177     }
178     
179 }
180
Popular Tags