KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > configurator > BundleManifest


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  * James D Miles (IBM Corp.) - bug 182666, trim spaces from version id
11  *******************************************************************************/

12 package org.eclipse.update.internal.configurator;
13 import java.io.*;
14 import java.util.jar.*;
15
16 import org.eclipse.osgi.util.*;
17 import org.osgi.framework.*;
18 /**
19  * Parses MANIFEST.MF
20  */

21 public class BundleManifest implements IConfigurationConstants {
22     private File manifestFile;
23     private PluginEntry pluginEntry;
24     private IOException exception;
25     private String JavaDoc bundleURL;
26     /**
27      * Constructor for local file
28      */

29     public BundleManifest(File manifest) {
30         super();
31         manifestFile = manifest;
32         if (manifest.exists() && !manifest.isDirectory()) {
33             FileInputStream fos = null;
34             try {
35                 fos = new FileInputStream(manifest);
36                 parse(fos);
37             } catch (IOException ioe) {
38             } finally {
39                 if (fos != null) {
40                     try {
41                         fos.close();
42                     } catch (IOException e) {
43                     }
44                 }
45             }
46         }
47     }
48         /**
49          * Constructor for local file
50          */

51         public BundleManifest(InputStream input, String JavaDoc bundleUrl) {
52             super();
53             bundleURL = bundleUrl;
54             if (input != null) {
55                 parse(input);
56             }
57         }
58     /**
59      * Parses manifest, creates PluginEntry if manifest is valid, stores
60      * exception if any occurs
61      *
62      * @param in
63      * InputStream
64      */

65     private void parse(InputStream in) {
66         try {
67             Manifest m = new Manifest(in);
68             Attributes a = m.getMainAttributes();
69             // plugin id
70
String JavaDoc symbolicName = a.getValue(Constants.BUNDLE_SYMBOLICNAME);
71             if (symbolicName == null) {
72                 // In Eclipse manifest must have Bundle-SymbolicName attribute
73
return;
74             }
75             String JavaDoc id;
76             try {
77                 ManifestElement[] elements = ManifestElement.parseHeader(
78                         Constants.BUNDLE_SYMBOLICNAME, symbolicName);
79                 id = elements[0].getValue();
80             } catch (BundleException be) {
81                 throw new IOException(be.getMessage());
82             }
83             // plugin version
84
String JavaDoc version = a.getValue(Constants.BUNDLE_VERSION);
85             if (version == null) {
86                 Utils.log(NLS.bind(Messages.BundleManifest_noVersion, (new String JavaDoc[] { Constants.BUNDLE_VERSION, id })));
87                 return;
88             }
89             version = version.trim();
90             String JavaDoc hostPlugin = a.getValue(Constants.FRAGMENT_HOST);
91             pluginEntry = new PluginEntry();
92             pluginEntry.setVersionedIdentifier(new VersionedIdentifier(id,
93                     version));
94             pluginEntry.isFragment(hostPlugin != null
95                     && hostPlugin.length() > 0);
96             // Set URL
97
if(bundleURL!=null){
98                 pluginEntry.setURL(bundleURL);
99             }else{
100                 File pluginDir = manifestFile.getParentFile();
101                 if (pluginDir != null) {
102                     pluginDir = pluginDir.getParentFile();
103                 }
104                 if (pluginDir != null){
105                     pluginEntry.setURL(PLUGINS + "/" + pluginDir.getName() + "/"); //$NON-NLS-1$ //$NON-NLS-2$
106
}
107             }
108             //
109
} catch (IOException ioe) {
110             exception = ioe;
111         }
112     }
113     public boolean exists() {
114         return exception != null || pluginEntry != null;
115     }
116     /**
117      * Obtains PluginEntry from a manifest.
118      *
119      * @return PluginEntry or null if valid manifest does not exist
120      * @throws IOException
121      * if exception during parsing
122      */

123     public PluginEntry getPluginEntry() throws IOException {
124         if (exception != null) {
125             throw exception;
126         }
127         return pluginEntry;
128     }
129 }
130
Popular Tags