下面列出了org.osgi.framework.wiring.BundleRequirement#org.osgi.framework.wiring.BundleWiring 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public byte[] processClass(String className, byte[] bytes, ClasspathEntry ce, BundleEntry be, ClasspathManager cm) {
final BaseData bd = ce.getBaseData();
if (bd == null) {
return bytes;
}
final Bundle b = bd.getBundle();
if (b == null) {
return bytes;
}
BundleWiring w = b.adapt(org.osgi.framework.wiring.BundleWiring.class);
if (w == null) {
return bytes;
}
ClassLoader loader = w.getClassLoader();
return archive.patchByteCode(loader, className, ce.getDomain(), bytes);
}
@Override
public BundleClasses getBundleClasses(ComponentSpecification bundleSpec, Set<String> packagesToScan) {
//Temporary hack: Using class name since ClassLoaderOsgiFramework is not available at compile time in this bundle.
if (osgiFramework.getClass().getName().equals("com.yahoo.application.container.impl.ClassLoaderOsgiFramework")) {
Bundle syntheticClassPathBundle = first(osgiFramework.bundles());
ClassLoader classLoader = syntheticClassPathBundle.adapt(BundleWiring.class).getClassLoader();
return new BundleClasses(
syntheticClassPathBundle,
OsgiUtil.getClassEntriesForBundleUsingProjectClassPathMappings(classLoader, bundleSpec, packagesToScan));
} else {
Bundle bundle = getBundle(bundleSpec);
if (bundle == null)
throw new RuntimeException("No bundle matching " + quote(bundleSpec));
return new BundleClasses(bundle, OsgiUtil.getClassEntriesInBundleClassPath(bundle, packagesToScan));
}
}
public JerseyNodeTest() {
resources = new HashMap<>();
resources.put("com/foo", "com/foo/Foo.class");
resources.put("com/bar", "com/bar/Bar.class");
bundle = new MockBundle() {
@Override
public Collection<String> listResources(String path, String ignored, int options) {
if ((options & BundleWiring.LISTRESOURCES_RECURSE) != 0 && path.equals("/")) {
return resources.values();
} else {
return Collections.singleton(resources.get(path));
}
}
};
bundleClasses = new ArrayList<>(resources.values());
}
private void getExportedPackages0(final Bundle bundle, final String name, final ArrayList<ExportedPackage> result) {
if (bundle == null) {
throw new IllegalArgumentException("bundle==null");
}
if (result == null) {
throw new IllegalArgumentException("result==null");
}
final List<BundleRevision> revs = bundle.adapt(BundleRevisions.class).getRevisions();
if (revs.isEmpty()) {
return;
}
for (final BundleRevision r : revs) {
final BundleWiring wiring = r.getWiring();
if (wiring != null && wiring.isInUse()) {
for (final Capability cap : wiring.getCapabilities(PackageNamespace.PACKAGE_NAMESPACE)) {
if (name == null || name.equals(cap.getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE))) {
result.add(new ExportedPackageImpl((BundleCapability) cap));
}
}
}
}
}
@Override
public void start(final BundleContext bundleContext) throws Exception {
ProviderUtil.STARTUP_LOCK.lock();
lockingProviderUtil = true;
final BundleWiring self = bundleContext.getBundle().adapt(BundleWiring.class);
final List<BundleWire> required = self.getRequiredWires(LoggerContextFactory.class.getName());
for (final BundleWire wire : required) {
loadProvider(bundleContext, wire.getProviderWiring());
}
bundleContext.addBundleListener(this);
final Bundle[] bundles = bundleContext.getBundles();
for (final Bundle bundle : bundles) {
loadProvider(bundle);
}
unlockIfReady();
}
/**
* @see org.osgi.service.packageadmin.PackageAdmin#getHosts(org.osgi.framework.Bundle)
*/
public Bundle[] getHosts(final Bundle bundle) {
final BundleWiring wiring = bundle.adapt(BundleWiring.class);
if (wiring == null) {
return null;
}
final List<BundleWire> wires = wiring.getRequiredWires(HostNamespace.HOST_NAMESPACE);
final ArrayList<Bundle> result = new ArrayList<Bundle>();
for (final BundleWire wire : wires) {
if (wire.getRequirer().getBundle() == bundle) {
result.add(wire.getProvider().getBundle());
}
}
return toArrayOrNull(result, Bundle.class);
}
/**
* @see org.osgi.service.packageadmin.ExportedPackage#getImportingBundles()
*/
public Bundle[] getImportingBundles() {
final ArrayList<Bundle> result = new ArrayList<Bundle>();
final BundleWiring wiring = cap.getResource().getWiring();
if (wiring != null) {
final List<BundleWire> wires = wiring.getProvidedWires(PackageNamespace.PACKAGE_NAMESPACE);
for (final BundleWire wire : wires) {
if (wire.getCapability().equals(cap)) {
final Bundle b = wire.getRequirer().getBundle();
if (b != cap.getResource().getBundle()) {
result.add(wire.getRequirer().getBundle());
}
}
}
addRequiringBundles(wiring, result);
}
return toArrayOrNull(result, Bundle.class);
}
protected final BundleWiringDTO getBundleWiringDTO(BundleRevision revision){
BundleWiringDTO dto = new BundleWiringDTO();
dto.bundle = revision.getBundle().getBundleId();
BundleWiring wiring = revision.getWiring();
// TODO what is root
dto.root = wiring.hashCode();
dto.resources = new HashSet<BundleRevisionDTO>();
dto.resources.add(getBundleRevisionDTO(revision));
dto.nodes = new HashSet<BundleWiringDTO.NodeDTO>();
addBundleWiringNodeDTO(wiring, dto.resources, dto.nodes);
return dto;
}
/**
* @see org.osgi.framework.wiring.FrameworkWiring#getRemovalPendingBundles()
* @category FrameworkWiring
*/
public Collection<Bundle> getRemovalPendingBundles() {
final ArrayList<Bundle> removalPending = new ArrayList<Bundle>();
bundleLoop: for (final AbstractBundle bundle : bundles) {
if (bundle instanceof BundleImpl) {
final List<BundleRevision> revisions = bundle.getRevisions();
for (final BundleRevision rev : revisions) {
final BundleWiring wiring = rev.getWiring();
if (wiring != null && !wiring.isCurrent()
&& wiring.isInUse()) {
removalPending.add(bundle);
continue bundleLoop;
}
}
}
}
return removalPending;
}
/**
* Test if startup of framework does work when java.specification.name is
* NOT null. This is the standard case for JavaSE 7/8 etc JVMs.
*/
@Test
public void testJavaSpecificationNameNotNull() throws Exception {
String propName = "java.specification.name";
String propValue = System.getProperty(propName);
Assert.assertNotNull(propValue);
try {
startFramework();
Assert.assertEquals(0, framework.getBundleId());
BundleWiring w = framework.adapt(BundleWiring.class);
List<BundleCapability> caps = w.getCapabilities("osgi.ee");
checkEECaps(caps);
} finally {
stopFramework();
}
}
/**
* Test if startup of framework does work when java.specification.name IS
* null. This will for example happen when running on CEE-J.
*/
@Test
public void testJavaSpecificationNameNull() throws Exception {
String propName = "java.specification.name";
String savedPropValue = System.getProperty(propName);
try {
System.clearProperty(propName);
String propValue = System.getProperty(propName);
Assert.assertNull(propValue);
startFramework();
Assert.assertEquals(0, framework.getBundleId());
BundleWiring w = framework.adapt(BundleWiring.class);
List<BundleCapability> caps = w.getCapabilities("osgi.ee");
checkEECaps(caps);
} finally {
stopFramework();
System.setProperty(propName, savedPropValue);
}
}
private void prepareDependencies(final Bundle bundle) {
final BundleWiring wiring = bundle.adapt(BundleWiring.class);
final List<BundleWire> wires = wiring.getRequiredWires(BundleRevision.PACKAGE_NAMESPACE);
if (wires != null) {
for (final BundleWire wire : wires) {
try {
final Bundle dependency = wire.getProviderWiring().getBundle();
if (!visited.contains(dependency.getSymbolicName()) && hasComponents(dependency)) {
if (!live(dependency)) {
dependency.start();
}
if (live(dependency)) {
// pseudo-event to trigger bundle activation
addingBundle(dependency, null /* unused */);
}
}
}
catch (final Exception e) {
log.warn("MISSING {}", wire, e);
}
}
}
}
/**
* Present a generic requirement by showing its filter.
*
* @param requirement The requirement to present.
* @return string presentation of the requirement.
*/
String getReqName(BundleRequirement requirement)
{
final StringBuffer sb = new StringBuffer(50);
final String filter = requirement.getDirectives().get("filter");
if (filter != null) {
sb.append(filter);
} else {
sb.append(" &emdash; ");
}
final BundleWiring reqWiring = requirement.getRevision().getWiring();
appendPendingRemovalOnRefresh(sb, reqWiring);
return sb.toString();
}
@Override
String getReqName(final BundleRequirement requirement)
{
final StringBuffer sb = new StringBuffer(50);
final String filter = requirement.getDirectives().get("filter");
final String eeName = getFilterValue(filter, ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE);
if (eeName != null) {
sb.append(eeName);
appendVersion(sb, requirement);
} else {
// Filter too complex to extract info from...
sb.append(filter);
}
final BundleWiring reqWiring = requirement.getRevision().getWiring();
appendPendingRemovalOnRefresh(sb, reqWiring);
return sb.toString();
}
@Override
String getReqName(final BundleRequirement requirement)
{
final StringBuffer sb = new StringBuffer(50);
final String filter = requirement.getDirectives().get("filter");
final String hostName = getFilterValue(filter, BundleRevision.HOST_NAMESPACE);
if (hostName != null) {
sb.append(hostName);
appendVersion(sb, requirement);
} else {
// Filter too complex to extract info from...
sb.append(filter);
}
final BundleWiring reqWiring = requirement.getRevision().getWiring();
appendPendingRemovalOnRefresh(sb, reqWiring);
return sb.toString();
}
@Override
String getReqName(final BundleRequirement requirement)
{
final StringBuffer sb = new StringBuffer(50);
final String filter = requirement.getDirectives().get("filter");
final String idName = getFilterValue(filter, IdentityNamespace.IDENTITY_NAMESPACE);
if (idName != null) {
sb.append(idName);
appendVersionAndType(sb, requirement);
} else {
// Filter too complex to extract info from...
sb.append(filter);
}
final BundleWiring reqWiring = requirement.getRevision().getWiring();
appendPendingRemovalOnRefresh(sb, reqWiring);
return sb.toString();
}
@Override
String getReqName(final BundleRequirement requirement)
{
final StringBuffer sb = new StringBuffer(50);
final String filter = requirement.getDirectives().get("filter");
final String bundleName =
getFilterValue(filter, BundleRevision.BUNDLE_NAMESPACE);
if (bundleName != null) {
sb.append(bundleName);
appendVersion(sb, requirement);
} else {
// Filter too complex to extract info from...
sb.append(filter);
}
final BundleWiring reqWiring = requirement.getRevision().getWiring();
appendPendingRemovalOnRefresh(sb, reqWiring);
return sb.toString();
}
@Override
String getReqName(final BundleRequirement requirement)
{
final StringBuffer sb = new StringBuffer(50);
final String filter = requirement.getDirectives().get("filter");
final String pkgName =
getFilterValue(filter, BundleRevision.PACKAGE_NAMESPACE);
if (pkgName != null) {
sb.append(pkgName);
appendVersionAndResolutionDirective(sb, requirement);
} else {
sb.append(filter);
}
final BundleWiring reqWiring = requirement.getRevision().getWiring();
appendPendingRemovalOnRefresh(sb, reqWiring);
return sb.toString();
}
public void runTest() throws Throwable {
buB = Util.installBundle(bc, "bundleB_test-1.0.0.jar");
assertNotNull(buB);
FrameworkWiring fw = bc.getBundle(0).adapt(FrameworkWiring.class);
assertNotNull(fw);
fw.resolveBundles(Collections.singleton(buB));
BundleWiring wiring = buB.adapt(BundleWiring.class);
assertNotNull(wiring);
Collection resources = wiring.listResources("/", "*.class", 0);
assertEquals("Class files in top of bundleB_test", 0, resources.size());
resources = wiring.listResources("/org/knopflerfish/bundle", "*.class",
BundleWiring.FINDENTRIES_RECURSE);
assertEquals("Class files in bundleB_test", 2, resources.size());
resources = wiring.listResources("/", "*.class",
BundleWiring.FINDENTRIES_RECURSE + BundleWiring.LISTRESOURCES_LOCAL);
assertEquals("Class files in bundleB_test", 3, resources.size());
resources = wiring.listResources("org/", null,
BundleWiring.FINDENTRIES_RECURSE + BundleWiring.LISTRESOURCES_LOCAL);
assertEquals("All entires in bundleB_test org", 8, resources.size());
out.println("### framework test bundle :FRAME700A:PASS");
}
private void loadProvider(final BundleContext bundleContext, final BundleWiring bundleWiring) {
final String filter = "(APIVersion>=2.6.0)";
try {
final Collection<ServiceReference<Provider>> serviceReferences = bundleContext.getServiceReferences(Provider.class, filter);
Provider maxProvider = null;
for (final ServiceReference<Provider> serviceReference : serviceReferences) {
final Provider provider = bundleContext.getService(serviceReference);
if (maxProvider == null || provider.getPriority() > maxProvider.getPriority()) {
maxProvider = provider;
}
}
if (maxProvider != null) {
ProviderUtil.addProvider(maxProvider);
}
} catch (final InvalidSyntaxException ex) {
LOGGER.error("Invalid service filter: " + filter, ex);
}
final List<URL> urls = bundleWiring.findEntries("META-INF", "log4j-provider.properties", 0);
for (final URL url : urls) {
ProviderUtil.loadProvider(url, bundleWiring.getClassLoader());
}
}
private void removeExtensions(Bundle bundle) {
bundleExtensions.entrySet().stream().filter(entry -> entry.getValue() ==
bundle.getBundleId()).forEachOrdered(entry -> {
siddhiExtensionsMap.remove(entry.getKey());
});
bundleExtensions.entrySet().removeIf(entry -> entry.getValue() ==
bundle.getBundleId());
BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);
if (bundleWiring != null) {
ClassLoader classLoader = bundleWiring.getClassLoader();
Iterable<Class<?>> extensions = ClassIndex.getAnnotated(Extension.class, classLoader);
for (Class extension : extensions) {
Extension siddhiExtensionAnnotation = (Extension) extension.getAnnotation(Extension.class);
if (!siddhiExtensionAnnotation.namespace().isEmpty()) {
String key = siddhiExtensionAnnotation.namespace() + SiddhiConstants.EXTENSION_SEPARATOR +
siddhiExtensionAnnotation.name();
removeFromExtensionHolderMap(key, extension, extensionHolderConcurrentHashMap);
}
}
}
}
public BundleLoader(String bundleSymbolicName) {
Bundle bundle = AdminBundlesInstalled.getBundles().get(bundleSymbolicName);
if (bundle != null) {
BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);
this.setClassLoader(bundleWiring.getClassLoader());
}
}
private void collectClassPathForBundle(Bundle bundle, List<URL> classPath,
List<String> compilerClassPath) {
try {
File file = FileLocator.getBundleFile(bundle);
URL url = file.toURI().toURL();
if (classPath.contains(url)) {
return; // kill infinite loop
}
classPath.add(0, url);
compilerClassPath.add(0, file.getAbsolutePath());
BundleWiring wiring = bundle.adapt(BundleWiring.class);
for (BundleWire dep : wiring.getRequiredWires(null)) {
collectClassPathForBundle(dep.getProviderWiring().getBundle(),
classPath, compilerClassPath);
}
List<String> requiredLibs = new RascalBundleManifest().getRequiredLibraries(bundle);
if (requiredLibs != null) {
for (String lib : requiredLibs) {
classPath.add(bundle.getResource(lib));
}
}
}
catch (IOException e) {
Rasctivator.logException("error while tracing dependencies", e);
}
}
private void doClassloading(){
// Classloading
BundleWiring bundleWiring = FrameworkUtil.getBundle(AuthenticationHandlerSAML2.class).adapt(BundleWiring.class);
ClassLoader loader = bundleWiring.getClassLoader();
Thread thread = Thread.currentThread();
thread.setContextClassLoader(loader);
}
@Override
@SuppressWarnings("unchecked")
public <T> T adapt(Class<T> clazz) {
if (clazz.equals(BundleRevision.class)) {
return (T) new BundleRevisionImpl();
} else if (clazz.equals(BundleWiring.class)) {
return (T) new BundleWiringImpl();
} else {
return null;
}
}
public static BundleInfo createBundleInfo(Bundle bundle, Collection<String> classEntries) {
BundleInfo bundleInfo = new BundleInfo(bundle.getSymbolicName(), bundle.getVersion(), bundle.getLocation(), webInfUrl(bundle),
bundle.adapt(BundleWiring.class).getClassLoader());
bundleInfo.setClassEntries(classEntries);
return bundleInfo;
}
public static Collection<String> getClassEntriesInBundleClassPath(Bundle bundle, Set<String> packagesToScan) {
BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);
if (packagesToScan.isEmpty()) {
return bundleWiring.listResources("/", "*" + CLASS_FILE_TYPE_SUFFIX,
BundleWiring.LISTRESOURCES_LOCAL | BundleWiring.LISTRESOURCES_RECURSE);
} else {
List<String> ret = new ArrayList<>();
for (String pkg : packagesToScan) {
ret.addAll(bundleWiring.listResources(packageToPath(pkg), "*" + CLASS_FILE_TYPE_SUFFIX, BundleWiring.LISTRESOURCES_LOCAL));
}
return ret;
}
}
private ClassLoader getClassLoader(ServiceTuple<?> serviceTuple) {
return serviceTuple.
getCachingServiceReference().
getServiceReference().
getBundle().
adapt(BundleWiring.class).
getClassLoader();
}
private void loadBundleFilesLocations() {
sqoopBundleFileLocations.clear();
String bundleLocation = bundleContext.getBundle().getDataFile( "" ).getParent();
sqoopBundleFileLocations.add( bundleLocation );
BundleWiring wiring = bundleContext.getBundle().adapt( BundleWiring.class );
List<BundleWire> fragments = wiring.getProvidedWires( "osgi.wiring.host" );
for ( BundleWire fragment : fragments ) {
Bundle fragmentBundle = fragment.getRequirerWiring().getBundle();
String fragmentBundleLocation = fragmentBundle.getDataFile( "" ).getParent();
sqoopBundleFileLocations.add( fragmentBundleLocation );
}
}
private void addRequiringBundles(final BundleWiring wiring, final ArrayList<Bundle> result) {
final List<BundleWire> wires = wiring.getProvidedWires(BundleNamespace.BUNDLE_NAMESPACE);
for (final BundleWire wire : wires) {
result.add(wire.getRequirer().getBundle());
if (BundleNamespace.VISIBILITY_REEXPORT.equals(
wire.getRequirement().getDirectives().get(BundleNamespace.REQUIREMENT_VISIBILITY_DIRECTIVE))) {
addRequiringBundles(wire.getRequirer().getWiring(), result);
}
}
}