KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.framework.internal.core;
12
13 import java.io.IOException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.Enumeration JavaDoc;
16
17 import org.osgi.service.packageadmin.ExportedPackage;
18 import org.osgi.service.packageadmin.PackageAdmin;
19
20 /**
21  * Global policy is an implementation of a buddy policy. It is responsible
22  * for looking up a class within the global set of exported classes. If multiple
23  * version of the same package are exported in the system, the exported package
24  * with the highest version will be returned.
25  */

26 public class GlobalPolicy implements IBuddyPolicy {
27     private PackageAdmin admin;
28
29     public GlobalPolicy(PackageAdmin admin) {
30         this.admin = admin;
31     }
32
33     public Class JavaDoc loadClass(String JavaDoc name) {
34         ExportedPackage pkg = admin.getExportedPackage(BundleLoader.getPackageName(name));
35         if (pkg == null)
36             return null;
37         try {
38             return pkg.getExportingBundle().loadClass(name);
39         } catch (ClassNotFoundException JavaDoc e) {
40             return null;
41         }
42     }
43
44     public URL JavaDoc loadResource(String JavaDoc name) {
45         //get all exported packages that match the resource's package
46
ExportedPackage pkg = admin.getExportedPackage(BundleLoader.getResourcePackageName(name));
47         if (pkg == null)
48             return null;
49         return pkg.getExportingBundle().getResource(name);
50     }
51
52     public Enumeration JavaDoc loadResources(String JavaDoc name) {
53         //get all exported packages that match the resource's package
54
ExportedPackage[] pkgs = admin.getExportedPackages(BundleLoader.getResourcePackageName(name));
55         if (pkgs == null || pkgs.length == 0)
56             return null;
57
58         //get all matching resources for each package
59
Enumeration JavaDoc results = null;
60         for (int i = 0; i < pkgs.length; i++) {
61             try {
62                 results = BundleLoader.compoundEnumerations(results, pkgs[i].getExportingBundle().getResources(name));
63             } catch (IOException JavaDoc e) {
64                 //ignore IO problems and try next package
65
}
66         }
67
68         return results;
69     }
70 }
71
Popular Tags