incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1508846 - in /sling/branches/SLING-2987-healthcheck-redesign/hc-core: ./ src/main/java/org/apache/sling/hc/impl/ src/test/java/org/apache/sling/hc/impl/
Date Wed, 31 Jul 2013 13:20:39 GMT
Author: bdelacretaz
Date: Wed Jul 31 13:20:39 2013
New Revision: 1508846

URL: http://svn.apache.org/r1508846
Log:
SLING-2987 - ScriptableHealthCheck added

Added:
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/JmxScriptBinding.java
  (with props)
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/ScriptableHealthCheck.java
  (with props)
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/ScriptableHealthCheckTest.java
  (with props)
Modified:
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/pom.xml
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/JmxAttributeHealthCheckTest.java

Modified: sling/branches/SLING-2987-healthcheck-redesign/hc-core/pom.xml
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/pom.xml?rev=1508846&r1=1508845&r2=1508846&view=diff
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/pom.xml (original)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/pom.xml Wed Jul 31 13:20:39 2013
@@ -100,5 +100,11 @@
             <version>1.9.5</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.scripting.javascript</artifactId>
+            <version>2.0.12</version>
+            <scope>test</scope>
+        </dependency>
      </dependencies>
 </project>

Added: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/JmxScriptBinding.java
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/JmxScriptBinding.java?rev=1508846&view=auto
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/JmxScriptBinding.java
(added)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/JmxScriptBinding.java
Wed Jul 31 13:20:39 2013
@@ -0,0 +1,57 @@
+/*
+ * 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.impl;
+
+import java.lang.management.ManagementFactory;
+
+import javax.management.AttributeNotFoundException;
+import javax.management.InstanceNotFoundException;
+import javax.management.MBeanException;
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+import javax.management.ReflectionException;
+
+import org.slf4j.Logger;
+
+/** The JmxBinding is meant to be bound as "jmx" global variables
+ *  in scripted rules, to allow for writing scripted expressions
+ *  like jmx.attribute("java.lang:type=ClassLoading", "LoadedClassCount") > 100
+ */
+public class JmxScriptBinding {
+    private MBeanServer jmxServer = ManagementFactory.getPlatformMBeanServer();
+    private final Logger logger;
+    
+    JmxScriptBinding(Logger logger) {
+        this.logger = logger;
+    }
+    
+    public Object attribute(String objectNameString, String attributeName) 
+            throws MalformedObjectNameException, AttributeNotFoundException, InstanceNotFoundException,
MBeanException, ReflectionException {
+        final ObjectName name = new ObjectName(objectNameString);
+        if(jmxServer.queryNames(name, null).size() == 0) {
+            final String msg = "JMX object name not found: [" + objectNameString + "]";
+            logger.warn(msg);
+            throw new IllegalStateException(msg);
+        }
+        logger.debug("Got JMX Object [{}]", name);
+        final Object value = jmxServer.getAttribute(name, attributeName);
+        logger.debug("JMX Object [{}] Attribute [{}] = [{}]", new Object[] { name, attributeName,
value });
+        return value;
+    }
+}
\ No newline at end of file

Propchange: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/JmxScriptBinding.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/JmxScriptBinding.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/ScriptableHealthCheck.java
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/ScriptableHealthCheck.java?rev=1508846&view=auto
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/ScriptableHealthCheck.java
(added)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/ScriptableHealthCheck.java
Wed Jul 31 13:20:39 2013
@@ -0,0 +1,101 @@
+/*
+ * 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.impl;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.script.Bindings;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.ConfigurationPolicy;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.commons.osgi.PropertiesUtil;
+import org.apache.sling.hc.api.Constants;
+import org.apache.sling.hc.api.HealthCheck;
+import org.apache.sling.hc.api.Result;
+import org.apache.sling.hc.api.ResultLog;
+import org.osgi.service.component.ComponentContext;
+
+/** {@link HealthCheck} that checks a scriptable expression */
+@Component(configurationFactory=true, policy=ConfigurationPolicy.REQUIRE, metatype=true)
+@Service
+public class ScriptableHealthCheck implements HealthCheck {
+
+    private final Map<String, String> info = new HashMap<String, String>();
+    private String expression;
+    private String languageExtension;
+    
+    public static final String DEFAULT_LANGUAGE_EXTENSION = "ecma";
+
+    @Property
+    public static final String PROP_EXPRESSION = "expression";
+    
+    @Property
+    public static final String PROP_LANGUAGE_EXTENSION = "language.extension";
+    
+    @Property(cardinality=50)
+    public static final String PROP_TAGS = Constants.HC_TAGS;
+    
+    @Reference
+    private ScriptEngineManager scriptEngineManager;
+    
+    @Activate
+    public void activate(ComponentContext ctx) {
+        expression = PropertiesUtil.toString(ctx.getProperties().get(PROP_EXPRESSION), "");
+        languageExtension = PropertiesUtil.toString(ctx.getProperties().get(PROP_LANGUAGE_EXTENSION),
DEFAULT_LANGUAGE_EXTENSION);
+        
+        info.put(PROP_EXPRESSION, expression);
+        info.put(PROP_LANGUAGE_EXTENSION, languageExtension);
+    }
+    
+    @Override
+    public Result execute(ResultLog log) {
+        final Result result = new Result(this, log);
+        log.debug("Checking expression [{}], language extension=[{}]", expression, languageExtension);
+        try {
+            final ScriptEngine engine = scriptEngineManager.getEngineByExtension(languageExtension);
+            if(engine == null) {
+                log.warn("No ScriptEngine available for extension {}", languageExtension);
+            } else {
+                // TODO pluggable Bindings? Reuse the Sling bindings providers?
+                final Bindings b = engine.createBindings();
+                b.put("jmx", new JmxScriptBinding(log));
+                final Object value = engine.eval(expression, b);
+                if(value!=null && "true".equals(value.toString())) {
+                    log.debug("Expression [{}] evaluates to true as expected", expression);
+                } else {
+                    log.warn("Expression [{}] does not evaluate to true, value={}", expression,
value);
+                }
+            }
+        } catch(Exception e) {
+            log.warn(e.toString(), e);
+        }
+        return result;
+    }
+
+    @Override
+    public Map<String, String> getInfo() {
+        return info;
+    }
+}
\ No newline at end of file

