incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1508081 - in /sling/trunk/contrib/extensions/healthcheck/hc-rules/src: main/java/org/apache/sling/hc/rules/scriptable/ test/java/org/apache/sling/hc/rules/impl/
Date Mon, 29 Jul 2013 14:32:26 GMT
Author: bdelacretaz
Date: Mon Jul 29 14:32:26 2013
New Revision: 1508081

URL: http://svn.apache.org/r1508081
Log:
SLING-2983 - jmx binding for easy access to jmx attributes in scripted rules

Added:
    sling/trunk/contrib/extensions/healthcheck/hc-rules/src/main/java/org/apache/sling/hc/rules/scriptable/JmxBinding.java
Modified:
    sling/trunk/contrib/extensions/healthcheck/hc-rules/src/main/java/org/apache/sling/hc/rules/scriptable/ScriptableRuleBuilder.java
    sling/trunk/contrib/extensions/healthcheck/hc-rules/src/test/java/org/apache/sling/hc/rules/impl/ScriptableRuleBuilderTest.java

Added: sling/trunk/contrib/extensions/healthcheck/hc-rules/src/main/java/org/apache/sling/hc/rules/scriptable/JmxBinding.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-rules/src/main/java/org/apache/sling/hc/rules/scriptable/JmxBinding.java?rev=1508081&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/hc-rules/src/main/java/org/apache/sling/hc/rules/scriptable/JmxBinding.java
(added)
+++ sling/trunk/contrib/extensions/healthcheck/hc-rules/src/main/java/org/apache/sling/hc/rules/scriptable/JmxBinding.java
Mon Jul 29 14:32:26 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.rules.scriptable;
+
+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 JmxBinding {
+    private MBeanServer jmxServer = ManagementFactory.getPlatformMBeanServer();
+    private final Logger logger;
+    
+    JmxBinding(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

Modified: sling/trunk/contrib/extensions/healthcheck/hc-rules/src/main/java/org/apache/sling/hc/rules/scriptable/ScriptableRuleBuilder.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-rules/src/main/java/org/apache/sling/hc/rules/scriptable/ScriptableRuleBuilder.java?rev=1508081&r1=1508080&r2=1508081&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/hc-rules/src/main/java/org/apache/sling/hc/rules/scriptable/ScriptableRuleBuilder.java
(original)
+++ sling/trunk/contrib/extensions/healthcheck/hc-rules/src/main/java/org/apache/sling/hc/rules/scriptable/ScriptableRuleBuilder.java
Mon Jul 29 14:32:26 2013
@@ -17,6 +17,7 @@
  */
 package org.apache.sling.hc.rules.scriptable;
 
+import javax.script.Bindings;
 import javax.script.ScriptEngine;
 import javax.script.ScriptEngineManager;
 import javax.script.ScriptException;
@@ -83,9 +84,11 @@ public class ScriptableRuleBuilder imple
                 logger.error("Cannot evaluate: {}", scriptEngineMsg);
             } else {
                 try {
-                    return scriptEngine.eval(expression);
+                    final Bindings b = scriptEngine.createBindings();
+                    b.put("jmx", new JmxBinding(logger));
+                    return scriptEngine.eval(expression, b);
                 } catch(ScriptException e) {
-                    logger.error("Script evaluation error (" + expression + ")", e);
+                    logger.error("Script evaluation error [" + expression + "]: " + e, e);
                 }
             }
             return null;

Modified: sling/trunk/contrib/extensions/healthcheck/hc-rules/src/test/java/org/apache/sling/hc/rules/impl/ScriptableRuleBuilderTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-rules/src/test/java/org/apache/sling/hc/rules/impl/ScriptableRuleBuilderTest.java?rev=1508081&r1=1508080&r2=1508081&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/hc-rules/src/test/java/org/apache/sling/hc/rules/impl/ScriptableRuleBuilderTest.java
(original)
+++ sling/trunk/contrib/extensions/healthcheck/hc-rules/src/test/java/org/apache/sling/hc/rules/impl/ScriptableRuleBuilderTest.java
Mon Jul 29 14:32:26 2013
@@ -17,9 +17,10 @@
  */
 package org.apache.sling.hc.rules.impl;
 
-import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 
+import javax.script.Bindings;
 import javax.script.ScriptEngine;
 import javax.script.ScriptEngineManager;
 import javax.script.ScriptException;
@@ -61,7 +62,8 @@ public class ScriptableRuleBuilderTest {
             serviceReference = Mockito.mock(ServiceReference.class);
             
             scriptEngine = Mockito.mock(ScriptEngine.class);
-            Mockito.when(scriptEngine.eval(Matchers.same(CODE))).thenReturn("true");
+            Mockito.when(scriptEngine.eval(Matchers.same(CODE), Matchers.isA(Bindings.class))).thenReturn("true");
+            Mockito.when(scriptEngine.createBindings()).thenReturn(Mockito.mock(Bindings.class));
             
             scriptEngineManager = Mockito.mock(ScriptEngineManager.class);
             Mockito.when(



Mime
View raw message