You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2008/03/12 09:00:34 UTC

svn commit: r636243 - /incubator/sling/trunk/jcr/jackrabbit-server/src/main/java/org/apache/sling/jcr/jackrabbit/server/RmiRegistrationSupport.java

Author: fmeschbe
Date: Wed Mar 12 01:00:31 2008
New Revision: 636243

URL: http://svn.apache.org/viewvc?rev=636243&view=rev
Log:
SLING-323 Publish the embedded Jackrabbit repository over RMI using the
JackrabbitServerAdapterFactory through a factory method, which may be
overwritten in extensions.

Modified:
    incubator/sling/trunk/jcr/jackrabbit-server/src/main/java/org/apache/sling/jcr/jackrabbit/server/RmiRegistrationSupport.java

Modified: incubator/sling/trunk/jcr/jackrabbit-server/src/main/java/org/apache/sling/jcr/jackrabbit/server/RmiRegistrationSupport.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/jackrabbit-server/src/main/java/org/apache/sling/jcr/jackrabbit/server/RmiRegistrationSupport.java?rev=636243&r1=636242&r2=636243&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/jackrabbit-server/src/main/java/org/apache/sling/jcr/jackrabbit/server/RmiRegistrationSupport.java (original)
+++ incubator/sling/trunk/jcr/jackrabbit-server/src/main/java/org/apache/sling/jcr/jackrabbit/server/RmiRegistrationSupport.java Wed Mar 12 01:00:31 2008
@@ -27,18 +27,18 @@
 
 import javax.jcr.Repository;
 
-import org.apache.jackrabbit.rmi.server.ServerAdapterFactory;
+import org.apache.jackrabbit.rmi.jackrabbit.JackrabbitServerAdapterFactory;
+import org.apache.jackrabbit.rmi.server.RemoteAdapterFactory;
 import org.osgi.service.log.LogService;
 
