下面列出了org.osgi.framework.Bundle#getHeaders ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Check whether the given bundle is a project artifact. If yes, return the app name. If no,
* return null
*
* @param bundle - bundle to check
* @return - app name
*/
public static String getProjectArtifactName(Bundle bundle) {
Dictionary dictionary = bundle.getHeaders();
if (dictionary != null) {
// Iterate through the headers and find the WSO2-Project-Artifact custom header
for (Enumeration e = dictionary.keys(); e.hasMoreElements();) {
String headerKey = (String) e.nextElement();
if (AppDeployerConstants.WSO2_APP_NAME_HEADER.equals(headerKey)) {
// retireve the header value
String headerValue = (String) dictionary.get(headerKey);
if (headerValue != null) {
return headerValue;
}
}
}
}
return null;
}
/**
* Checking whether a bundle contains the WSO2-Application-Deployer header
*
* @param bundle - input bundle
* @return - if found header - true, else - false
*/
public static boolean isAppDeployer(Bundle bundle) {
Dictionary dictionary = bundle.getHeaders();
if (dictionary != null) {
// Iterate through the headers and find the WSO2-Project-Artifact custom header
for (Enumeration e = dictionary.keys(); e.hasMoreElements();) {
String headerKey = (String) e.nextElement();
if (AppDeployerConstants.WSO2_APP_DEPLOYER_HEADER.equals(headerKey)) {
return true;
}
}
}
return false;
}
public @Override void bundleChanged(BundleEvent event) {
Bundle bundle = event.getBundle();
switch (event.getType()) {
case BundleEvent.STARTED:
// System.err.println("started " + bundle.getSymbolicName());
Dictionary<?,?> headers = bundle.getHeaders();
load(queue.offer(bundle, provides(headers), requires(headers), needs(headers)));
break;
case BundleEvent.STOPPED:
// System.err.println("stopped " + bundle.getSymbolicName());
if (framework.getState() == Bundle.STOPPING) {
// System.err.println("fwork stopping during " + bundle.getSymbolicName());
// ActiveQueue.stop();
} else {
unload(queue.retract(bundle));
}
break;
}
}
/**
* Get the symbolic name of the specified bundle. All directives and
* parameters attached to the symbolic name attribute will be
* stripped.
*
* @param bundle
* the bundle
* @return The bundles symbolic name or null if not specified.
*/
private static String getSymbolicName(Bundle bundle) {
if (bundle == null) {
return null;
}
final Dictionary<String, String> d = bundle.getHeaders("");
String bsn = d.get("Bundle-SymbolicName");
if (bsn != null && bsn.length() >0) {
// Remove parameters and directives from the value
final int semiPos = bsn.indexOf(';');
if (-1<semiPos) {
bsn = bsn.substring(0, semiPos).trim();
}
}
return bsn;
}
/**
* Get the symbolic name of the specified bundle. All directives and
* parameters attached to the symbolic name attribute will be stripped.
*
* @param bundle
* the bundle
* @return The bundles symbolic name or null if not specified.
*/
public static String symbolicName(Bundle bundle)
{
if (bundle == null) {
return null;
}
final Dictionary<String, String> d = bundle.getHeaders("");
String bsn = d.get("Bundle-SymbolicName");
if (bsn != null && bsn.length() > 0) {
// Remove parameters and directives from the value
final int semiPos = bsn.indexOf(';');
if (-1 < semiPos) {
bsn = bsn.substring(0, semiPos).trim();
}
}
return bsn;
}
public boolean runTest() {
try {
Bundle bundle = Util.installBundle(bc, "bundleEnd151_test-1.0.0.jar");
bundle.start();
for (int i = 0; i < loops; i++) {
for (int o = 0; o < locales.length; o++) {
bundle.getHeaders(locales[o]);
}
}
bundle.uninstall();
} catch (BundleException e) {
e.printStackTrace();
}
return true;
}
public boolean runTest() {
try {
Bundle fragment = Util.installBundle(bc, "bundleEnd152_test-1.0.0.jar");
Bundle bundle = Util.installBundle(bc, "bundleEnd151_test-1.0.0.jar");
bundle.start();
for (int i = 0; i < loops; i++) {
for (int o = 0; o < locales.length; o++) {
bundle.getHeaders(locales[o]);
}
}
bundle.uninstall();
fragment.uninstall();
} catch (BundleException e) {
e.printStackTrace();
}
return true;
}
/**
* @return whether the given bundle has all of its dependencies satisfied
* (including optional dependencies)
*/
public static boolean areBundlesDependenciesSatisfied(String bundleId) {
Bundle bundle = Platform.getBundle(bundleId);
if (bundle == null) {
return false;
}
// TODO: we need to do this using the bundle APIs, and not in such a brute
// force way
Dictionary<String, String> headers = bundle.getHeaders();
String requiredBundles[] = headers.get("Require-Bundle").split(",");
for (String requiredBundleId : requiredBundles) {
String id = requiredBundleId.split(";")[0];
if (!isBundleInstalled(id)) {
return false;
}
}
return true;
}
public static CatalogUpgrades parseBundleManifestForCatalogUpgrades(Bundle bundle, Supplier<? extends Iterable<? extends RegisteredType>> typeSupplier) {
// TODO Add support for the other options described in the proposal:
// https://docs.google.com/document/d/1Lm47Kx-cXPLe8BO34-qrL3ZMPosuUHJILYVQUswEH6Y/edit#
// section "Bundle Upgrade Metadata"
Dictionary<String, String> headers = bundle.getHeaders();
String upgradesForBundlesHeader = headers.get(MANIFEST_HEADER_UPGRADE_FOR_BUNDLES);
Multimap<VersionedName,VersionRangedName> upgradesForBundles = parseUpgradeForBundlesHeader(upgradesForBundlesHeader, bundle);
return CatalogUpgrades.builder()
.removedLegacyItems(parseForceRemoveLegacyItemsHeader(headers.get(MANIFEST_HEADER_FORCE_REMOVE_LEGACY_ITEMS), bundle, typeSupplier))
.removedBundles(parseForceRemoveBundlesHeader(headers.get(MANIFEST_HEADER_FORCE_REMOVE_BUNDLES), bundle))
.upgradeBundles(upgradesForBundles)
.upgradeTypes(parseUpgradeForTypesHeader(headers.get(MANIFEST_HEADER_UPGRADE_FOR_TYPES), bundle, typeSupplier,
upgradesForBundlesHeader==null ? null : upgradesForBundles))
.build();
}
/**
* reads component.xml from given bundle & returns an object representation of it
*
* @param registeredBundle The bundle that is being registered
* @param bundleContext The bundle context of the UI bundles
* @return Component
*/
public static Component build(Bundle registeredBundle, BundleContext bundleContext) {
Component component = null;
Dictionary headers = registeredBundle.getHeaders();
try {
URL url = registeredBundle.getEntry("META-INF/component.xml");
if (url != null) {
if (log.isDebugEnabled()) {
log.debug("Found component.xml in bundle : " + registeredBundle.getSymbolicName());
}
//found a Carbon OSGi bundle that should amend for admin UI
String bundleVersion = (String) headers.get("Bundle-Version");
String bundleName = (String) headers.get("Bundle-Name");
InputStream inputStream = url.openStream();
component = build(inputStream, bundleName, bundleVersion,
bundleContext);
}
} catch (Exception e) {
log.error("Cannot build component.xml for " + registeredBundle.getSymbolicName(), e);
}
return component;
}
/**
* @return the package names available for the passed bundles
*/
public List<String> setBundlesAndGetPackageNames(Bundle[] bundles) {
List<Bundle> acceptedBundles = new ArrayList<Bundle>();
List<String> names = new ArrayList<String>();
for (int i = 0; i < bundles.length; ++i) {
boolean addedSomePackage = false;
Bundle bundle = bundles[i];
Dictionary<String, String> headers = bundle.getHeaders();
addedSomePackage |= addPackageNames(bundle, names, headers.get("Provide-Package"));
addedSomePackage |= addPackageNames(bundle, names, headers.get("Export-Package"));
if (addedSomePackage) {
acceptedBundles.add(bundle);
}
}
this.bundles = acceptedBundles.toArray(new Bundle[acceptedBundles.size()]);
//for(Bundle b:this.bundles){
// System.out.println("Accepted:"+b.getHeaders().get("Bundle-Name"));
//}
return names;
}
public static Map<String, String> getManifestInfo() {
Map<String, String> result = new LinkedHashMap<String, String>();
Bundle bundle = getContext().getBundle();
Dictionary headers = bundle.getHeaders();
Enumeration enumer = headers.keys();
while (enumer.hasMoreElements()) {
Object elem = enumer.nextElement();
result.put(String.valueOf(elem), String.valueOf(headers.get(elem)));
}
return result;
}
private void processLoadedBundles() {
List<Bundle> toLoad = new ArrayList<Bundle>();
for (Bundle b : context.getBundles()) {
if (b.getState() == Bundle.ACTIVE) {
Dictionary<?,?> headers = b.getHeaders();
toLoad.addAll(queue.offer(b, provides(headers), requires(headers), needs(headers)));
}
}
// System.err.println("processing already loaded bundles: " + toLoad);
load(toLoad);
}
SlingModelsScriptEngineFactory(Bundle bundle) {
super();
setEngineName("Apache Sling Models");
// really the only time this is null is during testing
if (bundle != null && bundle.getHeaders() != null && bundle.getHeaders().get(Constants.BUNDLE_VERSION) != null) {
setEngineVersion(bundle.getHeaders().get(Constants.BUNDLE_VERSION).toString());
}
setNames("sling-models-exporter", "sling-models");
}
/**
* Provides a string to debug bundle information.
*
* @param bundle the bundle
* @return a debug string
*/
public static String debugString(final Bundle bundle) {
return "Bundle [getState()=" + bundle.getState() + ", getHeaders()=" + bundle.getHeaders() + ", getBundleId()="
+ bundle.getBundleId() + ", getLocation()=" + bundle.getLocation() + ", getRegisteredServices()="
+ Arrays.toString(bundle.getRegisteredServices()) + ", getServicesInUse()="
+ Arrays.toString(bundle.getServicesInUse()) + ", getSymbolicName()=" + bundle.getSymbolicName()
+ ", getLastModified()=" + bundle.getLastModified() + ", getBundleContext()="
+ bundle.getBundleContext() + ", getVersion()=" + bundle.getVersion() + "]";
}
public int cmdHeaders(Dictionary<String, ?> opts,
Reader in,
PrintWriter out,
Session session)
{
final Bundle[] b = getBundles((String[]) opts.get("bundle"),
opts.get("-i") != null);
final String locale = (String) opts.get("-l");
boolean found = false;
for (final Bundle element : b) {
if (element != null) {
out.println("Bundle: " + showBundle(element));
final Dictionary<String, String> d = (locale == null ? element.getHeaders()
: element.getHeaders(locale));
for (final Enumeration<String> e = d.keys(); e.hasMoreElements();) {
final String key = e.nextElement();
out.println(" " + key + " = " + Util.showObject(d.get(key)));
}
found = true;
}
}
if (!found) {
out.println("ERROR! No matching bundle");
return 1;
}
return 0;
}
/**
* 1)Search for the UIBundle header
* 2)Check for UI bundle fragments - for backward compatibility
*
* @param bundle
* @param action
* @throws CarbonException
*/
private void processUIBundle(Bundle bundle, String action) throws CarbonException {
Dictionary headers = bundle.getHeaders();
String value = (String) headers.get("Carbon-Component");
if (value != null && "UIBundle".equals(value)) { //this is a UI Bundle
if (CarbonConstants.ADD_UI_COMPONENT.equals(action)) {
if(log.isDebugEnabled()){
log.debug("UI component add action received in UIBundleDeployer : "+action);
}
if(log.isDebugEnabled()){
log.debug("Adding bundle resource paths : "+ bundle );
}
bundleBasedUIResourceProvider.addBundleResourcePaths(bundle);
if(log.isDebugEnabled()){
log.debug("processComponentXML in : "+ bundle +" "+action);
}
processComponentXML(bundle, action);
} else if (CarbonConstants.REMOVE_UI_COMPONENT.equals(action)){
if(log.isDebugEnabled()){
log.debug("UI component add action received in UIBundleDeployer : "+action);
}
if(log.isDebugEnabled()){
log.debug("Removing bundle resource paths : "+ bundle );
}
bundleBasedUIResourceProvider.removeBundleResourcePaths(bundle);
if(log.isDebugEnabled()){
log.debug("processComponentXML in : "+ bundle +" "+action);
}
processComponentXML(bundle, action);
}
}
}
private Version getBundleVersion(Bundle bundle) {
Dictionary<?, ?> headers = bundle.getHeaders();
String version = (String) headers.get(Constants.BUNDLE_VERSION);
return (version != null) ? Version.parseVersion(version) : Version.emptyVersion;
}
private Version getBundleVersion(Bundle bundle) {
Dictionary<?, ?> headers = bundle.getHeaders();
String version = (String) headers.get(Constants.BUNDLE_VERSION);
return (version != null) ? Version.parseVersion(version) : Version.emptyVersion;
}
public GoPluginManifest(Bundle bundle) {
headers = bundle.getHeaders();
}