Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 51ee2c0b4960f72363b17172c493dc7afba272c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*******************************************************************************
 * Copyright (c) 2003, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.osgi.framework.adaptor.core;

import java.io.*;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import org.eclipse.osgi.framework.adaptor.PermissionStorage;
import org.eclipse.osgi.framework.debug.Debug;
import org.eclipse.osgi.framework.internal.core.ConditionalPermissionInfoImpl;
import org.eclipse.osgi.framework.internal.reliablefile.*;
import org.osgi.service.condpermadmin.ConditionInfo;
import org.osgi.service.condpermadmin.ConditionalPermissionInfo;
import org.osgi.service.permissionadmin.PermissionInfo;

/**
 * Class to model permission data storage.
 */

class DefaultPermissionStorage implements PermissionStorage {
	/** Filename used to store the ConditionalPermissions. This name
	 * is relative to permissionDir.*/
	private static final String CONDPERMS = "condPerms"; //$NON-NLS-1$
	/** Directory into which permission data files are stored. */
	protected File permissionDir;

	/** List of permission files: String location => File permission file */
	protected Hashtable permissionFiles;

	/** Default permission data. */
	protected File defaultData;

	/** First permission data format version */
	protected static final int PERMISSIONDATA_VERSION_1 = 1;

	/** Current permission data format version */
	protected static final int PERMISSIONDATA_VERSION = PERMISSIONDATA_VERSION_1;

	/**
	 * Constructor.
	 *
	 * @throws IOException If an error occurs initializing the object.
	 */
	protected DefaultPermissionStorage(AbstractFrameworkAdaptor adaptor) throws IOException {
		permissionDir = new File(adaptor.getBundleStoreRootDir(), "permdata"); //$NON-NLS-1$
		permissionFiles = new Hashtable();

		if (!permissionDir.exists() && !permissionDir.mkdirs()) {
			if (Debug.DEBUG && Debug.DEBUG_GENERAL) {
				Debug.println("Unable to create directory: " + permissionDir.getPath()); //$NON-NLS-1$
			}

			throw new IOException(AdaptorMsg.formatter.getString("ADAPTOR_STORAGE_EXCEPTION")); //$NON-NLS-1$
		}

		defaultData = new File(permissionDir, ".default"); //$NON-NLS-1$

		loadLocations();
	}

	/**
	 * Returns the locations that have permission data assigned to them,
	 * that is, locations for which permission data
	 * exists in persistent storage.
	 *
	 * @return The locations that have permission data in
	 * persistent storage, or <tt>null</tt> if there is no permission data
	 * in persistent storage.
	 * @throws IOException If a failure occurs accessing peristent storage.
	 */
	public synchronized String[] getLocations() throws IOException {
		int size = permissionFiles.size();

		if (size == 0) {
			return null;
		}

		String[] locations = new String[size];

		Enumeration keysEnum = permissionFiles.keys();

		for (int i = 0; i < size; i++) {
			locations[i] = (String) keysEnum.nextElement();
		}

		return locations;
	}

	/**
	 * Gets the permission data assigned to the specified
	 * location.
	 *
	 * @param location The location whose permission data is to
	 * be returned.
	 *
	 * @return The permission data assigned to the specified
	 * location, or <tt>null</tt> if that location has not been assigned any
	 * permission data.
	 * @throws IOException If a failure occurs accessing peristent storage.
	 */
	public synchronized String[] getPermissionData(String location) throws IOException {
		File file;

		if (location == null) {
			file = defaultData;
		} else {
			file = (File) permissionFiles.get(location);

			if (file == null) {
				return null;
			}
		}

		try {
			return readData(file);
		} catch (FileNotFoundException e) {
			return null;
		}
	}

	/**
	 * Assigns the specified permission data to the specified
	 * location.
	 *
	 * @param location The location that will be assigned the
	 *                 permissions.
	 * @param data The permission data to be assigned, or <tt>null</tt>
	 * if the specified location is to be removed from persistent storaqe.
	 * @throws IOException If a failure occurs modifying peristent storage.
	 */
	public synchronized void setPermissionData(String location, String[] data) throws IOException {
		File file;

		if (location == null) {
			file = defaultData;

			if (data == null) {
				ReliableFile.delete(defaultData);
			} else {
				save(defaultData, null, data); /* Save the value in persistent storage */
			}
		} else {
			file = (File) permissionFiles.get(location);

			if (data == null) {
				if (file == null) {
					return;
				}

				permissionFiles.remove(location);

				ReliableFile.delete(file);
			} else {
				file = save(file, location, data); /* Save the value in persistent storage */

				permissionFiles.put(location, file);
			}
		}
	}

	/**
	 * Load the locations for which permission data exists.
	 *
	 * @throws IOException If an error occurs reading the files.
	 */
	protected void loadLocations() throws IOException {
		String list[] = permissionDir.list();
		if (list == null)
			return;
		int len = list.length;

		for (int i = 0; i < len; i++) {
			String name = list[i];

			if (name.endsWith(ReliableFile.newExt)) {
				continue;
			}

			if (name.endsWith(ReliableFile.oldExt)) {
				continue;
			}

			if (name.endsWith(ReliableFile.tmpExt)) {
				continue;
			}
			if (name.equals(CONDPERMS)) {
				continue;
			}

			File file = new File(permissionDir, name);

			try {
				String location = readLocation(file);

				if (location != null) {
					permissionFiles.put(location, file);
				}
			} catch (FileNotFoundException e) {
				/* the file should have been there */
			}
		}
	}

