jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1181745 - in /jackrabbit/trunk: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/ jackrabbit-jcr-servlet/src/main/java/org/apache/jackrabbit/servlet/jackrabbit/
Date Tue, 11 Oct 2011 12:17:57 GMT
Author: jukka
Date: Tue Oct 11 12:17:57 2011
New Revision: 1181745

URL: http://svn.apache.org/viewvc?rev=1181745&view=rev
Log:
JCR-3040: JMX Stats for the Session

Make repository statistics available to a webapp

Added:
    jackrabbit/trunk/jackrabbit-jcr-servlet/src/main/java/org/apache/jackrabbit/servlet/jackrabbit/StatisticsServlet.java
Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryContext.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/RepositoryStatistics.java
    jackrabbit/trunk/jackrabbit-jcr-servlet/src/main/java/org/apache/jackrabbit/servlet/jackrabbit/JackrabbitRepositoryServlet.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryContext.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryContext.java?rev=1181745&r1=1181744&r2=1181745&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryContext.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryContext.java
Tue Oct 11 12:17:57 2011
@@ -16,9 +16,14 @@
  */
 package org.apache.jackrabbit.core;
 
+import java.io.File;
+import java.io.IOException;
 import java.util.concurrent.ScheduledExecutorService;
 
+import javax.jcr.RepositoryException;
+
 import org.apache.jackrabbit.core.cluster.ClusterNode;
+import org.apache.jackrabbit.core.config.RepositoryConfig;
 import org.apache.jackrabbit.core.data.DataStore;
 import org.apache.jackrabbit.core.fs.FileSystem;
 import org.apache.jackrabbit.core.id.NodeId;
@@ -124,6 +129,39 @@ public class RepositoryContext {
     }
 
     /**
+     * Starts a repository with the given configuration and returns
+     * the internal component context of the started repository.
+     *
+     * @since Apache Jackrabbit 2.3.1
+     * @param config repository configuration
+     * @return component context of the repository
+     * @throws RepositoryException if the repository could not be started
+     */
+    public static RepositoryContext create(RepositoryConfig config)
+            throws RepositoryException {
+        RepositoryImpl repository = RepositoryImpl.create(config);
+        return repository.getRepositoryContext();
+    }
+
+    /**
+     * Starts a repository in the given directory and returns the
+     * internal component context of the started repository. If needed,
+     * the directory is created and a default repository configuration
+     * is installed inside it.
+     *
+     * @since Apache Jackrabbit 2.3.1
+     * @see RepositoryConfig#install(File)
+     * @param dir repository directory
+     * @return component context of the repository
+     * @throws RepositoryException if the repository could not be started
+     * @throws IOException if the directory could not be initialized
+     */
+    public static RepositoryContext install(File dir)
+            throws RepositoryException, IOException {
+        return create(RepositoryConfig.install(dir));
+    }
+
+    /**
      * Returns the repository instance to which this context is associated.
      *
      * @return repository instance

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/RepositoryStatistics.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/RepositoryStatistics.java?rev=1181745&r1=1181744&r2=1181745&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/RepositoryStatistics.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/RepositoryStatistics.java
Tue Oct 11 12:17:57 2011
@@ -17,12 +17,16 @@
 package org.apache.jackrabbit.core.stats;
 
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
+import java.util.Map.Entry;
+import java.util.TreeMap;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicLong;
 
-public class RepositoryStatistics {
+public class RepositoryStatistics
+        implements Iterable<Map.Entry<String, TimeSeries>> {
 
     private final Map<String, TimeSeriesRecorder> recorders =
             new HashMap<String, TimeSeriesRecorder>();
@@ -35,6 +39,12 @@ public class RepositoryStatistics {
         }, 1, 1, TimeUnit.SECONDS);
     }
 
+    public synchronized Iterator<Entry<String, TimeSeries>> iterator() {
+        Map<String, TimeSeries> map = new TreeMap<String, TimeSeries>();
+        map.putAll(recorders);
+        return map.entrySet().iterator();
+    }
+
     public AtomicLong getCounter(String name) {
         return getOrCreateRecorder(name).getCounter();
     }

Modified: jackrabbit/trunk/jackrabbit-jcr-servlet/src/main/java/org/apache/jackrabbit/servlet/jackrabbit/JackrabbitRepositoryServlet.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-servlet/src/main/java/org/apache/jackrabbit/servlet/jackrabbit/JackrabbitRepositoryServlet.java?rev=1181745&r1=1181744&r2=1181745&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-servlet/src/main/java/org/apache/jackrabbit/servlet/jackrabbit/JackrabbitRepositoryServlet.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-servlet/src/main/java/org/apache/jackrabbit/servlet/jackrabbit/JackrabbitRepositoryServlet.java
Tue Oct 11 12:17:57 2011
@@ -26,7 +26,7 @@ import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 import javax.servlet.ServletException;
 
-import org.apache.jackrabbit.api.JackrabbitRepository;
+import org.apache.jackrabbit.core.RepositoryContext;
 import org.apache.jackrabbit.core.RepositoryImpl;
 import org.apache.jackrabbit.core.config.RepositoryConfig;
 import org.apache.jackrabbit.servlet.AbstractRepositoryServlet;
@@ -38,6 +38,12 @@ import org.apache.jackrabbit.servlet.Abs
  * <p>
  * The supported initialization parameters of this servlet are:
  * <dl>
+ *   <dt>org.apache.jackrabbit.core.RepositoryContext</dt>
+ *   <dd>
+ *     Name of the servlet context attribute to put the internal
+ *     component context of the repository in. The default value is
+ *     "<code>org.apache.jackrabbit.core.RepositoryContext</code>".
+ *   </dd>
  *   <dt>javax.jcr.Repository</dt>
  *   <dd>
  *     Name of the servlet context attribute to put the repository in.
@@ -75,7 +81,7 @@ public class JackrabbitRepositoryServlet
     /**
      * Repository instance.
      */
