KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > internal > core > DefaultPermissionStorage


1 /*******************************************************************************
2  * Copyright (c) 2003, 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
12 package org.eclipse.osgi.framework.internal.core;
13
14 import java.io.*;
15 import java.util.*;
16 import org.eclipse.osgi.framework.adaptor.PermissionStorage;
17 import org.eclipse.osgi.framework.adaptor.core.AbstractFrameworkAdaptor;
18 import org.eclipse.osgi.framework.adaptor.core.AdaptorMsg;
19 import org.eclipse.osgi.framework.debug.Debug;
20 import org.eclipse.osgi.framework.internal.core.ConditionalPermissionInfoImpl;
21 import org.eclipse.osgi.framework.internal.reliablefile.*;
22 import org.eclipse.osgi.util.NLS;
23 import org.osgi.service.condpermadmin.ConditionInfo;
24 import org.osgi.service.condpermadmin.ConditionalPermissionInfo;
25 import org.osgi.service.permissionadmin.PermissionInfo;
26
27 /**
28  * Class to model permission data storage.
29  */

30
31 public class DefaultPermissionStorage implements PermissionStorage {
32     /** Filename used to store the ConditionalPermissions. This name
33      * is relative to permissionDir.*/

34     private static final String JavaDoc CONDPERMS = "condPerms"; //$NON-NLS-1$
35
/** Directory into which permission data files are stored. */
36     protected File permissionDir;
37
38     /** List of permission files: String location => File permission file */
39     protected Hashtable permissionFiles;
40
41     /** Default permission data. */
42     protected File defaultData;
43
44     /** First permission data format version */
45     protected static final int PERMISSIONDATA_VERSION_1 = 1;
46
47     /** Current permission data format version */
48     protected static final int PERMISSIONDATA_VERSION = PERMISSIONDATA_VERSION_1;
49
50     /**
51      * Constructor.
52      *
53      * @throws IOException If an error occurs initializing the object.
54      */

55     public DefaultPermissionStorage(AbstractFrameworkAdaptor adaptor) throws IOException {
56         permissionDir = new File(adaptor.getBundleStoreRootDir(), "permdata"); //$NON-NLS-1$
57
permissionFiles = new Hashtable();
58
59         if (!permissionDir.exists() && !permissionDir.mkdirs()) {
60             if (Debug.DEBUG && Debug.DEBUG_GENERAL) {
61                 Debug.println("Unable to create directory: " + permissionDir.getPath()); //$NON-NLS-1$
62
}
63
64             throw new IOException(NLS.bind(AdaptorMsg.ADAPTOR_DIRECTORY_CREATE_EXCEPTION, permissionDir)); //$NON-NLS-1$
65
}
66
67         defaultData = new File(permissionDir, ".default"); //$NON-NLS-1$
68

69         loadLocations();
70     }
71
72     /**
73      * Returns the locations that have permission data assigned to them,
74      * that is, locations for which permission data
75      * exists in persistent storage.
76      *
77      * @return The locations that have permission data in
78      * persistent storage, or <tt>null</tt> if there is no permission data
79      * in persistent storage.
80      * @throws IOException If a failure occurs accessing peristent storage.
81      */

82     public synchronized String JavaDoc[] getLocations() throws IOException {
83         int size = permissionFiles.size();
84
85         if (size == 0) {
86             return null;
87         }
88
89         String JavaDoc[] locations = new String JavaDoc[size];
90
91         Enumeration keysEnum = permissionFiles.keys();
92
93         for (int i = 0; i < size; i++) {
94             locations[i] = (String JavaDoc) keysEnum.nextElement();
95         }
96
97         return locations;
98     }
99
100     /**
101      * Gets the permission data assigned to the specified
102      * location.
103      *
104      * @param location The location whose permission data is to
105      * be returned.
106      *
107      * @return The permission data assigned to the specified
108      * location, or <tt>null</tt> if that location has not been assigned any
109      * permission data.
110      * @throws IOException If a failure occurs accessing peristent storage.
111      */

112     public synchronized String JavaDoc[] getPermissionData(String JavaDoc location) throws IOException {
113         File file;
114
115         if (location == null) {
116             file = defaultData;
117         } else {
118             file = (File) permissionFiles.get(location);
119
120             if (file == null) {
121                 return null;
122             }
123         }
124
125         try {
126             return readData(file);
127         } catch (FileNotFoundException e) {
128             return null;
129         }
130     }
131
132     /**
133      * Assigns the specified permission data to the specified
134      * location.
135      *
136      * @param location The location that will be assigned the
137      * permissions.
138      * @param data The permission data to be assigned, or <tt>null</tt>
139      * if the specified location is to be removed from persistent storaqe.
140      * @throws IOException If a failure occurs modifying peristent storage.
141      */

142     public synchronized void setPermissionData(String JavaDoc location, String JavaDoc[] data) throws IOException {
143         File file;
144
145         if (location == null) {
146             file = defaultData;
147
148             if (data == null) {
149                 ReliableFile.delete(defaultData);
150             } else {
151                 save(defaultData, null, data); /* Save the value in persistent storage */
152             }
153         } else {
154             file = (File) permissionFiles.get(location);
155
156             if (data == null) {
157                 if (file == null) {
158                     return;
159                 }
160
161                 permissionFiles.remove(location);
162
163                 ReliableFile.delete(file);
164             } else {
165                 file = save(file, location, data); /* Save the value in persistent storage */
166
167                 permissionFiles.put(location, file);
168             }
169         }
170     }
171
172     /**
173      * Load the locations for which permission data exists.
174      *
175      * @throws IOException If an error occurs reading the files.
176      */

177     protected void loadLocations() throws IOException {
178         String JavaDoc list[] = ReliableFile.getBaseFiles(permissionDir);
179         if (list == null)
180             return;
181         int len = list.length;
182
183         for (int i = 0; i < len; i++) {
184             String JavaDoc name = list[i];
185
186             if (name.endsWith(ReliableFile.tmpExt)) {
187                 continue;
188             }
189             if (name.equals(CONDPERMS)) {
190                 continue;
191             }
192
193             File file = new File(permissionDir, name);
194
195             try {
196                 String JavaDoc location = readLocation(file);
197
198                 if (location != null) {
199                     permissionFiles.put(location, file);
200                 }
201             } catch (FileNotFoundException e) {
202                 /* the file should have been there */
203             }
204         }
205     }
206
207     /**
208      * Read the location from the specified file.
209      *
210      * @param file File to read the location from.
211      * @return Location from the file or null if the file is unknown.
212      * @throws IOException If an error occurs reading the file.
213      * @throws FileNotFoundException if the data file does not exist.
214      */

215     private String JavaDoc readLocation(File file) throws IOException {
216         DataInputStream in = new DataInputStream(new ReliableFileInputStream(file));
217         try {
218             int version = in.readInt();
219
220             switch (version) {
221                 case PERMISSIONDATA_VERSION_1 : {
222                     boolean locationPresent = in.readBoolean();
223
224                     if (locationPresent) {
225                         String JavaDoc location = in.readUTF();
226
227                         return location;
228                     }
229                     break;
230                 }
231                 default : {
232                     throw new IOException(AdaptorMsg.ADAPTOR_STORAGE_EXCEPTION);
233                 }
234             }
235         } finally {
236             in.close();
237         }
238
239         return null;
240     }
241
242     /**
243      * Read the permission data from the specified file.
244      *
245      * @param file File to read the permission data from.
246      * @throws IOException If an error occurs reading the file.
247      * @throws FileNotFoundException if the data file does not exist.
248      */

249     private String JavaDoc[] readData(File file) throws IOException {
250         DataInputStream in = new DataInputStream(new ReliableFileInputStream(file));
251         try {
252             int version = in.readInt();
253
254             switch (version) {
255                 case PERMISSIONDATA_VERSION_1 : {
256                     boolean locationPresent = in.readBoolean();
257
258                     if (locationPresent) {
259                         String JavaDoc location = in.readUTF();
260                     }
261
262                     int size = in.readInt();
263                     String JavaDoc[] data = new String JavaDoc[size];
264
265                     for (int i = 0; i < size; i++) {
266                         data[i] = in.readUTF();
267                     }
268
269                     return data;
270                 }
271                 default : {
272                     throw new IOException(AdaptorMsg.ADAPTOR_STORAGE_EXCEPTION);
273                 }
274             }
275         } finally {
276             in.close();
277         }
278     }
279
280     /**
281      * Save the permission data for the specified location.
282      * This assumes an attempt has been made to load
283      * the specified location just prior to calling save.
284      */

285     protected File save(File file, String JavaDoc location, String JavaDoc[] data) throws IOException {
286         if (file == null) /* we need to create a filename */{
287             file = File.createTempFile("perm", "", permissionDir); //$NON-NLS-1$ //$NON-NLS-2$
288
file.delete(); /* delete the empty file */
289         }
290
291         int size = data.length;
292
293         DataOutputStream out = new DataOutputStream(new ReliableFileOutputStream(file));
294
295         try {
296             out.writeInt(PERMISSIONDATA_VERSION);
297             if (location == null) {
298                 out.writeBoolean(false);
299             } else {
300                 out.writeBoolean(true);
301                 out.writeUTF(location);
302             }
303             out.writeInt(size);
304
305             for (int i = 0; i < size; i++) {
306                 out.writeUTF(data[i]);
307             }
308
309         } finally {
310             out.close();
311         }
312
313         return file;
314     }
315
316     /**
317      * Serializes the ConditionalPermissionInfos to CONDPERMS. Serialization is done
318      * by writing out each ConditionalPermissionInfo as a set of ConditionInfos
319      * followed by PermissionInfos followed by a blank line.
320      *
321      * @param v the Vector to be serialized that contains the ConditionalPermissionInfos.
322      * @throws IOException
323      * @see org.eclipse.osgi.framework.adaptor.PermissionStorage#serializeConditionalPermissionInfos(Vector)
324      */

325     public void serializeConditionalPermissionInfos(Vector v) throws IOException {
326         BufferedWriter writer = null;
327         try {
328             writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(permissionDir, CONDPERMS))));
329             Enumeration en = v.elements();
330             while (en.hasMoreElements()) {
331                 ConditionalPermissionInfo cpi = (ConditionalPermissionInfo) en.nextElement();
332                 ConditionInfo cis[] = cpi.getConditionInfos();
333                 PermissionInfo pis[] = cpi.getPermissionInfos();
334                 writer.write('#');
335                 writer.write(((ConditionalPermissionInfoImpl) cpi).getName());
336                 writer.newLine();
337                 for (int i = 0; i < cis.length; i++) {
338                     writer.write(cis[i].getEncoded());
339                     writer.newLine();
340                 }
341                 for (int i = 0; i < pis.length; i++) {
342                     writer.write(pis[i].getEncoded());
343                     writer.newLine();
344                 }
345                 writer.newLine();
346             }
347         } finally {
348             if (writer != null)
349                 writer.close();
350         }
351     }
352
353     /**
354      * Deserializes the ConditionalPermissionInfos from CONDPERMS and returns the object.
355      *
356      * @return the deserialized object that was previously passed to serializeCondationalPermissionInfos.
357      * @throws IOException
358      * @see org.eclipse.osgi.framework.adaptor.PermissionStorage#deserializeConditionalPermissionInfos()
359      */

