From: Mathieu Baudier Date: Wed, 28 Mar 2012 16:13:40 +0000 (+0000) Subject: Login or create workspace X-Git-Tag: argeo-commons-2.1.30~948 X-Git-Url: http://git.argeo.org/?a=commitdiff_plain;h=43179ae5b61ba2dad93b2f74241f0b09572cee54;p=lgpl%2Fargeo-commons.git Login or create workspace git-svn-id: https://svn.argeo.org/commons/trunk@5264 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- 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 {