KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
15 import java.security.*;
16 import java.util.*;
17 import org.eclipse.osgi.framework.adaptor.PermissionStorage;
18 import org.osgi.framework.FrameworkEvent;
19 import org.osgi.service.condpermadmin.*;
20 import org.osgi.service.permissionadmin.PermissionInfo;
21
22 /**
23  *
24  * Implements ConditionalPermissionAdmin.
25  */

26 public class ConditionalPermissionAdminImpl implements ConditionalPermissionAdmin {
27     public static final ConditionalPermissionInfoImpl[] EMPTY_COND_PERM_INFO = new ConditionalPermissionInfoImpl[0];
28     public static final Condition[] EMPTY_COND = new Condition[0];
29     public static final ConditionInfo[] EMPTY_COND_INFO = new ConditionInfo[0];
30     public static final PermissionInfo[] EMPTY_PERM_INFO = new PermissionInfo[0];
31
32     /**
33      * The Vector of current ConditionalPermissionInfos.
34      */

35     private Vector condPerms;
36     private Framework framework;
37     private PermissionStorage storage;
38     private long nextID = System.currentTimeMillis();
39
40     /**
41      * @param framework
42      * @param permissionStorage
43      */

44     public ConditionalPermissionAdminImpl(Framework framework, PermissionStorage permissionStorage) {
45         ConditionalPermissionInfoImpl.setConditionalPermissionAdminImpl(this);
46         this.framework = framework;
47         this.storage = permissionStorage;
48         try {
49             String JavaDoc[] encodedCondPerms = permissionStorage.getConditionalPermissionInfos();
50             if (encodedCondPerms != null && encodedCondPerms.length > 0) {
51                 condPerms = new Vector(encodedCondPerms.length);
52                 for (int i = 0; i < encodedCondPerms.length; i++)
53                     condPerms.add(new ConditionalPermissionInfoImpl(encodedCondPerms[i]));
54             }
55             else
56                 condPerms = new Vector(0);
57         } catch (IOException JavaDoc e) {
58             framework.publishFrameworkEvent(FrameworkEvent.ERROR, framework.systemBundle, e);
59             condPerms = new Vector();
60         }
61     }
62
63     /**
64      * @see org.osgi.service.condpermadmin.ConditionalPermissionAdmin#addConditionalPermissionInfo(org.osgi.service.condpermadmin.ConditionInfo[], org.osgi.service.permissionadmin.PermissionInfo[])
65      */

66     public ConditionalPermissionInfo addConditionalPermissionInfo(ConditionInfo[] conds, PermissionInfo[] perms) {
67         return setConditionalPermissionInfo(null, conds, perms);
68     }
69
70     public ConditionalPermissionInfo setConditionalPermissionInfo(String JavaDoc name, ConditionInfo conds[], PermissionInfo perms[]) {
71         SecurityManager JavaDoc sm = System.getSecurityManager();
72         if (sm != null)
73             sm.checkPermission(new AllPermission());
74         if (name == null)
75             name = "generated_" + Long.toString(nextID++); //$NON-NLS-1$
76
ConditionalPermissionInfoImpl condPermInfo = null;
77         synchronized (condPerms) {
78             condPermInfo = (ConditionalPermissionInfoImpl) getConditionalPermissionInfo(name);
79             if (condPermInfo == null) {
80                 condPermInfo = new ConditionalPermissionInfoImpl(name, conds, perms);
81                 condPerms.add(condPermInfo);
82             } else {
83                 condPermInfo.conds = conds;
84                 condPermInfo.perms = perms;
85             }
86             saveCondPermInfos();
87         }
88         AbstractBundle bundles[] = framework.getAllBundles();
89         for (int i = 0; i < bundles.length; i++) {
90             AbstractBundle bundle = bundles[i];
91             if (bundle.domain == null) {
92                 continue;
93             }
94             BundleCombinedPermissions bcp = (BundleCombinedPermissions) bundle.domain.getPermissions();
95             /* TODO I don't think we need this check */
96             if (perms == null) {
97                 continue;
98             }
99
100             bcp.checkConditionalPermissionInfo(condPermInfo);
101         }
102         return condPermInfo;
103     }
104
105     public ConditionalPermissionInfo getConditionalPermissionInfo(String JavaDoc name) {
106         for (Enumeration eCondPerms = condPerms.elements(); eCondPerms.hasMoreElements();) {
107             ConditionalPermissionInfoImpl condPerm = (ConditionalPermissionInfoImpl) eCondPerms.nextElement();
108             if (name.equals(condPerm.getName()))
109                 return condPerm;
110         }
111         return null;
112     }
113
114     /**
115      * Returns an Enumeration of current ConditionalPermissionInfos. Each element will be of type
116      * ConditionalPermissionInfoImpl.
117      * @return an Enumeration of current ConditionalPermissionInfos.
118      * @see org.osgi.service.condpermadmin.ConditionalPermissionAdmin#getConditionalPermissionInfos()
119      */

120     public Enumeration getConditionalPermissionInfos() {
121         synchronized (condPerms) {
122             return condPerms.elements();
123         }
124     }
125
126     void deleteConditionalPermissionInfo(ConditionalPermissionInfo cpi) {
127         synchronized (condPerms) {
128             condPerms.remove(cpi);
129             saveCondPermInfos();
130         }
131     }
132
133     private void saveCondPermInfos() {
134         try {
135             String JavaDoc [] encodedCondPerms = new String JavaDoc[condPerms.size()];
136             int i = 0;
137             for (Enumeration eCondPerms = condPerms.elements(); eCondPerms.hasMoreElements(); i++)
138                 encodedCondPerms[i] = eCondPerms.nextElement().toString();
139             storage.saveConditionalPermissionInfos(encodedCondPerms);
140         } catch (IOException JavaDoc e) {
141             e.printStackTrace();
142             framework.publishFrameworkEvent(FrameworkEvent.ERROR, framework.systemBundle, e);
143         }
144     }
145
146     /**
147      * @see org.osgi.service.condpermadmin.ConditionalPermissionAdmin#getAccessControlContext(java.lang.String[])
148      */

149     public AccessControlContext getAccessControlContext(String JavaDoc[] signers) {
150         Enumeration infos = getConditionalPermissionInfos();
151         ArrayList permissionInfos = new ArrayList();
152         if (infos != null) {
153             while (infos.hasMoreElements()) {
154                 ConditionalPermissionInfoImpl condPermInfo = (ConditionalPermissionInfoImpl) infos.nextElement();
155                 ConditionInfo[] condInfo = condPermInfo.getConditionInfos();
156                 boolean match = true;
157                 for (int i = 0; i < condInfo.length; i++) {
158                     if (BundleSignerCondition.class.getName().equals(condInfo[i].getType())) {
159                         String JavaDoc[] args = condInfo[i].getArgs();
160                         for (int j = 0; j < args.length; j++)
161                             if (!framework.adaptor.matchDNChain(args[j], signers)) {
162                                 match = false;
163                                 break;
164                             }
165                     } else {
166                         match = false;
167                         break;
168                     }
169                 }
170                 if (match) {
171                     PermissionInfo[] addPermInfos = condPermInfo.getPermissionInfos();
172                     for (int i = 0; i < addPermInfos.length; i++)
173                         permissionInfos.add(addPermInfos[i]);
174                 }
175             }
176         }
177         BundlePermissionCollection collection = framework.permissionAdmin.createPermissions((PermissionInfo[]) permissionInfos.toArray(new PermissionInfo[permissionInfos.size()]), null, false);
178         return new AccessControlContext(collection == null ? new ProtectionDomain[0] : new ProtectionDomain[] {new ProtectionDomain(null, collection)});
179     }
180 }
181
Popular Tags