Author: ate
Date: Mon Aug 27 12:13:46 2012
New Revision: 1377650

URL: http://svn.apache.org/viewvc?rev=1377650&view=rev
Log:
RAVE-603: System bootstrap for jcr content services
- make sure to properly cleanup and logout jcr sessions after bootstrap

Modified:
    
rave/sandbox/content-services/rave-jcr-config/src/main/java/org/apache/rave/jcr/bootstrapping/Bootstrap.java
    
rave/sandbox/content-services/rave-jcr-config/src/main/java/org/apache/rave/jcr/bootstrapping/ModuleImporter.java
    
rave/sandbox/content-services/rave-jcr-config/src/main/java/org/apache/rave/jcr/bootstrapping/SessionProvider.java

Modified: 
rave/sandbox/content-services/rave-jcr-config/src/main/java/org/apache/rave/jcr/bootstrapping/Bootstrap.java
URL: 
http://svn.apache.org/viewvc/rave/sandbox/content-services/rave-jcr-config/src/main/java/org/apache/rave/jcr/bootstrapping/Bootstrap.java?rev=1377650&r1=1377649&r2=1377650&view=diff
==============================================================================
--- 
rave/sandbox/content-services/rave-jcr-config/src/main/java/org/apache/rave/jcr/bootstrapping/Bootstrap.java
 (original)
+++ 
rave/sandbox/content-services/rave-jcr-config/src/main/java/org/apache/rave/jcr/bootstrapping/Bootstrap.java
 Mon Aug 27 12:13:46 2012
@@ -70,8 +70,9 @@ public class Bootstrap implements LazyRe
     public synchronized void repositoryAvailable(final Object factoryKey, 
final Repository repository) {
         if (!bootstrapped) {
             bootstrapped = true;
+            Session session = null;
             try {
-                Session session = getSession(repository, credentials, 
workspaceName);
+                session = getSession(repository, credentials, workspaceName);
                 initializeSystem(session, namespacePrefix, namespaceUri);
                 updateModules(session, namespacePrefix);
                 importModules(servletContext, repository, session, 
credentials, reload, namespacePrefix);
@@ -79,6 +80,16 @@ public class Bootstrap implements LazyRe
             catch (Exception e) {
                 log.error("Failure bootstrapping", e);
             }
+            finally {
+                try {
+                    if (session != null && session.isLive()) {
+                        session.logout();
+                    }
+                }
+                catch (Exception e) {
+                    log.warn("Failed to properly logout session", e);
+                }
+            }
         }
     }
 
@@ -145,6 +156,17 @@ public class Bootstrap implements LazyRe
                 public Session getSession(String workspaceName) throws 
RepositoryException {
                     return repository.login(credentials, workspaceName);
                 }
+                @Override
+                public void returnSession(Session session) {
+                    try {
+                        if (session != null && session.isLive()) {
+                            session.logout();
+                        }
+                    }
+                    catch (Exception e) {
+                        log.warn("Failed to properly logout session", e);
+                    }
+                }
             }, moduleRegistry, moduleMap.values(), reload, new 