360     public Vector deserializeConditionalPermissionInfos() throws IOException {
361         BufferedReader reader = null;
362         Vector v = new Vector(15);
363         try {
364             reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(permissionDir, CONDPERMS))));
365             String JavaDoc line;
366             Vector c = new Vector(3);
367             Vector p = new Vector(3);
368             String JavaDoc id = null;
369             while ((line = reader.readLine()) != null) {
370                 if (line.length() == 0) {
371                     ConditionalPermissionInfoImpl cpi;
372                     cpi = new ConditionalPermissionInfoImpl(id, (ConditionInfo[]) c.toArray(new ConditionInfo[0]), (PermissionInfo[]) p.toArray(new PermissionInfo[0]));
373                     v.add(cpi);
374                     c.clear();
375                     p.clear();
376                     id = null;
377                 } else if (line.startsWith("(")) { //$NON-NLS-1$
378
p.add(new PermissionInfo(line));
379                 } else if (line.startsWith("[")) { //$NON-NLS-1$
380
c.add(new ConditionInfo(line));
381                 } else if (line.startsWith("#")) { //$NON-NLS-1$
382
id = line.substring(1);
383                 }
384             }
385         } catch (FileNotFoundException e) {
386             // do nothing return empty vector
387
} catch (IOException e) {
388             throw e;
389         } catch (Exception JavaDoc e) {
390             throw new IOException(e.getMessage());
391         } finally {
392             if (reader != null)
393                 reader.close();
394         }
395         return v;
396     }
397 }
398
Popular Tags