Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2008, 2011 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ import java.util.*; import java.util.Map.Entry; import java.util.jar.*; import org.eclipse.osgi.internal.baseadaptor.BaseStorageHook; import org.eclipse.osgi.service.resolver.*; import org.osgi.framework.*; public class Main { private static String ELEMENT_SEPARATOR = "; "; private static final Object EQUALS_QUOTE = "=\""; private static Manifest getSurrogateManifest(Dictionary compositeManifest, BundleDescription compositeDesc, ExportPackageDescription[] matchingExports) { Manifest manifest = new Manifest(); Attributes attributes = manifest.getMainAttributes(); attributes.putValue("Manifest-Version", "1.0"); //$NON-NLS-1$//$NON-NLS-2$ // Ignore the manifest version from the map // always use bundle manifest version 2 attributes.putValue(Constants.BUNDLE_MANIFESTVERSION, "2"); //$NON-NLS-1$ // Ignore the Equinox composite bundle header attributes.putValue(BaseStorageHook.COMPOSITE_HEADER, BaseStorageHook.SURROGATE_BUNDLE); if (compositeDesc != null && matchingExports != null) { // convert the exports from the composite into imports addImports(attributes, compositeDesc, matchingExports); // convert the matchingExports from the composite into exports addExports(attributes, compositeDesc, matchingExports); } // add the rest for (Enumeration keys = compositeManifest.keys(); keys.hasMoreElements();) { Object header = keys.nextElement(); if (Constants.BUNDLE_MANIFESTVERSION.equals(header) || BaseStorageHook.COMPOSITE_HEADER.equals(header) || Constants.IMPORT_PACKAGE.equals(header) || Constants.EXPORT_PACKAGE.equals(header)) continue; if (header instanceof String && compositeManifest.get(header) instanceof String) attributes.putValue((String) header, (String) compositeManifest.get(header)); } return manifest; } private static void addImports(Attributes attrigutes, BundleDescription compositeDesc, ExportPackageDescription[] matchingExports) { ExportPackageDescription[] exports = compositeDesc.getExportPackages(); List systemExports = getSystemExports(matchingExports); if (exports.length == 0 && systemExports.size() == 0) return; StringBuffer importStatement = new StringBuffer(); Collection importedNames = new ArrayList(exports.length); int i = 0; for (; i < exports.length; i++) { if (i != 0) importStatement.append(','); importedNames.add(exports[i].getName()); getImportFrom(exports[i], importStatement); } for (Iterator iSystemExports = systemExports.iterator(); iSystemExports.hasNext();) { ExportPackageDescription systemExport = (ExportPackageDescription) iSystemExports.next(); if (!importedNames.contains(systemExport.getName())) { if (i != 0) importStatement.append(','); i++; importStatement.append(systemExport.getName()).append(ELEMENT_SEPARATOR) .append(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE).append('=') .append(Constants.SYSTEM_BUNDLE_SYMBOLICNAME); } } attrigutes.putValue(Constants.IMPORT_PACKAGE, importStatement.toString()); } private static void addExports(Attributes attributes, BundleDescription compositeDesc, ExportPackageDescription[] matchingExports) { if (matchingExports.length == 0) return; StringBuffer exportStatement = new StringBuffer(); for (int i = 0; i < matchingExports.length; i++) { if (matchingExports[i].getExporter() == compositeDesc) { // the matching export from outside is the composite bundle itself // this must be one of our own substitutable exports, it must be ignored (bug 345640) continue; } if (exportStatement.length() > 0) exportStatement.append(','); getExportFrom(matchingExports[i], exportStatement); } if (exportStatement.length() > 0) attributes.putValue(Constants.EXPORT_PACKAGE, exportStatement.toString()); } private static List getSystemExports(ExportPackageDescription[] matchingExports) { ArrayList list = null; for (int i = 0; i < matchingExports.length; i++) { if (matchingExports[i].getExporter().getBundleId() != 0) continue; if (list == null) list = new ArrayList(); list.add(matchingExports[i]); } return list == null ? Collections.EMPTY_LIST : list; } private static void getImportFrom(ExportPackageDescription export, StringBuffer importStatement) { importStatement.append(export.getName()).append(ELEMENT_SEPARATOR); Version version = export.getVersion(); importStatement.append(Constants.VERSION_ATTRIBUTE).append(EQUALS_QUOTE).append('[').append(version) .append(',').append(new Version(version.getMajor(), version.getMinor(), version.getMicro() + 1)) .append(')').append('\"'); addMap(importStatement, export.getAttributes(), "="); //$NON-NLS-1$ } private static void getExportFrom(ExportPackageDescription export, StringBuffer exportStatement) { exportStatement.append(export.getName()).append(ELEMENT_SEPARATOR); exportStatement.append(Constants.VERSION_ATTRIBUTE).append(EQUALS_QUOTE).append(export.getVersion()) .append('\"'); addMap(exportStatement, export.getDirectives(), ":="); //$NON-NLS-1$ addMap(exportStatement, export.getAttributes(), "="); //$NON-NLS-1$ } private static void addMap(StringBuffer manifest, Map values, String assignment) { if (values == null) return; // nothing to add for (Iterator iEntries = values.entrySet().iterator(); iEntries.hasNext();) { manifest.append(ELEMENT_SEPARATOR); Map.Entry entry = (Entry) iEntries.next(); manifest.append(entry.getKey()).append(assignment).append('\"'); Object value = entry.getValue(); if (value instanceof String[]) { String[] strings = (String[]) value; for (int i = 0; i < strings.length; i++) { if (i != 0) manifest.append(','); manifest.append(strings[i]); } } else { manifest.append(value); } manifest.append('\"'); } } }