jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1148836 - in /jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel: NodeTypeStore.java RepositoryServiceImpl.java util/NodeTypes.java
Date Wed, 20 Jul 2011 16:33:47 GMT
Author: mduerig
Date: Wed Jul 20 16:33:46 2011
New Revision: 1148836

URL: http://svn.apache.org/viewvc?rev=1148836&view=rev
Log:
spi2microkernel (WIP)
- persist node type definitions and name spaces

Added:
    jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/NodeTypeStore.java
Removed:
    jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/NodeTypes.java
Modified:
    jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/RepositoryServiceImpl.java

Added: 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=1148836&view=auto
==============================================================================
--- jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/NodeTypeStore.java
(added)
+++ jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/NodeTypeStore.java
Wed Jul 20 16:33:46 2011
@@ -0,0 +1,136 @@
+/*
+ * 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;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader;
+import org.apache.jackrabbit.commons.cnd.ParseException;
+import org.apache.jackrabbit.mk.api.MicroKernel;
+import org.apache.jackrabbit.mk.json.JsonBuilder;
+import org.apache.jackrabbit.spi.QNodeTypeDefinition;
+import org.apache.jackrabbit.spi.commons.namespace.NamespaceMapping;
+import org.apache.jackrabbit.spi.commons.nodetype.NodeTypeStorage;
+import org.apache.jackrabbit.spi.commons.nodetype.QDefinitionBuilderFactory;
+import org.apache.jackrabbit.spi2microkernel.util.Paths;
+import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.jcr.RepositoryException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.List;
+
+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 NodeTypeStore(MicroKernel microKernel) {
+        this.microKernel = microKernel;
+    }
+
+    public static NodeTypeStore create(MicroKernel microKernel)
+            throws RepositoryException {
+
+        if (getNodeTypes(microKernel) == null) {
+            try {
+                String cnd = IOUtils.toString(getDefaultNodeTypes());
+                StringBuilder sb = new StringBuilder();
+                JsonBuilder.create(sb)
+                    .value("cnd", cnd)
+                    .build();
+
+                String jsop = "+\"" + NT_DEFINITION_NAME + "\":" + sb.toString();
+                microKernel.commit('/' + Paths.SYSTEM_WORKSPACE, jsop, microKernel.getHeadRevision(),
"register default node types");
+            }
+            catch (IOException e) {
+                throw new RepositoryException(e); 
+            }
+        }
+
+        return new NodeTypeStore(microKernel);
+    }
+
+    public void read(NamespaceMapping namespaces, NodeTypeStorage nodeTypeDefinitions) throws
RepositoryException {
+        try {
+            Reader nodeTypes = getNodeTypes(microKernel);
+            if (nodeTypes == null) {
+                return;
+            }
+            
+            CompactNodeTypeDefReader<QNodeTypeDefinition, NamespaceMapping> reader
=
+                    new CompactNodeTypeDefReader<QNodeTypeDefinition, NamespaceMapping>(
+                    nodeTypes, "", namespaces, new QDefinitionBuilderFactory());
+
+            List<QNodeTypeDefinition> ntDefinitions = reader.getNodeTypeDefinitions();
+            nodeTypeDefinitions.registerNodeTypes(ntDefinitions.toArray(new QNodeTypeDefinition[ntDefinitions.size()]),
true);
+        }
+        catch (ParseException e) {
+            throw new RepositoryException("Error reading node type definitions", e);
+        }
+    }
+
+    public void write(NamespaceMapping namespaces, NodeTypeStorage nodeTypeDefinitions) {
+        // todo implement write
+    }
+
+    //------------------------------------------< private >---
+
+    private static Reader getNodeTypes(MicroKernel microKernel) {
+        String rev = microKernel.getHeadRevision();
+        if (microKernel.nodeExists(NT_DEFINITION_PATH, rev)) {
+            String ntDefinitions = microKernel.getNodes(NT_DEFINITION_PATH, rev);
+            JSONParser jsonParser = new JSONParser();
+            try {
+                Object parseResult = jsonParser.parse(ntDefinitions);
+                if (parseResult instanceof JSONObject) {
+                    Object value = ((JSONObject) parseResult).get("cnd");
+                    if (value instanceof String) {
+                        String cnd = (String) value;
+                        return new StringReader(cnd);
+                    }
+                }
+            }
+            catch (org.json.simple.parser.ParseException e) {
+                log.error("Error parsing Json string: " + ntDefinitions, e);
+            }
+
+        }
+
+        return null;
+    }
+
+    private static InputStream getDefaultNodeTypes() throws RepositoryException {
+        InputStream is = RepositoryServiceImpl.class.getResourceAsStream(DEFAULT_NODETYPES);
+        if (is == null) {
+            throw new RepositoryException("Resource not found: " + DEFAULT_NODETYPES);
+        }
+
+        return is;
+    }
+
+}

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=1148836&r1=1148835&r2=1148836&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
Wed Jul 20 16:33:46 2011
@@ -49,7 +49,6 @@ import org.apache.jackrabbit.spi.commons
 import org.apache.jackrabbit.spi.commons.batch.ConsolidatingChangeLog;
 import org.apache.jackrabbit.spi2microkernel.util.Function1;
 import org.apache.jackrabbit.spi2microkernel.util.JsonHandlerBase;
-import org.apache.jackrabbit.spi2microkernel.util.NodeTypes;
 import org.apache.jackrabbit.spi2microkernel.util.Paths;
 import org.apache.jackrabbit.spi2microkernel.util.Values;
 import org.json.simple.parser.JSONParser;
@@ -111,7 +110,7 @@ public class RepositoryServiceImpl exten
     private final Map<SessionInfo, SessionState> sessions = new ConcurrentHashMap<SessionInfo,
SessionState>();
 
     public RepositoryServiceImpl(MicroKernel microKernel) throws RepositoryException {
-        super(DESCRIPTORS, Collections.<String, String>emptyMap(), NodeTypes.getDefaultNodeTypes());
+        super(DESCRIPTORS, Collections.<String, String>emptyMap(), new QNodeTypeDefinition[0]);
 
         try {
             this.microKernel = microKernel;  // fixme who disposes the micro kernel?
@@ -119,6 +118,8 @@ public class RepositoryServiceImpl exten
             String head = microKernel.getHeadRevision();
             createWorkspace(head, Paths.DEFAULT_WORKSPACE, true);
             createWorkspace(head, Paths.SYSTEM_WORKSPACE, true);
+            NodeTypeStore nodeTypeStore = NodeTypeStore.create(microKernel);
+            nodeTypeStore.read(namespaces, nodeTypeDefs);
         }
         catch (MicroKernelException e) {
             throw new RepositoryException(e.getMessage(), e);



Mime
View raw message