You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2013/07/23 16:44:15 UTC
svn commit: r1506073 - in /sling/trunk/contrib/extensions/healthcheck:
hc-core/src/main/java/org/apache/sling/hc/util/RuleDynamicMBean.java
hc-sling/pom.xml
hc-sling/src/main/java/org/apache/sling/hc/sling/impl/RulesMBeans.java
Author: bdelacretaz
Date: Tue Jul 23 14:44:14 2013
New Revision: 1506073
URL: http://svn.apache.org/r1506073
Log:
SLING-2977 - basic JMX support for health check rules
Added:
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/util/RuleDynamicMBean.java
sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/RulesMBeans.java
Modified:
sling/trunk/contrib/extensions/healthcheck/hc-sling/pom.xml
Added: sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/util/RuleDynamicMBean.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/util/RuleDynamicMBean.java?rev=1506073&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/util/RuleDynamicMBean.java (added)
+++ sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/util/RuleDynamicMBean.java Tue Jul 23 14:44:14 2013
@@ -0,0 +1,108 @@
+/*
+ * 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.util;
+
+import java.io.Serializable;
+
+import javax.management.Attribute;
+import javax.management.AttributeList;
+import javax.management.AttributeNotFoundException;
+import javax.management.DynamicMBean;
+import javax.management.InvalidAttributeValueException;
+import javax.management.MBeanAttributeInfo;
+import javax.management.MBeanException;
+import javax.management.MBeanInfo;
+import javax.management.ReflectionException;
+
+import org.apache.sling.hc.api.Rule;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** A {@link DynamicMBean} that gives access to a {@link Rule}'s data */
+public class RuleDynamicMBean implements DynamicMBean, Serializable {
+
+ private static final long serialVersionUID = -90745301105975287L;
+ private static final Logger logger = LoggerFactory.getLogger(RuleDynamicMBean.class);
+ private final String beanName;
+ private final Rule rule;
+
+ public static final String RULE_OK_ATTRIBUTE_NAME = "ok";
+ public static final String LOG_ATTRIBUTE_NAME = "log";
+
+ public RuleDynamicMBean(Rule r) {
+ beanName = r.toString();
+ rule = r;
+ }
+
+ @Override
+ public Object getAttribute(String attribute)
+ throws AttributeNotFoundException, MBeanException, ReflectionException {
+ if(RULE_OK_ATTRIBUTE_NAME.equals(attribute)) {
+ return !rule.evaluate().anythingToReport();
+ } else {
+ final Object o = rule.getInfo().get(attribute);
+ if(o == null) {
+ throw new AttributeNotFoundException(attribute);
+ }
+ return o;
+ }
+ }
+
+ @Override
+ public AttributeList getAttributes(String[] attributes) {
+ final AttributeList result = new AttributeList();
+ for(String key : attributes) {
+ try {
+ result.add(new Attribute(key, getAttribute(key)));
+ } catch(Exception e) {
+ logger.error("Exception getting Attribute " + key, e);
+ }
+ }
+ return result;
+ }
+
+ @Override
+ public MBeanInfo getMBeanInfo() {
+ final MBeanAttributeInfo[] attrs = new MBeanAttributeInfo[rule.getInfo().size() + 1];
+ int i=0;
+ attrs[i++] = new MBeanAttributeInfo(RULE_OK_ATTRIBUTE_NAME, Boolean.class.getName(), "The rule value", true, false, false);
+ for(String key : rule.getInfo().keySet()) {
+ attrs[i++] = new MBeanAttributeInfo(key, String.class.getName(), "Description of " + key, true, false, false);
+ }
+
+ return new MBeanInfo(this.getClass().getName(), beanName, attrs, null, null, null);
+ }
+
+ @Override
+ public Object invoke(String actionName, Object[] params, String[] signature)
+ throws MBeanException, ReflectionException {
+ throw new UnsupportedOperationException(getClass().getSimpleName() + " does not support operations on Rules");
+ }
+
+ @Override
+ public void setAttribute(Attribute attribute)
+ throws AttributeNotFoundException, InvalidAttributeValueException,
+ MBeanException, ReflectionException {
+ throw new UnsupportedOperationException(getClass().getSimpleName() + " does not support setting Rules attributes");
+ }
+
+ @Override
+ public AttributeList setAttributes(AttributeList attributes) {
+ throw new UnsupportedOperationException(getClass().getSimpleName() + " does not support setting Rules attributes");
+ }
+}
\ No newline at end of file
Modified: sling/trunk/contrib/extensions/healthcheck/hc-sling/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-sling/pom.xml?rev=1506073&r1=1506072&r2=1506073&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/hc-sling/pom.xml (original)
+++ sling/trunk/contrib/extensions/healthcheck/hc-sling/pom.xml Tue Jul 23 14:44:14 2013
@@ -134,8 +134,8 @@
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
- <artifactId>org.apache.sling.commons.json</artifactId>
- <version>2.0.6</version>
+ <artifactId>org.apache.sling.commons.osgi</artifactId>
+ <version>2.2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
Added: sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/RulesMBeans.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/RulesMBeans.java?rev=1506073&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/RulesMBeans.java (added)
+++ sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/RulesMBeans.java Tue Jul 23 14:44:14 2013
@@ -0,0 +1,118 @@
+/*
+ * 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.sling.impl;
+
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+
+import javax.management.DynamicMBean;
+
+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.Deactivate;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.commons.osgi.PropertiesUtil;
+import org.apache.sling.hc.api.HealthCheckFacade;
+import org.apache.sling.hc.api.Rule;
+import org.apache.sling.hc.api.RulesEngine;
+import org.apache.sling.hc.sling.api.RulesResourceParser;
+import org.apache.sling.hc.util.RuleDynamicMBean;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Registers MBeans for our health check rules */
+@Component(configurationFactory=true, policy=ConfigurationPolicy.REQUIRE,metatype=true)
+public class RulesMBeans {
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ @Property
+ public static final String RULES_PATHS_PROP = "rules.path";
+
+ @Reference
+ private ResourceResolverFactory resourceResolverFactory;
+
+ @Reference
+ private HealthCheckFacade healthcheck;
+
+ @Reference
+ private RulesResourceParser parser;
+
+ private String rulesPath;
+ private RulesEngine engine;
+ private List<ServiceRegistration> mBeansRegistrations;
+
+ @Activate
+ public void activate(ComponentContext ctx) throws Exception {
+ rulesPath = PropertiesUtil.toString(ctx.getProperties().get(RULES_PATHS_PROP), null);
+ if(rulesPath == null) {
+ throw new IllegalStateException("rulesPath is null, cannot activate");
+ }
+
+ final ResourceResolver resolver = resourceResolverFactory.getAdministrativeResourceResolver(null);
+ try {
+ final Resource rulesRoot = resolver.getResource(rulesPath);
+ if(rulesRoot == null) {
+ throw new IllegalStateException("Resource not found, cannot parse Rules: " + rulesPath);
+ }
+
+ // Parse Rules found under our configured root
+ engine = healthcheck.getNewRulesEngine();
+ final List<Rule> rules = parser.parseResource(rulesRoot);
+ engine.addRules(rules);
+
+ // And register MBeans for those Rules
+ mBeansRegistrations = new ArrayList<ServiceRegistration>();
+ final String RESOURCE_PATH_PROP = "sling.resource.path";
+ for(Rule r : rules) {
+ final Object rulePath = r.getInfo().get(RESOURCE_PATH_PROP);
+ if(rulePath == null) {
+ // TODO this happens with scripted rules
+ log.warn("Rule {} does not have a {} property, ignored", r, RESOURCE_PATH_PROP);
+ continue;
+ }
+ final Dictionary<String, String> mbeanProps = new Hashtable<String, String>();
+ mbeanProps.put("jmx.objectname", "org.apache.sling.healthcheck:type=rules,service=" + rulePath);
+ final RuleDynamicMBean mbean = new RuleDynamicMBean(r);
+ mBeansRegistrations.add(ctx.getBundleContext().registerService(DynamicMBean.class.getName(), mbean, mbeanProps));
+ log.debug("Registered {} with properties {}", mbean, mbeanProps);
+ }
+ log.info("Registered {} Rule MBeans", mBeansRegistrations.size());
+
+ } finally {
+ resolver.close();
+ }
+ }
+
+ @Deactivate
+ public void deactivate(ComponentContext ctx) {
+ for(ServiceRegistration r : mBeansRegistrations) {
+ r.unregister();
+ }
+ log.info("Unregistered {} Rule MBeans", mBeansRegistrations.size());
+ mBeansRegistrations = null;
+ }
+}