jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r543821 - in /jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi: client/JNDIRMIRepository.java client/JNDIRMIRepositoryFactory.java servlet/JNDIRMIBindingServlet.java servlet/JNDIRMIRepositoryServlet.java
Date Sun, 03 Jun 2007 01:26:34 GMT
Author: jukka
Date: Sat Jun  2 18:26:33 2007
New Revision: 543821

URL: http://svn.apache.org/viewvc?view=rev&rev=543821
Log:
JCR-419: Added support for binding and accessing a remote repository in JNDI

Added:
    jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/client/JNDIRMIRepository.java
  (with props)
    jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/client/JNDIRMIRepositoryFactory.java
  (with props)
    jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/servlet/JNDIRMIBindingServlet.java
  (with props)
    jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/servlet/JNDIRMIRepositoryServlet.java
  (with props)

Added: jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/client/JNDIRMIRepository.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/client/JNDIRMIRepository.java?view=auto&rev=543821
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/client/JNDIRMIRepository.java
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/client/JNDIRMIRepository.java
Sat Jun  2 18:26:33 2007
@@ -0,0 +1,46 @@
+/*
+ * 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.rmi.client;
+
+import javax.naming.Context;
+
+import org.apache.jackrabbit.commons.repository.ProxyRepository;
+
+/**
+ * Proxy for a remote repository bound in JNDI. The configured repository is
+ * looked up from JNDI lazily during each method call. Thus the JNDI entry
+ * does not need to exist when this class is instantiated. The JNDI entry
+ * can also be replaced with another repository during the lifetime of an
+ * instance of this class.
+ *
+ * @since 1.4
+ */
+public class JNDIRMIRepository extends ProxyRepository {
+
+    /**
+     * Creates a proxy for a remote repository in JNDI.
+     *
+     * @param factory local adapter factory
+     * @param context JNDI context
+     * @param location JNDI location
+     */
+    public JNDIRMIRepository(
+            LocalAdapterFactory factory, Context context, String location) {
+        super(new JNDIRMIRepositoryFactory(factory, context, location));
+    }
+
+}

Propchange: jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/client/JNDIRMIRepository.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/client/JNDIRMIRepositoryFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/client/JNDIRMIRepositoryFactory.java?view=auto&rev=543821
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/client/JNDIRMIRepositoryFactory.java
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/client/JNDIRMIRepositoryFactory.java
Sat Jun  2 18:26:33 2007
@@ -0,0 +1,91 @@
+/*
+ * 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.rmi.client;
+
+import javax.jcr.Repository;
+import javax.jcr.RepositoryException;
+import javax.naming.Context;
+import javax.naming.NamingException;
+
+import org.apache.jackrabbit.commons.repository.RepositoryFactory;
+import org.apache.jackrabbit.rmi.remote.RemoteRepository;
+
+/**
+ * Factory that looks up a remote repository from JNDI.
+ *
+ * @since 1.4
+ */
+public class JNDIRMIRepositoryFactory implements RepositoryFactory {
+
+    /**
+     * Local adapter factory.
+     */
+    private final LocalAdapterFactory factory;
+
+    /**
+     * JNDI context of the remote repository.
+     */
+    private final Context context;
+
+    /**
+     * JNDI location of the remote repository.
+     */
+    private final String location;
+
+    /**
+     * Creates a factory for looking up a remote repository from JNDI.
+     *
+     * @param factory local adapter factory
+     * @param context JNDI context
+     * @param location JNDI location
+     */
+    public JNDIRMIRepositoryFactory(
+            LocalAdapterFactory factory, Context context, String location) {
+        this.factory = factory;
+        this.context = context;
+        this.location = location;
+    }
+
+    /**
+     * Looks up and returns a remote repository from JNDI.
+     *
+     * @return local adapter for the remote repository
+     * @throws RepositoryException if the repository could not be accessed
+     */
+    public Repository getRepository() throws RepositoryException {
+        try {
+            Object remote = context.lookup(location);
+            if (remote instanceof RemoteRepository) {
+                return factory.getRepository((RemoteRepository) remote);
+            } else if (remote == null) {
+                throw new RepositoryException(
+                        "Remote repository not found: The JNDI entry "
+                        + location + " is null");
+            } else {
+                throw new RepositoryException(
+                        "Invalid remote repository: The JNDI entry "
+                        + location + " is an instance of "
+                        + remote.getClass().getName());
+            }
+        } catch (NamingException e) {
+            throw new RepositoryException(
+                    "Remote repository not found: The JNDI entry " + location
+                    + " could not be looked up", e);
+        }
+    }
+
+}

