3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.karaf.tooling.utils;
20 import java.util.LinkedList;
21 import java.util.List;
22 import java.util.jar.Manifest;
24 import org.apache.felix.utils.manifest.Clause;
25 import org.apache.felix.utils.manifest.Parser;
26 import org.apache.felix.utils.version.VersionRange;
27 import org.osgi.framework.Constants;
31 * A set of utility methods to ease working with {@link org.apache.felix.utils.manifest.Parser} and
32 * {@link org.apache.felix.utils.manifest.Clause}
35 public class ManifestUtils {
37 private ManifestUtils() {
38 // hide the constructor
42 * Get the list of imports from the manifest. If no imports have been defined, this method returns an empty list.
44 * @param manifest the manifest
45 * @return the list of imports
47 public static List<Clause> getImports(Manifest manifest) {
48 List<Clause> result = new LinkedList<Clause>();
49 Clause[] clauses = Parser.parseHeader(getHeader(Constants.IMPORT_PACKAGE, manifest));
50 for (Clause clause : clauses) {
57 * Get the list of non-optional imports from the manifest.
59 * @param manifest the manifest
60 * @return the list of non-optional imports
62 public static List<Clause> getMandatoryImports(Manifest manifest) {
63 List<Clause> result = new LinkedList<Clause>();
64 for (Clause clause : getImports(manifest)) {
65 if (!isOptional(clause)) {
73 * Get the list of exports from the manifest. If no exports have been defined, this method returns an empty list.
75 * @param manifest the manifest
76 * @return the list of exports
78 public static List<Clause> getExports(Manifest manifest) {
79 List<Clause> result = new LinkedList<Clause>();
80 Clause[] clauses = Parser.parseHeader(getHeader(Constants.EXPORT_PACKAGE, manifest));
81 for (Clause clause : clauses) {
88 * Check if a given manifest clause represents an optional import
90 * @param clause the manifest clause
91 * @return <code>true</code> for an optional import, <code>false</code> for mandatory imports
93 public static boolean isOptional(Clause clause) {
94 return "optional".equals(clause.getDirective("resolution"));
98 * Check if the manifest contains the mandatory Bundle-Symbolic-Name
100 * @param manifest the manifest
101 * @return <code>true</code> if the manifest specifies a Bundle-Symbolic-Name
103 public static boolean isBundle(Manifest manifest) {
104 return getBsn(manifest) != null;
107 public static boolean matches(Clause requirement, Clause export) {
108 if (requirement.getName().equals(export.getName())) {
109 VersionRange importVersionRange = getVersionRange(requirement);
110 VersionRange exportVersionRange = getVersionRange(export);
111 VersionRange intersection = importVersionRange.intersect(exportVersionRange);
112 return intersection != null;
117 public static String getHeader(String name, Manifest manifest) {
118 String value = manifest.getMainAttributes().getValue(name);
122 public static String getBsn(Manifest manifest) {
123 String bsn = getHeader(Constants.BUNDLE_SYMBOLICNAME, manifest);
127 @SuppressWarnings("deprecation")
128 public static VersionRange getVersionRange(Clause clause)
130 String v = clause.getAttribute(Constants.VERSION_ATTRIBUTE);
133 v = clause.getAttribute(Constants.PACKAGE_SPECIFICATION_VERSION);
137 v = clause.getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE);
139 return VersionRange.parseVersionRange(v);