jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r543997 - in /jackrabbit/trunk/jackrabbit-core: pom.xml src/main/java/org/apache/jackrabbit/core/servlet/ src/main/java/org/apache/jackrabbit/core/servlet/JackrabbitRepositoryServlet.java
Date Sun, 03 Jun 2007 21:43:54 GMT
Author: jukka
Date: Sun Jun  3 14:43:53 2007
New Revision: 543997

URL: http://svn.apache.org/viewvc?view=rev&rev=543997
Log:
JCR-956: Added a Jackrabbit core repository servlet

Added:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/servlet/
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/servlet/JackrabbitRepositoryServlet.java
  (with props)
Modified:
    jackrabbit/trunk/jackrabbit-core/pom.xml

Modified: jackrabbit/trunk/jackrabbit-core/pom.xml
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/pom.xml?view=diff&rev=543997&r1=543996&r2=543997
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/pom.xml (original)
+++ jackrabbit/trunk/jackrabbit-core/pom.xml Sun Jun  3 14:43:53 2007
@@ -287,6 +287,10 @@
       <artifactId>derby</artifactId>
     </dependency>
     <dependency>
+      <groupId>javax.servlet</groupId>
+      <artifactId>servlet-api</artifactId>
+    </dependency>
+    <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <scope>test</scope>

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/servlet/JackrabbitRepositoryServlet.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/servlet/JackrabbitRepositoryServlet.java?view=auto&rev=543997
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/servlet/JackrabbitRepositoryServlet.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/servlet/JackrabbitRepositoryServlet.java
Sun Jun  3 14:43:53 2007
@@ -0,0 +1,161 @@
+/*
+ * 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.core.servlet;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import javax.jcr.Repository;
+import javax.jcr.RepositoryException;
+import javax.servlet.ServletException;
+
+import org.apache.jackrabbit.api.JackrabbitRepository;
+import org.apache.jackrabbit.commons.servlet.AbstractRepositoryServlet;
+import org.apache.jackrabbit.core.RepositoryImpl;
+import org.apache.jackrabbit.core.config.RepositoryConfig;
+
+/**
+ * Servlet that makes a Jackrabbit repository available as a servlet context
+ * attribute. The repository is started during servlet initialization and
+ * shut down when the servlet is destroyed.
+ * <p>
+ * The supported initialization parameters of this servlet are:
+ * <dl>
+ *   <dt>javax.jcr.Repository</dt>
+ *   <dd>
+ *     Name of the servlet context attribute to put the repository in.
+ *     The default value is "<code>javax.jcr.Repository</code>".
+ *   </dd>
+ *   <dt>repository.home</dt>
+ *   <dd>
+ *     Path of the repository home directory. The default value is
+ *     "<code>jackrabbit-repository</code>". The home directory is
+ *     automatically created during servlet initialization if it does
+ *     not already exist.
+ *   </dd>
+ *   <dt>repository.config</dt>
+ *   <dd>
+ *     Path of the repository configuration file. The default value is
+ *     "<code>repository.xml</code>" within the configured repository home
+ *     directory. A standard configuration file is automatically copied to
+ *     the configured location during servlet initialization if the file
+ *     does not already exist. 
+ *   </dd>
+ * </dl>
+ * <p>
+ * The repository servlet can also be mapped to the URL space. See
+ * {@link AbstractRepositoryServlet} for the details.
+ *
+ * @since 1.4
+ */
+public class JackrabbitRepositoryServlet extends AbstractRepositoryServlet {
+
+    /**
+     * Serial version UID.
+     */
+    private static final long serialVersionUID = 7102770011290708450L;
+
+    /**
+     * Repository instance.
+     */
+    private JackrabbitRepository repository;
+
+    /**
+     * Starts the repository instance and makes it available in the
+     * servlet context.
+     *
+     * @throws ServletException if the repository can not be started
+     */
+    public void init() throws ServletException {
+        try {
+            File home = new File(getInitParameter(
+                    "repository.home", "jackrabbit-repository"));
+            if (!home.exists()) {
+                log("Creating repository home directory: " + home);
+                home.mkdirs();
+            }
+
+            File config = new File(getInitParameter(
+                    "repository.config",
+                    new File(home, "repository.xml").getPath()));
+            if (!config.exists()) {
+                log("Creating default repository configuration: " + config);
+                createDefaultConfiguration(config);
+            }
+
+            repository = RepositoryImpl.create(RepositoryConfig.create(
+                    config.toURI(), home.getPath()));
+        } catch (RepositoryException e) {
+            throw new ServletException("Failed to start Jackrabbit", e);
+        }
+        
+        super.init();
+    }
+
+    /**
+     * Removes the repository from the servlet context and shuts it down.
+     */
+    public void destroy() {
+        super.destroy();
+        repository.shutdown();
+    }
+
+    /**
+     * Returns the repository instance.
+     *
+     * @return repository instance
+     */
+    protected Repository getRepository() {
+        return repository;
+    }
+
+    /**
+     * Copies the default repository configuration file to the given location.
+     *  
+     * @param config path of the configuration file
+     * @throws ServletException if the configuration file could not be copied
+     */
+    private void createDefaultConfiguration(File config)
+            throws ServletException {
+        try {
+            OutputStream output = new FileOutputStream(config);
+            try {
+                InputStream input =
+                    RepositoryImpl.class.getResourceAsStream("repository.xml");
+                try {
+                    byte[] buffer = new byte[8192];
+                    int n = input.read(buffer);
+                    while (n != -1) {
+                        output.write(buffer, 0, n);
+                        n = input.read(buffer);
+                    }
+                } finally {
+                    input.close();
+                }
+            } finally {
+                output.close();
+            }
+        } catch (IOException e) {
+            throw new ServletException(
+                    "Failed to copy default configuration: " + config, e);
+        }
+    }
+
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/servlet/JackrabbitRepositoryServlet.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message