jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1152237 - in /jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel: NodeTypeStore.java RepositoryServiceImpl.java Spi2MicrokernelRepositoryServiceFactory.java util/Maps.java util/Paths.java
Date Fri, 29 Jul 2011 14:29:32 GMT
Author: mduerig
Date: Fri Jul 29 14:29:31 2011
New Revision: 1152237

URL: http://svn.apache.org/viewvc?rev=1152237&view=rev
Log:
spi2microkernel (WIP)
- make workspace names configurable

Added:
    jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Maps.java
Modified:
    jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/NodeTypeStore.java
    jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/RepositoryServiceImpl.java
    jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/Spi2MicrokernelRepositoryServiceFactory.java
    jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Paths.java

Modified: jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/NodeTypeStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/NodeTypeStore.java?rev=1152237&r1=1152236&r2=1152237&view=diff
==============================================================================
--- jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/NodeTypeStore.java
(original)
+++ jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/NodeTypeStore.java
Fri Jul 29 14:29:31 2011
@@ -29,7 +29,6 @@ import org.apache.jackrabbit.spi.commons
 import org.apache.jackrabbit.spi.commons.nodetype.NodeTypeStorage;
 import org.apache.jackrabbit.spi.commons.nodetype.QDefinitionBuilderFactory;
 import org.apache.jackrabbit.spi.commons.nodetype.compact.CompactNodeTypeDefWriter;
-import org.apache.jackrabbit.spi2microkernel.util.Paths;
 import org.json.simple.JSONObject;
 import org.json.simple.parser.JSONParser;
 import org.slf4j.Logger;
