jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1364543 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins: name/ type/
Date Mon, 23 Jul 2012 08:51:10 GMT
Author: jukka
Date: Mon Jul 23 08:51:10 2012
New Revision: 1364543

URL: http://svn.apache.org/viewvc?rev=1364543&view=rev
Log:
OAK-66: JCR Node Type Management

Content-based NodeTypeManagerImpl

Added:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeManagerImpl.java
  (with props)
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NamespaceRegistryImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/ItemDefinitionImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeTemplateImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeUtil.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/PropertyDefinitionImpl.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NamespaceRegistryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NamespaceRegistryImpl.java?rev=1364543&r1=1364542&r2=1364543&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NamespaceRegistryImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NamespaceRegistryImpl.java
Mon Jul 23 08:51:10 2012
@@ -75,16 +75,16 @@ public class NamespaceRegistryImpl imple
 
     @Override
     public void unregisterNamespace(String prefix) throws RepositoryException {
+        Root root = session.getCurrentRoot();
+        Tree namespaces = root.getTree("/jcr:system/jcr:namespaces");
+        if (namespaces == null || !namespaces.hasProperty(prefix)) {
+            throw new NamespaceException(
+                    "Namespace mapping from " + prefix + " to "
+                    + getURI(prefix) + " can not be unregistered");
+        }
+
         try {
-            Root root = session.getCurrentRoot();
-            Tree namespaces = getOrCreate(root, "jcr:system", Namespaces.NSMAPNODENAME);
-            if (namespaces.hasProperty(prefix)) {
-                namespaces.removeProperty(prefix);
-            } else {
-                throw new NamespaceException(
-                        "Namespace mapping from " + prefix + " to "
-                        + getURI(prefix) + " can not be unregistered");
-            }
+            namespaces.removeProperty(prefix);
             root.commit(DefaultConflictHandler.OURS);
             refresh();
         } catch (NamespaceValidatorException e) {

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/ItemDefinitionImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/ItemDefinitionImpl.java?rev=1364543&r1=1364542&r2=1364543&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/ItemDefinitionImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/ItemDefinitionImpl.java
Mon Jul 23 08:51:10 2012
@@ -16,17 +16,13 @@
  */
 package org.apache.jackrabbit.oak.plugins.type;
 
-import static javax.jcr.version.OnParentVersionAction.ACTIONNAME_ABORT;
-import static javax.jcr.version.OnParentVersionAction.ACTIONNAME_COMPUTE;
-import static javax.jcr.version.OnParentVersionAction.ACTIONNAME_COPY;
-import static javax.jcr.version.OnParentVersionAction.ACTIONNAME_IGNORE;
-import static javax.jcr.version.OnParentVersionAction.ACTIONNAME_INITIALIZE;
-import static javax.jcr.version.OnParentVersionAction.ACTIONNAME_VERSION;
-
 import javax.jcr.nodetype.ItemDefinition;
 import javax.jcr.nodetype.NodeType;
 import javax.jcr.version.OnParentVersionAction;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 /**
  * <pre>
  * [nt:{propertyDefinition,childNodeDefinition}]
@@ -41,6 +37,9 @@ import javax.jcr.version.OnParentVersion
  */
 class ItemDefinitionImpl implements ItemDefinition {
 
+    private static final Logger log =
+            LoggerFactory.getLogger(ItemDefinitionImpl.class);
+
     private final NodeType type;
 
     protected final NodeUtil node;
@@ -72,18 +71,12 @@ class ItemDefinitionImpl implements Item
 
     @Override
     public int getOnParentVersion() {
-        String opv = node.getString("jcr:onParentVersion", ACTIONNAME_COPY);
-        if (ACTIONNAME_ABORT.equalsIgnoreCase(opv)) {
-            return OnParentVersionAction.ABORT;
-        } else if (ACTIONNAME_COMPUTE.equalsIgnoreCase(opv)) {
-            return OnParentVersionAction.COMPUTE;
-        } else if (ACTIONNAME_IGNORE.equalsIgnoreCase(opv)) {
-            return OnParentVersionAction.IGNORE;
-        } else if (ACTIONNAME_INITIALIZE.equalsIgnoreCase(opv)) {
-            return OnParentVersionAction.INITIALIZE;
-        } else if (ACTIONNAME_VERSION.equalsIgnoreCase(opv)) {
-            return OnParentVersionAction.VERSION;
-        } else {
+        try {
+            return OnParentVersionAction.valueFromName(node.getString(
+                    "jcr:onParentVersion",
+                    OnParentVersionAction.ACTIONNAME_COPY));
+        } catch (IllegalArgumentException e) {
+            log.warn("Unexpected jcr:onParentVersion value", e);
             return OnParentVersionAction.COPY;
         }
     }

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeManagerImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeManagerImpl.java?rev=1364543&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeManagerImpl.java
(added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeManagerImpl.java
Mon Jul 23 08:51:10 2012
@@ -0,0 +1,374 @@
+/*
+ * 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.oak.plugins.type;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.List;
+import java.util.Map;
+
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.Value;
+import javax.jcr.ValueFactory;
+import javax.jcr.nodetype.ItemDefinition;
+import javax.jcr.nodetype.NoSuchNodeTypeException;
+import javax.jcr.nodetype.NodeDefinition;
+import javax.jcr.nodetype.NodeDefinitionTemplate;
+import javax.jcr.nodetype.NodeType;
+import javax.jcr.nodetype.NodeTypeDefinition;
+import javax.jcr.nodetype.NodeTypeExistsException;
+import javax.jcr.nodetype.NodeTypeIterator;
+import javax.jcr.nodetype.NodeTypeManager;
+import javax.jcr.nodetype.NodeTypeTemplate;
+import javax.jcr.nodetype.PropertyDefinition;
+import javax.jcr.nodetype.PropertyDefinitionTemplate;
+import javax.jcr.version.OnParentVersionAction;
+
+import org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader;
+import org.apache.jackrabbit.commons.iterator.NodeTypeIteratorAdapter;
+import org.apache.jackrabbit.oak.api.CommitFailedException;
+import org.apache.jackrabbit.oak.api.ContentSession;
+import org.apache.jackrabbit.oak.api.CoreValueFactory;
+import org.apache.jackrabbit.oak.api.Root;
+import org.apache.jackrabbit.oak.api.Tree;
+import org.apache.jackrabbit.oak.core.DefaultConflictHandler;
+import org.apache.jackrabbit.oak.namepath.NameMapper;
+
+import com.google.common.collect.Lists;
+
+public class NodeTypeManagerImpl implements NodeTypeManager {
+
+    private final ContentSession session;
+
+    private final NameMapper mapper;
+
+    private final ValueFactory factory;
+
+    public NodeTypeManagerImpl(
+            ContentSession session, NameMapper mapper, ValueFactory factory) {
+        this.session = session;
+        this.mapper = mapper;
+        this.factory = factory;
+
+        try {
+            InputStream stream = NodeTypeManagerImpl.class.getResourceAsStream(
+                    "builtin_nodetypes.cnd");
+            try {
+                CompactNodeTypeDefReader<NodeTypeTemplate, Map<String, String>>
reader =
+                        new CompactNodeTypeDefReader<NodeTypeTemplate, Map<String,
String>>(
+                                new InputStreamReader(stream, "UTF-8"), null, new DefBuilderFactory());
+                registerNodeTypes(reader.getNodeTypeDefinitions().toArray(
+                        new NodeTypeTemplate[0]), true);
+            } finally {
+                stream.close();
+            }
+        } catch (Exception e) {
+            throw new IllegalStateException(
+                    "Unable to load built-in node types", e);
+        }
+}
+
+    protected String getOakName(String name) throws RepositoryException {
+        return name; // TODO
+    }
+
+    /**
+     * Called by the {@link NodeTypeManager} implementation methods to
+     * refresh the state of the session associated with this instance.
+     * That way the session is kept in sync with the latest global state
+     * seen by the node type manager.
+     *
+     * @throws RepositoryException if the session could not be refreshed
+     */
+    protected void refresh() throws RepositoryException {
+    }
+
+    //---------------------------------------------------< NodeTypeManager >--
+
+    @Override
+    public boolean hasNodeType(String name) throws RepositoryException {
+        Tree types = session.getCurrentRoot().getTree(
+                "/jcr:system/jcr:nodeTypes");
+        return types != null && types.hasChild(mapper.getOakName(name));
+    }
+
+    @Override
+    public NodeType getNodeType(String name) throws RepositoryException {
+        Tree types = session.getCurrentRoot().getTree(
+                "/jcr:system/jcr:nodeTypes");
+        if (types != null) {
+            Tree type = types.getChild(mapper.getOakName(name));
+            if (type != null) {
+                return new NodeTypeImpl(this, new NodeUtil(
+                        session.getCoreValueFactory(), mapper, type));
+            }
+        }
+        throw new NoSuchNodeTypeException(name);
+    }
+
+    @Override
+    public NodeTypeIterator getAllNodeTypes() throws RepositoryException {
+        List<NodeType> list = Lists.newArrayList();
+        Tree types = session.getCurrentRoot().getTree(
+                "/jcr:system/jcr:nodeTypes");
+        if (types != null) {
+            for (Tree type : types.getChildren()) {
+                list.add(new NodeTypeImpl(this, new NodeUtil(
+                        session.getCoreValueFactory(), mapper, type)));
+                
+            }
+        }
+        return new NodeTypeIteratorAdapter(list);
+    }
+
+    @Override
+    public NodeTypeIterator getPrimaryNodeTypes() throws RepositoryException {
+        List<NodeType> list = Lists.newArrayList();
+        NodeTypeIterator iterator = getAllNodeTypes();
+        while (iterator.hasNext()) {
+            NodeType type = iterator.nextNodeType();
+            if (!type.isMixin()) {
+                list.add(type);
+            }
+        }
+        return new NodeTypeIteratorAdapter(list);
+    }
+
+    @Override
+    public NodeTypeIterator getMixinNodeTypes() throws RepositoryException {
+        List<NodeType> list = Lists.newArrayList();
+        NodeTypeIterator iterator = getAllNodeTypes();
+        while (iterator.hasNext()) {
+            NodeType type = iterator.nextNodeType();
+            if (type.isMixin()) {
+                list.add(type);
+            }
+        }
+        return new NodeTypeIteratorAdapter(list);
+    }
+
+    @Override
+    public NodeTypeTemplate createNodeTypeTemplate() throws RepositoryException {
+        return new NodeTypeTemplateImpl(this, factory);
+    }
+
+    @Override
+    public NodeTypeTemplate createNodeTypeTemplate(NodeTypeDefinition ntd) throws RepositoryException
{
+        return new NodeTypeTemplateImpl(this, factory, ntd);
+    }
+
+    @Override
+    public NodeDefinitionTemplate createNodeDefinitionTemplate() {
+        return new NodeDefinitionTemplateImpl();
+    }
+
+    @Override
+    public PropertyDefinitionTemplate createPropertyDefinitionTemplate() {
+        return new PropertyDefinitionTemplateImpl();
+    }
+
+    @Override
+    public NodeType registerNodeType(NodeTypeDefinition ntd, boolean allowUpdate) throws
RepositoryException {
+        // TODO proper node type registration... (OAK-66)
+        Root root = session.getCurrentRoot();
+        Tree types = getOrCreateNodeTypes(root);
+        try {
+            NodeType type = internalRegister(types, ntd, allowUpdate);
+            root.commit(DefaultConflictHandler.OURS);
+            return type;
+        } catch (CommitFailedException e) {
+            throw new RepositoryException(e);
+        }
+    }
+
+    @Override
+    public NodeTypeIterator registerNodeTypes(NodeTypeDefinition[] ntds, boolean allowUpdate)
throws RepositoryException {
+        // TODO handle inter-type dependencies (OAK-66)
+        Root root = session.getCurrentRoot();
+        Tree types = getOrCreateNodeTypes(root);
+        try {
+            List<NodeType> list = Lists.newArrayList();
+            for (int i = 0; i < ntds.length; i++) {
+                list.add(internalRegister(types, ntds[i], allowUpdate));
+            }
+            root.commit(DefaultConflictHandler.OURS);
+            return new NodeTypeIteratorAdapter(list);
+        } catch (CommitFailedException e) {
+            throw new RepositoryException(e);
+        }
+    }
+
+    private NodeType internalRegister(
+            Tree types, NodeTypeDefinition ntd, boolean allowUpdate)
+            throws RepositoryException {
+        String jcrName = ntd.getName();
+        String oakName = mapper.getOakName(jcrName);
+
+        Tree type = types.getChild(oakName);
+        if (type != null) {
+            if (allowUpdate) {
+                type.remove();
+            } else {
+                throw new NodeTypeExistsException(
+                        "Node type " + jcrName + " already exists");
+            }
+        }
+        type = types.addChild(oakName);
+
+        CoreValueFactory factory = session.getCoreValueFactory();
+        NodeUtil node = new NodeUtil(factory, mapper, type);
+        node.setName("jcr:nodeTypeName", jcrName);
+        node.setNames("jcr:supertypes", ntd.getDeclaredSupertypeNames());
+        node.setBoolean("jcr:isAbstract", ntd.isAbstract());
+        node.setBoolean("jcr:isQueryable", ntd.isQueryable());
+        node.setBoolean("jcr:isMixin", ntd.isMixin());
+        node.setBoolean("jcr:hasOrderableChildNodes", ntd.hasOrderableChildNodes());
+        String primaryItemName = ntd.getPrimaryItemName();
+        if (primaryItemName != null) {
+            node.setName("jcr:primaryItemName", primaryItemName);
+        }
+
+        Tree props = type.addChild("jcr:propertyDefinition");
+        for (PropertyDefinition pd : ntd.getDeclaredPropertyDefinitions()) {
+            Tree def = props.addChild(mapper.getOakName(pd.getName()));
+            internalRegisterPropertyDefinition(
+                    new NodeUtil(factory, mapper, def), pd);
+        }
+
+        Tree nodes = type.addChild("jcr:childNodeDefinition");
+        for (NodeDefinition nd : ntd.getDeclaredChildNodeDefinitions()) {
+            Tree def = nodes.addChild(mapper.getOakName(nd.getName()));
+            internalRegisterNodeDefinition(
+                    new NodeUtil(factory, mapper, def), nd);
+        }
+
+        return new NodeTypeImpl(this, node);
+    }
+
+    private void internalRegisterItemDefinition(
+            NodeUtil node, ItemDefinition def) {
+        node.setName("jcr:name", def.getName());
+        node.setBoolean("jcr:autoCreated", def.isAutoCreated());
+        node.setBoolean("jcr:mandatory", def.isMandatory());
+        node.setString(
+                "jcr:onParentVersion",
+                OnParentVersionAction.nameFromValue(def.getOnParentVersion()));
+    }
+
+    private void internalRegisterPropertyDefinition(
+            NodeUtil node, PropertyDefinition def)
+            throws RepositoryException {
+        internalRegisterItemDefinition(node, def);
+
+        node.setString(
+                "jcr:requiredType",
+                PropertyType.nameFromValue(def.getRequiredType()));
+        node.setBoolean("jcr:multiple", def.isMultiple());
+        node.setBoolean("jcr:isFullTextSearchable", def.isFullTextSearchable());
+        node.setBoolean("jcr:isQueryOrderable", def.isQueryOrderable());
+        node.setStrings("jcr:availableQueryOperators", def.getAvailableQueryOperators());
+
+        String[] constraints = def.getValueConstraints();
+        if (constraints != null) {
+            node.setStrings("jcr:valueConstraints", constraints);
+        }
+
+        Value[] values = def.getDefaultValues();
+        if (values != null) {
+            // TODO: convert to CoreValues
+        }
+    }
+
+    private void internalRegisterNodeDefinition(
+            NodeUtil node, NodeDefinition def) {
+        internalRegisterItemDefinition(node, def);
+
+        node.setBoolean("jcr:sameNameSiblings", def.allowsSameNameSiblings());
+        node.setNames(
+                "jcr:requiredPrimaryTypes",
+                def.getRequiredPrimaryTypeNames());
+        String defaultPrimaryType = def.getDefaultPrimaryTypeName();
+        if (defaultPrimaryType != null) {
+            node.setName("jcr:defaultPrimaryType", defaultPrimaryType);
+        }
+    }
+
+    private Tree getOrCreateNodeTypes(Root root) {
+        Tree types = root.getTree("/jcr:system/jcr:nodeTypes");
+        if (types == null) {
+            Tree system = root.getTree("/jcr:system");
+            if (system == null) {
+                system = root.getTree("/").addChild("jcr:system");
+            }
+            types = system.addChild("jcr:nodeTypes");
+        }
+        return types;
+    }
+
+    @Override
+    public void unregisterNodeType(String name) throws RepositoryException {
+        Tree type = null;
+        Root root = session.getCurrentRoot();
+        Tree types = root.getTree("/jcr:system/jcr:nodeTypes");
+        if (types != null) {
+            type = types.getChild(mapper.getOakName(name));
+        }
+        if (type == null) {
+            // TODO: Degrade gracefully? Or throw NoSuchNodeTypeException?
+            throw new RepositoryException(
+                    "Node type " + name + " can not be unregistered");
+        }
+
+        try {
+            type.remove();
+            root.commit(DefaultConflictHandler.OURS);
+            refresh();
+        } catch (CommitFailedException e) {
+            throw new RepositoryException(
+                    "Failed to unregister node type " + name, e);
+        }
+    }
+
+    @Override
+    public void unregisterNodeTypes(String[] names) throws RepositoryException {
+        Root root = session.getCurrentRoot();
+        Tree types = root.getTree("/jcr:system/jcr:nodeTypes");
+        if (types == null) {
+            // TODO: Degrade gracefully? Or throw NoSuchNodeTypeException?
+            throw new RepositoryException("Node types can not be unregistered");
+        }
+
+        try {
+            for (String name : names) {
+                Tree type = types.getChild(mapper.getOakName(name));
+                if (type == null) {
+                    // TODO: Degrade gracefully? Or throw NoSuchNodeTypeException?
+                    throw new RepositoryException(
+                            "Node type " + name + " can not be unregistered");
+                }
+                type.remove();
+            }
+            root.commit(DefaultConflictHandler.OURS);
+            refresh();
+        } catch (CommitFailedException e) {
+            throw new RepositoryException(
+                    "Failed to unregister node types", e);
+        }
+    }
+
+}

Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeManagerImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeTemplateImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeTemplateImpl.java?rev=1364543&r1=1364542&r2=1364543&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeTemplateImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeTemplateImpl.java
Mon Jul 23 08:51:10 2012
@@ -51,7 +51,7 @@ class NodeTypeTemplateImpl
     private final List<NodeDefinitionTemplate> nodeDefinitionTemplates =
             new ArrayList<NodeDefinitionTemplate>();
 
-    private NodeTypeTemplateImpl(NodeTypeManager manager, ValueFactory factory) {
+    public NodeTypeTemplateImpl(NodeTypeManager manager, ValueFactory factory) {
         this.manager = manager;
         this.factory = factory;
     }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeUtil.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeUtil.java?rev=1364543&r1=1364542&r2=1364543&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeUtil.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeUtil.java
Mon Jul 23 08:51:10 2012
@@ -19,7 +19,10 @@ package org.apache.jackrabbit.oak.plugin
 import java.util.ArrayList;
 import java.util.List;
 
+import javax.jcr.PropertyType;
+
 import org.apache.jackrabbit.oak.api.CoreValue;
+import org.apache.jackrabbit.oak.api.CoreValueFactory;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.Tree;
 import org.apache.jackrabbit.oak.namepath.NameMapper;
@@ -29,15 +32,22 @@ import org.apache.jackrabbit.oak.namepat
  */
 class NodeUtil {
 
+    private final CoreValueFactory factory;
+
     private final NameMapper mapper;
 
     private final Tree tree;
 
-    public NodeUtil(NameMapper mapper, Tree tree) {
+    public NodeUtil(CoreValueFactory factory, NameMapper mapper, Tree tree) {
+        this.factory = factory;
         this.mapper = mapper;
         this.tree = tree;
     }
 
+    public void remove(String name) {
+        tree.removeProperty(name);
+    }
+
     public String getName() {
         return mapper.getJcrName(tree.getName());
     }
@@ -48,6 +58,10 @@ class NodeUtil {
                 && property.getValue().getBoolean();
     }
 
+    public void setBoolean(String name, boolean value) {
+        tree.setProperty(name, factory.createValue(value));
+    }
+
     public String getString(String name, String defaultValue) {
         PropertyState property = tree.getProperty(name);
         if (property != null && !property.isArray()) {
@@ -57,6 +71,10 @@ class NodeUtil {
         }
     }
 
+    public void setString(String name, String value) {
+        tree.setProperty(name, factory.createValue(value));
+    }
+
     public String[] getStrings(String name) {
         PropertyState property = tree.getProperty(name);
         if (property == null) {
@@ -71,6 +89,14 @@ class NodeUtil {
         return strings;
     }
 
+    public void setStrings(String name, String... values) {
+        List<CoreValue> cvs = new ArrayList<CoreValue>(values.length);
+        for (String value : values) {
+            cvs.add(factory.createValue(value));
+        }
+        tree.setProperty(name, cvs);
+    }
+
     public String getName(String name) {
         return getName(name, null);
     }
@@ -84,6 +110,11 @@ class NodeUtil {
         }
     }
 
+    public void setName(String name, String value) {
+        tree.setProperty(name, factory.createValue(
+                mapper.getOakName(value), PropertyType.NAME));
+    }
+
     public String[] getNames(String name, String... defaultValues) {
         String[] strings = getStrings(name);
         if (strings == null) {
@@ -95,12 +126,21 @@ class NodeUtil {
         return strings;
     }
 
+    public void setNames(String name, String... values) {
+        List<CoreValue> cvs = new ArrayList<CoreValue>(values.length);
+        for (String value : values) {
+            cvs.add(factory.createValue(
+                    mapper.getOakName(value), PropertyType.NAME));
+        }
+        tree.setProperty(name, cvs);
+    }
+
     public NodeUtil[] getNodes(String name) {
         List<NodeUtil> nodes = new ArrayList<NodeUtil>();
         Tree child = tree.getChild(name);
         if (child != null) {
             for (Tree tree : child.getChildren()) {
-                nodes.add(new NodeUtil(mapper, tree));
+                nodes.add(new NodeUtil(factory, mapper, tree));
             }
         }
         return nodes.toArray(new NodeUtil[nodes.size()]);

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/PropertyDefinitionImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/PropertyDefinitionImpl.java?rev=1364543&r1=1364542&r2=1364543&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/PropertyDefinitionImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/PropertyDefinitionImpl.java
Mon Jul 23 08:51:10 2012
@@ -16,14 +16,15 @@
  */
 package org.apache.jackrabbit.oak.plugins.type;
 
-import static javax.jcr.PropertyType.TYPENAME_UNDEFINED;
-
 import javax.jcr.PropertyType;
 import javax.jcr.Value;
 import javax.jcr.nodetype.NodeType;
 import javax.jcr.nodetype.PropertyDefinition;
 import javax.jcr.query.qom.QueryObjectModelConstants;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 /**
  * <pre>
  * [nt:propertyDefinition]
@@ -43,38 +44,20 @@ import javax.jcr.query.qom.QueryObjectMo
 class PropertyDefinitionImpl extends ItemDefinitionImpl
         implements PropertyDefinition {
 
+    private static final Logger log =
+            LoggerFactory.getLogger(PropertyDefinitionImpl.class);
+
     public PropertyDefinitionImpl(NodeType type, NodeUtil node) {
         super(type, node);
     }
 
     @Override
     public int getRequiredType() {
-        String type = node.getString("jcr:requiredType", TYPENAME_UNDEFINED);
-        if (PropertyType.TYPENAME_BINARY.equalsIgnoreCase(type)) {
-            return PropertyType.BINARY;
-        } else if (PropertyType.TYPENAME_BOOLEAN.equalsIgnoreCase(type)) {
-            return PropertyType.BOOLEAN;
-        } else if (PropertyType.TYPENAME_DATE.equalsIgnoreCase(type)) {
-            return PropertyType.DATE;
-        } else if (PropertyType.TYPENAME_DECIMAL.equalsIgnoreCase(type)) {
-            return PropertyType.DECIMAL;
-        } else if (PropertyType.TYPENAME_DOUBLE.equalsIgnoreCase(type)) {
-            return PropertyType.DOUBLE;
-        } else if (PropertyType.TYPENAME_LONG.equalsIgnoreCase(type)) {
-            return PropertyType.LONG;
-        } else if (PropertyType.TYPENAME_NAME.equalsIgnoreCase(type)) {
-            return PropertyType.NAME;
-        } else if (PropertyType.TYPENAME_PATH.equalsIgnoreCase(type)) {
-            return PropertyType.PATH;
-        } else if (PropertyType.TYPENAME_REFERENCE.equalsIgnoreCase(type)) {
-            return PropertyType.REFERENCE;
-        } else if (PropertyType.TYPENAME_STRING.equalsIgnoreCase(type)) {
-            return PropertyType.STRING;
-        } else if (PropertyType.TYPENAME_URI.equalsIgnoreCase(type)) {
-            return PropertyType.URI;
-        } else if (PropertyType.TYPENAME_WEAKREFERENCE.equalsIgnoreCase(type)) {
-            return PropertyType.WEAKREFERENCE;
-        } else {
+        try {
+            return PropertyType.valueFromName(node.getString(
+                    "jcr:requiredType", PropertyType.TYPENAME_UNDEFINED));
+        } catch (IllegalArgumentException e) {
+            log.warn("Unexpected jcr:requiredType value", e);
             return PropertyType.UNDEFINED;
         }
     }



Mime
View raw message