From 43179ae5b61ba2dad93b2f74241f0b09572cee54 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Wed, 28 Mar 2012 16:13:40 +0000 Subject: [PATCH] Login or create workspace git-svn-id: https://svn.argeo.org/commons/trunk@5264 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../src/main/java/org/argeo/jcr/JcrUtils.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java index e2f5b5427..b32e4404e 100644 --- a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java +++ b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java @@ -36,6 +36,7 @@ import java.util.TreeMap; import javax.jcr.Binary; import javax.jcr.NamespaceRegistry; +import javax.jcr.NoSuchWorkspaceException; import javax.jcr.Node; import javax.jcr.NodeIterator; import javax.jcr.Property; @@ -966,6 +967,29 @@ public class JcrUtils implements ArgeoJcrConstants { } } + /** + * Login to a workspace with implicit credentials, creates the workspace + * with these credentials if it does not already exist. + */ + public static Session loginOrCreateWorkspace(Repository repository, + String workspaceName) throws RepositoryException { + Session workspaceSession = null; + Session defaultSession = null; + try { + try { + workspaceSession = repository.login(workspaceName); + } catch (NoSuchWorkspaceException e) { + // try to create workspace + defaultSession = repository.login(); + defaultSession.getWorkspace().createWorkspace(workspaceName); + workspaceSession = repository.login(workspaceName); + } + return workspaceSession; + } finally { + logoutQuietly(defaultSession); + } + } + /** Logs out the session, not throwing any exception, even if it is null. */ public static void logoutQuietly(Session session) { try { -- 2.30.2