KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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.File JavaDoc;
15 import java.io.Serializable JavaDoc;
16 import java.lang.reflect.*;
17 import java.security.*;
18 import java.util.ArrayList JavaDoc;
19 import org.osgi.framework.Bundle;
20 import org.osgi.service.condpermadmin.*;
21 import org.osgi.service.permissionadmin.PermissionInfo;
22
23 /**
24  *
25  * This is a runtime embodiment of the data stored in ConditionalPermissionInfo.
26  * It has methods to facilitate the management of Conditions and Permissions at
27  * runtime.
28  */

29 public class ConditionalPermissionInfoImpl implements ConditionalPermissionInfo, Serializable JavaDoc {
30     private static final long serialVersionUID = 3258130245704825139L;
31     /**
32      * The permissions enabled by the associated Conditions.
33      */

34     PermissionInfo perms[];
35     /**
36      * The conditions that must be satisfied to enable the corresponding
37      * permissions.
38      */

39     ConditionInfo conds[];
40
41     /**
42      * The name of the ConditionalPermissionInfo
43      */

44     private String JavaDoc name;
45
46     /**
47      * When true, this object has been deleted and any information retrieved
48      * from it should be discarded.
49      */

50     private boolean deleted = false;
51
52     /**
53      * When true, this object has been deleted and any information retrieved
54      * from it should be discarded.
55      */

56     boolean isDeleted() {
57         return deleted;
58     }
59
60     public ConditionalPermissionInfoImpl(String JavaDoc encoded) {
61         decode(encoded);
62     }
63
64     public ConditionalPermissionInfoImpl(String JavaDoc name, ConditionInfo conds[], PermissionInfo perms[]) {
65         this.name = name;
66         this.conds = conds;
67         this.perms = perms;
68     }
69
70     private void decode(String JavaDoc encoded) {
71         int start = encoded.indexOf('{');
72         int end = encoded.lastIndexOf('}');
73         if (start < 0 || end < start)
74             throw new IllegalArgumentException JavaDoc(encoded);
75         if (start != 0)
76             name = encoded.substring(0, start);
77         char[] chars = encoded.substring(start + 1, end).toCharArray();
78         ArrayList JavaDoc condList = new ArrayList JavaDoc();
79         ArrayList JavaDoc permList = new ArrayList JavaDoc();
80         int pos = 0;
81         while (pos < chars.length) {
82             while (pos < chars.length && chars[pos] != '[' && chars[pos] != '(')
83                 pos++;
84             if (pos == chars.length)
85                 break; // no perms or conds left
86
int startPos = pos;
87             char endChar = chars[startPos] == '[' ? ']' : ')';
88             while (chars[pos] != endChar) {
89                 if (chars[pos] == '"') {
90                     pos++;
91                     while (chars[pos] != '"') {
92                         if (chars[pos] == '\\')
93                             pos++;
94                         pos++;
95                     }
96                 }
97                 pos++;
98             }
99             int endPos = pos;
100             String JavaDoc token = new String JavaDoc(chars, startPos, endPos - startPos + 1);
101             if (endChar == ']')
102                 condList.add(new ConditionInfo(token));
103             else
104                 permList.add(new PermissionInfo(token));
105             pos++;
106         }
107         conds = (ConditionInfo[]) condList.toArray(new ConditionInfo[condList.size()]);
108         perms = (PermissionInfo[]) permList.toArray(new PermissionInfo[permList.size()]);
109     }
110
111     public String JavaDoc getName() {
112         return name;
113     }
114
115     /**
116      * @see org.osgi.service.condpermadmin.ConditionalPermissionInfo#getConditionInfos()
117      */

118     public ConditionInfo[] getConditionInfos() {
119         if (conds == null)
120             return null;
121         ConditionInfo[] results = new ConditionInfo[conds.length];
122         System.arraycopy(conds, 0, results, 0, conds.length);
123         return results;
124     }
125
126     /* Used to find permission constructors in addPermissions */
127     static private final Class JavaDoc twoStringClassArray[] = new Class JavaDoc[] {String JavaDoc.class, String JavaDoc.class};
128     static private final Class JavaDoc oneStringClassArray[] = new Class JavaDoc[] {String JavaDoc.class};
129     static private final Class JavaDoc noArgClassArray[] = new Class JavaDoc[] {};
130     static private final Class JavaDoc[][] permClassArrayArgs = new Class JavaDoc[][] {noArgClassArray, oneStringClassArray, twoStringClassArray};
131     /* Used to find condition constructors getConditions */
132     static private final Class JavaDoc[] condClassArray = new Class JavaDoc[] {Bundle.class, ConditionInfo.class};
133
134     /**
135      * Adds the permissions of the given type (if any) that are part of this
136      * ConditionalPermissionInfo to the specified collection. The Permission
137      * instances are constructed using the specified permClass.
138      *
139      * @param collection the collection to add to.
140      * @param permClass the class to use to construct Permission instances.
141      * @return the number of Permissions added.
142      * @throws NoSuchMethodException
143      * @throws SecurityException
144      * @throws InvocationTargetException
145      * @throws IllegalAccessException
146      * @throws InstantiationException
147      * @throws IllegalArgumentException
148      */

149     int addPermissions(AbstractBundle bundle, PermissionCollection collection, Class JavaDoc permClass) throws SecurityException JavaDoc, NoSuchMethodException JavaDoc, IllegalArgumentException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException {
150         String JavaDoc permClassName = permClass.getName();
151         Constructor constructor = null;
152         int numArgs = -1;
153         for (int i = permClassArrayArgs.length - 1 ; i >= 0; i--) {
154             try {
155                 constructor = permClass.getConstructor(permClassArrayArgs[i]);
156                 numArgs = i;
157                 break;
158             } catch (NoSuchMethodException JavaDoc e) {
159                 // ignore
160
}
161         }
162         if (constructor == null)
163             throw new NoSuchMethodException JavaDoc(permClass.getName() + ".<init>()"); //$NON-NLS-1$
164
int count = 0;
165         /*
166          * TODO: We need to cache the permission constructors to enhance performance (see bug 118813).
167          */

168         for (int i = 0; i < perms.length; i++) {
169             if (perms[i].getType().equals(permClassName)) {
170                 count++;
171                 String JavaDoc args[] = new String JavaDoc[numArgs];
172                 if (numArgs > 0)
173                     args[0] = perms[i].getName();
174                 if (numArgs > 1)
175                     args[1] = perms[i].getActions();
176                 if (perms[i].getType().equals("java.io.FilePermission")) { //$NON-NLS-1$
177
// map FilePermissions for relative names to the bundle's data area
178
if (!args[0].equals("<<ALL FILES>>")) { //$NON-NLS-1$
179
File JavaDoc file = new File JavaDoc(args[0]);
180                         if (!file.isAbsolute()) { // relative name
181
if (bundle == null) // default permissions
182
continue; // no relative file permissions
183
File JavaDoc target = bundle.framework.getDataFile(bundle, args[0]);
184                             if (target == null) // no bundle data file area
185
continue; // no relative file permissions
186
args[0] = target.getPath();
187                         }
188                     }
189                 }
190                 collection.add((Permission) constructor.newInstance(args));
191             }
192         }
193         return count;
194     }
195
196     /**
197      * Returns the Condition objects associated with this ConditionalPermissionInfo.
198      *
199      * @param bundle the bundle to be used to construct the Conditions.
200      *
201      * @return the array of Conditions that must be satisfied before permissions
202      * in the ConditionPermissionInfoImpl can be used.
203      */

204     Condition[] getConditions(Bundle bundle) {
205         Condition conditions[] = new Condition[conds.length];
206         for (int i = 0; i < conds.length; i++) {
207             /*
208              * TODO: I think we can pre-get the Constructors in our own
209              * constructor
210              */

211             Class JavaDoc clazz;
212             try {
213                 clazz = Class.forName(conds[i].getType());
214             } catch (ClassNotFoundException JavaDoc e) {
215                 /* If the class isn't there, we fail */
216                 return null;
217             }
218             Constructor constructor = null;
219             Method method = null;
220             try {
221                 method = clazz.getMethod("getCondition", condClassArray); //$NON-NLS-1$
222
if ((method.getModifiers() & Modifier.STATIC) == 0)
223                     method = null;
224             } catch (NoSuchMethodException JavaDoc e) {
225                 // This is a normal case
226
}
227             if (method == null)
228                 try {
229                     constructor = clazz.getConstructor(condClassArray);
230                 } catch (NoSuchMethodException JavaDoc e) {
231                     // TODO should post a FrameworkEvent of type error here
232
conditions[i] = Condition.FALSE;
233                     continue;
234                 }
235
236             Object JavaDoc args[] = {bundle, conds[i]};
237             try {
238                 if (method != null)
239                     conditions[i] = (Condition) method.invoke(null, args);
240                 else
241                     conditions[i] = (Condition) constructor.newInstance(args);
242             } catch (Throwable JavaDoc t) {
243                 // TODO should post a FrameworkEvent of type error here
244
conditions[i] = Condition.FALSE;
245             }
246         }
247         return conditions;
248     }
249
250     /**
251      * @see org.osgi.service.condpermadmin.ConditionalPermissionInfo#getPermissionInfos()
252      */

253     public PermissionInfo[] getPermissionInfos() {
254         if (perms == null)
255             return null;
256         PermissionInfo[] results = new PermissionInfo[perms.length];
257         System.arraycopy(perms, 0, results, 0, perms.length);
258         return results;
259     }
260
261     /**
262      *
263      * @see org.osgi.service.condpermadmin.ConditionalPermissionInfo#delete()
264      */

265     public void delete() {
266         SecurityManager JavaDoc sm = System.getSecurityManager();
267         if (sm != null)
268             sm.checkPermission(new AllPermission());
269         deleted = true;
270         condAdmin.deleteConditionalPermissionInfo(this);
271     }
272
273     private static ConditionalPermissionAdminImpl condAdmin;
274
275     static void setConditionalPermissionAdminImpl(ConditionalPermissionAdminImpl condAdmin) {
276         ConditionalPermissionInfoImpl.condAdmin = condAdmin;
277     }
278
279     public String JavaDoc toString() {
280         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
281         if (name != null)
282             result.append(name);
283         ConditionInfo[] curConds = getConditionInfos();
284         PermissionInfo[] curPerms = getPermissionInfos();
285         result.append('{').append(' ');
286         if (curConds != null)
287             for (int i = 0; i < curConds.length; i++)
288                 result.append(curConds[i].getEncoded()).append(' ');
289         if (curPerms != null)
290             for (int i = 0; i < curPerms.length; i++)
291                 result.append(curPerms[i].getEncoded()).append(' ');
292         result.append('}');
293         return result.toString();
294     }
295 }
296
Popular Tags