@@ -48,33 +47,34 @@ public class NodeTypeStore {
     private static final Logger log = LoggerFactory.getLogger(NodeTypeStore.class);
     private static final String DEFAULT_NODETYPES = "default-nodetypes.cnd";
     private static final String NT_DEFINITION_NAME = "nodeTypeDefinitions";
-    private static final String NT_DEFINITION_PATH = '/' + Paths.SYSTEM_WORKSPACE + '/' +
NT_DEFINITION_NAME;
 
     private final MicroKernel microKernel;
+    private final String systemWorkspace;
 
-    private NodeTypeStore(MicroKernel microKernel) {
+    private NodeTypeStore(MicroKernel microKernel, String systemWorkspace) {
         this.microKernel = microKernel;
+        this.systemWorkspace = systemWorkspace;
     }
 
-    public static NodeTypeStore create(MicroKernel microKernel)
+    public static NodeTypeStore create(MicroKernel microKernel, String systemWorkspace)
             throws RepositoryException {
 
-        if (readNodeTypes(microKernel) == null) {
+        if (readNodeTypes(microKernel, systemWorkspace) == null) {
             try {
                 String cnd = IOUtils.toString(getDefaultNodeTypes());
-                writeNodeTypes(microKernel, cnd);
+                writeNodeTypes(microKernel, cnd, systemWorkspace);
             }
             catch (IOException e) {
                 throw new RepositoryException(e); 
             }
         }
 
-        return new NodeTypeStore(microKernel);
+        return new NodeTypeStore(microKernel, systemWorkspace);
     }
 
     public void read(NamespaceMapping namespaces, NodeTypeStorage nodeTypeDefinitions) throws
RepositoryException {
         try {
-            Reader nodeTypes = readNodeTypes(microKernel);
+            Reader nodeTypes = readNodeTypes(microKernel, systemWorkspace);
             if (nodeTypes == null) {
                 return;
             }
@@ -108,7 +108,7 @@ public class NodeTypeStore {
             cndWriter.close();
 
             String cnd = writer.toString();
-            writeNodeTypes(microKernel, cnd);
+            writeNodeTypes(microKernel, cnd, systemWorkspace);
         }
         catch (IOException e) {
             throw new RepositoryException("Error persisting node type definitions", e);
@@ -117,7 +117,7 @@ public class NodeTypeStore {
 
     //------------------------------------------< private >---
 
-    private static void writeNodeTypes(MicroKernel microKernel, String cnd) throws RepositoryException
{
+    private static void writeNodeTypes(MicroKernel microKernel, String cnd, String systemWorkspace)
throws RepositoryException {
         try {
             StringBuilder sb = new StringBuilder();
             JsonBuilder.create(sb)
@@ -126,21 +126,22 @@ public class NodeTypeStore {
 
             String rev = microKernel.getHeadRevision();
             StringBuilder jsop = new StringBuilder();
-            if (microKernel.nodeExists(NT_DEFINITION_PATH, rev)) {
+            if (microKernel.nodeExists('/' + systemWorkspace + '/' + NT_DEFINITION_NAME,
rev)) {
                 jsop.append("-\"").append(NT_DEFINITION_NAME).append("\"\r");
             }
             jsop.append("+\"").append(NT_DEFINITION_NAME).append("\":").append(sb);
-            microKernel.commit('/' + Paths.SYSTEM_WORKSPACE, jsop.toString(), rev, "register
default node types");
+            microKernel.commit('/' + systemWorkspace, jsop.toString(), rev, "register default
node types");
         }
         catch (IOException e) {
             throw new RepositoryException("Error persisting node type definitions", e);
         }
     }
 
-    private static Reader readNodeTypes(MicroKernel microKernel) {
+    private static Reader readNodeTypes(MicroKernel microKernel, String systemWorkspace)
{
         String rev = microKernel.getHeadRevision();
-        if (microKernel.nodeExists(NT_DEFINITION_PATH, rev)) {
-            String ntDefinitions = microKernel.getNodes(NT_DEFINITION_PATH, rev);
+        String ntDefinitionPath = '/' + systemWorkspace + '/' + NT_DEFINITION_NAME;
+        if (microKernel.nodeExists(ntDefinitionPath, rev)) {
+            String ntDefinitions = microKernel.getNodes(ntDefinitionPath, rev);
             JSONParser jsonParser = new JSONParser();
             try {
                 Object parseResult = jsonParser.parse(ntDefinitions);

Modified: jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/RepositoryServiceImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/RepositoryServiceImpl.java?rev=1152237&r1=1152236&r2=1152237&view=diff
==============================================================================
--- jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/RepositoryServiceImpl.java
(original)
+++ jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/RepositoryServiceImpl.java
Fri Jul 29 14:29:31 2011
@@ -84,11 +84,14 @@ import static javax.jcr.Repository.*;
 import static org.apache.jackrabbit.spi.commons.name.NameConstants.*;
 import static org.apache.jackrabbit.spi2microkernel.util.Arrays.contains;
 import static org.apache.jackrabbit.spi2microkernel.util.ItemInfos.*;
+import static org.apache.jackrabbit.spi2microkernel.util.Maps.getOrElse;
 import static org.apache.jackrabbit.spi2microkernel.util.Predicates.containsElement;
 import static org.apache.jackrabbit.spi2microkernel.util.Predicates.exists;
 
 public class RepositoryServiceImpl extends AbstractRepositoryService {
     private static final long BINARY_VALUE_INLINE_THRESHOLD = 0x1000;
+    private static final String DEFAULT_DEFAULT_WORKSPACE = "default";
+    private static final String DEFAULT_SYSTEM_WORKSPACE = "system";
 
     static final Logger log = LoggerFactory.getLogger(RepositoryServiceImpl.class);
 
@@ -117,17 +120,24 @@ public class RepositoryServiceImpl exten
     private final MicroKernel microKernel;
     private final Map<SessionInfo, SessionState> sessions = new ConcurrentHashMap<SessionInfo,
SessionState>();
     private final NodeTypeStore nodeTypeStore;
+    private final String defaultWorkspace;
+    private final String systemWorkspace;
 
-    public RepositoryServiceImpl(MicroKernel microKernel) throws RepositoryException {
+    public RepositoryServiceImpl(MicroKernel microKernel, Map<?, ?> parameters) throws
RepositoryException {
         super(DESCRIPTORS, Collections.<String, String>emptyMap(), new QNodeTypeDefinition[0]);
 
         try { 
             this.microKernel = microKernel;  // fixme who disposes the micro kernel?
 
-            String head = microKernel.getHeadRevision();
-            createWorkspace(head, Paths.DEFAULT_WORKSPACE, true);
-            createWorkspace(head, Paths.SYSTEM_WORKSPACE, true);
-            nodeTypeStore = NodeTypeStore.create(microKernel);
+            defaultWorkspace = getWorkspaceName(parameters,
+                    Spi2MicrokernelRepositoryServiceFactory.PARAM_DEFAULT_WORKSPACE, DEFAULT_DEFAULT_WORKSPACE);
+            createWorkspace(microKernel.getHeadRevision(), defaultWorkspace, true);
+
+            systemWorkspace = getWorkspaceName(parameters,
+                    Spi2MicrokernelRepositoryServiceFactory.PARAM_SYSTEM_WORKSPACE, DEFAULT_SYSTEM_WORKSPACE);
+            createWorkspace(microKernel.getHeadRevision(), systemWorkspace, true);
+
+            nodeTypeStore = NodeTypeStore.create(microKernel, systemWorkspace);
             nodeTypeStore.read(namespaces, nodeTypeDefs);
         }
         catch (MicroKernelException e) {
@@ -135,6 +145,13 @@ public class RepositoryServiceImpl exten
         }
     }
 
+    private static String getWorkspaceName(Map<?, ?> parameters, String key, String
defaultName) {
+        Object value = getOrElse(parameters, (Object) key, (Object) defaultName);
+        return value instanceof String
+            ? (String) value
+            : defaultName;
+    }
+
     public ItemInfoCache getItemInfoCache(SessionInfo sessionInfo) throws RepositoryException
{
         return new ItemInfoCacheImpl();
     }
@@ -150,7 +167,7 @@ public class RepositoryServiceImpl exten
     @Override
     protected SessionInfo createSessionInfo(SessionInfo sessionInfo, String workspaceName)
throws RepositoryException {
         if (workspaceName == null) {
-            workspaceName = Paths.DEFAULT_WORKSPACE;
+            workspaceName = defaultWorkspace;
         }
         SessionInfo s = super.createSessionInfo(sessionInfo, workspaceName);
         createSessionState(s);
@@ -163,7 +180,7 @@ public class RepositoryServiceImpl exten
             credentials = new GuestCredentials();
         }
         if (workspaceName == null) {
-            workspaceName = Paths.DEFAULT_WORKSPACE;
+            workspaceName = defaultWorkspace;
         }
         
         SessionInfo s = super.createSessionInfo(credentials, workspaceName);
@@ -222,9 +239,15 @@ public class RepositoryServiceImpl exten
 
     @Override
     protected void checkWorkspace(String workspaceName) throws NoSuchWorkspaceException {
-        String wspName = Paths.getWorkspaceName(workspaceName);
+        if (workspaceName == null || "".equals(workspaceName)) {
+            workspaceName = defaultWorkspace;
+        }
+        else if (workspaceName.contains("/")) {
+            throw new NoSuchWorkspaceException(workspaceName);
+        }
+
         String head = microKernel.getHeadRevision();
-        if (!microKernel.nodeExists('/' + wspName, head)) {
+        if (!microKernel.nodeExists('/' + workspaceName, head)) {
             throw new NoSuchWorkspaceException(workspaceName);
         }
     }
@@ -473,8 +496,7 @@ public class RepositoryServiceImpl exten
 
     private void createWorkspace(String revisionId, String name, boolean ignoreExisting)
throws RepositoryException {
         try {
-            String wspName = Paths.getWorkspaceName(name);
-            if (microKernel.nodeExists('/' + wspName, revisionId)) {
+            if (microKernel.nodeExists('/' + name, revisionId)) {
                 if (ignoreExisting) {
                     return;
                 }
@@ -483,7 +505,7 @@ public class RepositoryServiceImpl exten
                 }
             }
 
-            microKernel.commit("/", "+\"" + wspName + "\" : {}" , revisionId, "create workspace");
+            microKernel.commit("/", "+\"" + name + "\" : {}" , revisionId, "create workspace");
         }
         catch (MicroKernelException e) {
             throw new RepositoryException(e.getMessage(), e);
@@ -496,12 +518,11 @@ public class RepositoryServiceImpl exten
 
     private void deleteWorkspace(String revisionId, String name)  throws RepositoryException
{
         try {
-            String wspName = Paths.getWorkspaceName(name);
-            if (!microKernel.nodeExists('/' + wspName, revisionId)) {
+            if (!microKernel.nodeExists('/' + name, revisionId)) {
                 throw new NoSuchWorkspaceException("No such workspace: " + name);
             }
 
-            microKernel.commit("/", "-\"" + wspName + '\"', revisionId, "delete workspace");
+            microKernel.commit("/", "-\"" + name + '\"', revisionId, "delete workspace");
         }
         catch (MicroKernelException e) {
             throw new RepositoryException(e.getMessage(), e);

Modified: jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/Spi2MicrokernelRepositoryServiceFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/Spi2MicrokernelRepositoryServiceFactory.java?rev=1152237&r1=1152236&r2=1152237&view=diff
==============================================================================
--- jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/Spi2MicrokernelRepositoryServiceFactory.java
(original)
+++ jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/Spi2MicrokernelRepositoryServiceFactory.java
Fri Jul 29 14:29:31 2011
@@ -27,13 +27,16 @@ import javax.jcr.RepositoryException;
 import java.util.Map;
 
 public class Spi2MicrokernelRepositoryServiceFactory implements RepositoryServiceFactory
{
-    public static final String PARAM_URL = "org.apache.jackrabbit.spi2microkernel.url";
+    private static final String PARAM_BASE_NAME = Spi2MicrokernelRepositoryServiceFactory.class.getName();
+    public static final String PARAM_URL = PARAM_BASE_NAME + ".url";
+    public static final String PARAM_DEFAULT_WORKSPACE = PARAM_BASE_NAME + ".default-workspace";
+    public static final String PARAM_SYSTEM_WORKSPACE = PARAM_BASE_NAME + ".system-workspace";
 
     public RepositoryService createRepositoryService(Map<?, ?> parameters) throws RepositoryException
{
         Object u = parameters.get(PARAM_URL);
         String url = u == null ? null : u.toString();
         MicroKernel microKernel = MicroKernelFactory.getInstance(url);
-        return new RepositoryServiceImpl(microKernel);
+        return new RepositoryServiceImpl(microKernel, parameters);
     }
 
 }

Added: jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Maps.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Maps.java?rev=1152237&view=auto
==============================================================================
--- jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Maps.java
(added)
+++ jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Maps.java
Fri Jul 29 14:29:31 2011
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.jackrabbit.spi2microkernel.util;
+
+import java.util.Map;
+
+public class Maps {
+    private Maps() {}
+
+    public static <K, V> V getOrElse(Map<? extends K, ? extends V> map, K key,
V defaultValue) {
+        V value = map.get(key);
+        return value == null
+            ? defaultValue
+            : value;
+    }
+}

Modified: jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Paths.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Paths.java?rev=1152237&r1=1152236&r2=1152237&view=diff
==============================================================================
--- jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Paths.java
(original)
+++ jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Paths.java
Fri Jul 29 14:29:31 2011
@@ -30,16 +30,12 @@ import org.apache.jackrabbit.spi.PathFac
 import org.apache.jackrabbit.spi.commons.name.NameFactoryImpl;
 import org.apache.jackrabbit.spi.commons.name.PathFactoryImpl;
 
-import javax.jcr.NoSuchWorkspaceException;
 import javax.jcr.RepositoryException;
 import java.util.LinkedList;
 
 import static org.apache.jackrabbit.spi.Name.NS_DEFAULT_URI;
 
 public final class Paths {
-    public static final String DEFAULT_WORKSPACE = "default";
-    public static final String SYSTEM_WORKSPACE = "system";
-
     private static final NameFactory NAME_FACTORY = NameFactoryImpl.getInstance();
     private static final PathFactory PATH_FACTORY = PathFactoryImpl.getInstance();
 
@@ -89,25 +85,17 @@ public final class Paths {
     }
 
     public static String pathToString(String wspName, Path absPath) throws RepositoryException
{
-        String wspPath = '/' + getWorkspaceName(wspName);
+        if (wspName == null || "".equals(wspName) || wspName.contains("/")) {
+            throw new RepositoryException("Invalid workspace name: " + wspName);
+        }
+
+        String wspPath = '/' + wspName;
 
         return absPath.denotesRoot()
             ? wspPath
             : wspPath + translateQPath(absPath);
     }
 
-    public static String getWorkspaceName(String name) throws NoSuchWorkspaceException {
-        if (name == null || "".equals(name)) {
-            return DEFAULT_WORKSPACE;
-        }
-        else if (name.contains("/")) {
-            throw new NoSuchWorkspaceException(name);
-        }
-        else {
-            return name;
-        }
-    }
-
     //------------------------------------------< private >---
 
     private static final BidiMap KNOWN_NAME_SPACES = new DualHashBidiMap() {{



Mime
View raw message