KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.update.internal.model;
12 import java.io.*;
13 import java.util.jar.*;
14
15 import org.eclipse.osgi.util.*;
16 import org.eclipse.update.core.*;
17 import org.osgi.framework.*;
18 /**
19  * Parses MANIFEST.MF
20  */

21 public class BundleManifest {
22     private PluginEntry pluginEntry;
23     private IOException exception;
24     /**
25      * Constructor for local file
26      */

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

48     public BundleManifest(InputStream input) {
49         super();
50         if (input != null) {
51             parse(input);
52         }
53     }
54     /**
55      * Parses manifest, creates PluginEntry if manifest is valid, stores
56      * exception if any occurs
57      *
58      * @param in
59      * InputStream
60      */

61     private void parse(InputStream in) {
62         try {
63             Manifest m = new Manifest(in);
64             Attributes a = m.getMainAttributes();
65             // plugin id
66
String JavaDoc symbolicName = a.getValue(Constants.BUNDLE_SYMBOLICNAME);
67             if (symbolicName == null) {
68                 // In Eclipse manifest must have Bundle-SymbolicName attribute
69
return;
70             }
71             String JavaDoc id;
72             try {
73                 ManifestElement[] elements = ManifestElement.parseHeader(
74                         Constants.BUNDLE_SYMBOLICNAME, symbolicName);
75                 id = elements[0].getValue();
76             } catch (BundleException be) {
77                 throw new IOException(be.getMessage());
78             }
79             // plugin version
80
String JavaDoc version = a.getValue(Constants.BUNDLE_VERSION);
81             if (version == null) {
82                 return;
83             }
84             String JavaDoc hostPlugin = a.getValue(Constants.FRAGMENT_HOST);
85             pluginEntry = new PluginEntry();
86             pluginEntry.setVersionedIdentifier(new VersionedIdentifier(id,
87                     version));
88             pluginEntry.isFragment(hostPlugin != null
89                     && hostPlugin.length() > 0);
90         } catch (IOException ioe) {
91             exception = ioe;
92         }
93     }
94     public boolean exists() {
95         return exception != null || pluginEntry != null;
96     }
97     /**
98      * Obtains PluginEntry from a manifest.
99      *
100      * @return PluginEntry or null if valid manifest does not exist
101      * @throws IOException
102      * if exception during parsing
103      */

104     public PluginEntry getPluginEntry() throws IOException {
105         if (exception != null) {
106             throw exception;
107         } else {
108             return pluginEntry;
109         }
110     }
111 }
112
Popular Tags