Add .tox/ to .gitignore
[odlparent.git] / karaf / karaf-maven-plugin / src / main / java / org / apache / karaf / tooling / utils / ManifestUtils.java
1 /**
2  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18 package org.apache.karaf.tooling.utils;
19
20 import java.util.LinkedList;
21 import java.util.List;
22 import java.util.jar.Manifest;
23
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;
28
29
30 /**
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}
33  */
34
35 public class ManifestUtils {
36
37     private ManifestUtils() {
38         // hide the constructor
39     }
40
41     /**
42      * Get the list of imports from the manifest.  If no imports have been defined, this method returns an empty list.
43      *
44      * @param manifest the manifest
45      * @return the list of imports
46      */
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) {
51                 result.add(clause);
52         }
53         return result;
54     }
55
56     /**
57      * Get the list of non-optional imports from the manifest.
58      *
59      * @param manifest the manifest
60      * @return the list of non-optional imports
61      */
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)) {
66                 result.add(clause);
67             }
68         }
69         return result;
70     }
71
72     /**
73      * Get the list of exports from the manifest.  If no exports have been defined, this method returns an empty list.
74      *
75      * @param manifest the manifest
76      * @return the list of exports
77      */
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) {
82                 result.add(clause);
83         }
84         return result;
85     }
86
87     /**
88      * Check if a given manifest clause represents an optional import
89      *
90      * @param clause the manifest clause
91      * @return <code>true</code> for an optional import, <code>false</code> for mandatory imports
92      */
93     public static boolean isOptional(Clause clause) {
94         return "optional".equals(clause.getDirective("resolution"));
95     }
96
97     /**
98      * Check if the manifest contains the mandatory Bundle-Symbolic-Name
99      *
100      * @param manifest the manifest
101      * @return <code>true</code> if the manifest specifies a Bundle-Symbolic-Name
102      */
103     public static boolean isBundle(Manifest manifest) {
104         return getBsn(manifest) != null;
105     }
106
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;
113         }
114         return false;
115     }
116     
117     public static String getHeader(String name, Manifest manifest) {
118         String value = manifest.getMainAttributes().getValue(name);
119         return value;           
120     }
121     
122     public static String getBsn(Manifest manifest) {
123         String bsn = getHeader(Constants.BUNDLE_SYMBOLICNAME, manifest);
124         return bsn;
125     }
126     
127     @SuppressWarnings("deprecation")
128         public static VersionRange getVersionRange(Clause clause)
129     {
130         String v = clause.getAttribute(Constants.VERSION_ATTRIBUTE);
131         if (v == null)
132         {
133             v = clause.getAttribute(Constants.PACKAGE_SPECIFICATION_VERSION);
134         }
135         if (v == null)
136         {
137             v = clause.getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE);
138         }
139         return VersionRange.parseVersionRange(v);
140     }
141 }