Improve JCR
authorMathieu Baudier <mbaudier@argeo.org>
Thu, 17 Feb 2011 20:21:28 +0000 (20:21 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Thu, 17 Feb 2011 20:21:28 +0000 (20:21 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@4155 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/server/jackrabbit/JackrabbitContainer.java
server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/ArgeoJcrConstants.java [deleted file]
server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java
server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/ThreadBoundJcrSessionFactory.java

index 87738a5cdc5dbb2b589652b502f6988136561488..1be03827a914aba67432d86df4d7a63729ca096f 100644 (file)
@@ -158,7 +158,12 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
        public Session login(Credentials credentials, String workspaceName)
                        throws LoginException, NoSuchWorkspaceException,
                        RepositoryException {
-               Session session = repository.login(credentials, workspaceName);
+               Session session;
+               try {
+                       session = repository.login(credentials, workspaceName);
+               } catch (NoSuchWorkspaceException e) {
+                       session = createWorkspaceAndLogsIn(credentials, workspaceName);
+               }
                processNewSession(session);
                return session;
        }
@@ -172,7 +177,12 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
 
        public Session login(String workspaceName) throws LoginException,
                        NoSuchWorkspaceException, RepositoryException {
-               Session session = repository.login(workspaceName);
+               Session session;
+               try {
+                       session = repository.login(workspaceName);
+               } catch (NoSuchWorkspaceException e) {
+                       session = createWorkspaceAndLogsIn(null, workspaceName);
+               }
                processNewSession(session);
                return session;
        }
@@ -190,6 +200,20 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
                }
        }
 
+       /**
+        * Logs in to the default workspace, creates the required workspace, logs
+        * out, logs in to the required workspace.
+        */
+       protected Session createWorkspaceAndLogsIn(Credentials credentials,
+                       String workspaceName) throws RepositoryException {
+               if (workspaceName == null)
+                       throw new ArgeoException("No workspace specified.");
+               Session session = repository.login(credentials);
+               session.getWorkspace().createWorkspace(workspaceName);
+               session.logout();
+               return repository.login(credentials, workspaceName);
+       }
+
        public void setResourceLoader(ResourceLoader resourceLoader) {
                this.resourceLoader = resourceLoader;
        }
