Author: bdelacretaz
Date: Wed Jul 30 14:55:42 2014
New Revision: 1614676

URL: http://svn.apache.org/r1614676
Log:
SLING-3624 - SlingHealthCheck annotation, contributed by  Bjoern Weide, thanks!

Added:
    
sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/annotations/
    
sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/annotations/SlingHealthCheck.java
    
sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/annotations/SlingHealthCheckProcessor.java
    sling/trunk/bundles/extensions/healthcheck/core/src/main/resources/
    sling/trunk/bundles/extensions/healthcheck/core/src/main/resources/META-INF/
    
sling/trunk/bundles/extensions/healthcheck/core/src/main/resources/META-INF/services/
    
sling/trunk/bundles/extensions/healthcheck/core/src/main/resources/META-INF/services/org.apache.felix.scrplugin.annotations.AnnotationProcessor
    
sling/trunk/bundles/extensions/healthcheck/samples/src/main/java/org/apache/sling/hc/samples/impl/AnnotatedHealthCheckSample.java
Modified:
    sling/trunk/bundles/extensions/healthcheck/core/pom.xml

Modified: sling/trunk/bundles/extensions/healthcheck/core/pom.xml
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/core/pom.xml?rev=1614676&r1=1614675&r2=1614676&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/healthcheck/core/pom.xml (original)
+++ sling/trunk/bundles/extensions/healthcheck/core/pom.xml Wed Jul 30 14:55:42 
2014
@@ -42,6 +42,17 @@
                 <groupId>org.apache.felix</groupId>
                 <artifactId>maven-bundle-plugin</artifactId>
                 <extensions>true</extensions>
+                <configuration>
+                    <instructions>
+                        <Import-Package>
+                            !org.apache.felix.scr.annotations,
+                            !org.apache.felix.scrplugin.*,
+                            *
+                        </Import-Package>
+                        
<Sling-Initial-Content>SLING-CONTENT/apps/hc/demo;path:=/apps/hc/demo;overwrite:=true</Sling-Initial-Content>
+                        <Sling-Test-Regexp>.*Test</Sling-Test-Regexp>
+                    </instructions>
+                </configuration>
             </plugin>
         </plugins>
     </build>
@@ -61,6 +72,12 @@
             <scope>provided</scope>
         </dependency>
         <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>maven-scr-plugin</artifactId>
+            <version>1.16.0</version>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
             <version>1.6.2</version>

Added: 
sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/annotations/SlingHealthCheck.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/annotations/SlingHealthCheck.java?rev=1614676&view=auto
==============================================================================
--- 
sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/annotations/SlingHealthCheck.java
 (added)
+++ 
sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/annotations/SlingHealthCheck.java
 Wed Jul 30 14:55:42 2014
@@ -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 {};
+
+}

Added: 
sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/annotations/SlingHealthCheckProcessor.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/annotations/SlingHealthCheckProcessor.java?rev=1614676&view=auto
==============================================================================
--- 
sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/annotations/SlingHealthCheckProcessor.java
 (added)
+++ 
sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/annotations/SlingHealthCheckProcessor.java
 Wed Jul 30 14:55:42 2014
@@ -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.";
+    }
+}

Added: 
sling/trunk/bundles/extensions/healthcheck/core/src/main/resources/META-INF/services/org.apache.felix.scrplugin.annotations.AnnotationProcessor
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/core/src/main/resources/META-INF/services/org.apache.felix.scrplugin.annotations.AnnotationProcessor?rev=1614676&view=auto
==============================================================================
--- 
sling/trunk/bundles/extensions/healthcheck/core/src/main/resources/META-INF/services/org.apache.felix.scrplugin.annotations.AnnotationProcessor
 (added)
+++ 
sling/trunk/bundles/extensions/healthcheck/core/src/main/resources/META-INF/services/org.apache.felix.scrplugin.annotations.AnnotationProcessor
 Wed Jul 30 14:55:42 2014
@@ -0,0 +1 @@
+org.apache.sling.hc.annotations.SlingHealthCheckProcessor
\ No newline at end of file

Added: 
sling/trunk/bundles/extensions/healthcheck/samples/src/main/java/org/apache/sling/hc/samples/impl/AnnotatedHealthCheckSample.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/samples/src/main/java/org/apache/sling/hc/samples/impl/AnnotatedHealthCheckSample.java?rev=1614676&view=auto
==============================================================================
--- 
sling/trunk/bundles/extensions/healthcheck/samples/src/main/java/org/apache/sling/hc/samples/impl/AnnotatedHealthCheckSample.java
 (added)
+++ 
sling/trunk/bundles/extensions/healthcheck/samples/src/main/java/org/apache/sling/hc/samples/impl/AnnotatedHealthCheckSample.java
 Wed Jul 30 14:55:42 2014
@@ -0,0 +1,43 @@
+/*
+ * 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.samples.impl;
+
+import java.util.Date;
+
+import org.apache.sling.hc.annotations.SlingHealthCheck;
+import org.apache.sling.hc.api.HealthCheck;
+import org.apache.sling.hc.api.Result;
+import org.apache.sling.hc.util.FormattingResultLog;
+
+/** Test creating a Health Check using the SlingHealthCheck
+ *  annotation. */
+
+@SlingHealthCheck(
+        name="annotatedHC", 
+        description="Sample Health Check defined by a java annotation",
+        tags={"sample","annotation"})
+
+public class AnnotatedHealthCheckSample implements HealthCheck{
+
+    @Override
+    public Result execute() {
+        final FormattingResultLog resultLog = new FormattingResultLog();
+        resultLog.info("All good at {}", new Date());
+        return new Result(resultLog);
+    }
+}
\ No newline at end of file


Reply via email to