KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.core;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Dictionary JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import javax.xml.parsers.DocumentBuilder JavaDoc;
22 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
23 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
24 import javax.xml.parsers.ParserConfigurationException JavaDoc;
25
26 import org.eclipse.osgi.service.resolver.BundleDescription;
27 import org.eclipse.osgi.util.ManifestElement;
28 import org.eclipse.pde.core.plugin.IPlugin;
29 import org.eclipse.pde.core.plugin.IPluginBase;
30 import org.eclipse.pde.core.plugin.IPluginLibrary;
31 import org.eclipse.pde.core.plugin.IPluginModelBase;
32 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
33 import org.osgi.framework.BundleException;
34 import org.osgi.framework.Constants;
35 import org.w3c.dom.Document JavaDoc;
36 import org.w3c.dom.Element JavaDoc;
37 import org.w3c.dom.Node JavaDoc;
38 import org.w3c.dom.NodeList JavaDoc;
39
40 public class PDEAuxiliaryState {
41     
42     private static String JavaDoc CACHE_EXTENSION = ".pluginInfo"; //$NON-NLS-1$
43

44     private static String JavaDoc ATTR_BUNDLE_ID = "bundleID"; //$NON-NLS-1$
45
private static String JavaDoc ATTR_BUNDLE_STRUCTURE = "isBundle"; //$NON-NLS-1$
46
private static String JavaDoc ATTR_CLASS = "class"; //$NON-NLS-1$
47
private static String JavaDoc ATTR_EXPORTED = "exported"; //$NON-NLS-1$
48
private static String JavaDoc ATTR_EXTENSIBLE_API = "hasExtensibleAPI"; //$NON-NLS-1$
49
private static String JavaDoc ATTR_LOCALIZATION = "localization"; //$NON-NLS-1$
50
private static String JavaDoc ATTR_NAME = "name"; //$NON-NLS-1$
51
private static String JavaDoc ATTR_PATCH = "patch"; //$NON-NLS-1$
52
private static String JavaDoc ATTR_PROJECT = "project"; //$NON-NLS-1$
53
private static String JavaDoc ATTR_PROVIDER = "provider"; //$NON-NLS-1$
54

55     private static String JavaDoc ELEMENT_BUNDLE = "bundle"; //$NON-NLS-1$
56
private static String JavaDoc ELEMENT_LIB = "library"; //$NON-NLS-1$
57
private static String JavaDoc ELEMENT_ROOT = "map"; //$NON-NLS-1$
58

59     protected Map JavaDoc fPluginInfos = new HashMap JavaDoc();
60     
61     protected PDEAuxiliaryState() {
62     }
63     
64     protected PDEAuxiliaryState(PDEAuxiliaryState state) {
65         this.fPluginInfos = new HashMap JavaDoc(state.fPluginInfos);
66     }
67
68     class PluginInfo {
69         String JavaDoc name;
70         String JavaDoc providerName;
71         String JavaDoc className;
72         boolean hasExtensibleAPI;
73         boolean isPatchFragment;
74         boolean hasBundleStructure;
75         String JavaDoc[] libraries;
76         String JavaDoc project;
77         String JavaDoc localization;
78     }
79     
80     private void createPluginInfo(Element element) {
81         PluginInfo info = new PluginInfo();
82         if (element.hasAttribute(ATTR_NAME))
83             info.name = element.getAttribute(ATTR_NAME);
84         if (element.hasAttribute(ATTR_PROVIDER))
85             info.providerName = element.getAttribute(ATTR_PROVIDER);
86         if (element.hasAttribute(ATTR_CLASS))
87             info.className = element.getAttribute(ATTR_CLASS);
88         info.hasExtensibleAPI = "true".equals(element.getAttribute(ATTR_EXTENSIBLE_API)); //$NON-NLS-1$
89
info.isPatchFragment = "true".equals(element.getAttribute(ATTR_PATCH)); //$NON-NLS-1$
90
info.hasBundleStructure = !"false".equals(element.getAttribute(ATTR_BUNDLE_STRUCTURE)); //$NON-NLS-1$
91
if (element.hasAttribute(ATTR_PROJECT))
92             info.project = element.getAttribute(ATTR_PROJECT);
93         if (element.hasAttribute(ATTR_LOCALIZATION))
94             info.localization = element.getAttribute(ATTR_LOCALIZATION);
95         
96         NodeList JavaDoc libs = element.getChildNodes();
97         ArrayList JavaDoc list = new ArrayList JavaDoc(libs.getLength());
98         for (int i = 0; i < libs.getLength(); i++) {
99             if (libs.item(i).getNodeType() == Node.ELEMENT_NODE) {
100                 Element lib = (Element)libs.item(i);
101                 list.add(lib.getAttribute(ATTR_NAME));
102             }
103         }
104         info.libraries = (String JavaDoc[])list.toArray(new String JavaDoc[list.size()]);
105         fPluginInfos.put(element.getAttribute(ATTR_BUNDLE_ID), info);
106     }
107     
108     public String JavaDoc getClassName(long bundleID) {
109         PluginInfo info = (PluginInfo)fPluginInfos.get(Long.toString(bundleID));
110         return info == null ? null : info.className;
111     }
112     
113     public boolean hasExtensibleAPI(long bundleID) {
114         PluginInfo info = (PluginInfo)fPluginInfos.get(Long.toString(bundleID));
115         return info == null ? false : info.hasExtensibleAPI;
116     }
117
118     public boolean isPatchFragment(long bundleID) {
119         PluginInfo info = (PluginInfo)fPluginInfos.get(Long.toString(bundleID));
120         return info == null ? false : info.isPatchFragment;
121     }
122     public boolean hasBundleStructure(long bundleID) {
123         PluginInfo info = (PluginInfo)fPluginInfos.get(Long.toString(bundleID));
124         return info == null ? false : info.hasBundleStructure;
125     }
126     
127     public String JavaDoc getPluginName(long bundleID) {
128         PluginInfo info = (PluginInfo)fPluginInfos.get(Long.toString(bundleID));
129         return info == null ? null : info.name;
130     }
131     
132     public String JavaDoc getProviderName(long bundleID) {
133         PluginInfo info = (PluginInfo)fPluginInfos.get(Long.toString(bundleID));
134         return info == null ? null : info.providerName;
135     }
136     
137     public String JavaDoc[] getLibraryNames(long bundleID) {
138         PluginInfo info = (PluginInfo)fPluginInfos.get(Long.toString(bundleID));
139         return info == null ? new String JavaDoc[0] : info.libraries;
140     }
141     
142     public String JavaDoc getBundleLocalization(long bundleID) {
143         PluginInfo info = (PluginInfo)fPluginInfos.get(Long.toString(bundleID));
144         return info == null ? null : info.localization;
145     }
146     
147     public String JavaDoc getProject(long bundleID) {
148         PluginInfo info = (PluginInfo)fPluginInfos.get(Long.toString(bundleID));
149         return info == null ? null : info.project;
150     }
151     
152     protected void savePluginInfo(File JavaDoc dir) {
153         try {
154             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
155             Document JavaDoc doc = factory.newDocumentBuilder().newDocument();
156             Element root = doc.createElement(ELEMENT_ROOT);
157             
158             Iterator JavaDoc iter = fPluginInfos.keySet().iterator();
159             while (iter.hasNext()) {
160                 String JavaDoc key = iter.next().toString();
161                 Element element = doc.createElement(ELEMENT_BUNDLE);
162                 element.setAttribute(ATTR_BUNDLE_ID, key);
163                 PluginInfo info = (PluginInfo)fPluginInfos.get(key);
164                 if (info.className != null)
165                     element.setAttribute(ATTR_CLASS, info.className);
166                 if (info.providerName != null)
167                     element.setAttribute(ATTR_PROVIDER, info.providerName);
168                 if (info.name != null)
169                     element.setAttribute(ATTR_NAME, info.name);
170                 if (info.hasExtensibleAPI)
171                     element.setAttribute(ATTR_EXTENSIBLE_API, "true"); //$NON-NLS-1$
172
if (info.isPatchFragment)
173                     element.setAttribute(ATTR_PATCH, "true"); //$NON-NLS-1$
174
if (!info.hasBundleStructure)
175                     element.setAttribute(ATTR_BUNDLE_STRUCTURE, "false"); //$NON-NLS-1$
176
if (info.localization != null)
177                     element.setAttribute(ATTR_LOCALIZATION, info.localization);
178                 if (info.libraries != null) {
179                     for (int i = 0; i < info.libraries.length; i++) {
180                         Element lib = doc.createElement(ELEMENT_LIB);
181                         lib.setAttribute(ATTR_NAME, info.libraries[i]);
182                         element.appendChild(lib);
183                     }
184                 }
185                 root.appendChild(element);
186             }
187             doc.appendChild(root);
188             XMLPrintHandler.writeFile(doc, new File JavaDoc(dir, CACHE_EXTENSION));
189         } catch (Exception JavaDoc e) {
190             PDECore.log(e);
191         }
192     }
193     
194     protected boolean readPluginInfoCache(File JavaDoc dir) {
195         File JavaDoc file = new File JavaDoc(dir, CACHE_EXTENSION);
196         if (file.exists() && file.isFile()) {
197             try {
198                 DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
199                 Document JavaDoc doc = factory.newDocumentBuilder().parse(file);
200                 Element root = doc.getDocumentElement();
201                 if (root != null) {
202                     NodeList JavaDoc list = root.getChildNodes();
203                     for (int i = 0; i < list.getLength(); i++) {
204                         if (list.item(i).getNodeType() == Node.ELEMENT_NODE)
205                             createPluginInfo((Element)list.item(i));
206                     }
207                 }
208                 return true;
209             } catch (org.xml.sax.SAXException JavaDoc e) {
210                 PDECore.log(e);
211             } catch (IOException JavaDoc e) {
212                 PDECore.log(e);
213             } catch (ParserConfigurationException JavaDoc e) {
214                 PDECore.log(e);
215             }
216         }
217         return false;
218     }
219
220     public static void writePluginInfo(IPluginModelBase[] models, File JavaDoc destination) {
221         try {
222             DocumentBuilder JavaDoc builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
223             Document JavaDoc doc = builder.newDocument();
224         
225             Element root = doc.createElement(ELEMENT_ROOT);
226             doc.appendChild(root);
227             for (int i = 0; i < models.length; i++) {
228                 IPluginBase plugin = models[i].getPluginBase();
229                 BundleDescription desc = models[i].getBundleDescription();
230                 Element element = doc.createElement(ELEMENT_BUNDLE);
231                 element.setAttribute(ATTR_BUNDLE_ID, Long.toString(desc.getBundleId()));
232                 element.setAttribute(ATTR_PROJECT, models[i].getUnderlyingResource().getProject().getName());
233                 if (plugin instanceof IPlugin && ((IPlugin)plugin).getClassName() != null)
234                     element.setAttribute(ATTR_CLASS, ((IPlugin)plugin).getClassName());
235                 if (plugin.getProviderName() != null)
236                     element.setAttribute(ATTR_PROVIDER, plugin.getProviderName());
237                 if (plugin.getName() != null)
238                     element.setAttribute(ATTR_NAME, plugin.getName());
239                 if (ClasspathUtilCore.hasExtensibleAPI(models[i]))
240                     element.setAttribute(ATTR_EXTENSIBLE_API, "true"); //$NON-NLS-1$
241
else if (ClasspathUtilCore.isPatchFragment(models[i]))
242                     element.setAttribute(ATTR_PATCH, "true"); //$NON-NLS-1$
243
if (!(models[i] instanceof IBundlePluginModelBase))
244                     element.setAttribute(ATTR_BUNDLE_STRUCTURE, "false"); //$NON-NLS-1$
245
if (models[i] instanceof IBundlePluginModelBase) {
246                     String JavaDoc localization = ((IBundlePluginModelBase)models[i]).getBundleLocalization();
247                     if (localization != null)
248                         element.setAttribute(ATTR_LOCALIZATION, localization);
249                 }
250                 IPluginLibrary[] libraries = plugin.getLibraries();
251                 for (int j = 0; j < libraries.length; j++) {
252                         Element lib = doc.createElement(ELEMENT_LIB);
253                         lib.setAttribute(ATTR_NAME, libraries[j].getName());
254                         if (!libraries[j].isExported())
255                             lib.setAttribute(ATTR_EXPORTED, "false"); //$NON-NLS-1$
256
element.appendChild(lib);
257                 }
258                 root.appendChild(element);
259             }
260             XMLPrintHandler.writeFile(doc, new File JavaDoc(destination, CACHE_EXTENSION));
261         } catch (ParserConfigurationException JavaDoc e) {
262         } catch (FactoryConfigurationError JavaDoc e) {
263         } catch (IOException JavaDoc e) {
264         }
265     }
266     
267     protected void addAuxiliaryData(BundleDescription desc, Dictionary JavaDoc manifest, boolean hasBundleStructure) {
268         PluginInfo info = new PluginInfo();
269         info.name = (String JavaDoc)manifest.get(Constants.BUNDLE_NAME);
270         info.providerName = (String JavaDoc)manifest.get(Constants.BUNDLE_VENDOR);
271         
272         String JavaDoc className = (String JavaDoc)manifest.get(ICoreConstants.PLUGIN_CLASS);
273         info.className = className != null ? className : (String JavaDoc)manifest.get(Constants.BUNDLE_ACTIVATOR);
274         info.libraries = getClasspath(manifest);
275         info.hasExtensibleAPI = "true".equals(manifest.get(ICoreConstants.EXTENSIBLE_API)); //$NON-NLS-1$
276
info.isPatchFragment = "true".equals(manifest.get(ICoreConstants.PATCH_FRAGMENT)); //$NON-NLS-1$
277
info.localization = (String JavaDoc)manifest.get(Constants.BUNDLE_LOCALIZATION);
278         info.hasBundleStructure = hasBundleStructure;
279         fPluginInfos.put(Long.toString(desc.getBundleId()), info);
280     }
281     
282     protected String JavaDoc[] getClasspath(Dictionary JavaDoc manifest) {
283         String JavaDoc fullClasspath = (String JavaDoc) manifest.get(Constants.BUNDLE_CLASSPATH);
284         String JavaDoc[] result = new String JavaDoc[0];
285         try {
286             if (fullClasspath != null) {
287                 ManifestElement[] classpathEntries = ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH, fullClasspath);
288                 result = new String JavaDoc[classpathEntries.length];
289                 for (int i = 0; i < classpathEntries.length; i++) {
290                     result[i] = classpathEntries[i].getValue();
291                 }
292             }
293         } catch (BundleException e) {
294         }
295         return result;
296     }
297     
298     protected void clear() {
299         fPluginInfos.clear();
300     }
301
302
303 }
304
Popular Tags