Propchange: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/ScriptableHealthCheck.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/ScriptableHealthCheck.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/JmxAttributeHealthCheckTest.java
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/JmxAttributeHealthCheckTest.java?rev=1508846&r1=1508845&r2=1508846&view=diff
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/JmxAttributeHealthCheckTest.java
(original)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/JmxAttributeHealthCheckTest.java
Wed Jul 31 13:20:39 2013
@@ -40,8 +40,7 @@ public class JmxAttributeHealthCheckTest
 
     @Before
     public void setup() {
-        final JmxAttributeHealthCheck c = new JmxAttributeHealthCheck();
-        hc = c;
+        hc = new JmxAttributeHealthCheck();
         log = new ResultLog(LoggerFactory.getLogger(getClass()));
         
         ctx = Mockito.mock(ComponentContext.class);

Added: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/ScriptableHealthCheckTest.java
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/ScriptableHealthCheckTest.java?rev=1508846&view=auto
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/ScriptableHealthCheckTest.java
(added)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/ScriptableHealthCheckTest.java
Wed Jul 31 13:20:39 2013
@@ -0,0 +1,99 @@
+/*
+ * 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.impl;
+
+import static org.junit.Assert.fail;
+
+import java.lang.reflect.Field;
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+
+import org.apache.sling.hc.api.Result;
+import org.apache.sling.hc.api.ResultLog;
+import org.junit.Test;
+import org.mockito.Matchers;
+import org.mockito.Mockito;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.LoggerFactory;
+
+import com.sun.script.javascript.RhinoScriptEngine;
+
+public class ScriptableHealthCheckTest {
+    
+    private ScriptableHealthCheck hc;
+    private ResultLog log;
+    private Dictionary<String, String> props;
+    private ComponentContext ctx;
+
+    private void assertExpression(String expression, String languageExtension, boolean expected)
throws Exception {
+        hc = new ScriptableHealthCheck();
+        log = new ResultLog(LoggerFactory.getLogger(getClass()));
+        ctx = Mockito.mock(ComponentContext.class);
+        props = new Hashtable<String, String>();
+        
+        final ScriptEngine engine = new RhinoScriptEngine();
+        final ScriptEngineManager manager = Mockito.mock(ScriptEngineManager.class);
+        Mockito.when(manager.getEngineByExtension(Matchers.same("ecma"))).thenReturn(engine);
+        final Field f = hc.getClass().getDeclaredField("scriptEngineManager");
+        f.setAccessible(true);
+        f.set(hc, manager);
+        
+        props.put(ScriptableHealthCheck.PROP_EXPRESSION, expression);
+        if(languageExtension != null) {
+            props.put(ScriptableHealthCheck.PROP_LANGUAGE_EXTENSION, languageExtension);
+        }
+        Mockito.when(ctx.getProperties()).thenReturn(props);
+        hc.activate(ctx);
+        final Result r = hc.execute(log);
+        if(r.isOk() != expected) {
+            fail("HealthCheck result is " + r.isOk() + ", log=" + r.getLogEntries());
+        }
+    }
+    
+    @Test
+    public void testSimpleExpression() throws Exception {
+        assertExpression("2 + 3 == 5", null, true);
+    }
+    
+    @Test
+    public void testJmxExpression() throws Exception {
+        assertExpression(
+                "jmx.attribute('java.lang:type=ClassLoading', 'LoadedClassCount') > 10"
+                + " && jmx.attribute('java.lang:type=Runtime', 'ManagementSpecVersion')
> 1", 
+                "ecma", true);
+    }
+    
+    @Test
+    public void testFalseExpression() throws Exception {
+        assertExpression("2 + 3 == 15", null, false);
+    }
+    
+    @Test
+    public void testSyntaxError() throws Exception {
+        assertExpression("{not [valid ok?", null, false);
+    }
+    
+    @Test
+    public void testNoEngine() throws Exception {
+        assertExpression("2 + 3 == 5", null, true);
+        assertExpression("2 + 3 == 5", "groovy", false);
+    }
+}

Propchange: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/ScriptableHealthCheckTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/ScriptableHealthCheckTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL



Mime
View raw message