You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ju...@apache.org on 2013/03/07 15:34:53 UTC

svn commit: r1453896 - in /sling/trunk/contrib/extensions/jolokia: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/sling/ src/main/java/org/apache/sling/jmx/ src/main/java/org/apache/sling/jmx/jolo...

Author: justin
Date: Thu Mar  7 14:34:52 2013
New Revision: 1453896

URL: http://svn.apache.org/r1453896
Log:
SLING-2778 - initial version of Jolokia integration bundle

Added:
    sling/trunk/contrib/extensions/jolokia/
    sling/trunk/contrib/extensions/jolokia/pom.xml
    sling/trunk/contrib/extensions/jolokia/src/
    sling/trunk/contrib/extensions/jolokia/src/main/
    sling/trunk/contrib/extensions/jolokia/src/main/java/
    sling/trunk/contrib/extensions/jolokia/src/main/java/org/
    sling/trunk/contrib/extensions/jolokia/src/main/java/org/apache/
    sling/trunk/contrib/extensions/jolokia/src/main/java/org/apache/sling/
    sling/trunk/contrib/extensions/jolokia/src/main/java/org/apache/sling/jmx/
    sling/trunk/contrib/extensions/jolokia/src/main/java/org/apache/sling/jmx/jolokia/
    sling/trunk/contrib/extensions/jolokia/src/main/java/org/apache/sling/jmx/jolokia/impl/
    sling/trunk/contrib/extensions/jolokia/src/main/java/org/apache/sling/jmx/jolokia/impl/AuthHttpContext.java
    sling/trunk/contrib/extensions/jolokia/src/main/java/org/apache/sling/jmx/jolokia/impl/SlingJolokiaServlet.java

Added: sling/trunk/contrib/extensions/jolokia/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/jolokia/pom.xml?rev=1453896&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/jolokia/pom.xml (added)
+++ sling/trunk/contrib/extensions/jolokia/pom.xml Thu Mar  7 14:34:52 2013
@@ -0,0 +1,111 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+    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 ASF 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.
+-->
+<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>11</version>
+    </parent>
+    
+    <artifactId>org.apache.sling.jmx.jolokia</artifactId>
+    <packaging>bundle</packaging>
+    <version>0.0.1-SNAPSHOT</version>
+    
+    <name>Jolokia Sling Integration</name>
+    <description>Integration with the Jolokia project to expose JMX mbeans via a JSON interface.</description>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-scr-plugin</artifactId>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <extensions>true</extensions>
+                <configuration>
+                    <instructions>
+                        <Embed-Dependency>
+                            jolokia-osgi,json-simple
+                        </Embed-Dependency>
+                    </instructions>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+    <dependencies>
+        <dependency>
+            <groupId>org.jolokia</groupId>
+            <artifactId>jolokia-osgi</artifactId>
+            <version>1.1.0</version>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
+          <groupId>com.googlecode.json-simple</groupId>
+          <artifactId>json-simple</artifactId>
+          <version>1.1</version>
+          <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.api</artifactId>
+            <version>2.2.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.auth.core</artifactId>
+            <version>1.0.6</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr.annotations</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+</project>

Added: sling/trunk/contrib/extensions/jolokia/src/main/java/org/apache/sling/jmx/jolokia/impl/AuthHttpContext.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/jolokia/src/main/java/org/apache/sling/jmx/jolokia/impl/AuthHttpContext.java?rev=1453896&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/jolokia/src/main/java/org/apache/sling/jmx/jolokia/impl/AuthHttpContext.java (added)
+++ sling/trunk/contrib/extensions/jolokia/src/main/java/org/apache/sling/jmx/jolokia/impl/AuthHttpContext.java Thu Mar  7 14:34:52 2013
@@ -0,0 +1,63 @@
+package org.apache.sling.jmx.jolokia.impl;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Collection;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.sling.auth.core.AuthenticationSupport;
+import org.osgi.service.http.HttpContext;
+
+public class AuthHttpContext implements HttpContext {
+
+    /**
+     * Handles security
+     * 
+     * @see #handleSecurity(HttpServletRequest, HttpServletResponse)
+     */
+    private final AuthenticationSupport authenticator;
+
+    /**
+     * The set of userIds which will be allowed access to Jolokia.
+     */
+    private Collection<String> allowedUserIds;
+
+    public AuthHttpContext(final AuthenticationSupport authSupport, final Collection<String> allowedUserIds) {
+        this.authenticator = authSupport;
+        this.allowedUserIds = allowedUserIds;
+    }
+
+    public boolean handleSecurity(final HttpServletRequest request, final HttpServletResponse response)
+            throws IOException {
+        final AuthenticationSupport localAuthenticator = this.authenticator;
+        if (localAuthenticator != null) {
+            final boolean authResult = localAuthenticator.handleSecurity(request, response);
+            if (authResult) {
+                if (this.allowedUserIds.size() == 0) {
+                    return true;
+                } else {
+                    final String userId = (String) request.getAttribute(HttpContext.REMOTE_USER);
+                    return this.allowedUserIds.contains(userId);
+                }
+            }
+            return false;
+        }
+        // send 503/SERVICE UNAVAILABLE, flush to ensure delivery
+        response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
+        response.flushBuffer();
+
+        // terminate this request now
+        return false;
+    }
+
+    public URL getResource(String name) {
+        return null;
+    }
+
+    public String getMimeType(String name) {
+        return null;
+    }
+
+}

