You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:21:40 UTC
[sling-org-apache-sling-testing-osgi-mock] 11/13: SLING-6362
osgi-mock: ManifestScanner
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.testing.osgi-mock-2.2.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git
commit e6448851a31f63aed86cec887850c2bea2167010
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Sun Dec 4 07:22:34 2016 +0000
SLING-6362 osgi-mock: ManifestScanner
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1772506 13f79535-47bb-0310-9956-ffa450edef68
---
.../sling/testing/mock/osgi/ManifestScanner.java | 93 ++++++++++++++++++++++
.../sling/testing/mock/osgi/package-info.java | 2 +-
.../testing/mock/osgi/ManifestScannerTest.java} | 28 ++++++-
3 files changed, 118 insertions(+), 5 deletions(-)
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/ManifestScanner.java b/src/main/java/org/apache/sling/testing/mock/osgi/ManifestScanner.java
new file mode 100644
index 0000000..2058e71
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/ManifestScanner.java
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.testing.mock.osgi;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Collection;
+import java.util.Enumeration;
+import java.util.LinkedHashSet;
+import java.util.Set;
+import java.util.jar.Attributes;
+import java.util.jar.JarFile;
+import java.util.jar.Manifest;
+
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Scan METAINF/MANIFEST.MF files.
+ */
+public final class ManifestScanner {
+
+ private static final Logger log = LoggerFactory.getLogger(ManifestScanner.class);
+
+ private ManifestScanner() {
+ // static methods only
+ }
+
+ /**
+ * Get all bundle header values stored in MANIFEST.MF files as attributes.
+ * Attributes values from all manifest files are collected, and values separated by "," are returned individually.
+ * The order of the values from each entry is preserved, but the overall order when multiple bundles define such an entry
+ * is not deterministic. Duplicate values are eliminated.
+ * @param attributeName Attribute / Bundle header name.
+ * @return List of values.
+ */
+ public static Collection<String> getValues(final String attributeName) {
+ Set<String> values = new LinkedHashSet<String>();
+ try {
+ Enumeration<URL> resEnum = ManifestScanner.class.getClassLoader().getResources(JarFile.MANIFEST_NAME);
+ while (resEnum.hasMoreElements()) {
+ try {
+ URL url = (URL)resEnum.nextElement();
+ InputStream is = url.openStream();
+ if (is != null) {
+ try {
+ Manifest manifest = new Manifest(is);
+ Attributes mainAttribs = manifest.getMainAttributes();
+ String valueList = mainAttribs.getValue(attributeName);
+ String[] valueArray = StringUtils.split(valueList, ",");
+ if (valueArray != null) {
+ for (String value : valueArray) {
+ if (!StringUtils.isBlank(value)) {
+ values.add(StringUtils.trim(value));
+ }
+ }
+ }
+ }
+ finally {
+ is.close();
+ }
+ }
+ }
+ catch (Throwable ex) {
+ log.warn("Unable to read JAR manifest.", ex);
+ }
+ }
+ }
+ catch (IOException ex) {
+ log.warn("Unable to read JAR manifests.", ex);
+ }
+ return values;
+ }
+
+}
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java b/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java
index b0f0eaf..94cd4bc 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java
@@ -19,5 +19,5 @@
/**
* Mock implementation of selected OSGi APIs.
*/
-@org.osgi.annotation.versioning.Version("3.1")
+@org.osgi.annotation.versioning.Version("3.2")
package org.apache.sling.testing.mock.osgi;
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java b/src/test/java/org/apache/sling/testing/mock/osgi/ManifestScannerTest.java
similarity index 53%
copy from src/main/java/org/apache/sling/testing/mock/osgi/package-info.java
copy to src/test/java/org/apache/sling/testing/mock/osgi/ManifestScannerTest.java
index b0f0eaf..8a536c5 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java
+++ b/src/test/java/org/apache/sling/testing/mock/osgi/ManifestScannerTest.java
@@ -16,8 +16,28 @@
* specific language governing permissions and limitations
* under the License.
*/
-/**
- * Mock implementation of selected OSGi APIs.
- */
-@org.osgi.annotation.versioning.Version("3.1")
package org.apache.sling.testing.mock.osgi;
+
+import static org.junit.Assert.assertTrue;
+
+import java.util.Collection;
+
+import org.junit.Test;
+import org.osgi.framework.Constants;
+
+public class ManifestScannerTest {
+
+ /**
+ * Test some MANIFEST entries from commons-io:commons-io:2.4
+ */
+ @Test
+ public void testGetValues() {
+ Collection<String> bundleSymbolicNames = ManifestScanner.getValues(Constants.BUNDLE_SYMBOLICNAME);
+ assertTrue(bundleSymbolicNames.contains("org.apache.commons.io"));
+
+ Collection<String> includeResource = ManifestScanner.getValues("Include-Resource");
+ assertTrue(includeResource.contains("META-INF/LICENSE.txt=LICENSE.txt"));
+ assertTrue(includeResource.contains("META-INF/NOTICE.txt=NOTICE.txt"));
+ }
+
+}
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.