Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a7f29d1dd2974ea9118132884fd6a4ff61095921 (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
/*******************************************************************************
 * Copyright (c) 2003, 2012 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.storage;

import java.io.*;
import java.util.*;

/**
 * Permission Storage interface for managing a persistent storage of
 * bundle permissions.
 *
 * <p>This class is used to provide methods to manage
 * persistent storage of bundle permissions.
 */
public class PermissionData {
	private static final int PERMDATA_VERSION = 1;
	private final Map<String, String[]> locations = new HashMap<String, String[]>();
	private String[] defaultInfos;
	private String[] condPermInfos;
	private boolean dirty;

	/**
	 * 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.
	 */
	public String[] getLocations() {
		synchronized (locations) {
			String[] result = new String[locations.size()];
			int i = 0;
			for (Iterator<String> iLocs = locations.keySet().iterator(); iLocs.hasNext(); i++)
				result[i] = iLocs.next();
			return result;
		}
	}

	/**
	 * Gets the permission data assigned to the specified
	 * location.
	 *
	 * @param location The location whose permission data is to
	 * be returned.
	 * The location can be <tt>null</tt> for the default permission data.
	 *
	 * @return The permission data assigned to the specified
	 * location, or <tt>null</tt> if that location has not been assigned any
	 * permission data.
	 */
	public String[] getPermissionData(String location) {
		if (location == null)
			return defaultInfos;
		synchronized (locations) {
			if (locations.size() == 0)
				return null;
			return locations.get(location);
		}
	}

	/**
	 * Assigns the specified permission data to the specified
	 * location.
	 *
	 * @param location The location that will be assigned the
	 * permissions.
	 * The location can be <tt>null</tt> for the default permission data.
	 * @param data The permission data to be assigned, or <tt>null</tt>
	 * if the specified location is to be removed from persistent storaqe.
	 */
	public void setPermissionData(String location, String[] data) {
		if (location == null) {
			defaultInfos = data;
			return;
		}
		synchronized (locations) {
			if (data == null)
				locations.remove(location);
			else
				locations.put(location, data);
		}
		setDirty(true);
	}

	/**
	 * Persists the array of encoded ConditionalPermissionInfo strings
	 * @param infos an array of encoded ConditionalPermissionInfo strings
	 */
	public void saveConditionalPermissionInfos(String[] infos) {
		condPermInfos = infos;
		setDirty(true);
	}

	/**
	 * Returns the persistent array of encoded ConditionalPermissionInfo strings
	 * @return an array of encoded ConditionalPermissionInfo strings or null 
	 * if none exist in persistent storage.
	 */
	public String[] getConditionalPermissionInfos() {
		return condPermInfos;
	}

	boolean isDirty() {
		return dirty;
	}

	private void setDirty(boolean dirty) {
		this.dirty = dirty;
	}

	void readPermissionData(DataInputStream in) throws IOException {
		int version = in.readInt();
		int dataSize = in.readInt();
		byte[] bytes = new byte[dataSize];
		in.readFully(bytes);
		if (PERMDATA_VERSION == version) {
			DataInputStream temp = new DataInputStream(new ByteArrayInputStream(bytes));
			try {
				// read the default permissions first
				int numPerms = temp.readInt();
				if (numPerms > 0) {
					String[] perms = new String[numPerms];
					for (int i = 0; i < numPerms; i++)
						perms[i] = temp.readUTF();
					setPermissionData(null, perms);
				}
				int numLocs = temp.readInt();
				if (numLocs > 0) {
					for (int i = 0; i < numLocs; i++) {
						String loc = temp.readUTF();
						numPerms = temp.readInt();
						String[] perms = new String[numPerms];
						for (int j = 0; j < numPerms; j++)
							perms[j] = temp.readUTF();
						setPermissionData(loc, perms);
					}
				}
				int numCondPerms = temp.readInt();
				if (numCondPerms > 0) {
					String[] condPerms = new String[numCondPerms];
					for (int i = 0; i < numCondPerms; i++) {
						condPerms[i] = temp.readUTF();
					}
					saveConditionalPermissionInfos(condPerms);
				}

			} finally {
				setDirty(false);
				temp.close();
			}
		}
	}

	void savePermissionData(DataOutputStream out) throws IOException {
		out.writeInt(PERMDATA_VERSION);
		// create a temporary in memory stream so we can figure out the length
		ByteArrayOutputStream tempBytes = new ByteArrayOutputStream();
		DataOutputStream temp = new DataOutputStream(tempBytes);
		// always write the default permissions first
		String[] defaultPerms = getPermissionData(null);
		temp.writeInt(defaultPerms == null ? 0 : defaultPerms.length);
		if (defaultPerms != null)
			for (int i = 0; i < defaultPerms.length; i++)
				temp.writeUTF(defaultPerms[i]);
		String[] locs = getLocations();
		temp.writeInt(locs == null ? 0 : locs.length);
		if (locs != null)
			for (int i = 0; i < locs.length; i++) {
				temp.writeUTF(locs[i]);
				String[] perms = getPermissionData(locs[i]);
				temp.writeInt(perms == null ? 0 : perms.length);
				if (perms != null)
					for (int j = 0; j < perms.length; j++)
						temp.writeUTF(perms[j]);
			}
		String[] condPerms = getConditionalPermissionInfos();
		temp.writeInt(condPerms == null ? 0 : condPerms.length);
		if (condPerms != null)
			for (int i = 0; i < condPerms.length; i++)
				temp.writeUTF(condPerms[i]);
		temp.close();

		out.writeInt(tempBytes.size());
		out.write(tempBytes.toByteArray());
		setDirty(false);
	}
}

Back to the top