Propchange: jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/client/JNDIRMIRepositoryFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/servlet/JNDIRMIBindingServlet.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/servlet/JNDIRMIBindingServlet.java?view=auto&rev=543821
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/servlet/JNDIRMIBindingServlet.java
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/servlet/JNDIRMIBindingServlet.java
Sat Jun  2 18:26:33 2007
@@ -0,0 +1,165 @@
+/*
+ * 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.rmi.servlet;
+
+import java.rmi.Remote;
+import java.rmi.RemoteException;
+import java.util.Enumeration;
+import java.util.Hashtable;
+
+import javax.jcr.Repository;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+
+import org.apache.jackrabbit.commons.servlet.ServletRepository;
+import org.apache.jackrabbit.rmi.jackrabbit.JackrabbitServerAdapterFactory;
+import org.apache.jackrabbit.rmi.remote.RemoteRepository;
+import org.apache.jackrabbit.rmi.server.RemoteAdapterFactory;
+
+/**
+ * Servlet that binds a repository from a servlet context attribute to JNDI
+ * as a remote repository reference.
+ * <p>
+ * The initialization parameters of this servlet are:
+ * <dl>
+ *   <dt>javax.jcr.Repository</dt>
+ *   <dd>
+ *     Name of the servlet context attribute that contains the repository.
+ *     The default value is "<code>javax.jcr.Repository</code>".
+ *   </dd>
+ *   <dt>org.apache.jackrabbit.rmi.server.RemoteAdapterFactory</dt>
+ *   <dd>
+ *     Name of the remote adapter factory class used to create the remote
+ *     repository reference. The configured class should have public
+ *     constructor that takes no arguments. The default class is
+ *     {@link JackrabbitServerAdapterFactory}.
+ *   </dd>
+ *   <dt>location</dt>
+ *   <dd>
+ *     Location where to bind the repository in the JNDI directory.
+ *     The default value is
+ *      "<code>org/apache/jackrabbit/rmi/remote/RepoteRepository</code>".
+ *   </dd>
+ *   <dt>*</dt>
+ *   <dd>
+ *     All other init parameters are used as the JNDI environment when
+ *     instantiating {@link InitialContext} for binding up the repository. 
+ *   </dd>
+ * </dl>
+ *
+ * @since 1.4
+ */
+public class JNDIRMIBindingServlet extends HttpServlet {
+
+    /**
+     * Serial version UID.
+     */
+    private static final long serialVersionUID = -8981536241655836775L;
+
+    /**
+     * JNDI context to which to bind the repository.
+     */
+    private Context context;
+
+    /**
+     * Location of the repository within the JNDI context.
+     */
+    private String location =
+        RemoteRepository.class.getName().replace('.', '/');
+
+    /**
+     * The remote repository reference. Kept to avoid it from being
+     * collected as garbage when no clients are connected.
+     */
+    private Remote remote;
+
+    /**
+     * Binds a repository from the servlet context in the configured RMI URL.
+     *
+     * @throws ServletException if the repository could not be bound in RMI
+     */
+    public void init() throws ServletException {
+        try {
+            Hashtable environment = new Hashtable();
+            Enumeration names = getInitParameterNames();
+            while (names.hasMoreElements()) {
+                String name = (String) names.nextElement();
+                if (name.equals("location")) {
+                    location = getInitParameter(name);
+                } else if (!name.equals(Repository.class.getName())
+                        && !name.equals(RemoteAdapterFactory.class.getName())) {
+                    environment.put(name, getInitParameter(name));
+                }
+            }
+            context =  new InitialContext(environment);
+            RemoteAdapterFactory factory = getRemoteAdapterFactory();
+            remote = factory.getRemoteRepository(new ServletRepository(this));
+            context.bind(location, remote);
+        } catch (RemoteException e) {
+            throw new ServletException(
+                    "Failed to create the remote repository reference", e);
+        } catch (NamingException e) {
+            throw new ServletException(
+                    "Failed to bind remote repository to JNDI: " + location, e);
+        }
+    }
+
+    /**
+     * Unbinds the remote repository from JNDI.
+     */
+    public void destroy() {
+        try {
+            context.unbind(location);
+        } catch (NamingException e) {
+            log("Failed to unbind remote repository from JNDI: " + location, e);
+        }
+    }
+
+    /**
+     * Instantiates and returns the configured remote adapter factory.
+     *
+     * @return remote adapter factory
+     * @throws ServletException if the factory could not be instantiated
+     */
+    private RemoteAdapterFactory getRemoteAdapterFactory()
+            throws ServletException {
+        String name = getInitParameter(RemoteAdapterFactory.class.getName());
+        if (name == null) {
+            name = JackrabbitServerAdapterFactory.class.getName();
+        }
+        try {
+            Class factoryClass = Class.forName(name);
+            return (RemoteAdapterFactory) factoryClass.newInstance();
+        } catch (ClassNotFoundException e) {
+            throw new ServletException(
+                    "Remote adapter factory class not found: " + name, e);
+        } catch (InstantiationException e) {
+            throw new ServletException(
+                    "Failed to instantiate the adapter factory: " + name, e);
+        } catch (IllegalAccessException e) {
+            throw new ServletException(
+                    "Adapter factory constructor is not public: " + name, e);
+        } catch (ClassCastException e) {
+            throw new ServletException(
+                    "Invalid remote adapter factory class: " + name, e);
+        }
+    }
+
+}