diff --git a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/ArgeoJcrConstants.java b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/ArgeoJcrConstants.java
deleted file mode 100644 (file)
index 664c2c1..0000000
+++ /dev/null
@@ -1,306 +0,0 @@
-/*
- * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *         http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.argeo.jcr;
-
-/**
- * Copied from org.apache.jackrabbit.JcrConstants v1.6.0 in order to avoid a
- * dependency to Jackrabbit.
- */
-public interface ArgeoJcrConstants {
-       /**
-        * jcr:autoCreated
-        */
-       public static final String JCR_AUTOCREATED = "jcr:autoCreated";
-       /**
-        * jcr:baseVersion
-        */
-       public static final String JCR_BASEVERSION = "jcr:baseVersion";
-       /**
-        * jcr:child
-        */
-       public static final String JCR_CHILD = "jcr:child";
-       /**
-        * jcr:childNodeDefinition
-        */
-       public static final String JCR_CHILDNODEDEFINITION = "jcr:childNodeDefinition";
-       /**
-        * jcr:content
-        */
-       public static final String JCR_CONTENT = "jcr:content";
-       /**
-        * jcr:created
-        */
-       public static final String JCR_CREATED = "jcr:created";
-       /**
-        * jcr:data
-        */
-       public static final String JCR_DATA = "jcr:data";
-       /**
-        * jcr:defaultPrimaryType
-        */
-       public static final String JCR_DEFAULTPRIMARYTYPE = "jcr:defaultPrimaryType";
-       /**
-        * jcr:defaultValues
-        */
-       public static final String JCR_DEFAULTVALUES = "jcr:defaultValues";
-       /**
-        * jcr:encoding
-        */
-       public static final String JCR_ENCODING = "jcr:encoding";
-       /**
-        * jcr:frozenMixinTypes
-        */
-       public static final String JCR_FROZENMIXINTYPES = "jcr:frozenMixinTypes";
-       /**
-        * jcr:frozenNode
-        */
-       public static final String JCR_FROZENNODE = "jcr:frozenNode";
-       /**
-        * jcr:frozenPrimaryType
-        */
-       public static final String JCR_FROZENPRIMARYTYPE = "jcr:frozenPrimaryType";
-       /**
-        * jcr:frozenUuid
-        */
-       public static final String JCR_FROZENUUID = "jcr:frozenUuid";
-       /**
-        * jcr:hasOrderableChildNodes
-        */
-       public static final String JCR_HASORDERABLECHILDNODES = "jcr:hasOrderableChildNodes";
-       /**
-        * jcr:isCheckedOut
-        */
-       public static final String JCR_ISCHECKEDOUT = "jcr:isCheckedOut";
-       /**
-        * jcr:isMixin
-        */
-       public static final String JCR_ISMIXIN = "jcr:isMixin";
-       /**
-        * jcr:language
-        */
-       public static final String JCR_LANGUAGE = "jcr:language";
-       /**
-        * jcr:lastModified
-        */
-       public static final String JCR_LASTMODIFIED = "jcr:lastModified";
-       /**
-        * jcr:lockIsDeep
-        */
-       public static final String JCR_LOCKISDEEP = "jcr:lockIsDeep";
-       /**
-        * jcr:lockOwner
-        */
-       public static final String JCR_LOCKOWNER = "jcr:lockOwner";
-       /**
-        * jcr:mandatory
-        */
-       public static final String JCR_MANDATORY = "jcr:mandatory";
-       /**
-        * jcr:mergeFailed
-        */
-       public static final String JCR_MERGEFAILED = "jcr:mergeFailed";
-       /**
-        * jcr:mimeType
-        */
-       public static final String JCR_MIMETYPE = "jcr:mimeType";
-       /**
-        * jcr:mixinTypes
-        */
-       public static final String JCR_MIXINTYPES = "jcr:mixinTypes";
-       /**
-        * jcr:multiple
-        */
-       public static final String JCR_MULTIPLE = "jcr:multiple";
-       /**
-        * jcr:name
-        */
-       public static final String JCR_NAME = "jcr:name";
-       /**
-        * jcr:nodeTypeName
-        */
-       public static final String JCR_NODETYPENAME = "jcr:nodeTypeName";
-       /**
-        * jcr:onParentVersion
-        */
-       public static final String JCR_ONPARENTVERSION = "jcr:onParentVersion";
-       /**
-        * jcr:predecessors
-        */
-       public static final String JCR_PREDECESSORS = "jcr:predecessors";
-       /**
-        * jcr:primaryItemName
-        */
-       public static final String JCR_PRIMARYITEMNAME = "jcr:primaryItemName";
-       /**
-        * jcr:primaryType
-        */
-       public static final String JCR_PRIMARYTYPE = "jcr:primaryType";
-       /**
-        * jcr:propertyDefinition
-        */
-       public static final String JCR_PROPERTYDEFINITION = "jcr:propertyDefinition";
-       /**
-        * jcr:protected
-        */
-       public static final String JCR_PROTECTED = "jcr:protected";
-       /**
-        * jcr:requiredPrimaryTypes
-        */
-       public static final String JCR_REQUIREDPRIMARYTYPES = "jcr:requiredPrimaryTypes";
-       /**
-        * jcr:requiredType
-        */
-       public static final String JCR_REQUIREDTYPE = "jcr:requiredType";
-       /**
-        * jcr:rootVersion
-        */
-       public static final String JCR_ROOTVERSION = "jcr:rootVersion";
-       /**
-        * jcr:sameNameSiblings
-        */
-       public static final String JCR_SAMENAMESIBLINGS = "jcr:sameNameSiblings";
-       /**
-        * jcr:statement
-        */
-       public static final String JCR_STATEMENT = "jcr:statement";
-       /**
-        * jcr:successors
-        */
-       public static final String JCR_SUCCESSORS = "jcr:successors";
-       /**
-        * jcr:supertypes
-        */
-       public static final String JCR_SUPERTYPES = "jcr:supertypes";
-       /**
-        * jcr:system
-        */
-       public static final String JCR_SYSTEM = "jcr:system";
-       /**
-        * jcr:uuid
-        */
-       public static final String JCR_UUID = "jcr:uuid";
-       /**
-        * jcr:valueConstraints
-        */
-       public static final String JCR_VALUECONSTRAINTS = "jcr:valueConstraints";
-       /**
-        * jcr:versionHistory
-        */
-       public static final String JCR_VERSIONHISTORY = "jcr:versionHistory";
-       /**
-        * jcr:versionLabels
-        */
-       public static final String JCR_VERSIONLABELS = "jcr:versionLabels";
-       /**
-        * jcr:versionStorage
-        */
-       public static final String JCR_VERSIONSTORAGE = "jcr:versionStorage";
-       /**
-        * jcr:versionableUuid
-        */
-       public static final String JCR_VERSIONABLEUUID = "jcr:versionableUuid";
-
-       /**
-        * Pseudo property jcr:path used with query results
-        */
-       public static final String JCR_PATH = "jcr:path";
-       /**
-        * Pseudo property jcr:score used with query results
-        */
-       public static final String JCR_SCORE = "jcr:score";
-
-       /**
-        * mix:lockable
-        */
-       public static final String MIX_LOCKABLE = "mix:lockable";
-       /**
-        * mix:referenceable
-        */
-       public static final String MIX_REFERENCEABLE = "mix:referenceable";
-       /**
-        * mix:versionable
-        */
-       public static final String MIX_VERSIONABLE = "mix:versionable";
-       /**
-        * mix:shareable
-        */
-       public static final String MIX_SHAREABLE = "mix:shareable";
-       /**
-        * nt:base
-        */
-       public static final String NT_BASE = "nt:base";
-       /**
-        * nt:childNodeDefinition
-        */
-       public static final String NT_CHILDNODEDEFINITION = "nt:childNodeDefinition";
-       /**
-        * nt:file
-        */
-       public static final String NT_FILE = "nt:file";
-       /**
-        * nt:folder
-        */
-       public static final String NT_FOLDER = "nt:folder";
-       /**
-        * nt:frozenNode
-        */
-       public static final String NT_FROZENNODE = "nt:frozenNode";
-       /**
-        * nt:hierarchyNode
-        */
-       public static final String NT_HIERARCHYNODE = "nt:hierarchyNode";
-       /**
-        * nt:linkedFile
-        */
-       public static final String NT_LINKEDFILE = "nt:linkedFile";
-       /**
-        * nt:nodeType
-        */
-       public static final String NT_NODETYPE = "nt:nodeType";
-       /**
-        * nt:propertyDefinition
-        */
-       public static final String NT_PROPERTYDEFINITION = "nt:propertyDefinition";
-       /**
-        * nt:query
-        */
-       public static final String NT_QUERY = "nt:query";
-       /**
-        * nt:resource
-        */
-       public static final String NT_RESOURCE = "nt:resource";
-       /**
-        * nt:unstructured
-        */
-       public static final String NT_UNSTRUCTURED = "nt:unstructured";
-       /**
-        * nt:version
-        */
-       public static final String NT_VERSION = "nt:version";
-       /**
-        * nt:versionHistory
-        */
-       public static final String NT_VERSIONHISTORY = "nt:versionHistory";
-       /**
-        * nt:versionLabels
-        */
-       public static final String NT_VERSIONLABELS = "nt:versionLabels";
-       /**
-        * nt:versionedChild
-        */
-       public static final String NT_VERSIONEDCHILD = "nt:versionedChild";
-}
index b862e8fc4da2fec04be3bb76a25b6e015cdb37a5..554e9f34c6e8ff2118699a16cd172760238a9ab4 100644 (file)
@@ -35,6 +35,7 @@ import javax.jcr.PropertyIterator;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.Value;
+import javax.jcr.nodetype.NodeType;
 import javax.jcr.query.Query;
 import javax.jcr.query.QueryResult;
 
