下面列出了org.osgi.framework.wiring.BundleRequirement#org.osgi.framework.wiring.BundleWire 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @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);
}
void addWire(final BundleWire wire) {
final Capability cap = wire.getCapability();
final Requirement req = wire.getRequirement();
if (wire.getProvider() == revision) {
providedWires.insert(cap.getNamespace(), wire);
inUseSet.add(wire.getRequirer());
((ConciergeBundleWire) wire).providerWiring = this;
} else {
requiredWires.insert(req.getNamespace(), wire);
if (HostNamespace.HOST_NAMESPACE.equals(wire.getRequirement()
.getNamespace())) {
inUseSet.add(wire.getProvider());
}
((ConciergeBundleWire) wire).requirerWiring = this;
}
}
protected final WireDTO getBundleWireDTO(BundleWire w){
BundleWireDTO dto = new BundleWireDTO();
dto.capability = new CapabilityRefDTO();
dto.capability.capability = w.getCapability().hashCode();
dto.capability.resource = w.getCapability().getResource().hashCode();
dto.requirement = new RequirementRefDTO();
dto.requirement.requirement = w.getRequirement().hashCode();
dto.requirement.resource = w.getRequirement().getResource().hashCode();
dto.provider = w.getProvider().hashCode();
dto.requirer = w.getRequirer().hashCode();
dto.providerWiring = w.getProviderWiring().hashCode();
dto.requirerWiring = w.getRequirerWiring().hashCode();
return dto;
}
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);
}
}
}
}
@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();
}
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 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);
}
}
}
/**
* @see org.osgi.service.packageadmin.PackageAdmin#getFragments(org.osgi.framework.Bundle)
*/
public Bundle[] getFragments(final Bundle bundle) {
final BundleWiring wiring = bundle.adapt(BundleWiring.class);
// this will happen if a bundle has no current revision, e.g.
// is INSTALLED only
if (wiring == null) {
// System.err.println("o.e.c.service.packageadmin: getFragments has
// no wiring for bundle "
// + bundle.getSymbolicName());
return null;
}
final List<BundleWire> wires = wiring.getProvidedWires(HostNamespace.HOST_NAMESPACE);
if (wires == null || wires.isEmpty()) {
return null;
}
final Bundle[] result = new Bundle[wires.size()];
final Iterator<BundleWire> iter = wires.iterator();
for (int i = 0; i < result.length; i++) {
final BundleWire wire = iter.next();
result[i] = wire.getRequirer().getBundle();
}
return result;
}
HashMap<String, BundleWire> getPackageImportWires() {
final List<BundleWire> list = getRequiredWires(PackageNamespace.PACKAGE_NAMESPACE);
final HashMap<String, BundleWire> result = new HashMap<String, BundleWire>();
if (list != null) {
for (final BundleWire wire : list) {
result.put((String) wire.getCapability().getAttributes()
.get(PackageNamespace.PACKAGE_NAMESPACE), wire);
}
}
return result;
}
public List<BundleWire> getProvidedWires(final String namespace) {
if (!isInUse()) {
return null;
}
return namespace == null ? providedWires.getAllValues()
: providedWires.lookup(namespace);
}
public List<BundleWire> getRequiredWires(final String namespace) {
if (!isInUse()) {
return null;
}
return namespace == null ? requiredWires.getAllValues()
: requiredWires.lookup(namespace);
}
ConciergeBundleWiring addAdditionalWires(final List<Wire> wires) {
for (final Wire wire : wires) {
wiring.addWire((BundleWire) wire);
}
packageImportWires = wiring.getPackageImportWires();
requireBundleWires = wiring.getRequireBundleWires();
return wiring;
}
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 Set<Bundle> getBundlesInClassSpace(BundleContext context,
Bundle bundle, Set<Bundle> bundleSet) {
Set<Bundle> bundles = new HashSet<>(); // The set containing the
// bundles either being
// imported or required
if (bundle == null) {
log.error("Incoming bundle is null");
return bundles;
}
if (context == null) {
log.error("Incoming context is null");
return bundles;
}
BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);
if (bundleWiring == null) {
log.error("BundleWiring is null for: " + bundle);
return bundles;
}
// This will give us all required Wires (including require-bundle)
List<BundleWire> requiredWires = bundleWiring.getRequiredWires(null);
for (BundleWire bundleWire : requiredWires) {
Bundle exportingBundle = bundleWire.getCapability().getRevision()
.getBundle();
if (exportingBundle.getBundleId() == 0) {
continue; // system bundle is skipped this one isn't needed
}
if (!bundles.contains(exportingBundle)) {
bundles.add(exportingBundle);
}
}
if (!bundleSet.containsAll(bundles)) { // now let's scan transitively
bundles.removeAll(bundleSet);
bundleSet.addAll(bundles);
}
// Sanity checkpoint to remove uninstalled bundles
bundleSet.removeIf(auxBundle -> auxBundle.getState() == Bundle.UNINSTALLED);
return bundleSet;
}
@Override
public List<BundleWire> getRequiredWires(String p1) {
throw new UnsupportedOperationException();
}
@Override
public List<BundleWire> getProvidedWires(String p1) {
throw new UnsupportedOperationException();
}
@Override
public List<BundleWire> getRequiredWires(String p1) {
throw new UnsupportedOperationException();
}
@Override
public List<BundleWire> getProvidedWires(String p1) {
throw new UnsupportedOperationException();
}
List<BundleWire> getRequireBundleWires() {
return getRequiredWires(BundleNamespace.BUNDLE_NAMESPACE);
}
public List<Wire> getProvidedResourceWires(final String namespace) {
final List<BundleWire> bwires = getProvidedWires(namespace);
return bwires == null ? null : new ArrayList<Wire>(bwires);
}
public List<Wire> getRequiredResourceWires(final String namespace) {
final List<BundleWire> bwires = getRequiredWires(namespace);
return bwires == null ? null : new ArrayList<Wire>(bwires);
}
private Collection<Bundle> getDependencies(final Collection<Bundle> bundles,
final boolean allRevisions) {
// build up the dependency graph. See specs for details.
final ArrayList<Bundle> toProcess = new ArrayList<Bundle>(bundles);
final Set<Bundle> dependencySet = new HashSet<Bundle>();
while (!toProcess.isEmpty()) {
final Bundle b = toProcess.remove(0);
if (b == this) {
dependencySet.add(b);
continue;
}
if (dependencySet.contains(b)) {
continue;
}
if (!(b instanceof BundleImpl)) {
throw new IllegalArgumentException(
"Bundles were not created by this framework instance "
+ b.getClass().getName());
}
dependencySet.add(b);
final BundleImpl bundle = (BundleImpl) b;
for (final BundleRevision brev : bundle.revisions) {
final BundleWiring wiring = brev.getWiring();
// all package exports
if (wiring != null) {
for (final BundleRevision rev : ((ConciergeBundleWiring) wiring).inUseSet) {
toProcess.add(rev.getBundle());
}
/*
* final List<BundleWire> importWires = wiring
* .getProvidedWires(null);
*
* if (importWires != null) { for (final BundleWire
* importWire : importWires) {
* toProcess.add(importWire.getRequirer().getBundle()); } }
*/
final List<BundleWire> hostWires = wiring
.getRequiredWires(HostNamespace.HOST_NAMESPACE);
if (hostWires != null) {
for (final BundleWire hostWire : hostWires) {
toProcess.add(hostWire.getProvider().getBundle());
}
}
}
}
}
return dependencySet;
}