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/20 14:38:15 UTC

[sling-org-apache-sling-hc-annotations] 01/21: SLING-3624 - move SlingHealthCheck annotation to a new annotations jar

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-hc-annotations.git

commit 318e26e931e708237b8d1837a0b5cb0ebd557cb6
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Wed Jul 30 19:51:03 2014 +0000

    SLING-3624 - move SlingHealthCheck annotation to a new annotations jar
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1614748 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  57 +++++++++
 .../sling/hc/annotations/SlingHealthCheck.java     |  68 +++++++++++
 .../hc/annotations/SlingHealthCheckProcessor.java  | 134 +++++++++++++++++++++
 ...felix.scrplugin.annotations.AnnotationProcessor |   1 +
 4 files changed, 260 insertions(+)

diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..13547ef
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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 http://maven.apache.org/maven-v4_0_0.xsd">
+    
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.sling</groupId>
+        <artifactId>sling</artifactId>
+        <version>19</version>
+        <relativePath>../../../../parent/pom.xml</relativePath>
+    </parent>
+
+    <groupId>org.apache.sling</groupId>
+    <artifactId>org.apache.sling.hc.annotations</artifactId>
+    <packaging>jar</packaging>
+    <version>0.9.9-SNAPSHOT</version>
+
+    <name>Apache Sling Health Check Annotations</name>
+    <inceptionYear>2013</inceptionYear>
+    
+    <description>
+        Sling Health Check Annotations
+    </description>
+
+    <properties>
+        <sling.java.version>6</sling.java.version>
+    </properties>
+
+    <scm>
+        <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/healthcheck/annotations</connection>
+        <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/healthcheck/annotations</developerConnection>
+        <url>http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/annotations</url>
+    </scm>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr.generator</artifactId>
+            <version>1.10.0</version>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.hc.core</artifactId>
+            <version>1.1.0</version>
+            <scope>provided</scope>
+        </dependency>
+     </dependencies>
+</project>
diff --git a/src/main/java/org/apache/sling/hc/annotations/SlingHealthCheck.java b/src/main/java/org/apache/sling/hc/annotations/SlingHealthCheck.java
new file mode 100644
index 0000000..dcba01f
--- /dev/null
+++ b/src/main/java/org/apache/sling/hc/annotations/SlingHealthCheck.java
@@ -0,0 +1,68 @@
+/*
+ * 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 SF 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.hc.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import org.apache.felix.scr.annotations.ConfigurationPolicy;
+
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.CLASS)
+public @interface SlingHealthCheck {
+
+    /** Whether to generate a default SCR component tag. If set to false, a {@link org.apache.felix.scr.annotations.Component} annotation can be added manually
+     * with defined whatever configuration needed. */
+    boolean generateComponent() default true;
+
+    /** Whether to generate a default SCR service tag with "interface=org.apache.sling.hc.api.HealthCheck". If set to false, a
+     * {@link org.apache.felix.scr.annotations.Service} annotation can be added manually with defined whatever configuration needed. */
+    boolean generateService() default true;
+
+    /** Defines the name of the health check. */
+    String name();
+    
+    /** Defines the Component name also used as the PID for the Configuration Admin Service. Default value: Fully qualified name of the Java class. */
+    String componentName() default "";
+
+    /** Whether Metatype Service data is generated or not. If this parameter is set to true Metatype Service data is generated in the <code>metatype.xml</code>
+     * file for this component. Otherwise no Metatype Service data is generated for this component. */
+    boolean metatype() default true;
+
+    /** Set the metatype factory pid property (only for non factory components). */
+    boolean configurationFactory() default false;
+
+    /** The component configuration policy */
+    ConfigurationPolicy configurationPolicy() default ConfigurationPolicy.OPTIONAL;
+
+    /** This is generally used as a title for the object described by the meta type. This name may be localized by prepending a % sign to the name. Default
+     * value: %&lt;name&gt;.name */
+    String label() default "";
+
+    /** This is generally used as a description for the object described by the meta type. This name may be localized by prepending a % sign to the name. Default
+     * value: %&lt;name&gt;.description */
+    String description() default "";
+
+    /** One ore more tags.
+     * <p>
+     * This attribute is converted to values for the <code>hc.tags</code> property. */
+    String[] tags() default {};
+
+}
diff --git a/src/main/java/org/apache/sling/hc/annotations/SlingHealthCheckProcessor.java b/src/main/java/org/apache/sling/hc/annotations/SlingHealthCheckProcessor.java
new file mode 100644
index 0000000..84562e4
--- /dev/null
+++ b/src/main/java/org/apache/sling/hc/annotations/SlingHealthCheckProcessor.java
@@ -0,0 +1,134 @@
+/*
+ * 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 SF 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.hc.annotations;
+
+import java.util.List;
+
+import org.apache.felix.scrplugin.SCRDescriptorException;
+import org.apache.felix.scrplugin.SCRDescriptorFailureException;
+import org.apache.felix.scrplugin.annotations.AnnotationProcessor;
+import org.apache.felix.scrplugin.annotations.ClassAnnotation;
+import org.apache.felix.scrplugin.annotations.ScannedClass;
+import org.apache.felix.scrplugin.description.ClassDescription;
+import org.apache.felix.scrplugin.description.ComponentConfigurationPolicy;
+import org.apache.felix.scrplugin.description.ComponentDescription;
+import org.apache.felix.scrplugin.description.PropertyDescription;
+import org.apache.felix.scrplugin.description.PropertyType;
+import org.apache.felix.scrplugin.description.PropertyUnbounded;
+import org.apache.felix.scrplugin.description.ServiceDescription;
+import org.apache.sling.hc.api.HealthCheck;
+
+/** Annotation processor for the SlingHealthCheck annotation. */
+public class SlingHealthCheckProcessor implements AnnotationProcessor {
+
+    @Override
+    public void process(final ScannedClass scannedClass, final ClassDescription classDescription) throws SCRDescriptorException, SCRDescriptorFailureException {
+        final List<ClassAnnotation> servlets = scannedClass.getClassAnnotations(SlingHealthCheck.class.getName());
+        scannedClass.processed(servlets);
+
+        for (final ClassAnnotation cad : servlets) {
+            processHealthCheck(cad, classDescription);
+        }
+    }
+
+    /** Processes the given healthcheck annotation.
+     * 
+     * @param cad the annotation
+     * @param classDescription the class description */
+    private void processHealthCheck(final ClassAnnotation cad, final ClassDescription classDescription) {
+
+        final boolean generateComponent = cad.getBooleanValue("generateComponent", true);
+        final boolean metatype = cad.getBooleanValue("metatype", true);
+
+        // generate ComponentDescription if required
+        if (generateComponent) {
+            final ComponentDescription cd = new ComponentDescription(cad);
+            cd.setName(cad.getStringValue("componentName", classDescription.getDescribedClass().getName()));
+            cd.setConfigurationPolicy(ComponentConfigurationPolicy.valueOf(cad.getEnumValue("configurationPolicy",
+                    ComponentConfigurationPolicy.OPTIONAL.name())));
+            cd.setSetMetatypeFactoryPid(cad.getBooleanValue("configurationFactory", false));
+
+            cd.setLabel(cad.getStringValue("label", null));
+            cd.setDescription(cad.getStringValue("description", null));
+
+            cd.setCreateMetatype(metatype);
+
+            classDescription.add(cd);
+        }
+
+        // generate ServiceDescription if required
+        final boolean generateService = cad.getBooleanValue("generateService", true);
+        if (generateService) {
+            final ServiceDescription sd = new ServiceDescription(cad);
+            sd.addInterface(HealthCheck.class.getName());
+            classDescription.add(sd);
+        }
+
+        // generate PropertyDescriptions
+        generateStringArrPropertyDescriptor(cad, classDescription, metatype, "tags", HealthCheck.TAGS);
+        generateStringPropertyDescriptor(cad, classDescription, metatype, "name", HealthCheck.NAME);
+    }
+
+    /** Generates a property descriptor of type {@link PropertyType#String[]} */
+    private void generateStringArrPropertyDescriptor(final ClassAnnotation cad, final ClassDescription classDescription,
+            final boolean metatype, final String annotationName, final String propertyDescriptorName) {
+
+        final String[] values = (String[]) cad.getValue(annotationName);
+        if (values == null) {
+            return;
+        }
+
+        final PropertyDescription pd = new PropertyDescription(cad);
+        pd.setName(propertyDescriptorName);
+        pd.setMultiValue(values);
+        pd.setType(PropertyType.String);
+        pd.setUnbounded(PropertyUnbounded.ARRAY);
+        pd.setCardinality(Integer.MAX_VALUE);
+        if (metatype) {
+            pd.setPrivate(true);
+        }
+        classDescription.add(pd);
+    }
+
+    
+    /** Generates a property descriptor of type {@link PropertyType#String} */
+    private void generateStringPropertyDescriptor(final ClassAnnotation cad, final ClassDescription classDescription,
+            final boolean metatype, final String annotationName, final String propertyDescriptorName) {
+
+        final String hcName = (String) cad.getValue(annotationName);
+
+        final PropertyDescription pd = new PropertyDescription(cad);
+        pd.setName(propertyDescriptorName);
+        pd.setValue(hcName);
+        pd.setType(PropertyType.String);
+        if (metatype) {
+            pd.setPrivate(true);
+        }
+        classDescription.add(pd);
+    }
+    
+    @Override
+    public int getRanking() {
+        return 500;
+    }
+
+    @Override
+    public String getName() {
+        return SlingHealthCheck.class.getName() + " annotation processor.";
+    }
+}
diff --git a/src/main/resources/META-INF/services/org.apache.felix.scrplugin.annotations.AnnotationProcessor b/src/main/resources/META-INF/services/org.apache.felix.scrplugin.annotations.AnnotationProcessor
new file mode 100644
index 0000000..c463fbb
--- /dev/null
+++ b/src/main/resources/META-INF/services/org.apache.felix.scrplugin.annotations.AnnotationProcessor
@@ -0,0 +1 @@
+org.apache.sling.hc.annotations.SlingHealthCheckProcessor
\ No newline at end of file

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