KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > internal > module > PermissionChecker


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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 package org.eclipse.osgi.internal.module;
12
13 import java.security.Permission JavaDoc;
14 import org.eclipse.osgi.service.resolver.*;
15 import org.osgi.framework.*;
16
17 public class PermissionChecker {
18     private BundleContext context;
19     private boolean checkPermissions = false;
20     private ResolverImpl resolver;
21
22     public PermissionChecker(BundleContext context, boolean checkPermissions, ResolverImpl resolver) {
23         this.context = context;
24         this.checkPermissions = checkPermissions && context != null; // only can check permissions if context is not null
25
this.resolver = resolver;
26     }
27
28     /*
29      * checks the permission for a bundle to import/reqiure a constraint
30      * and for a bundle to export/provide a package/BSN
31      */

32     public boolean checkPermission(VersionConstraint vc, BaseDescription bd) {
33         if (!checkPermissions)
34             return true;
35         boolean success = false;
36         Permission JavaDoc producerPermission = null, consumerPermission = null;
37         Bundle producer = null, consumer = null;
38         int errorType = 0;
39         if (vc instanceof ImportPackageSpecification) {
40             errorType = ResolverError.IMPORT_PACKAGE_PERMISSION;
41             producerPermission = new PackagePermission(bd.getName(), PackagePermission.EXPORT);
42             consumerPermission = new PackagePermission(vc.getName(), PackagePermission.IMPORT);
43             producer = context.getBundle(((ExportPackageDescription) bd).getExporter().getBundleId());
44         } else {
45             boolean requireBundle = vc instanceof BundleSpecification;
46             errorType = requireBundle ? ResolverError.REQUIRE_BUNDLE_PERMISSION : ResolverError.FRAGMENT_BUNDLE_PERMISSION;
47             producerPermission = new BundlePermission(bd.getName(), requireBundle ? BundlePermission.PROVIDE : BundlePermission.HOST);
48             consumerPermission = new BundlePermission(vc.getName(), requireBundle ? BundlePermission.REQUIRE : BundlePermission.FRAGMENT);
49             producer = context.getBundle(((BundleDescription) bd).getBundleId());
50         }
51         consumer = context.getBundle(vc.getBundle().getBundleId());
52         if (producer != null && (producer.getState() & Bundle.UNINSTALLED) == 0) {
53             success = producer.hasPermission(producerPermission);
54             if (!success) {
55                 switch (errorType) {
56                     case ResolverError.IMPORT_PACKAGE_PERMISSION:
57                         errorType = ResolverError.EXPORT_PACKAGE_PERMISSION;
58                         break;
59                     case ResolverError.REQUIRE_BUNDLE_PERMISSION:
60                     case ResolverError.FRAGMENT_BUNDLE_PERMISSION:
61                         errorType = errorType == ResolverError.REQUIRE_BUNDLE_PERMISSION ? ResolverError.PROVIDE_BUNDLE_PERMISSION : ResolverError.HOST_BUNDLE_PERMISSION;
62                         break;
63                 }
64                 resolver.getState().addResolverError(vc.getBundle(), errorType, producerPermission.toString(), vc);
65             }
66         }
67         if (success && consumer != null && (consumer.getState() & Bundle.UNINSTALLED) == 0) {
68             success = consumer.hasPermission(consumerPermission);
69             if (!success)
70                 resolver.getState().addResolverError(vc.getBundle(), errorType, consumerPermission.toString(), vc);
71         }
72
73         return success;
74     }
75 }
76
Popular Tags