jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1149186 - in /jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel: NodeTypeStore.java RepositoryServiceImpl.java
Date Thu, 21 Jul 2011 14:01:54 GMT
Author: mduerig
Date: Thu Jul 21 14:01:53 2011
New Revision: 1149186

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

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

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=1149186&r1=1149185&r2=1149186&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
Thu Jul 21 14:01:53 2011
@@ -28,6 +28,7 @@ import org.apache.jackrabbit.spi.QNodeTy
 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.spi.commons.nodetype.compact.CompactNodeTypeDefWriter;
 import org.apache.jackrabbit.spi2microkernel.util.Paths;
 import org.json.simple.JSONObject;
 import org.json.simple.parser.JSONParser;
@@ -39,6 +40,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.Reader;
 import java.io.StringReader;
+import java.io.StringWriter;
+import java.util.Iterator;
 import java.util.List;
 
 public class NodeTypeStore {
@@ -56,16 +59,10 @@ public class NodeTypeStore {
     public static NodeTypeStore create(MicroKernel microKernel)
             throws RepositoryException {
 
-        if (getNodeTypes(microKernel) == null) {
+        if (readNodeTypes(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");
+                writeNodeTypes(microKernel, cnd);
             }
             catch (IOException e) {
                 throw new RepositoryException(e); 
@@ -77,16 +74,16 @@ public class NodeTypeStore {
 
     public void read(NamespaceMapping namespaces, NodeTypeStorage nodeTypeDefinitions) throws
RepositoryException {
         try {
-            Reader nodeTypes = getNodeTypes(microKernel);
+            Reader nodeTypes = readNodeTypes(microKernel);
             if (nodeTypes == null) {
                 return;
             }
-            
-            CompactNodeTypeDefReader<QNodeTypeDefinition, NamespaceMapping> reader
=
+
+            CompactNodeTypeDefReader<QNodeTypeDefinition, NamespaceMapping> cndReader
=
                     new CompactNodeTypeDefReader<QNodeTypeDefinition, NamespaceMapping>(
                     nodeTypes, "", namespaces, new QDefinitionBuilderFactory());
 
-            List<QNodeTypeDefinition> ntDefinitions = reader.getNodeTypeDefinitions();
+            List<QNodeTypeDefinition> ntDefinitions = cndReader.getNodeTypeDefinitions();
             nodeTypeDefinitions.registerNodeTypes(ntDefinitions.toArray(new QNodeTypeDefinition[ntDefinitions.size()]),
true);
         }
         catch (ParseException e) {
@@ -94,13 +91,53 @@ public class NodeTypeStore {
         }
     }
 
-    public void write(NamespaceMapping namespaces, NodeTypeStorage nodeTypeDefinitions) {
-        // todo implement write
+    public void write(NamespaceMapping namespaces, NodeTypeStorage nodeTypeDefinitions) throws
RepositoryException {
+        try {
+            StringWriter writer = new StringWriter();
+            CompactNodeTypeDefWriter cndWriter = new CompactNodeTypeDefWriter(writer, namespaces,
true);
+
+            Iterator<QNodeTypeDefinition> ntDefinitions = nodeTypeDefinitions.getAllDefinitions();
+            while (ntDefinitions.hasNext()) {
+                cndWriter.write(ntDefinitions.next());
+            }
+
+            for (String prefix : namespaces.getPrefixToURIMapping().keySet()) {
+                cndWriter.writeNamespaceDelclaration(prefix);
+            }
+
+            cndWriter.close();
+
+            String cnd = writer.toString();
+            writeNodeTypes(microKernel, cnd);
+        }
+        catch (IOException e) {
+            throw new RepositoryException("Error persisting node type definitions", e);
+        }
     }
 
     //------------------------------------------< private >---
 
-    private static Reader getNodeTypes(MicroKernel microKernel) {
+    private static void writeNodeTypes(MicroKernel microKernel, String cnd) throws RepositoryException
{
+        try {
+            StringBuilder sb = new StringBuilder();
+            JsonBuilder.create(sb)
+                    .value("cnd", cnd)
+                    .build();
+
+            String rev = microKernel.getHeadRevision();
+            StringBuilder jsop = new StringBuilder();
+            if (microKernel.nodeExists(NT_DEFINITION_PATH, 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");
+        }
+        catch (IOException e) {
+            throw new RepositoryException("Error persisting node type definitions", e);
+        }
+    }
+
+    private static Reader readNodeTypes(MicroKernel microKernel) {
         String rev = microKernel.getHeadRevision();
         if (microKernel.nodeExists(NT_DEFINITION_PATH, rev)) {
             String ntDefinitions = microKernel.getNodes(NT_DEFINITION_PATH, rev);

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=1149186&r1=1149185&r2=1149186&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
Thu Jul 21 14:01:53 2011
@@ -108,6 +108,7 @@ public class RepositoryServiceImpl exten
 
     private final MicroKernel microKernel;
     private final Map<SessionInfo, SessionState> sessions = new ConcurrentHashMap<SessionInfo,
SessionState>();
+    private final NodeTypeStore nodeTypeStore;
 
     public RepositoryServiceImpl(MicroKernel microKernel) throws RepositoryException {
         super(DESCRIPTORS, Collections.<String, String>emptyMap(), new QNodeTypeDefinition[0]);
@@ -118,7 +119,7 @@ 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 = NodeTypeStore.create(microKernel);
             nodeTypeStore.read(namespaces, nodeTypeDefs);
         }
         catch (MicroKernelException e) {
@@ -344,11 +345,13 @@ public class RepositoryServiceImpl exten
             throws RepositoryException {
 
         nodeTypeDefs.registerNodeTypes(nodeTypeDefinitions, true);
+        nodeTypeStore.write(namespaces, nodeTypeDefs);
     }
 
     @Override
     public void unregisterNodeTypes(SessionInfo sessionInfo, Name[] nodeTypeNames) throws
RepositoryException {
         nodeTypeDefs.unregisterNodeTypes(nodeTypeNames);
+        nodeTypeStore.write(namespaces, nodeTypeDefs);
     }
 
     //------------------------------------------< Name space management >---
@@ -356,11 +359,13 @@ public class RepositoryServiceImpl exten
     @Override
     public void registerNamespace(SessionInfo sessionInfo, String prefix, String uri) throws
RepositoryException {
         namespaces.setMapping(prefix, uri);
+        nodeTypeStore.write(namespaces, nodeTypeDefs);
     }
 
     @Override
     public void unregisterNamespace(SessionInfo sessionInfo, String uri) throws RepositoryException
{
         namespaces.removeMapping(uri);
+        nodeTypeStore.write(namespaces, nodeTypeDefs);
     }
 
     //------------------------------------------< Observation >---



Mime
View raw message