incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1138496 - in /sling/whiteboard/bdelacretaz/experimental: ./ 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/bdelacretaz/ src/main/java/org/apache/sli...
Date Wed, 22 Jun 2011 14:58:59 GMT
Author: bdelacretaz
Date: Wed Jun 22 14:58:59 2011
New Revision: 1138496

URL: http://svn.apache.org/viewvc?rev=1138496&view=rev
Log:
Experimenting with HTTP access to JMX attributes - might be useful for integration testing

Added:
    sling/whiteboard/bdelacretaz/experimental/   (with props)
    sling/whiteboard/bdelacretaz/experimental/pom.xml   (with props)
    sling/whiteboard/bdelacretaz/experimental/src/
    sling/whiteboard/bdelacretaz/experimental/src/main/
    sling/whiteboard/bdelacretaz/experimental/src/main/java/
    sling/whiteboard/bdelacretaz/experimental/src/main/java/org/
    sling/whiteboard/bdelacretaz/experimental/src/main/java/org/apache/
    sling/whiteboard/bdelacretaz/experimental/src/main/java/org/apache/sling/
    sling/whiteboard/bdelacretaz/experimental/src/main/java/org/apache/sling/bdelacretaz/
    sling/whiteboard/bdelacretaz/experimental/src/main/java/org/apache/sling/bdelacretaz/experimental/
    sling/whiteboard/bdelacretaz/experimental/src/main/java/org/apache/sling/bdelacretaz/experimental/impl/
    sling/whiteboard/bdelacretaz/experimental/src/main/java/org/apache/sling/bdelacretaz/experimental/impl/jmxservlet/
    sling/whiteboard/bdelacretaz/experimental/src/main/java/org/apache/sling/bdelacretaz/experimental/impl/jmxservlet/JmxServlet.java
  (with props)
    sling/whiteboard/bdelacretaz/experimental/src/main/java/org/apache/sling/bdelacretaz/experimental/impl/jmxservlet/JmxServletMBean.java
  (with props)

Propchange: sling/whiteboard/bdelacretaz/experimental/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Wed Jun 22 14:58:59 2011
@@ -0,0 +1,12 @@
+target
+bin
+*.iml
+*.ipr
+*.iws
+.settings
+.project
+.classpath
+.externalToolBuilders
+maven-eclipse.xml
+sling
+

Added: sling/whiteboard/bdelacretaz/experimental/pom.xml
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/experimental/pom.xml?rev=1138496&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/experimental/pom.xml (added)
+++ sling/whiteboard/bdelacretaz/experimental/pom.xml Wed Jun 22 14:58:59 2011
@@ -0,0 +1,78 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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>9</version>
+    </parent>
+    
+    <groupId>org.apache.sling</groupId>
+    <artifactId>org.apache.sling.whiteboard.bdelacretaz.experimental</artifactId>
+    <version>0.9.9-SNAPSHOT</version>
+    <packaging>bundle</packaging>
+    <name>Bertrand's experimental Sling stuff</name>
+    
+    <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>
+                        <Private-Package>org.apache.sling.bdelacretaz.experimental.impl.*</Private-Package>
+                    </instructions>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+    
+    <dependencies>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.api</artifactId>
+            <version>2.1.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.jcr.api</artifactId>
+            <version>2.1.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr.annotations</artifactId>
+            <version>1.2.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>javax.jcr</groupId>
+            <artifactId>jcr</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+            <scope>provided</scope>
+        </dependency>
+    </dependencies>
+</project>

Propchange: sling/whiteboard/bdelacretaz/experimental/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: sling/whiteboard/bdelacretaz/experimental/src/main/java/org/apache/sling/bdelacretaz/experimental/impl/jmxservlet/JmxServlet.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/experimental/src/main/java/org/apache/sling/bdelacretaz/experimental/impl/jmxservlet/JmxServlet.java?rev=1138496&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/experimental/src/main/java/org/apache/sling/bdelacretaz/experimental/impl/jmxservlet/JmxServlet.java
(added)
+++ sling/whiteboard/bdelacretaz/experimental/src/main/java/org/apache/sling/bdelacretaz/experimental/impl/jmxservlet/JmxServlet.java
Wed Jun 22 14:58:59 2011
@@ -0,0 +1,154 @@
+/*
+ * 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.bdelacretaz.experimental.impl.jmxservlet;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import javax.management.MBeanAttributeInfo;
+import javax.management.MBeanInfo;
+import javax.management.MBeanRegistration;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
+
+@SuppressWarnings("serial")
+@Component
+@Service
+@Properties({
+    @Property(name="sling.servlet.paths", value="/system/sling/jmx"),
+    @Property(name="jmx.objectname", value=JmxServlet.OBJECT_NAME)
+})
+/** HTTP access to MBeans attribute values - could be used for integration
+ *  testing of Sling MBeans.
+ */
+public class JmxServlet extends SlingSafeMethodsServlet implements JmxServletMBean, MBeanRegistration
{
+
+    private AtomicInteger requestsCount = new AtomicInteger();
+    private MBeanServer mbeanServer;
+    
+    public static final String OBJECT_NAME = "org.apache.sling:type=jmx,service=JmxServlet";
+    
+    @Override
+    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)

