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/10/18 23:29:24 UTC

[sling-org-apache-sling-caconfig-bnd-plugin] 01/03: SLING-6427 Move Sling Context-Aware Config out of contrib - move in svn - update all SCM URLs - update jenkins build jobs

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-caconfig-bnd-plugin.git

commit 8406947766a638606ea0f1fa6e627559e74db801
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Thu Dec 22 09:52:16 2016 +0000

    SLING-6427 Move Sling Context-Aware Config out of contrib
    - move in svn
    - update all SCM URLs
    - update jenkins build jobs
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1775601 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            | 78 +++++++++++++++++
 .../bndplugin/ConfigurationClassScannerPlugin.java | 97 ++++++++++++++++++++++
 src/test/java/dummy/example/pkg1/Config1.java      | 26 ++++++
 src/test/java/dummy/example/pkg1/Config2.java      | 26 ++++++
 src/test/java/dummy/example/pkg1/sub1/Config3.java | 26 ++++++
 .../AbstractConfigurationClassScannerPlugin.java   | 79 ++++++++++++++++++
 .../caconfig/bndplugin/GenerateHeaderTest.java     | 39 +++++++++
 7 files changed, 371 insertions(+)

diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..b0d3abd
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,78 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.sling</groupId>
+        <artifactId>sling</artifactId>
+        <version>29</version>
+        <relativePath />
+    </parent>
+
+    <artifactId>org.apache.sling.caconfig.bnd-plugin</artifactId>
+    <packaging>jar</packaging>
+    <version>1.0.3-SNAPSHOT</version>
+    <name>Apache Sling Context-Aware Configuration bnd Plugin</name>
+    <description>This bnd plugin detected all configuration annotation classes and adds a bundle header 'Sling-ContextAware-Configuration-Classes' containing their names.</description>
+
+    <scm>
+        <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/tooling/bnd/caconfig-bnd-plugin</connection>
+        <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/tooling/bnd/caconfig-bnd-plugin</developerConnection>
+        <url>http://svn.apache.org/viewvc/sling/trunk/tooling/bnd/caconfig-bnd-plugin</url>
+    </scm>
+
+    <dependencies>
+        <dependency>
+            <groupId>biz.aQute.bnd</groupId>
+            <artifactId>biz.aQute.bndlib</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-lang3</artifactId>
+            <version>3.3.2</version>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <version>2.2.15</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.caconfig.api</artifactId>
+            <version>1.1.0</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+</project>
diff --git a/src/main/java/org/apache/sling/caconfig/bndplugin/ConfigurationClassScannerPlugin.java b/src/main/java/org/apache/sling/caconfig/bndplugin/ConfigurationClassScannerPlugin.java
new file mode 100644
index 0000000..9584741
--- /dev/null
+++ b/src/main/java/org/apache/sling/caconfig/bndplugin/ConfigurationClassScannerPlugin.java
@@ -0,0 +1,97 @@
+/*
+ * 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.caconfig.bndplugin;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+import org.apache.commons.lang3.StringUtils;
+
+import aQute.bnd.osgi.Analyzer;
+import aQute.bnd.osgi.Clazz;
+import aQute.bnd.osgi.Clazz.QUERY;
+import aQute.bnd.osgi.Instruction;
+import aQute.bnd.service.AnalyzerPlugin;
+import aQute.bnd.service.Plugin;
+import aQute.service.reporter.Reporter;
+
+/**
+ * Scans the classpath of the bundle for annotation classes with the "Configuration" annotation
+ * from the Sling Context-Aware Configuraiton API.
+ * All class names found are stored in a bundle header for processing them at runtime and reading their metadata.
+ */
+public class ConfigurationClassScannerPlugin implements AnalyzerPlugin, Plugin {
+    
+    static final String CONFIGURATION_ANNOTATION_CLASS = "org.apache.sling.caconfig.annotation.Configuration";
+    
+    static final String CONFIGURATION_CLASSES_HEADER = "Sling-ContextAware-Configuration-Classes";
+    
+    private Reporter reporter;
+
+    @Override
+    public void setProperties(Map<String, String> map) throws Exception {
+        // ignore
+    }
+
+    @Override
+    public void setReporter(Reporter reporter) {
+        this.reporter = reporter;
+    }
+
+    @Override
+    public boolean analyzeJar(Analyzer analyzer) throws Exception {
+        
+        // get all annotation classes from this project with Configuration annotation
+        Collection<String> classNames = getClassesWithAnnotation(CONFIGURATION_ANNOTATION_CLASS, analyzer);
+
+        // set bundle header containing all class names found
+        if (!classNames.isEmpty()) {
+            analyzer.set(CONFIGURATION_CLASSES_HEADER, StringUtils.join(classNames, ","));
+        }
+        
+        // we did not change any classes - no need to re-analyze
+        return false;
+    }
+    
+    /**
+     * Get all classes that implement the given annotation via bnd Analyzer.
+     * @param analyzer Analyzer
+     * @param annotation Annotation
+     * @return Class names
+     */
+    private Collection<String> getClassesWithAnnotation(String annotationClassName, Analyzer analyzer) {
+        SortedSet<String> classNames = new TreeSet<>();
+        Collection<Clazz> clazzes = analyzer.getClassspace().values();
+        Instruction instruction = new Instruction(annotationClassName);
+        try {
+            for (Clazz clazz : clazzes) {
+                if (clazz.isAnnotation() && clazz.is(QUERY.ANNOTATED, instruction, analyzer)) {
+                    classNames.add(clazz.getClassName().getFQN());
+                }
+            }
+        }
+        catch (Exception ex) {
+            reporter.exception(ex, "Error querying for classes with annotation: " + annotationClassName);
+        }
+        return classNames;
+    }
+    
+}
diff --git a/src/test/java/dummy/example/pkg1/Config1.java b/src/test/java/dummy/example/pkg1/Config1.java
new file mode 100644
index 0000000..9dd90b7
--- /dev/null
+++ b/src/test/java/dummy/example/pkg1/Config1.java
@@ -0,0 +1,26 @@
+/*
+ * 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 dummy.example.pkg1;
+
+import org.apache.sling.caconfig.annotation.Configuration;
+
+@Configuration
+public @interface Config1 {
+
+}
diff --git a/src/test/java/dummy/example/pkg1/Config2.java b/src/test/java/dummy/example/pkg1/Config2.java
new file mode 100644
index 0000000..322a1df
--- /dev/null
+++ b/src/test/java/dummy/example/pkg1/Config2.java
@@ -0,0 +1,26 @@
+/*
+ * 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 dummy.example.pkg1;
+
+import org.apache.sling.caconfig.annotation.Configuration;
+
+@Configuration
+public @interface Config2 {
+
+}
diff --git a/src/test/java/dummy/example/pkg1/sub1/Config3.java b/src/test/java/dummy/example/pkg1/sub1/Config3.java
new file mode 100644
index 0000000..456413c
--- /dev/null
+++ b/src/test/java/dummy/example/pkg1/sub1/Config3.java
@@ -0,0 +1,26 @@
+/*
+ * 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 dummy.example.pkg1.sub1;
+
+import org.apache.sling.caconfig.annotation.Configuration;
+
+@Configuration
+public @interface Config3 {
+
+}
diff --git a/src/test/java/org/apache/sling/caconfig/bndplugin/AbstractConfigurationClassScannerPlugin.java b/src/test/java/org/apache/sling/caconfig/bndplugin/AbstractConfigurationClassScannerPlugin.java
new file mode 100644
index 0000000..e0e9e31
--- /dev/null
+++ b/src/test/java/org/apache/sling/caconfig/bndplugin/AbstractConfigurationClassScannerPlugin.java
@@ -0,0 +1,79 @@
+/*
+ * 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.caconfig.bndplugin;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.jar.Manifest;
+
+import org.apache.commons.lang3.StringUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.runner.RunWith;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import aQute.bnd.osgi.Builder;
+import aQute.bnd.osgi.Jar;
+import aQute.bnd.service.Plugin;
+import aQute.service.reporter.Reporter;
+
+@RunWith(MockitoJUnitRunner.class)
+public abstract class AbstractConfigurationClassScannerPlugin {
+    
+    protected Builder builder;
+
+    @Before
+    public final void setUp() throws Exception {
+        builder = new Builder();
+        
+        Jar classesDirJar = new Jar("test.jar", new File("target/test-classes"));
+        classesDirJar.setManifest(new Manifest());
+        builder.setJar(classesDirJar);
+        
+        builder.setSourcepath(new File[] { new File("src/test/java") } );
+        
+        Plugin plugin = new ConfigurationClassScannerPlugin();
+        plugin.setReporter(mock(Reporter.class));
+        plugin.setProperties(new HashMap<String,String>());
+        builder.addBasicPlugin(plugin);
+    }
+
+    @After
+    public final void tearDown() throws Exception {
+        if (!builder.getErrors().isEmpty()) {
+            fail(StringUtils.join(builder.getErrors(), "\n"));
+        }
+        builder.close();
+    }
+    
+    protected final void assertHeader(Jar jar, String headerName, String... headerValues) throws Exception {
+        Set<String> expectedValues = new HashSet<>(Arrays.asList(headerValues));
+        String[] actual = StringUtils.split(jar.getManifest().getMainAttributes().getValue(headerName), ",");
+        Set<String> actualValues = new HashSet<>(Arrays.asList(actual));
+        assertEquals(expectedValues, actualValues);
+    }
+
+}
diff --git a/src/test/java/org/apache/sling/caconfig/bndplugin/GenerateHeaderTest.java b/src/test/java/org/apache/sling/caconfig/bndplugin/GenerateHeaderTest.java
new file mode 100644
index 0000000..aed0980
--- /dev/null
+++ b/src/test/java/org/apache/sling/caconfig/bndplugin/GenerateHeaderTest.java
@@ -0,0 +1,39 @@
+/*
+ * 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.caconfig.bndplugin;
+
+import static org.apache.sling.caconfig.bndplugin.ConfigurationClassScannerPlugin.*;
+
+import org.junit.Test;
+
+import aQute.bnd.osgi.Jar;
+
+public class GenerateHeaderTest extends AbstractConfigurationClassScannerPlugin {
+
+    @Test
+    public void testBuild() throws Exception {
+        Jar jar = builder.build();
+        
+        assertHeader(jar, CONFIGURATION_CLASSES_HEADER, 
+                "dummy.example.pkg1.Config1",
+                "dummy.example.pkg1.Config2",
+                "dummy.example.pkg1.sub1.Config3");        
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.