	/**
	 * Read the location from the specified file.
	 *
	 * @param file File to read the location from.
	 * @return Location from the file or null if the file is unknown.
	 * @throws IOException If an error occurs reading the file.
	 * @throws FileNotFoundException if the data file does not exist.
	 */
	private String readLocation(File file) throws IOException {
		DataInputStream in = new DataInputStream(new ReliableFileInputStream(file));
		try {
			int version = in.readInt();

			switch (version) {
				case PERMISSIONDATA_VERSION_1 : {
					boolean locationPresent = in.readBoolean();

					if (locationPresent) {
						String location = in.readUTF();

						return location;
					}
					break;
				}
				default : {
					throw new IOException(AdaptorMsg.formatter.getString("ADAPTOR_STORAGE_EXCEPTION")); //$NON-NLS-1$
				}
			}
		} finally {
			in.close();
		}

		return null;
	}

	/**
	 * Read the permission data from the specified file.
	 *
	 * @param file File to read the permission data from.
	 * @throws IOException If an error occurs reading the file.
	 * @throws FileNotFoundException if the data file does not exist.
	 */
	private String[] readData(File file) throws IOException {
		DataInputStream in = new DataInputStream(new ReliableFileInputStream(file));
		try {
			int version = in.readInt();

			switch (version) {
				case PERMISSIONDATA_VERSION_1 : {
					boolean locationPresent = in.readBoolean();

					if (locationPresent) {
						String location = in.readUTF();
					}

					int size = in.readInt();
					String[] data = new String[size];

					for (int i = 0; i < size; i++) {
						data[i] = in.readUTF();
					}

					return data;
				}
				default : {
					throw new IOException(AdaptorMsg.formatter.getString("ADAPTOR_STORAGE_EXCEPTION")); //$NON-NLS-1$
				}
			}
		} finally {
			in.close();
		}
	}

	/**
	 * Save the permission data for the specified location.
	 * This assumes an attempt has been made to load
	 * the specified location just prior to calling save.
	 */
	protected File save(File file, String location, String[] data) throws IOException {
		if (file == null) /* we need to create a filename */{
			file = File.createTempFile("perm", "", permissionDir); //$NON-NLS-1$ //$NON-NLS-2$
			file.delete(); /* delete the empty file */
		}

		int size = data.length;

		DataOutputStream out = new DataOutputStream(new ReliableFileOutputStream(file));

		try {
			out.writeInt(PERMISSIONDATA_VERSION);
			if (location == null) {
				out.writeBoolean(false);
			} else {
				out.writeBoolean(true);
				out.writeUTF(location);
			}
			out.writeInt(size);

			for (int i = 0; i < size; i++) {
				out.writeUTF(data[i]);
			}

		} finally {
			out.close();
		}

		return file;
	}

	/**
	 * Serializes the ConditionalPermissionInfos to CONDPERMS. Serialization is done
	 * by writing out each ConditionalPermissionInfo as a set of ConditionInfos 
	 * followed by PermissionInfos followed by a blank line.
	 * 
	 * @param v the Vector to be serialized that contains the ConditionalPermissionInfos.
	 * @throws IOException
	 * @see org.eclipse.osgi.framework.adaptor.PermissionStorage#serializeConditionalPermissionInfos(Vector)
	 */
	public void serializeConditionalPermissionInfos(Vector v) throws IOException {
		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(permissionDir, CONDPERMS))));
			Enumeration en = v.elements();
			while (en.hasMoreElements()) {
				ConditionalPermissionInfo cpi = (ConditionalPermissionInfo) en.nextElement();
				ConditionInfo cis[] = cpi.getConditionInfos();
				PermissionInfo pis[] = cpi.getPermissionInfos();
				for (int i = 0; i < cis.length; i++) {
					writer.write(cis[i].getEncoded());
					writer.newLine();
				}
				for (int i = 0; i < pis.length; i++) {
					writer.write(pis[i].getEncoded());
					writer.newLine();
				}
				writer.newLine();
			}
		} finally {
			if (writer != null)
				writer.close();
		}
	}

	/**
	 * Deserializes the ConditionalPermissionInfos from CONDPERMS and returns the object.
	 * 
	 * @return the deserialized object that was previously passed to serializeCondationalPermissionInfos.
	 * @throws IOException
	 * @see org.eclipse.osgi.framework.adaptor.PermissionStorage#deserializeConditionalPermissionInfos()
	 */
	public Vector deserializeConditionalPermissionInfos() throws IOException {
		BufferedReader reader = null;
		Vector v = new Vector(15);
		try {
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(permissionDir, CONDPERMS))));
			String line;
			Vector c = new Vector(3);
			Vector p = new Vector(3);
			while((line = reader.readLine()) != null) {
				if (line.length() == 0) {
					ConditionalPermissionInfoImpl cpi;
					cpi = new ConditionalPermissionInfoImpl((ConditionInfo[])c.toArray(new ConditionInfo[0]), (PermissionInfo[])p.toArray(new PermissionInfo[0]));
					v.add(cpi);
					c.clear();
					p.clear();
				} else if (line.startsWith("(")) { //$NON-NLS-1$
					p.add(new PermissionInfo(line));
				} else if (line.startsWith("[")) { //$NON-NLS-1$
					c.add(new ConditionInfo(line));
				}
			}
		} catch (FileNotFoundException e) {
			// do nothing return empty vector
		} catch (IOException e) {
			throw e;
		} catch (Exception e) {
			throw new IOException(e.getMessage());
		} finally {
			if (reader != null)
				reader.close();
		}
		return v;
	}
}

Back to the top