下面列出了org.osgi.framework.wiring.BundleRequirement#org.osgi.framework.wiring.BundleRevision 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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));
}
}
}
}
}
/**
* @see org.osgi.service.packageadmin.PackageAdmin#getRequiredBundles(java.lang.String)
*/
public RequiredBundle[] getRequiredBundles(final String symbolicName) {
final Bundle[] bundles = context.getBundles();
final ArrayList<RequiredBundle> result = new ArrayList<RequiredBundle>();
for (final Bundle bundle : bundles) {
if (bundle.getState() == Bundle.INSTALLED || bundle.getState() == Bundle.UNINSTALLED) {
continue;
}
final BundleRevision rev = bundle.adapt(BundleRevision.class);
if (isFragment(rev)) {
continue;
}
if (symbolicName == null || symbolicName.equals(rev.getSymbolicName())) {
result.add(new RequiredBundleImpl(rev));
}
}
return toArrayOrNull(result, RequiredBundle.class);
}
final InputStream getURLResource(final URL url, final int rev)
throws IOException {
String frag;
try {
frag = url.toURI().getFragment();
} catch (final URISyntaxException e) {
e.printStackTrace();
frag = null;
}
for (final BundleRevision brevision : revisions) {
final Revision revision = (Revision) brevision;
if (revision.revId == rev) {
if (frag == null) {
return revision.retrieveFile(null, url.getPath());
} else {
return revision.retrieveFile(url.getPath(), frag);
}
}
}
return null;
}
protected boolean resolve(final boolean critical)
throws BundleException {
if (!resolveMetadata(critical)) {
return false;
}
if (!framework.resolve(
Collections.<BundleRevision> singletonList(this),
critical)) {
return false;
}
markResolved();
return true;
}
/**
* Creates a {@link BundleCapability} from one entry of the Bundle-Capability
* header.
*
* @param gen
* the owning bundle revision.
* @param he
* the parsed bundle capability header entry.
*/
public BundleCapabilityImpl(final BundleGeneration gen, final HeaderEntry he)
{
this.gen = gen;
owner = gen;
namespace = he.getKey();
if (gen.bundle.id != 0) {
for (final String ns : Arrays
.asList(new String[] { BundleRevision.BUNDLE_NAMESPACE,
BundleRevision.HOST_NAMESPACE,
BundleRevision.PACKAGE_NAMESPACE,
ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE,
IdentityNamespace.IDENTITY_NAMESPACE,
NativeNamespace.NATIVE_NAMESPACE})) {
if (ns.equals(namespace)) {
throw new IllegalArgumentException("Capability with name-space '" + ns
+ "' must not be provided in the "
+ Constants.PROVIDE_CAPABILITY
+ " manifest header.");
}
}
}
attributes = Collections.unmodifiableMap(he.getAttributes());
directives = Collections.unmodifiableMap(he.getDirectives());
}
/**
* @see org.osgi.framework.wiring.FrameworkWiring#resolveBundles(java.util.Collection)
* @category FrameworkWiring
*/
public boolean resolveBundles(final Collection<Bundle> bundles) {
final ArrayList<BundleRevision> resources = new ArrayList<BundleRevision>();
boolean resolved = true;
for (final Bundle bundle : bundles == null ? Concierge.this.bundles
: bundles) {
if (bundle.getState() == UNINSTALLED) {
resolved = false;
continue;
}
resources.add(bundle.adapt(BundleRevision.class));
}
try {
resolved &= resolve(resources, false);
return resolved;
} catch (final BundleException e) {
// should not be thrown for critical==false
return false;
}
}
protected void filterResources(final Collection<ResolverHook> hooks,
final Collection<Resource> resources,
final Collection<Resource> removed) {
final ArrayList<BundleRevision> revisions = new ArrayList<BundleRevision>();
removed.addAll(resources);
for (final Iterator<Resource> iter = resources.iterator(); iter
.hasNext();) {
final Resource res = iter.next();
if (res instanceof BundleRevision) {
revisions.add((BundleRevision) res);
iter.remove();
}
}
final ConciergeCollections.RemoveOnlyList<BundleRevision> filteredResources = new ConciergeCollections.RemoveOnlyList<BundleRevision>(
revisions);
for (final ResolverHook hook : resolver.hooks.keySet()) {
hook.filterResolvable(filteredResources);
}
resources.addAll(filteredResources);
removed.removeAll(filteredResources);
}
private List<Content> initializeContentPath() throws Exception
{
List<Content> contentList = new ArrayList();
calculateContentPath(this, getContent(), contentList, true);
List<BundleRevision> fragments = null;
List<Content> fragmentContents = null;
if (m_wiring != null)
{
// Get fragments and their contents from the wiring.
// Note that we don't use Util.getFragments() here because
// the wiring returns parallel arrays and the utility method
// doesn't necessarily return the correct order.
fragments = m_wiring.getFragments();
fragmentContents = m_wiring.getFragmentContents();
}
if (fragments != null)
{
for (int i = 0; i < fragments.size(); i++)
{
calculateContentPath(
fragments.get(i), fragmentContents.get(i), contentList, false);
}
}
return contentList;
}
/**
* Get Fragments matching a host bundle.
*
* @param hostBundle
* , the host bundle, for which fragments should be found
* @return an array of fragments, which should be attached to the host
* bundle
*/
List<Revision> getFragments(final BundleRevision hostBundle) {
final List<Revision> candidates = new ArrayList<Revision>(
fragmentIndex.lookup(hostBundle.getSymbolicName()));
if (!candidates.isEmpty()) {
final Capability cap = hostBundle
.getCapabilities(HostNamespace.HOST_NAMESPACE).get(0);
for (final Iterator<Revision> iter = candidates.iterator(); iter
.hasNext();) {
final Requirement req = iter.next()
.getRequirements(HostNamespace.HOST_NAMESPACE).get(0);
if (!matches(req, cap)) {
iter.remove();
}
}
}
return candidates;
}
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);
}
}
}
}
static int whichNameSpaces(String namespace) {
int ns;
if (namespace == null) {
ns = NS_BUNDLE|NS_HOST|NS_IDENTITY|NS_PACKAGE|NS_OTHER;
} else if (BundleRevision.BUNDLE_NAMESPACE.equals(namespace)) {
ns = NS_BUNDLE;
} else if (BundleRevision.HOST_NAMESPACE.equals(namespace)) {
ns = NS_HOST;
} else if (IdentityNamespace.IDENTITY_NAMESPACE.equals(namespace)) {
ns = NS_IDENTITY;
} else if (NativeNamespace.NATIVE_NAMESPACE.equals(namespace)) {
ns = NS_NATIVE;
} else if (BundleRevision.PACKAGE_NAMESPACE.equals(namespace)) {
ns = NS_PACKAGE;
} else {
ns = NS_OTHER;
}
return ns;
}
@Override
public String toHTML(Capability capability)
{
// Make a modifiable clone of the attributes.
final Map<String, Object> attrs =
new HashMap<String, Object>(capability.getAttributes());
final StringBuffer sb = new StringBuffer(50);
sb.append(attrs.remove(BundleRevision.BUNDLE_NAMESPACE));
final Version version =
(Version) attrs.remove(Constants.BUNDLE_VERSION_ATTRIBUTE);
if (version != null) {
sb.append(" ");
sb.append(version);
}
if (!attrs.isEmpty()) {
sb.append(" ");
sb.append(attrs);
}
return sb.toString();
}
@Override
public String toHTML(Requirement requirement)
{
final StringBuffer sb = new StringBuffer(50);
final String filter = requirement.getDirectives().get("filter");
final String bundleName =
Util.getFilterValue(filter, BundleRevision.BUNDLE_NAMESPACE);
if (bundleName != null) {
sb.append(bundleName);
Util.appendVersion(sb, filter, Constants.BUNDLE_VERSION_ATTRIBUTE);
} else {
// Filter too complex to extract info from...
sb.append(filter);
}
return sb.toString();
}
@Override
public String toHTML(Requirement requirement)
{
final StringBuffer sb = new StringBuffer(50);
final String filter = requirement.getDirectives().get("filter");
final String hostName =
Util.getFilterValue(filter, BundleRevision.HOST_NAMESPACE);
if (hostName != null) {
sb.append(hostName);
Util.appendVersion(sb, filter, Constants.BUNDLE_VERSION_ATTRIBUTE);
} else {
// Filter too complex to extract info from...
sb.append(filter);
}
return sb.toString();
}
@Override
public String toHTML(Requirement requirement)
{
final StringBuffer sb = new StringBuffer(50);
final String filter = requirement.getDirectives().get("filter");
final String pkgName =
Util.getFilterValue(filter, BundleRevision.PACKAGE_NAMESPACE);
if (pkgName != null) {
sb.append(pkgName);
appendVersionAndResolutionDirective(sb, requirement);
} else {
sb.append(filter);
}
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 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();
}
/**
* Determine if the given bundle can and needs to be started.
*
* <ol>
* <li>A fragment bundle must never be started.
* <li>If no start-level support is present in the framework then any bundle
* in state installed, resolved or starting can be started.
* <li>A bundle that is not persistently started can be started.
* <li>A bundle that is persistently started and in any of the states
* installed, resolved, starting and assigned to a start level that is lower
* or equal to the current start level can be started.
* </ol>
*
* @param bundle
* the bundle to check.
* @return {@code true} if the bundle needs to be started.
*/
boolean startBundlePossible(final Bundle bundle)
{
final BundleRevision bRevCur = bundle.adapt(BundleRevision.class);
final boolean isFragment =
bRevCur.getTypes() == BundleRevision.TYPE_FRAGMENT;
if (isFragment) {
return false;
}
final int state = bundle.getState();
final boolean startable =
(state & (Bundle.INSTALLED | Bundle.RESOLVED | Bundle.STARTING)) != 0;
final BundleStartLevel bsl = bundle.adapt(BundleStartLevel.class);
if (bsl == null) {
return startable;
}
if (!bsl.isPersistentlyStarted()) {
return true;
}
return startable && bsl.getStartLevel() <= getCurrentStartLevel();
}
/**
* Determine if the given bundle can and needs to be stopped.
*
* <ol>
* <li>A fragment bundle must never be stopped.
* <li>If no start-level support is present in the framework then any bundle
* in state starting, active can be stopped.
* <li>A bundle that is persistently started can be stopped.
* </ol>
*
* @param bundle
* the bundle to check.
* @return {@code true} if the bundle needs to be stopped.
*/
boolean stopBundlePossible(final Bundle bundle)
{
final BundleRevision bRevCur = bundle.adapt(BundleRevision.class);
final boolean isFragment =
bRevCur.getTypes() == BundleRevision.TYPE_FRAGMENT;
if (isFragment) {
return false;
}
final int state = bundle.getState();
final boolean stoppable =
(state & (Bundle.STARTING | Bundle.ACTIVE)) != 0;
final BundleStartLevel bsl = bundle.adapt(BundleStartLevel.class);
if (bsl == null) {
return stoppable;
}
if (bsl.isPersistentlyStarted()) {
return true;
}
return stoppable && bsl.getStartLevel() > getCurrentStartLevel();
}
/**
* Determine if the given bundle needs to be refreshed.
*
* <ol>
* <li>A bundle with no bundle revisions does not need a refreshed. The bundle
* has been uninstalled and does not have any active wires since there are no
* bundle revisions.
* <li>A bundle with more than one bundle revision needs to be refreshed.
* <li>A fragment bundle can always be refreshed, since the refresh operation
* will attach it to any matching host that it has not yet been attached to.
* </ol>
*
* @param bundle
* the bundle to check.
* @return {@code true} if the bundle needs to be refreshed.
*/
boolean refreshBundleNeeded(final Bundle bundle)
{
final List<BundleRevision> bRevs =
bundle.adapt(BundleRevisions.class).getRevisions();
if (bRevs.isEmpty()) {
// Uninstalled bundle with no active bundle revision.
return false;
}
final BundleRevision bRevCur = bRevs.get(0);
final boolean isFragment =
bRevCur.getTypes() == BundleRevision.TYPE_FRAGMENT;
if (isFragment) {
// A fragment may attach to new hosts as the result of a refresh.
return true;
}
// Are there any old bundle revisions pending deletion?
return bRevs.size() > 1;
}
@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;
}
}
private void debugBundle(Bundle b) {
BundleRevision br = b.adapt(BundleRevision.class);
br.getCapabilities(null).forEach(System.out::println);
b.adapt(BundleWiring.class).getProvidedWires(null).forEach(System.out::println);
;
}
@Override
public boolean matches(BundleCapability capability) {
if (BundleRevision.HOST_NAMESPACE.equals(capability.getNamespace())) {
return toFilter().matches(capability.getAttributes());
}
return false;
}
@Override
public boolean matches(BundleCapability capability)
{
if (BundleRevision.BUNDLE_NAMESPACE.equals(capability.getNamespace())) {
return toFilter().matches(capability.getAttributes());
}
return false;
}
@Override
public Map<String, Object> getAttributes() {
final Map<String,Object> res
= new HashMap<String, Object>(4+attributes.size());
res.put(BundleRevision.PACKAGE_NAMESPACE, name);
res.put(Constants.VERSION_ATTRIBUTE, version);
res.put(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE, bpkgs.bg.symbolicName);
res.put(Constants.BUNDLE_VERSION_ATTRIBUTE, bpkgs.bg.version);
res.putAll(attributes);
return Collections.unmodifiableMap(res);
}
private Filter toFilter()
{
final StringBuffer sb = new StringBuffer(80);
boolean multipleConditions = false;
sb.append('(');
sb.append(BundleRevision.BUNDLE_NAMESPACE);
sb.append('=');
sb.append(name);
sb.append(')');
if (bundleRange != null) {
sb.append(bundleRange.toFilterString(Constants.BUNDLE_VERSION_ATTRIBUTE));
multipleConditions = true;
}
for (final Entry<String,Object> entry : attributes.entrySet()) {
sb.append('(');
sb.append(entry.getKey());
sb.append('=');
sb.append(entry.getValue().toString());
sb.append(')');
multipleConditions = true;
}
if (multipleConditions) {
sb.insert(0, "(&");
sb.append(')');
}
try {
return FrameworkUtil.createFilter(sb.toString());
} catch (final InvalidSyntaxException _ise) {
throw new RuntimeException("Internal error, createFilter: '" +sb.toString() +"': " +_ise.getMessage());
}
}
@Override
public boolean matches(BundleCapability capability) {
if (BundleRevision.PACKAGE_NAMESPACE.equals(capability.getNamespace())) {
for (NativeCodeEntry e : entries) {
if (e.filter == null || e.filter.matches(capability.getAttributes())) {
return true;
}
}
}
return false;
}
public BundleRequirementImpl(final BundleRevision revision,
final String namespace, final Map<String, String> directives,
final Map<String, Object> attributes, final String prettyPrint) {
super(namespace, directives, attributes);
this.revision = revision;
this.prettyPrint = "BundleRequirement{" + prettyPrint + "}";
}
/**
* Construct a new BundleGeneration for the System Bundle.
*
* @param b BundleImpl this bundle data.
* @param exportStr The value of the Export-Package header.
* @param capabilityStr The value of the Provide-Capability header.
*/
BundleGeneration(BundleImpl b, String exportStr, String capabilityStr) {
bundle = b;
archive = null;
generation = 0;
v2Manifest = true;
symbolicName = KNOPFLERFISH_SYMBOLICNAME;
symbolicNameParameters = null;
singleton = false;
version = new Version(Util.readFrameworkVersion());
attachPolicy = Constants.FRAGMENT_ATTACHMENT_ALWAYS;
fragment = null;
protectionDomain = null;
lazyActivation = false;
lazyIncludes = null;
lazyExcludes = null;
timeStamp = System.currentTimeMillis();
bpkgs = new BundlePackages(this, exportStr);
bundleRevision = new BundleRevisionImpl(this);
classLoader = b.getClassLoader();
processCapabilities(capabilityStr);
capabilities.put(NativeNamespace.NATIVE_NAMESPACE, Collections.singletonList(new BundleCapabilityImpl(this, b.fwCtx.props)));
identity = new BundleCapabilityImpl(this);
bundleCapability = new BundleNameVersionCapability(this, BundleRevision.BUNDLE_NAMESPACE);
hostCapability = new BundleNameVersionCapability(this, BundleRevision.HOST_NAMESPACE);
nativeRequirement = null;
}
void refresh() {
// iterate over old and current revisions
for (final BundleRevision brev : revisions) {
final Revision rev = (Revision) brev;
if (rev.wiring != null) {
rev.wiring.cleanup();
rev.wiring = null;
}
}
revisions.clear();
if (currentRevision != null) {
revisions.add(currentRevision);
// detach fragments (if any) and reset classloader
currentRevision.refresh();
// remove from framework wirings
framework.wirings.remove(currentRevision);
// clear and restore dynamic imports
currentRevision.dynamicImports.clear();
for (final BundleRequirement req : currentRevision.requirements
.lookup(PackageNamespace.PACKAGE_NAMESPACE)) {
if (PackageNamespace.RESOLUTION_DYNAMIC
.equals(req.getDirectives().get(
Namespace.REQUIREMENT_RESOLUTION_DIRECTIVE))) {
currentRevision.dynamicImports.add(req);
}
}
}
}