Added: sling/trunk/contrib/extensions/jolokia/src/main/java/org/apache/sling/jmx/jolokia/impl/SlingJolokiaServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/jolokia/src/main/java/org/apache/sling/jmx/jolokia/impl/SlingJolokiaServlet.java?rev=1453896&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/jolokia/src/main/java/org/apache/sling/jmx/jolokia/impl/SlingJolokiaServlet.java (added)
+++ sling/trunk/contrib/extensions/jolokia/src/main/java/org/apache/sling/jmx/jolokia/impl/SlingJolokiaServlet.java Thu Mar  7 14:34:52 2013
@@ -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 ASF
+ * 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.jmx.jolokia.impl;
+
+import java.util.Arrays;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Map;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.PropertyUnbounded;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.sling.auth.core.AuthenticationSupport;
+import org.apache.sling.commons.osgi.OsgiUtil;
+import org.jolokia.osgi.servlet.JolokiaServlet;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.http.HttpService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component(metatype = true, label = "%jolokia.servlet.name", description = "%jolokia.servlet.description")
+@Properties({ @Property(name = Constants.SERVICE_DESCRIPTION, value = "Sling Jolokia Servlet"),
+        @Property(name = Constants.SERVICE_VENDOR, value = "The Apache Software Foundation") })
+public class SlingJolokiaServlet {
+    
+    private final Logger logger = LoggerFactory.getLogger(getClass());
+
+    /**
+     * Default value for the Jolokia servlet registration.
+     */
+    private static final String DEFAULT_JOLOKIA_ROOT = "/system/jolokia";
+
+    /**
+     * Name of the property to configure the location for the Jolokia servlet
+     * registration. Default for the property is {@link #DEFAULT_JOLOKIA_ROOT}.
+     */
+    @Property(value = DEFAULT_JOLOKIA_ROOT)
+    private static final String PROP_JOLOKIA_ROOT = "alias";
+
+    private static final String[] DEFAULT_ALLOWED_USER_IDS = { "admin" };
+
+    @Property(value = { "admin" }, unbounded=PropertyUnbounded.ARRAY)
+    private static final String PROP_ALLOWED_USER_IDS = "allowed.userids";
+    
+    /**
+     * The name of the service property of the registered dummy service to cause
+     * the path to the DavEx servlet to not be subject to forced authentication.
+     */
+    private static final String PAR_AUTH_REQ = "sling.auth.requirements";
+
+    @Reference
+    private HttpService httpService;
+
+    @Reference
+    private AuthenticationSupport authSupport;
+
+    /**
+     * The path at which the Jolokia servlet has successfully been registered in
+     * the {@link #activate(Map)} method. If this is <code>null</code> the
+     * Jolokia servlet is not registered with the Http Service.
+     */
+    private String servletAlias;
+
+    /**
+     * The dummy service registration to convey to the Sling Authenticator that
+     * everything under the alias must not be forcibly authenticated. This will
+     * be <code>null</code> if the Jolokia servlet registration fails.
+     */
+    private ServiceRegistration dummyService;
+
+    @Activate
+    protected void activate(final BundleContext bundleContext, final Map<String, ?> config) {
+        final String jolokiaRoot = OsgiUtil.toString(config.get(PROP_JOLOKIA_ROOT), DEFAULT_JOLOKIA_ROOT);
+        final String[] allowedUserIds = OsgiUtil.toStringArray(config.get(PROP_ALLOWED_USER_IDS), DEFAULT_ALLOWED_USER_IDS);
+        
+        if (allowedUserIds.length == 0) {
+            logger.warn("allowedUserId list is empty; all users will be allowed access to JMX data.");
+        }
+        
+        final AuthHttpContext context = new AuthHttpContext(authSupport, Arrays.asList(allowedUserIds));
+
+        // prepare servlet config
+        final Dictionary<String, String> initProps = new Hashtable<String, String>();
+
+        // register and handle registration failure
+        try {
+            this.httpService.registerServlet(jolokiaRoot, new JolokiaServlet(bundleContext), initProps, context);
+            this.servletAlias = jolokiaRoot;
+
+            java.util.Properties dummyServiceProperties = new java.util.Properties();
+            dummyServiceProperties.put(Constants.SERVICE_VENDOR, config.get(Constants.SERVICE_VENDOR));
+            dummyServiceProperties.put(Constants.SERVICE_DESCRIPTION,
+                    "Helper for " + config.get(Constants.SERVICE_DESCRIPTION));
+            dummyServiceProperties.put(PAR_AUTH_REQ, "-" + jolokiaRoot);
+            this.dummyService = bundleContext.registerService("java.lang.Object", new Object(), dummyServiceProperties);
+        } catch (Exception e) {
+            
+            logger.error("activate: Failed registering Jolokia Servlet at " + jolokiaRoot, e);
+        }
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        if (this.dummyService != null) {
+            this.dummyService.unregister();
+            this.dummyService = null;
+        }
+
+        if (this.servletAlias != null) {
+            this.httpService.unregister(servletAlias);
+            this.servletAlias = null;
+        }
+    }
+}