1 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 30 31 public class DefaultPermissionStorage implements PermissionStorage { 32 34 private static final String CONDPERMS = "condPerms"; 36 protected File permissionDir; 37 38 39 protected Hashtable permissionFiles; 40 41 42 protected File defaultData; 43 44 45 protected static final int PERMISSIONDATA_VERSION_1 = 1; 46 47 48 protected static final int PERMISSIONDATA_VERSION = PERMISSIONDATA_VERSION_1; 49 50 55 public DefaultPermissionStorage(AbstractFrameworkAdaptor adaptor) throws IOException { 56 permissionDir = new File(adaptor.getBundleStoreRootDir(), "permdata"); 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()); } 63 64 throw new IOException(NLS.bind(AdaptorMsg.ADAPTOR_DIRECTORY_CREATE_EXCEPTION, permissionDir)); } 66 67 defaultData = new File(permissionDir, ".default"); 69 loadLocations(); 70 } 71 72 82 public synchronized String [] getLocations() throws IOException { 83 int size = permissionFiles.size(); 84 85 if (size == 0) { 86 return null; 87 } 88 89 String [] locations = new String [size]; 90 91 Enumeration keysEnum = permissionFiles.keys(); 92 93 for (int i = 0; i < size; i++) { 94 locations[i] = (String ) keysEnum.nextElement(); 95 } 96 97 return locations; 98 } 99 100 112 public synchronized String [] getPermissionData(String 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 142 public synchronized void setPermissionData(String location, String [] 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); 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); 166 167 permissionFiles.put(location, file); 168 } 169 } 170 } 171 172 177 protected void loadLocations() throws IOException { 178 String 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 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 location = readLocation(file); 197 198 if (location != null) { 199 permissionFiles.put(location, file); 200 } 201 } catch (FileNotFoundException e) { 202 203 } 204 } 205 } 206 207 215 private String 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 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 249 private String [] 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 location = in.readUTF(); 260 } 261 262 int size = in.readInt(); 263 String [] data = new String [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 285 protected File save(File file, String location, String [] data) throws IOException { 286 if (file == null) { 287 file = File.createTempFile("perm", "", permissionDir); file.delete(); 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 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 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 line; 366 Vector c = new Vector(3); 367 Vector p = new Vector(3); 368 String 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("(")) { p.add(new PermissionInfo(line)); 379 } else if (line.startsWith("[")) { c.add(new ConditionInfo(line)); 381 } else if (line.startsWith("#")) { id = line.substring(1); 383 } 384 } 385 } catch (FileNotFoundException e) { 386 } catch (IOException e) { 388 throw e; 389 } catch (Exception 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 |