-    private JackrabbitRepository repository;
+    private RepositoryContext context;
 
     /**
      * Starts the repository instance and makes it available in the
@@ -100,8 +106,12 @@ public class JackrabbitRepositoryServlet
                 createDefaultConfiguration(config);
             }
 
-            repository = RepositoryImpl.create(RepositoryConfig.create(
+            context = RepositoryContext.create(RepositoryConfig.create(
                     config.toURI(), home.getPath()));
+
+            String name = RepositoryContext.class.getName();
+            getServletContext().setAttribute(
+                    getInitParameter(name, name), context);
         } catch (RepositoryException e) {
             throw new ServletException("Failed to start Jackrabbit", e);
         }
@@ -114,7 +124,7 @@ public class JackrabbitRepositoryServlet
      */
     public void destroy() {
         super.destroy();
-        repository.shutdown();
+        context.getRepository().shutdown();
     }
 
     /**
@@ -124,7 +134,7 @@ public class JackrabbitRepositoryServlet
      */
     @Override
     protected Repository getRepository() {
-        return repository;
+        return context.getRepository();
     }
 
     /**

Added: jackrabbit/trunk/jackrabbit-jcr-servlet/src/main/java/org/apache/jackrabbit/servlet/jackrabbit/StatisticsServlet.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-servlet/src/main/java/org/apache/jackrabbit/servlet/jackrabbit/StatisticsServlet.java?rev=1181745&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-servlet/src/main/java/org/apache/jackrabbit/servlet/jackrabbit/StatisticsServlet.java
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-servlet/src/main/java/org/apache/jackrabbit/servlet/jackrabbit/StatisticsServlet.java
Tue Oct 11 12:17:57 2011
@@ -0,0 +1,109 @@
+/*
+ * 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.jackrabbit.servlet.jackrabbit;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.Map;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.jackrabbit.core.RepositoryContext;
+import org.apache.jackrabbit.core.stats.RepositoryStatistics;
+import org.apache.jackrabbit.core.stats.TimeSeries;
+
+/**
+ * Servlet that makes Jackrabbit repository statistics available as
+ * a JSON object.
+ *
+ * @since Apache Jackrabbit 2.3.1
+ */
+public class StatisticsServlet extends HttpServlet {
+
+    /** Serial version UID */
+    private static final long serialVersionUID = -7494195499389135951L;
+
+    @Override
+    protected void doGet(
+            HttpServletRequest request, HttpServletResponse response)
+            throws ServletException, IOException {
+        String klass = RepositoryContext.class.getName();
+        String name = getServletConfig().getInitParameter(klass);
+        if (name == null) {
+            name = klass;
+        }
+
+        RepositoryContext context = (RepositoryContext)
+                getServletContext().getAttribute(name);
+        if (context != null) {
+            RepositoryStatistics statistics = context.getRepositoryStatistics();
+            response.setContentType("application/json");
+            Writer writer = response.getWriter();
+            writer.write('{');
+            boolean first = true;
+            for (Map.Entry<String, TimeSeries> entry : statistics) {
+                if (first) {
+                    first = false;
+                } else {
+                    writer.write(',');
+                }
+                write(writer, entry.getKey(), entry.getValue());
+            }
+            writer.write('}');
+        } else {
+            response.sendError(
+                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
+                    "Jackrabbit repository internals are not available");
+        }
+    }
+
+    private void write(Writer writer, String name, TimeSeries series)
+            throws IOException {
+        writer.write('"');
+        writer.write(name);
+        writer.write('"');
+        writer.write(':');
+        writer.write('{');
+        write(writer, "second", series.getEventsPerSecond());
+        writer.write(',');
+        write(writer, "minute", series.getEventsPerMinute());
+        writer.write(',');
+        write(writer, "hour", series.getEventsPerHour());
+        writer.write(',');
+        write(writer, "week", series.getEventsPerWeek());
+        writer.write('}');
+    }
+
+    private void write(Writer writer, String name, long[] values)
+            throws IOException {
+        writer.write('"');
+        writer.write(name);
+        writer.write('"');
+        writer.write(':');
+        writer.write('[');
+        for (int i = 0; i < values.length; i++) {
+            if (i > 0) {
+                writer.write(',');
+            }
+            writer.write(String.valueOf(values[i]));
+        }
+        writer.write(']');
+    }
+}



Mime
View raw message