blob: c1a3eb807c0fd951677c2e9d553fc48881be88e6 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
/*******************************************************************************
* Copyright (c) 2005, 2012 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.osgi.internal.loader.buddy;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import org.eclipse.osgi.internal.loader.BundleLoader;
import org.osgi.service.packageadmin.ExportedPackage;
import org.osgi.service.packageadmin.PackageAdmin;
/**
* Global policy is an implementation of a buddy policy. It is responsible
* for looking up a class within the global set of exported classes. If multiple
* version of the same package are exported in the system, the exported package
* with the highest version will be returned.
*/
@SuppressWarnings("deprecation")
public class GlobalPolicy implements IBuddyPolicy {
private PackageAdmin admin;
public GlobalPolicy(PackageAdmin admin) {
this.admin = admin;
}
@Override
public Class<?> loadClass(String name) {
ExportedPackage pkg = admin.getExportedPackage(BundleLoader.getPackageName(name));
if (pkg == null)
return null;
try {
return pkg.getExportingBundle().loadClass(name);
} catch (ClassNotFoundException e) {
return null;
}
}
@Override
public URL loadResource(String name) {
//get all exported packages that match the resource's package
ExportedPackage pkg = admin.getExportedPackage(BundleLoader.getResourcePackageName(name));
if (pkg == null)
return null;
return pkg.getExportingBundle().getResource(name);
}
@Override
public Enumeration<URL> loadResources(String name) {
//get all exported packages that match the resource's package
ExportedPackage[] pkgs = admin.getExportedPackages(BundleLoader.getResourcePackageName(name));
if (pkgs == null || pkgs.length == 0)
return null;
//get all matching resources for each package
Enumeration<URL> results = null;
for (int i = 0; i < pkgs.length; i++) {
try {
results = BundleLoader.compoundEnumerations(results, pkgs[i].getExportingBundle().getResources(name));
} catch (IOException e) {
//ignore IO problems and try next package
}
}
return results;
}
}
|