Propchange: jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/servlet/JNDIRMIBindingServlet.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/servlet/JNDIRMIRepositoryServlet.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/servlet/JNDIRMIRepositoryServlet.java?view=auto&rev=543821
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/servlet/JNDIRMIRepositoryServlet.java
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/servlet/JNDIRMIRepositoryServlet.java
Sat Jun  2 18:26:33 2007
@@ -0,0 +1,135 @@
+/*
+ * 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.rmi.servlet;
+
+import java.util.Enumeration;
+import java.util.Hashtable;
+
+import javax.jcr.Repository;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.servlet.ServletException;
+
+import org.apache.jackrabbit.commons.servlet.AbstractRepositoryServlet;
+import org.apache.jackrabbit.rmi.client.JNDIRMIRepository;
+import org.apache.jackrabbit.rmi.client.LocalAdapterFactory;
+import org.apache.jackrabbit.rmi.jackrabbit.JackrabbitClientAdapterFactory;
+import org.apache.jackrabbit.rmi.remote.RemoteRepository;
+
+/**
+ * Servlet that makes a remote repository from JNDI available as an attribute
+ * in the servlet context.
+ * <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>org.apache.jackrabbit.rmi.client.LocalAdapterFactory</dt>
+ *   <dd>
+ *     Name of the local adapter factory class used to create the local
+ *     adapter for the remote repository. The configured class should have
+ *     public constructor that takes no arguments. The default class is
+ *     {@link JackrabbitClientAdapterFactory}.
+ *   </dd>
+ *   <dt>location</dt>
+ *   <dd>
+ *     Location of the remote repository in the JNDI directory.
+ *     The default value is
+ *     "<code>org/apache/jackrabbit/rmi/remote/RemoteRepository</code>".
+ *   </dd>
+ *   <dt>*</dt>
+ *   <dd>
+ *     All other init parameters are used as the JNDI environment when
+ *     instantiating {@link InitialContext} for looking up the repository. 
+ *   </dd>
+ * </dl>
+ * <p>
+ * This servlet can also be mapped to the URL space. See
+ * {@link AbstractRepositoryServlet} for the details.
+ *
+ * @since 1.4
+ */
+public class JNDIRMIRepositoryServlet extends AbstractRepositoryServlet {
+
+    /**
+     * Serial version UID.
+     */
+    private static final long serialVersionUID = 6345077056681327186L;
+
+    /**
+     * Creates and returns a proxy for the remote repository in the configured
+     * JNDI location.
+     *
+     * @return repository proxy
+     */
+    protected Repository getRepository() throws ServletException {
+        String location =
+            "//localhost/" + RemoteRepository.class.getName().replace('.', '/');
+        try {
+            Hashtable environment = new Hashtable();
+            Enumeration names = getInitParameterNames();
+            while (names.hasMoreElements()) {
+                String name = (String) names.nextElement();
+                if (name.equals("location")) {
+                    location = getInitParameter(name);
+                } else if (!name.equals(Repository.class.getName())
+                        && !name.equals(LocalAdapterFactory.class.getName())) {
+                    environment.put(name, getInitParameter(name));
+                }
+            }
+            return new JNDIRMIRepository(
+                    getLocalAdapterFactory(),
+                    new InitialContext(environment), location);
+        } catch (NamingException e) {
+            throw new ServletException(
+                    "Repository not found: Invalid JNDI context", e);
+        }
+    }
+
+    /**
+     * Instantiates and returns the configured local adapter factory.
+     *
+     * @return local adapter factory
+     * @throws ServletException if the factory could not be instantiated
+     */
+    private LocalAdapterFactory getLocalAdapterFactory()
+            throws ServletException {
+        String name = getInitParameter(
+                LocalAdapterFactory.class.getName(),
+                JackrabbitClientAdapterFactory.class.getName());
+        try {
+            Class factoryClass = Class.forName(name);
+            return (LocalAdapterFactory) factoryClass.newInstance();
+        } catch (ClassNotFoundException e) {
+            throw new ServletException(
+                    "Local adapter factory class not found: " + name, e);
+        } catch (InstantiationException e) {
+            throw new ServletException(
+                    "Failed to instantiate the adapter factory: " + name, e);
+        } catch (IllegalAccessException e) {
+            throw new ServletException(
+                    "Adapter factory constructor is not public: " + name, e);
+        } catch (ClassCastException e) {
+            throw new ServletException(
+                    "Invalid local adapter factory class: " + name, e);
+        }
+    }
+
+}

Propchange: jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/servlet/JNDIRMIRepositoryServlet.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message