-
 /**
  * The <code>RmiRegistrationSupport</code> extends the
- * {@link AbstractRegistrationSupport} class to register repositories with
- * an RMI registry whose provider localhost port may be configured.
+ * {@link AbstractRegistrationSupport} class to register repositories with an
+ * RMI registry whose provider localhost port may be configured.
  * <p>
  * Note: Currently only registries in this Java VM are supported. In the future
  * support for external registries may be added.
- *
+ * 
  * @scr.component immediate="true" label="%rmi.name"
  *                description="%rmi.description"
  * @scr.reference name="Repository" interface="javax.jcr.Repository"
@@ -46,8 +46,7 @@
  * @scr.reference name="Log" interface="org.osgi.service.log.LogService"
  *                policy="dynamic" cardinality="0..1"
  * @scr.property name="service.vendor" value="The Apache Software Foundation"
- * @scr.property name="service.description"
- *      value="RMI based Repository Registration"
+ * @scr.property name="service.description" value="RMI based Repository Registration"
  */
 public class RmiRegistrationSupport extends AbstractRegistrationSupport {
 
@@ -61,35 +60,42 @@
 
     /** The private RMI registry, only defined if possible */
     private Registry registry;
+
     private boolean registryIsPrivate;
 
     // ---------- SCR intergration ---------------------------------------------
 
     /**
-     * Read the registry port from the configuration properties. If the value
-     * is invalid (higher than 65525), the RMI registry is disabled.
-     * Likewise the registry is disabled, if the port property is negative. If
-     * the port is zero or not a number, the default port (1099) is assumed.
+     * Read the registry port from the configuration properties. If the value is
+     * invalid (higher than 65525), the RMI registry is disabled. Likewise the
+     * registry is disabled, if the port property is negative. If the port is
+     * zero or not a number, the default port (1099) is assumed.
      */
     protected boolean doActivate() {
 
-        Object portProp = this.getComponentContext().getProperties().get(PROP_REGISTRY_PORT);
+        Object portProp = this.getComponentContext().getProperties().get(
+            PROP_REGISTRY_PORT);
         this.registryPort = (portProp instanceof Number)
                 ? ((Number) portProp).intValue()
                 : 0;
 
         // ensure correct value
         if (this.registryPort < 0) {
-            this.log(LogService.LOG_WARNING, "RMI registry disabled (no or negative RMI port configured)", null);
+            this.log(LogService.LOG_WARNING,
+                "RMI registry disabled (no or negative RMI port configured)",
+                null);
             return false;
         } else if (this.registryPort == 0) {
             this.registryPort = Registry.REGISTRY_PORT;
         } else if (this.registryPort == 0 || this.registryPort > 0xffff) {
-            this.log(LogService.LOG_WARNING, "Illegal RMI registry port number " + this.registryPort + ", disabling RMI registry", null);
+            this.log(LogService.LOG_WARNING,
+                "Illegal RMI registry port number " + this.registryPort
+                    + ", disabling RMI registry", null);
             return false;
         }
 
-        this.log(LogService.LOG_INFO, "Using RMI Registry port " + this.registryPort, null);
+        this.log(LogService.LOG_INFO, "Using RMI Registry port "
+            + this.registryPort, null);
         return true;
     }
 
@@ -103,12 +109,13 @@
         if (this.registry != null && this.registryIsPrivate) {
             try {
                 UnicastRemoteObject.unexportObject(this.registry, true);
-                this.log(LogService.LOG_INFO, "Unexported private RMI Registry at "
-                    + this.registryPort, null);
+                this.log(LogService.LOG_INFO,
+                    "Unexported private RMI Registry at " + this.registryPort,
+                    null);
             } catch (NoSuchObjectException nsoe) {
                 // not expected, but don't really care either
-                this.log(LogService.LOG_INFO, "Cannot unexport private RMI Registry reference",
-                    nsoe);
+                this.log(LogService.LOG_INFO,
+                    "Cannot unexport private RMI Registry reference", nsoe);
             }
         }
         this.registry = null;
@@ -127,10 +134,10 @@
 
     /**
      * Tries to create a private registry at the configured port. If this fails
-     * (for example because a registry already exists in the VM, a registry
-     * stub for the port is returned. This latter stub may or may not connect
-     * to a real registry, which may only be found out, when trying to
-     * register repositories.
+     * (for example because a registry already exists in the VM, a registry stub
+     * for the port is returned. This latter stub may or may not connect to a
+     * real registry, which may only be found out, when trying to register
+     * repositories.
      */
     private Registry getPrivateRegistry() {
         if (this.registry == null) {
@@ -145,13 +152,15 @@
                 // creating failed, check whether there is already one
                 this.log(LogService.LOG_INFO,
                     "Cannot create private registry, trying existing registry at "
-                        + this.registryPort + ", reason: " + re.toString(), null);
+                        + this.registryPort + ", reason: " + re.toString(),
+                    null);
 
                 try {
                     this.registry = LocateRegistry.getRegistry(this.registryPort);
                     this.registryIsPrivate = false;
-                    this.log(LogService.LOG_INFO, "Trying existing registry at "
-                        + this.registryPort, null);
+                    this.log(LogService.LOG_INFO,
+                        "Trying existing registry at " + this.registryPort,
+                        null);
 
                 } catch (RemoteException pre) {
                     this.log(
@@ -165,11 +174,26 @@
         return this.registry;
     }
 
-    //---------- Inner Class --------------------------------------------------
+    /**
+     * Returns a Jackrabbit JCR RMI <code>RemoteAdapterFactory</code> to be
+     * used to publish local (server-side) JCR objects to a remote client.
+     * <p>
+     * This method returns an instance of the
+     * <code>JackrabbitServerAdapterFactory</code> class to enable the use of
+     * the Jackrabbit API over RMI. Extensions of this class may overwrite this
+     * method to return a different implementation to provide different JCR
+     * extension API depending on the server implementation.
+     */
+    protected RemoteAdapterFactory getRemoteAdapterFactory() {
+        return new JackrabbitServerAdapterFactory();
+    }
+
+    // ---------- Inner Class --------------------------------------------------
 
     private class RmiRegistration {
 
         private String rmiName;
+
         private Remote rmiRepository;
 
         RmiRegistration(String rmiName, Repository repository) {
@@ -191,7 +215,8 @@
             } catch (IOException ignore) {
                 host = "localhost";
             }
-            return "//" + host + ":" + RmiRegistrationSupport.this.registryPort + "/" + this.getRmiName();
+            return "//" + host + ":" + RmiRegistrationSupport.this.registryPort
+                + "/" + this.getRmiName();
         }
 
         private void register(String rmiName, Repository repository) {
@@ -200,12 +225,17 @@
             // try to create remote repository and keep it to ensure it is
             // unexported in the unregister() method
             try {
-                this.rmiRepository = new ServerAdapterFactory().getRemoteRepository(repository);
+                RemoteAdapterFactory raf = getRemoteAdapterFactory();
+                this.rmiRepository = raf.getRemoteRepository(repository);
             } catch (RemoteException e) {
-                RmiRegistrationSupport.this.log(LogService.LOG_ERROR, "Unable to create remote repository.", e);
+                RmiRegistrationSupport.this.log(LogService.LOG_ERROR,
+                    "Unable to create remote repository.", e);
                 return;
             } catch (Exception e) {
-                RmiRegistrationSupport.this.log(LogService.LOG_ERROR, "Unable to create RMI repository. jcr-rmi.jar might be missing.", e);
+                RmiRegistrationSupport.this.log(
+                    LogService.LOG_ERROR,
+                    "Unable to create RMI repository. jcr-rmi.jar might be missing.",
+                    e);
                 return;
             }
 
@@ -215,15 +245,19 @@
                 if (registry != null) {
                     registry.bind(rmiName, this.rmiRepository);
                     this.rmiName = rmiName;
-                    RmiRegistrationSupport.this.log(LogService.LOG_INFO, "Repository bound to " + this.getRmiURL(), null);
+                    RmiRegistrationSupport.this.log(LogService.LOG_INFO,
+                        "Repository bound to " + this.getRmiURL(), null);
                 }
 
             } catch (NoSuchObjectException nsoe) {
                 // the registry does not really exist
-                RmiRegistrationSupport.this.log(LogService.LOG_WARNING, "Cannot contact RMI registry at "
-                    + RmiRegistrationSupport.this.registryPort + ", repository not registered", null);
+                RmiRegistrationSupport.this.log(LogService.LOG_WARNING,
+                    "Cannot contact RMI registry at "
+                        + RmiRegistrationSupport.this.registryPort
+                        + ", repository not registered", null);
             } catch (Exception e) {
-                RmiRegistrationSupport.this.log(LogService.LOG_ERROR, "Unable to bind repository via RMI.", e);
+                RmiRegistrationSupport.this.log(LogService.LOG_ERROR,
+                    "Unable to bind repository via RMI.", e);
             }
         }
 
@@ -231,10 +265,14 @@
             // unregister repository
             if (this.rmiName != null) {
                 try {
-                    RmiRegistrationSupport.this.getPrivateRegistry().unbind(this.rmiName);
-                    RmiRegistrationSupport.this.log(LogService.LOG_INFO, "Repository unbound from " + this.getRmiURL(), null);
+                    RmiRegistrationSupport.this.getPrivateRegistry().unbind(
+                        this.rmiName);
+                    RmiRegistrationSupport.this.log(LogService.LOG_INFO,
+                        "Repository unbound from " + this.getRmiURL(), null);
                 } catch (Exception e) {
-                    RmiRegistrationSupport.this.log(LogService.LOG_ERROR, "Error while unbinding repository from JNDI: " + e, null);
+                    RmiRegistrationSupport.this.log(LogService.LOG_ERROR,
+                        "Error while unbinding repository from JNDI: " + e,
+                        null);
                 }
             }
 
@@ -244,7 +282,8 @@
                     UnicastRemoteObject.unexportObject(this.rmiRepository, true);
                 } catch (NoSuchObjectException nsoe) {
                     // not expected, but don't really care either
-                    RmiRegistrationSupport.this.log(LogService.LOG_INFO, "Cannot unexport remote Repository reference", nsoe);
+                    RmiRegistrationSupport.this.log(LogService.LOG_INFO,
+                        "Cannot unexport remote Repository reference", nsoe);
                 }
             }
         }