@@ -201,7 +202,7 @@ public class JcrUtils {
                                        else
                                                currentNode = currentNode.addNode(part);
                                        if (versioning)
-                                               currentNode.addMixin(ArgeoJcrConstants.MIX_VERSIONABLE);
+                                               currentNode.addMixin(NodeType.MIX_VERSIONABLE);
                                        if (log.isTraceEnabled())
                                                log.debug("Added folder " + part + " as " + current);
                                } else {
@@ -262,7 +263,7 @@ public class JcrUtils {
                        // First output the node path
                        log.debug(node.getPath());
                        // Skip the virtual (and large!) jcr:system subtree
-                       if (node.getName().equals(ArgeoJcrConstants.JCR_SYSTEM)) {
+                       if (node.getName().equals("jcr:system")) {
                                return;
                        }
 
index 01a056aa29432d661d157e885ff1de485e9f228f..aaafdc26a704f2781c11164c07aec708a3a00769 100644 (file)
@@ -42,12 +42,14 @@ public class ThreadBoundJcrSessionFactory implements FactoryBean,
                        .getLog(ThreadBoundJcrSessionFactory.class);
 
        private Repository repository;
-       private List<Session> activeSessions = Collections
+       private final List<Session> activeSessions = Collections
                        .synchronizedList(new ArrayList<Session>());
 
        private ThreadLocal<Session> session = new ThreadLocal<Session>();
        private boolean destroying = false;
        private final Session proxiedSession;
+       /** If workspace is null, default will be used. */
+       private String workspace = null;
 
        private String defaultUsername = "demo";
        private String defaultPassword = "demo";
@@ -56,33 +58,11 @@ public class ThreadBoundJcrSessionFactory implements FactoryBean,
        public ThreadBoundJcrSessionFactory() {
                Class<?>[] interfaces = { Session.class };
                proxiedSession = (Session) Proxy.newProxyInstance(getClass()
-                               .getClassLoader(), interfaces, new InvocationHandler() {
-
-                       public Object invoke(Object proxy, Method method, Object[] args)
-                                       throws Throwable {
-                               Session threadSession = session.get();
-                               if (threadSession == null) {
-                                       if ("logout".equals(method.getName()))// no need to login
-                                               return Void.TYPE;
-                                       threadSession = login();
-                                       session.set(threadSession);
-                               }
-
-                               Object ret = method.invoke(threadSession, args);
-                               if ("logout".equals(method.getName())) {
-                                       session.remove();
-                                       if (!destroying)
-                                               activeSessions.remove(threadSession);
-                                       if (log.isTraceEnabled())
-                                               log.trace("Logged out from JCR session "
-                                                               + threadSession + "; userId="
-                                                               + threadSession.getUserID());
-                               }
-                               return ret;
-                       }
-               });
+                               .getClassLoader(), interfaces,
+                               new JcrSessionInvocationHandler());
        }
 
+       /** Logs in to the repository using various strategies. */
        protected Session login() {
                Session newSession = null;
                // first try to login without credentials, assuming the underlying login
@@ -90,7 +70,7 @@ public class ThreadBoundJcrSessionFactory implements FactoryBean,
                // Security)
                if (!forceDefaultCredentials)
                        try {
-                               newSession = repository.login();
+                               newSession = repository.login(workspace);
                        } catch (LoginException e1) {
                                log.warn("Cannot login without credentials: " + e1.getMessage());
                                // invalid credentials, go to the next step
@@ -104,7 +84,7 @@ public class ThreadBoundJcrSessionFactory implements FactoryBean,
                        try {
                                SimpleCredentials sc = new SimpleCredentials(defaultUsername,
                                                defaultPassword.toCharArray());
-                               newSession = repository.login(sc);
+                               newSession = repository.login(sc, workspace);
                        } catch (RepositoryException e) {
                                throw new ArgeoException("Cannot log in to repository", e);
                        }
@@ -157,4 +137,34 @@ public class ThreadBoundJcrSessionFactory implements FactoryBean,
                this.forceDefaultCredentials = forceDefaultCredentials;
        }
 
+       public void setWorkspace(String workspace) {
+               this.workspace = workspace;
+       }
+
+       protected class JcrSessionInvocationHandler implements InvocationHandler {
+
+               public Object invoke(Object proxy, Method method, Object[] args)
+                               throws Throwable {
+                       Session threadSession = session.get();
+                       if (threadSession == null) {
+                               if ("logout".equals(method.getName()))// no need to login
+                                       return Void.TYPE;
+                               else if ("toString".equals(method.getName()))// maybe logging
+                                       return "Uninitialized Argeo thread bound JCR session";
+                               threadSession = login();
+                               session.set(threadSession);
+                       }
+
+                       Object ret = method.invoke(threadSession, args);
+                       if ("logout".equals(method.getName())) {
+                               session.remove();
+                               if (!destroying)
+                                       activeSessions.remove(threadSession);
+                               if (log.isTraceEnabled())
+                                       log.trace("Logged out from JCR session " + threadSession
+                                                       + "; userId=" + threadSession.getUserID());
+                       }
+                       return ret;
+               }
+       }
 }