You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by ju...@apache.org on 2006/03/28 19:33:40 UTC

svn commit: r389553 - /jackrabbit/branches/1.0/jackrabbit/src/main/java/org/apache/jackrabbit/core/TransientRepository.java

Author: jukka
Date: Tue Mar 28 09:33:38 2006
New Revision: 389553

URL: http://svn.apache.org/viewcvs?rev=389553&view=rev
Log:
1.0: Merged revision 386114: JCR-353 TransientRepository does not shutdown if first login fails

Modified:
    jackrabbit/branches/1.0/jackrabbit/src/main/java/org/apache/jackrabbit/core/TransientRepository.java

Modified: jackrabbit/branches/1.0/jackrabbit/src/main/java/org/apache/jackrabbit/core/TransientRepository.java
URL: http://svn.apache.org/viewcvs/jackrabbit/branches/1.0/jackrabbit/src/main/java/org/apache/jackrabbit/core/TransientRepository.java?rev=389553&r1=389552&r2=389553&view=diff
==============================================================================
--- jackrabbit/branches/1.0/jackrabbit/src/main/java/org/apache/jackrabbit/core/TransientRepository.java (original)
+++ jackrabbit/branches/1.0/jackrabbit/src/main/java/org/apache/jackrabbit/core/TransientRepository.java Tue Mar 28 09:33:38 2006
@@ -43,7 +43,7 @@
  * when no longer used, this class can be used to avoid having to explicitly
  * shut down the repository.
  */
-public class TransientRepository implements Repository {
+public class TransientRepository implements Repository, SessionListener {
 
     /**
      * The logger instance used to log the repository and session lifecycles.
@@ -59,8 +59,7 @@
     /**
      * Resource path of the default repository configuration file.
      */
-    private static final String DEFAULT_REPOSITORY_XML =
-        "repository.xml";
+    private static final String DEFAULT_REPOSITORY_XML = "repository.xml";
 
     /**
      * Name of the repository configuration file property.
@@ -252,6 +251,31 @@
     }
 
     /**
+     * Starts the underlying repository.
+     *
+     * @throws RepositoryException if the repository cannot be started
+     */
+    private synchronized void startRepository() throws RepositoryException {
+        assert repository == null && sessions.isEmpty();
+        logger.debug("Initializing transient repository");
+        repository = factory.getRepository();
+        logger.info("Transient repository initialized");
+    }
+
+    /**
+     * Stops the underlying repository.
+     */
+    private synchronized void stopRepository() {
+        assert repository != null && sessions.isEmpty();
+        logger.debug("Shutting down transient repository");
+        repository.shutdown();
+        logger.info("Transient repository shut down");
+        repository = null;
+    }
+
+    //------------------------------------------------------------<Repository>
+
+    /**
      * Returns the available descriptor keys. If the underlying repository
      * is initialized, then the call is proxied to it, otherwise the static
      * descriptor keys are returned.
@@ -287,29 +311,6 @@
     }
 
     /**
-     * Removes the given session from the set of open sessions. If no open
-     * sessions remain, then the underlying repository instance is shut down.
-     *
-     * @param session closed session
-     */
-    private synchronized void removeSession(SessionImpl session) {
-        sessions.remove(session);
-        logger.info("Session closed");
-        if (sessions.isEmpty()) {
-            // FIXME: This is an ugly hack to avoid an infinite loop when
-            // RepositoryImpl.shutdown() repeatedly calls logout() on all
-            // remaining active sessions including the one that just emitted
-            // the loggedOut() message to us!
-            repository.loggedOut(session);
-
-            logger.debug("Shutting down transient repository");
-            repository.shutdown();
-            logger.info("Transient repository shut down");
-            repository = null;
-        }
-    }
-
-    /**
      * Logs in to the content repository. Initializes the underlying repository
      * instance if needed. The opened session is added to the set of open
      * sessions and a session listener is added to track when the session gets
@@ -323,29 +324,26 @@
      */
     public synchronized Session login(Credentials credentials, String workspaceName)
             throws RepositoryException {
-        if (repository == null) {
-            logger.debug("Initializing transient repository");
-            repository = factory.getRepository();
-            logger.info("Transient repository initialized");
+        // Start the repository if this is the first login
+        if (sessions.isEmpty()) {
+            startRepository();
         }
 
-        logger.debug("Opening a new session");
-        SessionImpl session = (SessionImpl)
-            repository.login(credentials, workspaceName);
-        sessions.add(session);
-        session.addListener(new SessionListener() {
-            
-            public void loggedOut(SessionImpl session) {
-                removeSession(session);
-            }
-            
-            public void loggingOut(SessionImpl session) {
-            }
-            
-        });
-        logger.info("Session opened");
+        try {
+            logger.debug("Opening a new session");
+            Session session = repository.login(credentials, workspaceName);
+            sessions.add(session);
+            ((SessionImpl) session).addListener(this);
+            logger.info("Session opened");
 
-        return session;
+            return session;
+        } finally {
+            // Stop the repository if the login failed
+            // and no other sessions are active
+            if (sessions.isEmpty()) {
+                stopRepository();
+            }
+        }
     }
 
     /**
@@ -384,6 +382,36 @@
      */
     public Session login() throws RepositoryException {
         return login(null, null);
+    }
+
+    //-------------------------------------------------------<SessionListener>
+
+    /**
+     * Removes the given session from the set of open sessions. If no open
+     * sessions remain, then the underlying repository instance is shut down.
+     *
+     * @param session closed session
+     * @see SessionListener#loggedOut(SessionImpl)
+     */
+    public synchronized void loggedOut(SessionImpl session) {
+        assert sessions.contains(session);
+        sessions.remove(session);
+        logger.info("Session closed");
+        if (sessions.isEmpty()) {
+            // FIXME: This is an ugly hack to avoid an infinite loop when
+            // RepositoryImpl.shutdown() repeatedly calls logout() on all
+            // remaining active sessions including the one that just emitted
+            // the loggedOut() message to us!
+            repository.loggedOut(session);
+            
+            stopRepository();
+        }
+    }
+
+    /**
+     * Ignored. {@inheritDoc}
+     */
+    public void loggingOut(SessionImpl session) {
     }
 
 }