You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2016/11/03 15:48:00 UTC

svn commit: r1767910 - in /sling/trunk/tooling/bnd: ./ models-bnd-plugin/ models-bnd-plugin/src/ models-bnd-plugin/src/main/ models-bnd-plugin/src/main/java/ models-bnd-plugin/src/main/java/org/ models-bnd-plugin/src/main/java/org/apache/ models-bnd-pl...

Author: sseifert
Date: Thu Nov  3 15:48:00 2016
New Revision: 1767910

URL: http://svn.apache.org/viewvc?rev=1767910&view=rev
Log:
SLING-6048 bnd plugin for sling models

Added:
    sling/trunk/tooling/bnd/
    sling/trunk/tooling/bnd/models-bnd-plugin/
    sling/trunk/tooling/bnd/models-bnd-plugin/pom.xml   (with props)
    sling/trunk/tooling/bnd/models-bnd-plugin/src/
    sling/trunk/tooling/bnd/models-bnd-plugin/src/main/
    sling/trunk/tooling/bnd/models-bnd-plugin/src/main/java/
    sling/trunk/tooling/bnd/models-bnd-plugin/src/main/java/org/
    sling/trunk/tooling/bnd/models-bnd-plugin/src/main/java/org/apache/
    sling/trunk/tooling/bnd/models-bnd-plugin/src/main/java/org/apache/sling/
    sling/trunk/tooling/bnd/models-bnd-plugin/src/main/java/org/apache/sling/bnd/
    sling/trunk/tooling/bnd/models-bnd-plugin/src/main/java/org/apache/sling/bnd/models/
    sling/trunk/tooling/bnd/models-bnd-plugin/src/main/java/org/apache/sling/bnd/models/ModelsScannerPlugin.java   (with props)

Added: sling/trunk/tooling/bnd/models-bnd-plugin/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/bnd/models-bnd-plugin/pom.xml?rev=1767910&view=auto
==============================================================================
--- sling/trunk/tooling/bnd/models-bnd-plugin/pom.xml (added)
+++ sling/trunk/tooling/bnd/models-bnd-plugin/pom.xml Thu Nov  3 15:48:00 2016
@@ -0,0 +1,56 @@
+<?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.bnd.models</artifactId>
+    <packaging>jar</packaging>
+    <version>1.0.0-SNAPSHOT</version>
+    <name>Apache Sling Models bnd Plugin</name>
+    <description>This bnd plugin detected all Sling Models classes and adds a bundle header 'Sling-Model-Classes' containing their names.</description>
+
+    <scm>
+        <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/tooling/bnd/models-bnd-plugin</connection>
+        <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/tooling/bnd/models-bnd-plugin</developerConnection>
+        <url>http://svn.apache.org/viewvc/sling/trunk/tooling/bnd/models-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>
+    </dependencies>
+
+</project>

Propchange: sling/trunk/tooling/bnd/models-bnd-plugin/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/tooling/bnd/models-bnd-plugin/pom.xml
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Thu Nov  3 15:48:00 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: sling/trunk/tooling/bnd/models-bnd-plugin/pom.xml
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: sling/trunk/tooling/bnd/models-bnd-plugin/src/main/java/org/apache/sling/bnd/models/ModelsScannerPlugin.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/bnd/models-bnd-plugin/src/main/java/org/apache/sling/bnd/models/ModelsScannerPlugin.java?rev=1767910&view=auto
==============================================================================
--- sling/trunk/tooling/bnd/models-bnd-plugin/src/main/java/org/apache/sling/bnd/models/ModelsScannerPlugin.java (added)
+++ sling/trunk/tooling/bnd/models-bnd-plugin/src/main/java/org/apache/sling/bnd/models/ModelsScannerPlugin.java Thu Nov  3 15:48:00 2016
@@ -0,0 +1,100 @@
+/*
+ * 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.bnd.models;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
+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 Sling Models classes.
+ * All class names found are stored in a bundle header for processing them at runtime and reading their metadata.
+ */
+public class ModelsScannerPlugin implements AnalyzerPlugin, Plugin {
+    
+    private static final String MODELS_ANNOTATION_CLASS = "org.apache.sling.models.annotations.Model";
+    
+    private static final String MODELS_PACKAGES_HEADER = "Sling-Model-Packages";
+    private static final String MODELS_CLASSES_HEADER = "Sling-Model-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 {
+        
+        // process only if no models packages or class header was set
+        if (analyzer.get(MODELS_PACKAGES_HEADER) == null && analyzer.get(MODELS_CLASSES_HEADER) == null) {
+
+            // get all annotation classes from this project with Configuration annotation
+            Collection<String> classNames = getClassesWithAnnotation(MODELS_ANNOTATION_CLASS, analyzer);
+
+            // set bundle header containing all class names found
+            analyzer.set(MODELS_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) {
+        List<String> classNames = new ArrayList<>();
+        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;
+    }
+    
+}

Propchange: sling/trunk/tooling/bnd/models-bnd-plugin/src/main/java/org/apache/sling/bnd/models/ModelsScannerPlugin.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/tooling/bnd/models-bnd-plugin/src/main/java/org/apache/sling/bnd/models/ModelsScannerPlugin.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Thu Nov  3 15:48:00 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: sling/trunk/tooling/bnd/models-bnd-plugin/src/main/java/org/apache/sling/bnd/models/ModelsScannerPlugin.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain