KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > util > SecurePermissionStorage


1 /*******************************************************************************
2  * Copyright (c) 2003, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.osgi.framework.util;
13
14 import java.io.IOException JavaDoc;
15 import java.security.*;
16 import org.eclipse.osgi.framework.adaptor.PermissionStorage;
17
18 /**
19  * PermissionStorage privileged action class.
20  */

21
22 public class SecurePermissionStorage implements PermissionStorage, PrivilegedExceptionAction {
23     private PermissionStorage storage;
24     private String JavaDoc location;
25     private String JavaDoc[] data;
26     private int action;
27     private static final int GET = 1;
28     private static final int SET = 2;
29     private static final int LOCATION = 3;
30
31     public SecurePermissionStorage(PermissionStorage storage) {
32         this.storage = storage;
33     }
34
35     public Object JavaDoc run() throws IOException JavaDoc {
36         switch (action) {
37             case GET :
38                 return storage.getPermissionData(location);
39             case SET :
40                 storage.setPermissionData(location, data);
41                 return null;
42             case LOCATION :
43                 return storage.getLocations();
44         }
45
46         throw new UnsupportedOperationException JavaDoc();
47     }
48
49     public String JavaDoc[] getPermissionData(String JavaDoc location) throws IOException JavaDoc {
50         this.location = location;
51         this.action = GET;
52
53         try {
54             return (String JavaDoc[]) AccessController.doPrivileged(this);
55         } catch (PrivilegedActionException e) {
56             throw (IOException JavaDoc) e.getException();
57         }
58     }
59
60     public String JavaDoc[] getLocations() throws IOException JavaDoc {
61         this.action = LOCATION;
62
63         try {
64             return (String JavaDoc[]) AccessController.doPrivileged(this);
65         } catch (PrivilegedActionException e) {
66             throw (IOException JavaDoc) e.getException();
67         }
68     }
69
70     public void setPermissionData(String JavaDoc location, String JavaDoc[] data) throws IOException JavaDoc {
71         this.location = location;
72         this.data = data;
73         this.action = SET;
74
75         try {
76             AccessController.doPrivileged(this);
77         } catch (PrivilegedActionException e) {
78             throw (IOException JavaDoc) e.getException();
79         }
80     }
81 }
Popular Tags