+    throws ServletException, IOException {
+        
+        if(mbeanServer == null) {
+            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "MBeanServer
not set");
+            return;
+        }
+        
+        requestsCount.incrementAndGet();
+        response.setContentType("text/plain");
+        response.setCharacterEncoding("UTF-8");
+        final PrintWriter pw = response.getWriter();
+        
+        String objectNameStr = request.getParameter("objectName");
+        if(objectNameStr == null) {
+            objectNameStr = OBJECT_NAME;
+        }
+        String attributeName = request.getParameter("attributeName");
+        if(attributeName == null) {
+            attributeName = "RequestsCount";
+        }
+        final String requestInfo = objectNameStr + " [" + attributeName + "]";
+        
+        try {
+            final ObjectName objectName = new ObjectName(objectNameStr);
+            final MBeanInfo info = mbeanServer.getMBeanInfo(objectName);
+            if(info == null) {
+                notFound(response, requestInfo, "MBeanInfo not found");
+                return;
+            }
+            final MBeanAttributeInfo [] attr = info.getAttributes();
+            if(attr == null) {
+                notFound(response, requestInfo, "MBeanAttributeInfo not found");
+                return;
+            }
+            
+            boolean found = false;
+            for(MBeanAttributeInfo a : attr) {
+                if(attributeName.equals(a.getName())) {
+                    found = true;
+                    outputPair(pw, "Request", requestInfo);
+                    outputPair(pw, "Description", a.getDescription());
+                    outputPair(pw, "Name", a.getName());
+                    outputPair(pw, "Type", a.getType());
+                    outputPair(pw, "Value", 
+                            mbeanServer.getAttribute(objectName, attributeName).toString());
+                }
+            }
+            
+            if(!found) {
+                notFound(response, requestInfo, "Even though MBeanAttributeInfo was found");
+                return;
+            }
+
+        } catch(Exception e) {
+            throw new ServletException(
+                    "Exception while retrieving JMX info " + requestInfo, e);
+        }
+        
+        pw.flush();
+    }
+    
+    private void notFound(HttpServletResponse response, String requestInfo, String additionalInfo)
throws IOException {
+        response.sendError(HttpServletResponse.SC_NOT_FOUND, 
+                "Not found: " + requestInfo + " (" + additionalInfo + ")");
+    }
+    
+    private static void outputPair(PrintWriter pw, String key, String value) {
+        pw.print(key);
+        pw.print('=');
+        pw.println(value);
+    }
+
+    /** @inheritDoc */
+    public void postDeregister() {
+    }
+
+    /** @inheritDoc */
+    public void postRegister(Boolean arg0) {
+    }
+
+    /** @inheritDoc */
+    public void preDeregister() throws Exception {
+        mbeanServer = null;
+    }
+
+    /** @inheritDoc */
+    public ObjectName preRegister(MBeanServer s, ObjectName name) {
+        mbeanServer = s;
+        return name;
+    }
+
+    public int getRequestsCount() {
+        return requestsCount.get();
+    }
+}

Propchange: sling/whiteboard/bdelacretaz/experimental/src/main/java/org/apache/sling/bdelacretaz/experimental/impl/jmxservlet/JmxServlet.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/whiteboard/bdelacretaz/experimental/src/main/java/org/apache/sling/bdelacretaz/experimental/impl/jmxservlet/JmxServlet.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: sling/whiteboard/bdelacretaz/experimental/src/main/java/org/apache/sling/bdelacretaz/experimental/impl/jmxservlet/JmxServletMBean.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/experimental/src/main/java/org/apache/sling/bdelacretaz/experimental/impl/jmxservlet/JmxServletMBean.java?rev=1138496&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/experimental/src/main/java/org/apache/sling/bdelacretaz/experimental/impl/jmxservlet/JmxServletMBean.java
(added)
+++ sling/whiteboard/bdelacretaz/experimental/src/main/java/org/apache/sling/bdelacretaz/experimental/impl/jmxservlet/JmxServletMBean.java
Wed Jun 22 14:58:59 2011
@@ -0,0 +1,23 @@
+/*
+ * 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.bdelacretaz.experimental.impl.jmxservlet;
+
+public interface JmxServletMBean {
+    int getRequestsCount();
+}

Propchange: sling/whiteboard/bdelacretaz/experimental/src/main/java/org/apache/sling/bdelacretaz/experimental/impl/jmxservlet/JmxServletMBean.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/whiteboard/bdelacretaz/experimental/src/main/java/org/apache/sling/bdelacretaz/experimental/impl/jmxservlet/JmxServletMBean.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL



Mime
View raw message