KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > baseadaptor > bundlefile > BundleFile


1 /*******************************************************************************
2  * Copyright (c) 2004, 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
12 package org.eclipse.osgi.baseadaptor.bundlefile;
13
14 import java.io.*;
15 import java.net.MalformedURLException JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.security.AccessController JavaDoc;
18 import java.util.*;
19 import org.eclipse.osgi.framework.internal.core.Constants;
20 import org.eclipse.osgi.framework.internal.core.FrameworkProperties;
21 import org.eclipse.osgi.framework.internal.protocol.bundleresource.Handler;
22 import org.eclipse.osgi.framework.util.SecureAction;
23 import org.eclipse.osgi.util.ManifestElement;
24
25 /**
26  * The BundleFile API is used by Adaptors to read resources out of an
27  * installed Bundle in the Framework.
28  * <p>
29  * Clients may extend this class.
30  * </p>
31  * @since 3.2
32  */

33 abstract public class BundleFile {
34     protected static final String JavaDoc PROP_SETPERMS_CMD = "osgi.filepermissions.command"; //$NON-NLS-1$
35
static final SecureAction secureAction = (SecureAction) AccessController.doPrivileged(SecureAction.createSecureAction());
36     /**
37      * The File object for this BundleFile.
38      */

39     protected File basefile;
40     private int mruIndex = -1;
41
42     /**
43      * Default constructor
44      *
45      */

46     public BundleFile() {
47         // do nothing
48
}
49
50     /**
51      * BundleFile constructor
52      * @param basefile The File object where this BundleFile is
53      * persistently stored.
54      */

55     public BundleFile(File basefile) {
56         this.basefile = basefile;
57     }
58
59     /**
60      * Returns a File for the bundle entry specified by the path.
61      * If required the content of the bundle entry is extracted into a file
62      * on the file system.
63      * @param path The path to the entry to locate a File for.
64      * @param nativeCode true if the path is native code.
65      * @return A File object to access the contents of the bundle entry.
66      */

67     abstract public File getFile(String JavaDoc path, boolean nativeCode);
68
69     /**
70      * Locates a file name in this bundle and returns a BundleEntry object
71      *
72      * @param path path of the entry to locate in the bundle
73      * @return BundleEntry object or null if the file name
74      * does not exist in the bundle
75      */

76     abstract public BundleEntry getEntry(String JavaDoc path);
77
78     /**
79      * Allows to access the entries of the bundle.
80      * Since the bundle content is usually a jar, this
81      * allows to access the jar contents.
82      *
83      * GetEntryPaths allows to enumerate the content of "path".
84      * If path is a directory, it is equivalent to listing the directory
85      * contents. The returned names are either files or directories
86      * themselves. If a returned name is a directory, it finishes with a
87      * slash. If a returned name is a file, it does not finish with a slash.
88      * @param path path of the entry to locate in the bundle
89      * @return an Enumeration of Strings that indicate the paths found or
90      * null if the path does not exist.
91      */

92     abstract public Enumeration getEntryPaths(String JavaDoc path);
93
94     /**
95      * Closes the BundleFile.
96      * @throws IOException if any error occurs.
97      */

98     abstract public void close() throws IOException;
99
100     /**
101      * Opens the BundleFiles.
102      * @throws IOException if any error occurs.
103      */

104     abstract public void open() throws IOException;
105
106     /**
107      * Determines if any BundleEntries exist in the given directory path.
108      * @param dir The directory path to check existence of.
109      * @return true if the BundleFile contains entries under the given directory path;
110      * false otherwise.
111      */

112     abstract public boolean containsDir(String JavaDoc dir);
113
114     /**
115      * Returns a URL to access the contents of the entry specified by the path
116      * @param path the path to the resource
117      * @param hostBundleID the host bundle ID
118      * @return a URL to access the contents of the entry specified by the path
119      */

120     public URL JavaDoc getResourceURL(String JavaDoc path, long hostBundleID) {
121         return getResourceURL(path, hostBundleID, 0);
122     }
123
124     /**
125      * Returns a URL to access the contents of the entry specified by the path
126      * @param path the path to the resource
127      * @param hostBundleID the host bundle ID
128      * @param index the resource index
129      * @return a URL to access the contents of the entry specified by the path
130      */

131     public URL JavaDoc getResourceURL(String JavaDoc path, long hostBundleID, int index) {
132         BundleEntry bundleEntry = getEntry(path);
133         if (bundleEntry == null)
134             return null;
135         if (path.length() == 0 || path.charAt(0) != '/')
136             path = '/' + path;
137         try {
138             //use the constant string for the protocol to prevent duplication
139
return secureAction.getURL(Constants.OSGI_RESOURCE_URL_PROTOCOL, Long.toString(hostBundleID), index, path, new Handler(bundleEntry));
140         } catch (MalformedURLException JavaDoc e) {
141             return null;
142         }
143     }
144
145     /**
146      * Returns the base file for this BundleFile
147      * @return the base file for this BundleFile
148      */

149     public File getBaseFile() {
150         return basefile;
151     }
152
153     void setMruIndex(int index) {
154         mruIndex = index;
155     }
156
157     int getMruIndex() {
158         return mruIndex;
159     }
160
161     /**
162      * Attempts to set the permissions of the file in a system dependant way.
163      * @param file the file to set the permissions on
164      */

165     public static void setPermissions(File file) {
166         String JavaDoc commandProp = FrameworkProperties.getProperty(PROP_SETPERMS_CMD);
167         if (commandProp == null)
168             return;
169         String JavaDoc[] temp = ManifestElement.getArrayFromList(commandProp, " "); //$NON-NLS-1$
170
ArrayList command = new ArrayList(temp.length + 1);
171         boolean foundFullPath = false;
172         for (int i = 0; i < temp.length; i++) {
173             if ("[fullpath]".equals(temp[i])) { //$NON-NLS-1$
174
command.add(file.getAbsolutePath());
175                 foundFullPath = true;
176             }
177             else
178                 command.add(temp[i]);
179         }
180         if (!foundFullPath)
181             command.add(file.getAbsolutePath());
182         try {
183             Runtime.getRuntime().exec((String JavaDoc[]) command.toArray(new String JavaDoc[command.size()])).waitFor();
184         } catch (Exception JavaDoc e) {
185             e.printStackTrace();
186         }
187     }
188
189     public String JavaDoc toString() {
190         return String.valueOf(basefile);
191     }
192 }
193
Popular Tags