MimeTypeResolver() {
                 @Override
                 public String getMimeType(final String file) {

Modified: 
rave/sandbox/content-services/rave-jcr-config/src/main/java/org/apache/rave/jcr/bootstrapping/ModuleImporter.java
URL: 
http://svn.apache.org/viewvc/rave/sandbox/content-services/rave-jcr-config/src/main/java/org/apache/rave/jcr/bootstrapping/ModuleImporter.java?rev=1377650&r1=1377649&r2=1377650&view=diff
==============================================================================
--- 
rave/sandbox/content-services/rave-jcr-config/src/main/java/org/apache/rave/jcr/bootstrapping/ModuleImporter.java
 (original)
+++ 
rave/sandbox/content-services/rave-jcr-config/src/main/java/org/apache/rave/jcr/bootstrapping/ModuleImporter.java
 Mon Aug 27 12:13:46 2012
@@ -184,34 +184,39 @@ public final class ModuleImporter {
             return;
         }
         final Session session = sessionProvider.getSession(null);
-        final NamespaceRegistry namespaceRegistry = 
session.getWorkspace().getNamespaceRegistry();
-        for (Module.Namespace namespace : namespaces) {
-            final Module module = namespace.getModule();
-            log.info("Registering namespace " + namespace.getPrefix() + " = " 
+ namespace.getUri() + " in module " + module.getName());
-            try {
-                final String existingUri = 
namespaceRegistry.getURI(namespace.getPrefix());
-                log.warn("Prefix '" + namespace.getPrefix() + "' is already 
mapped to uri " + existingUri);
-                if (!existingUri.equals(namespace.getUri())) {
-                    updateItemStatus(namespace, Module.Status.FAILED);
-                } else {
-                    updateItemStatus(namespace, Module.Status.DONE);
-                }
-            } catch (NamespaceException expected) {
+        try {
+            final NamespaceRegistry namespaceRegistry = 
session.getWorkspace().getNamespaceRegistry();
+            for (Module.Namespace namespace : namespaces) {
+                final Module module = namespace.getModule();
+                log.info("Registering namespace " + namespace.getPrefix() + " 
= " + namespace.getUri() + " in module " + module.getName());
                 try {
-                    namespaceRegistry.registerNamespace(namespace.getPrefix(), 
namespace.getUri());
-                    updateItemStatus(namespace, Module.Status.DONE);
-                } catch (NamespaceException e) {
-                    log.error("Failed to register namespace " + 
namespace.getPrefix() + "=" + namespace.getUri(), e);
-                    updateItemStatus(namespace, Module.Status.FAILED);
+                    final String existingUri = 
namespaceRegistry.getURI(namespace.getPrefix());
+                    log.warn("Prefix '" + namespace.getPrefix() + "' is 
already mapped to uri " + existingUri);
+                    if (!existingUri.equals(namespace.getUri())) {
+                        updateItemStatus(namespace, Module.Status.FAILED);
+                    } else {
+                        updateItemStatus(namespace, Module.Status.DONE);
+                    }
+                } catch (NamespaceException expected) {
+                    try {
+                        
namespaceRegistry.registerNamespace(namespace.getPrefix(), namespace.getUri());
+                        updateItemStatus(namespace, Module.Status.DONE);
+                    } catch (NamespaceException e) {
+                        log.error("Failed to register namespace " + 
namespace.getPrefix() + "=" + namespace.getUri(), e);
+                        updateItemStatus(namespace, Module.Status.FAILED);
+                    } catch (RepositoryException e) {
+                        log.error("Failed to register namespace " + 
namespace.getPrefix() + "=" + namespace.getUri(), e);
+                        updateItemStatus(namespace, Module.Status.FAILED);
+                    }
                 } catch (RepositoryException e) {
                     log.error("Failed to register namespace " + 
namespace.getPrefix() + "=" + namespace.getUri(), e);
                     updateItemStatus(namespace, Module.Status.FAILED);
                 }
-            } catch (RepositoryException e) {
-                log.error("Failed to register namespace " + 
namespace.getPrefix() + "=" + namespace.getUri(), e);
-                updateItemStatus(namespace, Module.Status.FAILED);
             }
         }
+        finally {
+            sessionProvider.returnSession(session);
+        }
     }
 
     private void registerNodeTypes() throws RepositoryException {
@@ -220,24 +225,29 @@ public final class ModuleImporter {
             return;
         }
         final Session session = sessionProvider.getSession(null);
-        for (Module.Cnd cnd : cnds) {
-            final Module module = cnd.getModule();
-            final boolean reregister = cnd.getStatus() == Module.Status.RELOAD;
-            log.info((reregister ? "Rer" : "R") + "egistering node types from 
" + cnd.getFile() + " in module " + module.getName());
-            try {
-                CndImporter.registerNodeTypes(new 
InputStreamReader(cnd.getURL().openStream()), session, reregister);
-                updateItemStatus(cnd, Module.Status.DONE);
-            } catch (ParseException e) {
-                log.error("Failed to parse cnd " + cnd.getFile() + " in module 
" + module.getName(), e);
-                updateItemStatus(cnd, Module.Status.FAILED);
-            } catch (IOException e) {
-                log.error("Failed to read cnd " + cnd.getFile() + " in module 
" + module.getName(), e);
-                updateItemStatus(cnd, Module.Status.FAILED);
-            } catch (RepositoryException e) {
-                log.error("Failed to import cnd " + cnd.getFile() + " in 
module " + module.getName(), e);
-                updateItemStatus(cnd, Module.Status.FAILED);
+        try {
+            for (Module.Cnd cnd : cnds) {
+                final Module module = cnd.getModule();
+                final boolean reregister = cnd.getStatus() == 
Module.Status.RELOAD;
+                log.info((reregister ? "Rer" : "R") + "egistering node types 
from " + cnd.getFile() + " in module " + module.getName());
+                try {
+                    CndImporter.registerNodeTypes(new 
InputStreamReader(cnd.getURL().openStream()), session, reregister);
+                    updateItemStatus(cnd, Module.Status.DONE);
+                } catch (ParseException e) {
+                    log.error("Failed to parse cnd " + cnd.getFile() + " in 
module " + module.getName(), e);
+                    updateItemStatus(cnd, Module.Status.FAILED);
+                } catch (IOException e) {
+                    log.error("Failed to read cnd " + cnd.getFile() + " in 
module " + module.getName(), e);
+                    updateItemStatus(cnd, Module.Status.FAILED);
+                } catch (RepositoryException e) {
+                    log.error("Failed to import cnd " + cnd.getFile() + " in 
module " + module.getName(), e);
+                    updateItemStatus(cnd, Module.Status.FAILED);
+                }
             }
         }
+        finally {
+            sessionProvider.returnSession(session);
+        }
     }
 
     private void importContent() throws RepositoryException {
@@ -249,33 +259,38 @@ public final class ModuleImporter {
             final Module module = content.getModule();
             final String workspace = content.getWorkspace() == null ? 
module.getWorkspace() : content.getWorkspace();
             final Session session = getSession(workspace);
-            final ContentImporter contentImporter = new 
ContentImporter(session);
-            if (!session.nodeExists(content.getParent())) {
-                log.error("Cannot import content from " + content.getFile() + 
" in module " + module.getName()
-                        + ": parent " + content.getParent() + " does not 
exist");
-                updateItemStatus(content, Module.Status.FAILED);
-                continue;
-            }
-            InputStream is = null;
             try {
-                is = content.getURL().openStream();
-                log.info("Importing content from " + content.getFile() + " in 
module " + module.getName());
-                contentImporter.importContent(content.getParent(), 
content.getName(), is, content.getImportBehavior());
-                session.save();
-                updateItemStatus(content, Module.Status.DONE);
-            } catch (IOException e) {
-                log.error("Failed to read content " + content.getFile() + " in 
module " + module.getName(), e);
-                updateItemStatus(content, Module.Status.FAILED);
-            } catch (ImportException e) {
-                log.error("Failed to import content " + content.getFile() + " 
from module " + module.getName() + ": " + e.getMessage());
-                updateItemStatus(content, Module.Status.FAILED);
-            } catch (RepositoryException e) {
-                log.error("Failed to import content " + content.getFile() + " 
from module " + module.getName(), e);
-                updateItemStatus(content, Module.Status.FAILED);
-            } finally {
-                if (is != null) {
-                    try { is.close(); } catch (IOException ignore) {}
+                final ContentImporter contentImporter = new 
ContentImporter(session);
+                if (!session.nodeExists(content.getParent())) {
+                    log.error("Cannot import content from " + 
content.getFile() + " in module " + module.getName()
+                            + ": parent " + content.getParent() + " does not 
exist");
+                    updateItemStatus(content, Module.Status.FAILED);
+                    continue;
                 }
+                InputStream is = null;
+                try {
+                    is = content.getURL().openStream();
+                    log.info("Importing content from " + content.getFile() + " 
in module " + module.getName());
+                    contentImporter.importContent(content.getParent(), 
content.getName(), is, content.getImportBehavior());
+                    session.save();
+                    updateItemStatus(content, Module.Status.DONE);
+                } catch (IOException e) {
+                    log.error("Failed to read content " + content.getFile() + 
" in module " + module.getName(), e);
+                    updateItemStatus(content, Module.Status.FAILED);
+                } catch (ImportException e) {
+                    log.error("Failed to import content " + content.getFile() 
+ " from module " + module.getName() + ": " + e.getMessage());
+                    updateItemStatus(content, Module.Status.FAILED);
+                } catch (RepositoryException e) {
+                    log.error("Failed to import content " + content.getFile() 
+ " from module " + module.getName(), e);
+                    updateItemStatus(content, Module.Status.FAILED);
+                } finally {
+                    if (is != null) {
+                        try { is.close(); } catch (IOException ignore) {}
+                    }
+                }
+            }
+            finally {
+                sessionProvider.returnSession(session);
             }
         }
     }
@@ -289,34 +304,39 @@ public final class ModuleImporter {
             final Module module = resource.getModule();
             final String workspace = resource.getWorkspace() == null ? 
module.getWorkspace() : resource.getWorkspace();
             final Session session = getSession(workspace);
-            final ResourceImporter resourceImporter = new 
ResourceImporter(session, mimeTypeResolver);
-            if (!session.nodeExists(resource.getParent())) {
-                log.error("Cannot import resources " + resource.getName() + " 
in module " + module.getName() +
-                        ": parent " + resource.getParent() + " does not 
exist");
-                updateItemStatus(resource, Module.Status.FAILED);
-                continue;
-            }
-            InputStream is = null;
             try {
-                final URL jarURL = resource.getJarURL();
-                if (jarURL == null) {
-                    log.error("Module does not seem to be packaged as a jar 
but resources can only be imported from jar file");
+                final ResourceImporter resourceImporter = new 
ResourceImporter(session, mimeTypeResolver);
+                if (!session.nodeExists(resource.getParent())) {
+                    log.error("Cannot import resources " + resource.getName() 
+ " in module " + module.getName() +
+                            ": parent " + resource.getParent() + " does not 
exist");
+                    updateItemStatus(resource, Module.Status.FAILED);
                     continue;
                 }
-                is = jarURL.openStream();
-                log.info("Importing resources " + resource.getName() + " in 
module " + module.getName());
-                resourceImporter.importResource(resource.getParent(), 
resource.getPath(), is, resource.getImportBehavior());
-                session.save();
-                updateItemStatus(resource, Module.Status.DONE);
-            } catch (IOException e) {
-                log.error("Failed to read resources in module " + 
module.getName(), e);
-            } catch (ImportException e) {
-                log.error("Failed import resources from module " + 
module.getName() + ": " + e.getMessage());
-            } finally {
-                if (is != null) {
-                    try { is.close(); } catch (IOException ignore) {}
+                InputStream is = null;
+                try {
+                    final URL jarURL = resource.getJarURL();
+                    if (jarURL == null) {
+                        log.error("Module does not seem to be packaged as a 
jar but resources can only be imported from jar file");
+                        continue;
+                    }
+                    is = jarURL.openStream();
+                    log.info("Importing resources " + resource.getName() + " 
in module " + module.getName());
+                    resourceImporter.importResource(resource.getParent(), 
resource.getPath(), is, resource.getImportBehavior());
+                    session.save();
+                    updateItemStatus(resource, Module.Status.DONE);
+                } catch (IOException e) {
+                    log.error("Failed to read resources in module " + 
module.getName(), e);
+                } catch (ImportException e) {
+                    log.error("Failed import resources from module " + 
module.getName() + ": " + e.getMessage());
+                } finally {
+                    if (is != null) {
+                        try { is.close(); } catch (IOException ignore) {}
+                    }
                 }
             }
+            finally {
+                sessionProvider.returnSession(session);
+            }
         }
     }
 

Modified: 
rave/sandbox/content-services/rave-jcr-config/src/main/java/org/apache/rave/jcr/bootstrapping/SessionProvider.java
URL: 
http://svn.apache.org/viewvc/rave/sandbox/content-services/rave-jcr-config/src/main/java/org/apache/rave/jcr/bootstrapping/SessionProvider.java?rev=1377650&r1=1377649&r2=1377650&view=diff
==============================================================================
--- 
rave/sandbox/content-services/rave-jcr-config/src/main/java/org/apache/rave/jcr/bootstrapping/SessionProvider.java
 (original)
+++ 
rave/sandbox/content-services/rave-jcr-config/src/main/java/org/apache/rave/jcr/bootstrapping/SessionProvider.java
 Mon Aug 27 12:13:46 2012
@@ -28,5 +28,5 @@ import javax.jcr.Session;
 public interface SessionProvider {
 
     Session getSession(String workspaceName) throws RepositoryException, 
NoSuchWorkspaceException;
-
+    void    returnSession(Session session);
 }


Reply via email to