jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1434620 - in /jackrabbit/oak/trunk: oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/ oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/
Date Thu, 17 Jan 2013 11:25:57 GMT
Author: jukka
Date: Thu Jan 17 11:25:57 2013
New Revision: 1434620

URL: http://svn.apache.org/viewvc?rev=1434620&view=rev
Log:
OAK-562: Clean up duplicate name conversion in plugins/nodetype

Refactor type template classes to use Oak names internally and to better
avoid code duplication. Instead of extending the builder base classes
from jcr-commons, we implement everything locally to be able to better
share the name mapping code and to avoid extra CND handling complexity.
As a consequence we use CndImporter instead of the lower-level
CompactNodeTypeDefReader mechanism for importing CND files.

Added:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/AbstractNamedTemplate.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ItemDefinitionTemplateImpl.java
Removed:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/DefBuilderFactory.java
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistry.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/BuiltInNodeTypes.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeDefinitionTemplateImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeTypeTemplateImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/PropertyDefinitionTemplateImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ReadOnlyNodeTypeManager.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ReadWriteNodeTypeManager.java
    jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/TestContentLoader.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistry.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistry.java?rev=1434620&r1=1434619&r2=1434620&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistry.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistry.java Thu Jan 17 11:25:57 2013
@@ -16,6 +16,8 @@
  */
 package org.apache.jackrabbit.oak.plugins.name;
 
+import java.util.Map;
+
 import javax.jcr.NamespaceException;
 import javax.jcr.RepositoryException;
 
@@ -73,6 +75,11 @@ public abstract class ReadWriteNamespace
     @Override
     public void registerNamespace(String prefix, String uri)
             throws RepositoryException {
+        Map<String, String> map = Namespaces.getNamespaceMap(getReadTree());
+        if (uri.equals(map.get(prefix))) {
+            return; // Namespace already registered, so we do nothing
+        }
+
         try {
             Root root = getWriteRoot();
             Tree namespaces =

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/AbstractNamedTemplate.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/AbstractNamedTemplate.java?rev=1434620&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/AbstractNamedTemplate.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/AbstractNamedTemplate.java Thu Jan 17 11:25:57 2013
@@ -0,0 +1,175 @@
+/*
+ * 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.nodetype;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
+import javax.jcr.nodetype.ConstraintViolationException;
+
+import org.apache.jackrabbit.oak.namepath.JcrNameParser;
+import org.apache.jackrabbit.oak.namepath.NameMapper;
+
+/**
+ * Abstract base class for the template implementations in this package.
+ * Keeps track of the Oak name of this template and provides utility methods
+ * for mapping between JCR and Oak names.
+ */
+abstract class AbstractNamedTemplate {
+
+    private final NameMapper mapper;
+
+    private String oakName = null; // not defined by default
+
+    protected AbstractNamedTemplate(NameMapper mapper) {
+        this.mapper = mapper;
+    }
+
+    protected AbstractNamedTemplate(NameMapper mapper, String jcrName)
+            throws ConstraintViolationException {
+        this(mapper);
+        if (jcrName != null) {
+            setName(jcrName);
+        }
+    }
+
+    /**
+     * Returns the name of this template, or {@code null} if the name
+     * has not yet been set.
+     *
+     * @return JCR name, or {@code null}
+     */
+    @CheckForNull
+    public String getName() {
+        return getJcrNameAllowNull(oakName);
+    }
+
+    /**
+     * Sets the name of this template.
+     *
+     * @param jcrName JCR name
+     * @throws ConstraintViolationException if the name is invalid
+     */
+    public void setName(@Nonnull String jcrName)
+            throws ConstraintViolationException {
+        this.oakName = getOakNameOrThrowConstraintViolation(jcrName);
+    }
+
+    /**
+     * Like {@link NameMapper#getJcrName(String)}, but allows the given Oak
+     * name to be {@code null}, in which case the return value is also
+     * {@code null}. Useful for the template implementations where
+     * {@code null} values are used to indicate undefined attributes.
+     *
+     * @param oakName Oak name, or {@code null}
+     * @return JCR name, or {@code null}
+     */
+    @CheckForNull
+    protected String getJcrNameAllowNull(@CheckForNull String oakName) {
+        if (oakName != null) {
+            return mapper.getJcrName(oakName);
+        } else {
+            return null;
+        }
+    }
+
+    /**
+     * Converts the given Oak names to corresponding JCR names. If the given
+     * array is {@code null} (signifying an undefined set of names), then the
+     * return value is also {@code null}.
+     *
+     * @param oakNames Oak names, or {@code null}
+     * @return JCR names, or {@code null}
+     */
+    @CheckForNull
+    protected String[] getJcrNamesAllowNull(@CheckForNull String[] oakNames) {
+        String[] jcrNames = null;
+        if (oakNames != null) {
+            jcrNames = new String[oakNames.length];
+            for (int i = 0; i < oakNames.length; i++) {
+                jcrNames[i] = mapper.getJcrName(oakNames[i]);
+            }
+        }
+        return jcrNames;
+    }
+
+    /**
+     * Converts the given JCR name to the corresponding Oak name. Throws
+     * a {@link ConstraintViolationException} if the name is {@code null}
+     * or otherwise invalid.
+     *
+     * @param jcrName JCR name
+     * @return Oak name
+     * @throws ConstraintViolationException if name is invalid or {@code null}
+     */
+    @Nonnull
+    protected String getOakNameOrThrowConstraintViolation(
+            @Nonnull String jcrName) throws ConstraintViolationException {
+        if (jcrName == null) {
+            throw new ConstraintViolationException("Missing JCR name");
+        }
+        String oakName = mapper.getOakNameOrNull(jcrName);
+        if (oakName == null || !JcrNameParser.validate(jcrName)) {
+            throw new ConstraintViolationException(
+                    "Invalid name: " + jcrName);
+        }
+        return oakName;
+    }
+
+    /**
+     * Like {@link #getOakNameOrThrowConstraintViolation(String)} but allows
+     * the given JCR name to be {@code null}, in which case the return value
+     * is also {@code null}.
+     *
+     * @param jcrName JCR name, or {@code null}
+     * @return Oak name, or {@code null}
+     * @throws ConstraintViolationException if the name is invalid
+     */
+    @CheckForNull
+    protected String getOakNameAllowNullOrThrowConstraintViolation(
+            @CheckForNull String jcrName) throws ConstraintViolationException {
+        if (jcrName == null) {
+            return null;
+        } else {
+            return getOakNameOrThrowConstraintViolation(jcrName);
+        }
+    }
+
+    /**
+     * Converts the given JCR names to corresponding Oak names. Throws
+     * a {@link ConstraintViolationException} if the given array is
+     * {@code null} or one of the contained JCR names is {@code null}
+     * or otherwise invalid.
+     *
+     * @param jcrNames JCR names
+     * @return Oak names
+     * @throws ConstraintViolationException if names are invalid or {@code null}
+     */
+    @Nonnull
+    protected String[] getOakNamesOrThrowConstraintViolation(
+            @Nonnull String[] jcrNames) throws ConstraintViolationException {
+        if (jcrNames != null) {
+            String[] oakNames = new String[jcrNames.length];
+            for (int i = 0; i < jcrNames.length; i++) {
+                oakNames[i] = getOakNameOrThrowConstraintViolation(jcrNames[i]);
+            }
+            return oakNames;
+        } else {
+            throw new ConstraintViolationException("Missing JCR names");
+        }
+    }
+
+}

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/BuiltInNodeTypes.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/BuiltInNodeTypes.java?rev=1434620&r1=1434619&r2=1434620&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/BuiltInNodeTypes.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/BuiltInNodeTypes.java Thu Jan 17 11:25:57 2013
@@ -16,15 +16,30 @@
  */
 package org.apache.jackrabbit.oak.plugins.nodetype;
 
+import static org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeConstants.NODE_TYPES_PATH;
+
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.util.Map;
 
 import javax.annotation.Nonnull;
+import javax.jcr.NamespaceRegistry;
+import javax.jcr.RepositoryException;
+import javax.jcr.ValueFactory;
+import javax.jcr.nodetype.NodeTypeManager;
 
+import org.apache.jackrabbit.commons.cnd.CndImporter;
+import org.apache.jackrabbit.commons.cnd.ParseException;
 import org.apache.jackrabbit.oak.api.Root;
 import org.apache.jackrabbit.oak.api.Tree;
+import org.apache.jackrabbit.oak.namepath.GlobalNameMapper;
+import org.apache.jackrabbit.oak.namepath.NamePathMapperImpl;
+import org.apache.jackrabbit.oak.plugins.name.Namespaces;
+import org.apache.jackrabbit.oak.plugins.name.ReadWriteNamespaceRegistry;
+import org.apache.jackrabbit.oak.plugins.value.ValueFactoryImpl;
 
-import static org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeConstants.NODE_TYPES_PATH;
+import com.google.common.base.Charsets;
 
 /**
  * <code>BuiltInNodeTypes</code> is a utility class that registers the built-in
@@ -32,7 +47,11 @@ import static org.apache.jackrabbit.oak.
  */
 public class BuiltInNodeTypes {
 
-    private final ReadWriteNodeTypeManager ntMgr;
+    private final NodeTypeManager ntMgr;
+
+    private final NamespaceRegistry nsReg;
+
+    private final ValueFactory vf;
 
     private BuiltInNodeTypes(final Root root) {
         this.ntMgr =  new ReadWriteNodeTypeManager() {
@@ -47,6 +66,25 @@ public class BuiltInNodeTypes {
                 return root;
             }
         };
+
+        this.nsReg = new ReadWriteNamespaceRegistry() {
+            @Override
+            protected Tree getReadTree() {
+                return root.getTree("/");
+            }
+            @Override
+            protected Root getWriteRoot() {
+                return root;
+            }
+        };
+
+        this.vf = new ValueFactoryImpl(null, new NamePathMapperImpl(
+                new GlobalNameMapper() {
+                    @Override
+                    protected Map<String, String> getNamespaceMap() {
+                        return Namespaces.getNamespaceMap(root.getTree("/"));
+                    }
+                }));
     }
 
     /**
@@ -60,24 +98,25 @@ public class BuiltInNodeTypes {
 
     private void registerBuiltinNodeTypes() {
         // FIXME: migrate custom node types as well.
-        if (!nodeTypesInContent()) {
+        try {
+            InputStream stream = BuiltInNodeTypes.class.getResourceAsStream("builtin_nodetypes.cnd");
             try {
-                InputStream stream = BuiltInNodeTypes.class.getResourceAsStream("builtin_nodetypes.cnd");
-                try {
-                    ntMgr.registerNodeTypes(new InputStreamReader(stream, "UTF-8"));
-                } finally {
-                    stream.close();
-                }
-            } catch (Exception e) {
-                throw new IllegalStateException(
-                        "Unable to load built-in node types", e);
+                CndImporter.registerNodeTypes(
+                        new InputStreamReader(stream, Charsets.UTF_8),
+                        "built-in node types", ntMgr, nsReg, vf, false);
+            } finally {
+                stream.close();
             }
+        } catch (IOException e) {
+            throw new IllegalStateException(
+                    "Unable to read built-in node types", e);
+        } catch (ParseException e) {
+            throw new IllegalStateException(
+                    "Unable to parse built-in node types", e);
+        } catch (RepositoryException e) {
+            throw new IllegalStateException(
+                    "Unable to register built-in node types", e);
         }
     }
 
-    private boolean nodeTypesInContent() {
-        Tree types = ntMgr.getTypes();
-        return types != null && types.getChildrenCount() > 0;
-    }
-
 }

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ItemDefinitionTemplateImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ItemDefinitionTemplateImpl.java?rev=1434620&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ItemDefinitionTemplateImpl.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ItemDefinitionTemplateImpl.java Thu Jan 17 11:25:57 2013
@@ -0,0 +1,140 @@
+/*
+ * 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.nodetype;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
+import javax.jcr.nodetype.ConstraintViolationException;
+import javax.jcr.nodetype.ItemDefinition;
+import javax.jcr.nodetype.NodeType;
+import javax.jcr.version.OnParentVersionAction;
+
+import org.apache.jackrabbit.oak.namepath.NameMapper;
+
+/**
+ * Base class for the node and property definition template implementations
+ * in this package. Takes care of the shared item definition attributes and
+ * manages mappings between JCR and Oak names.
+ */
+abstract class ItemDefinitionTemplateImpl extends AbstractNamedTemplate
+        implements ItemDefinition {
+
+    private boolean residual = false;
+
+    private boolean isAutoCreated = false;
+
+    private int onParentVersion = OnParentVersionAction.COPY;
+
+    protected boolean isProtected = false;
+
+    protected boolean isMandatory = false;
+
+    protected ItemDefinitionTemplateImpl(NameMapper mapper) {
+        super(mapper);
+    }
+
+    protected ItemDefinitionTemplateImpl(
+            NameMapper mapper, ItemDefinition definition)
+            throws ConstraintViolationException {
+        super(mapper, definition.getName());
+        setProtected(definition.isProtected());
+        setMandatory(definition.isMandatory());
+        setAutoCreated(definition.isAutoCreated());
+        setOnParentVersion(definition.getOnParentVersion());
+    }
+
+    /**
+     * Returns the name of this template, or {@code null} if the name
+     * has not yet been set. The special name "*" is used for residual
+     * item definitions.
+     *
+     * @return JCR name, "*", or {@code null}
+     */
+    @Override @CheckForNull
+    public String getName() {
+        if (residual) {
+            return NodeTypeConstants.RESIDUAL_NAME;
+        } else {
+            return super.getName();
+        }
+    }
+
+    /**
+     * Sets the name of this template. Use the special name "*" for a residual
+     * item definition.
+     *
+     * @param jcrName JCR name, or "*"
+     * @throws ConstraintViolationException if the name is invalid
+     */
+    @Override
+    public void setName(@Nonnull String jcrName)
+            throws ConstraintViolationException {
+        residual = NodeTypeConstants.RESIDUAL_NAME.equals(jcrName);
+        if (!residual) {
+            super.setName(jcrName);
+        }
+    }
+
+    /**
+     * Returns {@code null} since an item definition template is not
+     * attached to a live, already registered node type.
+     *
+     * @return {@code null}
+     */
+    @Override
+    public NodeType getDeclaringNodeType() {
+        return null;
+    }
+
+    @Override
+    public boolean isAutoCreated() {
+        return isAutoCreated;
+    }
+
+    public void setAutoCreated(boolean isAutoCreated) {
+        this.isAutoCreated = isAutoCreated;
+    }
+
+    @Override
+    public boolean isMandatory() {
+        return isMandatory;
+    }
+
+    public void setMandatory(boolean isMandatory) {
+        this.isMandatory = isMandatory;
+    }
+
+    @Override
+    public int getOnParentVersion() {
+        return onParentVersion;
+    }
+
+    public void setOnParentVersion(int onParentVersion) {
+        OnParentVersionAction.nameFromValue(onParentVersion); // validate
+        this.onParentVersion = onParentVersion;
+    }
+
+    @Override
+    public boolean isProtected() {
+        return isProtected;
+    }
+
+    public void setProtected(boolean isProtected) {
+        this.isProtected = isProtected;
+    }
+
+}

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeDefinitionTemplateImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeDefinitionTemplateImpl.java?rev=1434620&r1=1434619&r2=1434620&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeDefinitionTemplateImpl.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeDefinitionTemplateImpl.java Thu Jan 17 11:25:57 2013
@@ -16,205 +16,89 @@
  */
 package org.apache.jackrabbit.oak.plugins.nodetype;
 
-import java.util.ArrayList;
-import java.util.List;
-
-import javax.jcr.RepositoryException;
-import javax.jcr.UnsupportedRepositoryOperationException;
 import javax.jcr.nodetype.ConstraintViolationException;
+import javax.jcr.nodetype.NodeDefinition;
 import javax.jcr.nodetype.NodeDefinitionTemplate;
 import javax.jcr.nodetype.NodeType;
-import javax.jcr.nodetype.NodeTypeTemplate;
-import javax.jcr.version.OnParentVersionAction;
 
-import org.apache.jackrabbit.commons.cnd.DefinitionBuilderFactory.AbstractNodeDefinitionBuilder;
-import org.apache.jackrabbit.oak.namepath.JcrNameParser;
 import org.apache.jackrabbit.oak.namepath.NameMapper;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
-class NodeDefinitionTemplateImpl
-        extends AbstractNodeDefinitionBuilder<NodeTypeTemplate>
+class NodeDefinitionTemplateImpl extends ItemDefinitionTemplateImpl
         implements NodeDefinitionTemplate {
 
-    private static final Logger log =
-            LoggerFactory.getLogger(NodeDefinitionTemplateImpl.class);
-
-    private String defaultPrimaryTypeName;
+    private boolean allowSameNameSiblings = false;
 
-    private final NameMapper mapper;
-    private String[] requiredPrimaryTypeNames;
+    private String defaultPrimaryTypeOakName = null;
 
-    protected NodeType getNodeType(String name) throws RepositoryException {
-        throw new UnsupportedRepositoryOperationException();
-    }
+    private String[] requiredPrimaryTypeOakNames = null;
 
     public NodeDefinitionTemplateImpl(NameMapper mapper) {
-        this.mapper = mapper;
-        onParent = OnParentVersionAction.COPY;
+        super(mapper);
     }
 
-    @Override
-    public void build() {
-        // do nothing by default
-    }
-
-    @Override
-    public NodeType getDeclaringNodeType() {
-        return null;
-    }
-
-    @Override
-    public void setDeclaringNodeType(String name) {
-        // ignore
-    }
-
-    @Override
-    public void setName(String name) throws ConstraintViolationException {
-        JcrNameParser.checkName(name, true);
-        this.name = mapper.getJcrName(mapper.getOakNameOrNull(name));
-    }
-
-    @Override
-    public boolean isAutoCreated() {
-        return autocreate;
-    }
-
-    @Override
-    public void setAutoCreated(boolean autocreate) {
-        this.autocreate = autocreate;
-    }
-
-    @Override
-    public boolean isProtected() {
-        return isProtected;
-    }
-
-    @Override
-    public void setProtected(boolean isProtected) {
-        this.isProtected = isProtected;
-    }
-
-    @Override
-    public boolean isMandatory() {
-        return isMandatory;
-    }
-
-    @Override
-    public void setMandatory(boolean isMandatory) {
-        this.isMandatory = isMandatory;
-    }
-
-    @Override
-    public int getOnParentVersion() {
-        return onParent;
-    }
-
-    @Override
-    public void setOnParentVersion(int onParent) {
-        this.onParent = onParent;
+    public NodeDefinitionTemplateImpl(
+            NameMapper mapper, NodeDefinition definition)
+            throws ConstraintViolationException {
+        super(mapper, definition);
+        setSameNameSiblings(definition.allowsSameNameSiblings());
+        setDefaultPrimaryTypeName(definition.getDefaultPrimaryTypeName());
+        setRequiredPrimaryTypeNames(definition.getRequiredPrimaryTypeNames());
     }
 
     @Override
     public boolean allowsSameNameSiblings() {
-        return allowSns;
-    }
-
-    @Override
-    public void setSameNameSiblings(boolean allowSns) {
-        this.allowSns = allowSns;
+        return allowSameNameSiblings;
     }
 
     @Override
-    public void setAllowsSameNameSiblings(boolean allowSns) {
-        setSameNameSiblings(allowSns);
+    public void setSameNameSiblings(boolean allowSameNameSiblings) {
+        this.allowSameNameSiblings = allowSameNameSiblings;
     }
 
+    /**
+     * Returns {@code null} since an item definition template is not
+     * attached to a live, already registered node type.
+     *
+     * @return {@code null}
+     */
     @Override
     public NodeType getDefaultPrimaryType() {
-        if (defaultPrimaryTypeName != null) {
-            try {
-                return getNodeType(defaultPrimaryTypeName);
-            } catch (RepositoryException e) {
-                log.warn("Unable to access default primary type "
-                        + defaultPrimaryTypeName + " of " + name, e);
-            }
-        }
         return null;
     }
 
     @Override
     public String getDefaultPrimaryTypeName() {
-        return defaultPrimaryTypeName;
-    }
-
-    @Override
-    public void setDefaultPrimaryTypeName(String name) throws ConstraintViolationException {
-        if (name == null) {
-            this.defaultPrimaryTypeName = null;
-        }
-        else {
-            JcrNameParser.checkName(name, false);
-            this.defaultPrimaryTypeName = mapper.getJcrName(mapper.getOakNameOrNull(name));
-        }
+        return getJcrNameAllowNull(defaultPrimaryTypeOakName);
     }
 
     @Override
-    public void setDefaultPrimaryType(String name) throws ConstraintViolationException {
-        setDefaultPrimaryTypeName(name);
+    public void setDefaultPrimaryTypeName(String jcrName)
+            throws ConstraintViolationException {
+        this.defaultPrimaryTypeOakName =
+                    getOakNameAllowNullOrThrowConstraintViolation(jcrName);
     }
 
+    /**
+     * Returns {@code null} since an item definition template is not
+     * attached to a live, already registered node type.
+     *
+     * @return {@code null}
+     */
     @Override
     public NodeType[] getRequiredPrimaryTypes() {
-        if (requiredPrimaryTypeNames == null) {
-            return null;
-        } else {
-            List<NodeType> types =
-                    new ArrayList<NodeType>(requiredPrimaryTypeNames.length);
-            for (String requiredPrimaryTypeName : requiredPrimaryTypeNames) {
-                try {
-                    types.add(getNodeType(requiredPrimaryTypeName));
-                }
-                catch (RepositoryException e) {
-                    log.warn("Unable to required primary primary type "
-                            + requiredPrimaryTypeName + " of " + name, e);
-                }
-            }
-            return types.toArray(new NodeType[types.size()]);
-        }
+        return null;
     }
 
     @Override
     public String[] getRequiredPrimaryTypeNames() {
-        return requiredPrimaryTypeNames;
-    }
-
-    @Override
-    public void setRequiredPrimaryTypeNames(String[] names) throws ConstraintViolationException {
-        if (names == null) {
-            throw new ConstraintViolationException("null is not a valid array of JCR names");
-        }
-        int k = 0;
-        String[] n = new String[names.length];
-        for (String name : names) {
-            JcrNameParser.checkName(name, false);
-            n[k++] = mapper.getJcrName(mapper.getOakNameOrNull(name));
-        }
-        this.requiredPrimaryTypeNames = n;
+        return getJcrNamesAllowNull(requiredPrimaryTypeOakNames);
     }
 
     @Override
-    public void addRequiredPrimaryType(String name) throws ConstraintViolationException {
-        JcrNameParser.checkName(name, false);
-        if (requiredPrimaryTypeNames == null) {
-            requiredPrimaryTypeNames = new String[] { mapper.getJcrName(mapper.getOakNameOrNull(name)) };
-        } else {
-            String[] names = new String[requiredPrimaryTypeNames.length + 1];
-            System.arraycopy(requiredPrimaryTypeNames, 0, names, 0, requiredPrimaryTypeNames.length);
-            names[requiredPrimaryTypeNames.length] = mapper.getJcrName(mapper.getOakNameOrNull(name));
-            requiredPrimaryTypeNames = names;
-        }
-
+    public void setRequiredPrimaryTypeNames(String[] jcrNames)
+            throws ConstraintViolationException {
+        this.requiredPrimaryTypeOakNames =
+                getOakNamesOrThrowConstraintViolation(jcrNames);
     }
 
-}
\ No newline at end of file
+}

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeTypeTemplateImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeTypeTemplateImpl.java?rev=1434620&r1=1434619&r2=1434620&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeTypeTemplateImpl.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeTypeTemplateImpl.java Thu Jan 17 11:25:57 2013
@@ -16,165 +16,84 @@
  */
 package org.apache.jackrabbit.oak.plugins.nodetype;
 
-import java.util.ArrayList;
 import java.util.List;
 
-import javax.jcr.RepositoryException;
-import javax.jcr.Value;
-import javax.jcr.ValueFactory;
-import javax.jcr.ValueFormatException;
+import javax.annotation.Nonnull;
 import javax.jcr.nodetype.ConstraintViolationException;
 import javax.jcr.nodetype.NodeDefinition;
 import javax.jcr.nodetype.NodeDefinitionTemplate;
-import javax.jcr.nodetype.NodeType;
 import javax.jcr.nodetype.NodeTypeDefinition;
-import javax.jcr.nodetype.NodeTypeManager;
 import javax.jcr.nodetype.NodeTypeTemplate;
 import javax.jcr.nodetype.PropertyDefinition;
 import javax.jcr.nodetype.PropertyDefinitionTemplate;
 
-import org.apache.jackrabbit.commons.cnd.DefinitionBuilderFactory.AbstractNodeTypeDefinitionBuilder;
-import org.apache.jackrabbit.oak.namepath.JcrNameParser;
 import org.apache.jackrabbit.oak.namepath.NameMapper;
-import org.apache.jackrabbit.value.ValueFactoryImpl;
 
-final class NodeTypeTemplateImpl
-    extends AbstractNodeTypeDefinitionBuilder<NodeTypeTemplate>
-    implements NodeTypeTemplate {
+import com.google.common.collect.Lists;
 
-    private final NodeTypeManager manager;
+final class NodeTypeTemplateImpl extends AbstractNamedTemplate
+        implements NodeTypeTemplate {
 
-    private final NameMapper mapper;
+    private static final PropertyDefinition[] EMPTY_PROPERTY_DEFINITION_ARRAY =
+            new PropertyDefinition[0];
 
-    private final ValueFactory factory;
+    private static final NodeDefinition[] EMPTY_NODE_DEFINITION_ARRAY =
+            new NodeDefinition[0];
 
-    private String primaryItemName;
+    protected boolean isMixin;
 
-    private String[] superTypeNames = new String[0];
+    protected boolean isOrderable;
 
-    private List<PropertyDefinitionTemplate> propertyDefinitionTemplates;
+    protected boolean isAbstract;
 
-    private List<NodeDefinitionTemplate> nodeDefinitionTemplates;
+    protected boolean queryable;
 
-    public NodeTypeTemplateImpl(NodeTypeManager manager, NameMapper mapper, ValueFactory factory) {
-        this.manager = manager;
-        this.mapper = mapper;
-        this.factory = factory;
-    }
-
-    public NodeTypeTemplateImpl(NameMapper mapper) {
-        this(null, mapper, ValueFactoryImpl.getInstance());
-    }
+    private String primaryItemOakName = null; // not defined by default
 
-    public NodeTypeTemplateImpl(
-            NodeTypeManager manager, NameMapper mapper, ValueFactory factory,
-            NodeTypeDefinition ntd) throws ConstraintViolationException {
-        this(manager, mapper, factory);
-
-        setName(ntd.getName());
-        setAbstract(ntd.isAbstract());
-        setMixin(ntd.isMixin());
-        setOrderableChildNodes(ntd.hasOrderableChildNodes());
-        setQueryable(ntd.isQueryable());
-        String name = ntd.getPrimaryItemName();
-        if (name != null) {
-            setPrimaryItemName(name);
-        }
-        setDeclaredSuperTypeNames(ntd.getDeclaredSupertypeNames());
+    @Nonnull
+    private String[] superTypeOakNames = new String[0];
 
-        getPropertyDefinitionTemplates();  // Make sure propertyDefinitionTemplates is initialised
-        for (PropertyDefinition pd : ntd.getDeclaredPropertyDefinitions()) {
-            PropertyDefinitionTemplateImpl pdt = newPropertyDefinitionBuilder();
-            pdt.setDeclaringNodeType(pd.getDeclaringNodeType().getName());
-            pdt.setName(pd.getName());
-            pdt.setProtected(pd.isProtected());
-            pdt.setMandatory(pd.isMandatory());
-            pdt.setAutoCreated(pd.isAutoCreated());
-            pdt.setOnParentVersion(pd.getOnParentVersion());
-            pdt.setMultiple(pd.isMultiple());
-            pdt.setRequiredType(pd.getRequiredType());
-            pdt.setDefaultValues(pd.getDefaultValues());
-            pdt.setValueConstraints(pd.getValueConstraints());
-            pdt.setFullTextSearchable(pd.isFullTextSearchable());
-            pdt.setAvailableQueryOperators(pd.getAvailableQueryOperators());
-            pdt.setQueryOrderable(pd.isQueryOrderable());
-            pdt.build();
-        }
+    private List<PropertyDefinitionTemplate> propertyDefinitionTemplates = null;
 
-        getNodeDefinitionTemplates();   // Make sure nodeDefinitionTemplates is initialised
-        for (NodeDefinition nd : ntd.getDeclaredChildNodeDefinitions()) {
-            NodeDefinitionTemplateImpl ndt = newNodeDefinitionBuilder();
-            ndt.setDeclaringNodeType(nd.getDeclaringNodeType().getName());
-            ndt.setName(nd.getName());
-            ndt.setProtected(nd.isProtected());
-            ndt.setMandatory(nd.isMandatory());
-            ndt.setAutoCreated(nd.isAutoCreated());
-            ndt.setOnParentVersion(nd.getOnParentVersion());
-            ndt.setSameNameSiblings(nd.allowsSameNameSiblings());
-            ndt.setDefaultPrimaryTypeName(nd.getDefaultPrimaryTypeName());
-            ndt.setRequiredPrimaryTypeNames(nd.getRequiredPrimaryTypeNames());
-            ndt.build();
-        }
-    }
+    private List<NodeDefinitionTemplate> nodeDefinitionTemplates = null;
 
-    @Override
-    public NodeTypeTemplate build() {
-        return this;
+    public NodeTypeTemplateImpl(NameMapper mapper) {
+        super(mapper);
     }
 
-    @Override
-    public PropertyDefinitionTemplateImpl newPropertyDefinitionBuilder() {
-        return new PropertyDefinitionTemplateImpl(mapper) {
-            @Override
-            protected Value createValue(String value, int type)
-                    throws ValueFormatException {
-                return factory.createValue(value, type);
-            }
-            @Override
-            public void build() {
-                getPropertyDefinitionTemplates().add(this);
+    NodeTypeTemplateImpl(NameMapper mapper, NodeTypeDefinition definition)
+            throws ConstraintViolationException {
+        super(mapper, definition.getName());
+
+        setMixin(definition.isMixin());
+        setOrderableChildNodes(definition.hasOrderableChildNodes());
+        setAbstract(definition.isAbstract());
+        setQueryable(definition.isQueryable());
+        String primaryItemName = definition.getPrimaryItemName();
+        if (primaryItemName != null) {
+            setPrimaryItemName(primaryItemName);
+        }
+        setDeclaredSuperTypeNames(definition.getDeclaredSupertypeNames());
+
+        PropertyDefinition[] pds = definition.getDeclaredPropertyDefinitions();
+        if (pds != null) {
+            propertyDefinitionTemplates =
+                    Lists.newArrayListWithCapacity(pds.length);
+            for (int i = 0; pds != null && i < pds.length; i++) {
+                propertyDefinitionTemplates.add(
+                        new PropertyDefinitionTemplateImpl(mapper, pds[i]));
             }
-        };
-    }
+        }
 
-    @Override
-    public NodeDefinitionTemplateImpl newNodeDefinitionBuilder() {
-        return new NodeDefinitionTemplateImpl(mapper) {
-            @Override
-            protected NodeType getNodeType(String name)
-                    throws RepositoryException  {
-                if (manager != null) {
-                    return manager.getNodeType(name);
-                } else {
-                    return super.getNodeType(name);
-                }
-            }
-            @Override
-            public void build() {
-                getNodeDefinitionTemplates().add(this);
+        NodeDefinition[] nds = definition.getDeclaredChildNodeDefinitions();
+        if (nds != null) {
+            nodeDefinitionTemplates =
+                    Lists.newArrayListWithCapacity(nds.length);
+            for (int i = 0; i < nds.length; i++) {
+                nodeDefinitionTemplates.add(
+                        new NodeDefinitionTemplateImpl(mapper, nds[i]));
             }
-        };
-    }
-
-    @Override
-    public String getName() {
-        return name;
-    }
-
-    @Override
-    public void setName(String name) throws ConstraintViolationException {
-        JcrNameParser.checkName(name, false);
-        this.name = mapper.getJcrName(mapper.getOakNameOrNull(name));
-    }
-
-    @Override
-    public boolean isAbstract() {
-        return isAbstract;
-    }
-
-    @Override
-    public void setAbstract(boolean abstractStatus) {
-        this.isAbstract = abstractStatus;
+        }
     }
 
     @Override
@@ -198,6 +117,16 @@ final class NodeTypeTemplateImpl
     }
 
     @Override
+    public boolean isAbstract() {
+        return isAbstract;
+    }
+
+    @Override
+    public void setAbstract(boolean abstractStatus) {
+        this.isAbstract = abstractStatus;
+    }
+
+    @Override
     public boolean isQueryable() {
         return queryable;
     }
@@ -209,78 +138,62 @@ final class NodeTypeTemplateImpl
 
     @Override
     public String getPrimaryItemName() {
-        return primaryItemName ;
+        return getJcrNameAllowNull(primaryItemOakName);
     }
 
     @Override
-    public void setPrimaryItemName(String name) throws ConstraintViolationException {
-        if (name == null) {
-            this.primaryItemName = null;
-        }
-        else {
-            JcrNameParser.checkName(name, false);
-            this.primaryItemName = mapper.getJcrName(mapper.getOakNameOrNull(name));
-        }
+    public void setPrimaryItemName(String jcrName)
+            throws ConstraintViolationException {
+        this.primaryItemOakName =
+                getOakNameAllowNullOrThrowConstraintViolation(jcrName);
     }
 
     @Override
     public String[] getDeclaredSupertypeNames() {
-        return superTypeNames;
+        return getJcrNamesAllowNull(superTypeOakNames);
     }
 
     @Override
-    public void setDeclaredSuperTypeNames(String[] names) throws ConstraintViolationException {
-        if (names == null) {
-            throw new ConstraintViolationException("null is not a valid array of JCR names");
-        }
-        int k = 0;
-        String[] n = new String[names.length];
-        for (String name : names) {
-            JcrNameParser.checkName(name, false);
-            n[k++] = mapper.getJcrName(mapper.getOakNameOrNull(name));
-        }
-        this.superTypeNames = n;
+    public void setDeclaredSuperTypeNames(String[] jcrNames)
+            throws ConstraintViolationException {
+        this.superTypeOakNames =
+                getOakNamesOrThrowConstraintViolation(jcrNames);
     }
 
     @Override
-    public void addSupertype(String name) throws RepositoryException {
-        JcrNameParser.checkName(name, false);
-        String[] names = new String[superTypeNames.length + 1];
-        System.arraycopy(superTypeNames, 0, names, 0, superTypeNames.length);
-        names[superTypeNames.length] = mapper.getJcrName(mapper.getOakName(name));
-        superTypeNames = names;
+    public PropertyDefinition[] getDeclaredPropertyDefinitions() {
+        if (propertyDefinitionTemplates != null) {
+            return propertyDefinitionTemplates.toArray(
+                    EMPTY_PROPERTY_DEFINITION_ARRAY);
+        } else {
+            return null;
+        }
     }
 
     @Override
     public List<PropertyDefinitionTemplate> getPropertyDefinitionTemplates() {
         if (propertyDefinitionTemplates == null) {
-            propertyDefinitionTemplates = new ArrayList<PropertyDefinitionTemplate>();
+            propertyDefinitionTemplates = Lists.newArrayList();
         }
         return propertyDefinitionTemplates;
     }
 
     @Override
-    public List<NodeDefinitionTemplate> getNodeDefinitionTemplates() {
-        if (nodeDefinitionTemplates == null) {
-            nodeDefinitionTemplates = new ArrayList<NodeDefinitionTemplate>();
+    public NodeDefinition[] getDeclaredChildNodeDefinitions() {
+        if (nodeDefinitionTemplates != null) {
+            return nodeDefinitionTemplates.toArray(
+                    EMPTY_NODE_DEFINITION_ARRAY);
+        } else {
+            return null;
         }
-        return nodeDefinitionTemplates;
     }
 
     @Override
-    public PropertyDefinition[] getDeclaredPropertyDefinitions() {
-        return propertyDefinitionTemplates == null
-            ? null
-            : propertyDefinitionTemplates.toArray(
-                new PropertyDefinition[propertyDefinitionTemplates.size()]);
-    }
-
-    @Override
-    public NodeDefinition[] getDeclaredChildNodeDefinitions() {
-        return nodeDefinitionTemplates == null
-            ? null
-            : nodeDefinitionTemplates.toArray(
-                new NodeDefinition[nodeDefinitionTemplates.size()]);
+    public List<NodeDefinitionTemplate> getNodeDefinitionTemplates() {
+        if (nodeDefinitionTemplates == null) {
+            nodeDefinitionTemplates = Lists.newArrayList();
+        }
+        return nodeDefinitionTemplates;
     }
 
 }
\ No newline at end of file

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/PropertyDefinitionTemplateImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/PropertyDefinitionTemplateImpl.java?rev=1434620&r1=1434619&r2=1434620&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/PropertyDefinitionTemplateImpl.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/PropertyDefinitionTemplateImpl.java Thu Jan 17 11:25:57 2013
@@ -16,104 +16,70 @@
  */
 package org.apache.jackrabbit.oak.plugins.nodetype;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+
 import javax.jcr.PropertyType;
-import javax.jcr.RepositoryException;
-import javax.jcr.UnsupportedRepositoryOperationException;
 import javax.jcr.Value;
 import javax.jcr.nodetype.ConstraintViolationException;
-import javax.jcr.nodetype.NodeType;
-import javax.jcr.nodetype.NodeTypeTemplate;
+import javax.jcr.nodetype.PropertyDefinition;
 import javax.jcr.nodetype.PropertyDefinitionTemplate;
-import javax.jcr.version.OnParentVersionAction;
+import javax.jcr.query.qom.QueryObjectModelConstants;
 
-import org.apache.jackrabbit.commons.cnd.DefinitionBuilderFactory.AbstractPropertyDefinitionBuilder;
-import org.apache.jackrabbit.oak.namepath.JcrNameParser;
 import org.apache.jackrabbit.oak.namepath.NameMapper;
 
-class PropertyDefinitionTemplateImpl
-        extends AbstractPropertyDefinitionBuilder<NodeTypeTemplate>
+class PropertyDefinitionTemplateImpl extends ItemDefinitionTemplateImpl
         implements PropertyDefinitionTemplate {
 
-    private String[] valueConstraints;
-
-    private final NameMapper mapper;
-    private Value[] defaultValues;
-
-    public PropertyDefinitionTemplateImpl(NameMapper mapper) {
-        this.mapper = mapper;
-        onParent = OnParentVersionAction.COPY;
-        requiredType = PropertyType.STRING;
-    }
-
-    protected Value createValue(String value, int type)
-            throws RepositoryException {
-        throw new UnsupportedRepositoryOperationException();
-    }
-
-    @Override
-    public void build() {
-        // do nothing by default
-    }
+    private static final String[] ALL_OPERATORS = new String[]{
+        QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO,
+        QueryObjectModelConstants.JCR_OPERATOR_GREATER_THAN,
+        QueryObjectModelConstants.JCR_OPERATOR_GREATER_THAN_OR_EQUAL_TO,
+        QueryObjectModelConstants.JCR_OPERATOR_LESS_THAN,
+        QueryObjectModelConstants.JCR_OPERATOR_LESS_THAN_OR_EQUAL_TO,
+        QueryObjectModelConstants.JCR_OPERATOR_LIKE,
+        QueryObjectModelConstants.JCR_OPERATOR_NOT_EQUAL_TO
+    };
 
-    @Override
-    public NodeType getDeclaringNodeType() {
-        return null;
-    }
-
-    @Override
-    public void setDeclaringNodeType(String name) {
-        // ignore
-    }
+    private int requiredType = PropertyType.STRING;
 
-    @Override
-    public void setName(String name) throws ConstraintViolationException {
-        JcrNameParser.checkName(name, true);
-        this.name = mapper.getJcrName(mapper.getOakNameOrNull(name));
-    }
+    private boolean isMultiple = false;
 
-    @Override
-    public boolean isAutoCreated() {
-        return autocreate;
-    }
+    private boolean fullTextSearchable = true;
 
-    @Override
-    public void setAutoCreated(boolean autocreate) {
-        this.autocreate = autocreate;
-    }
+    private boolean queryOrderable = true;
 
-    @Override
-    public boolean isProtected() {
-        return isProtected;
-    }
+    private String[] queryOperators = ALL_OPERATORS;
 
-    @Override
-    public void setProtected(boolean isProtected) {
-        this.isProtected = isProtected;
-    }
+    private String[] valueConstraints = null;
 
-    @Override
-    public boolean isMandatory() {
-        return isMandatory;
-    }
+    private Value[] defaultValues = null;
 
-    @Override
-    public void setMandatory(boolean isMandatory) {
-        this.isMandatory = isMandatory;
+    PropertyDefinitionTemplateImpl(NameMapper mapper) {
+        super(mapper);
     }
 
-    @Override
-    public int getOnParentVersion() {
-        return onParent;
+    public PropertyDefinitionTemplateImpl(
+            NameMapper mapper, PropertyDefinition definition)
+            throws ConstraintViolationException {
+        super(mapper, definition);
+        setRequiredType(definition.getRequiredType());
+        setMultiple(definition.isMultiple());
+        setFullTextSearchable(definition.isFullTextSearchable());
+        setQueryOrderable(definition.isQueryOrderable());
+        setAvailableQueryOperators(definition.getAvailableQueryOperators());
+        setValueConstraints(definition.getValueConstraints());
+        setDefaultValues(definition.getDefaultValues());
     }
 
     @Override
-    public void setOnParentVersion(int onParent) {
-        this.onParent = onParent;
+    public int getRequiredType() {
+        return requiredType;
     }
 
     @Override
-    public void setRequiredType(int requiredType) {
-        this.requiredType = requiredType;
+    public void setRequiredType(int type) {
+        PropertyType.nameFromValue(type); // validation
+        this.requiredType = type;
     }
 
     @Override
@@ -127,16 +93,6 @@ class PropertyDefinitionTemplateImpl
     }
 
     @Override
-    public boolean isQueryOrderable() {
-        return queryOrderable;
-    }
-
-    @Override
-    public void setQueryOrderable(boolean queryOrderable) {
-        this.queryOrderable = queryOrderable;
-    }
-
-    @Override
     public boolean isFullTextSearchable() {
         return fullTextSearchable;
     }
@@ -147,57 +103,56 @@ class PropertyDefinitionTemplateImpl
     }
 
     @Override
-    public String[] getAvailableQueryOperators() {
-        return queryOperators;
+    public boolean isQueryOrderable() {
+        return queryOrderable;
     }
 
     @Override
-    public void setAvailableQueryOperators(String[] queryOperators) {
-        this.queryOperators = queryOperators;
+    public void setQueryOrderable(boolean queryOrderable) {
+        this.queryOrderable = queryOrderable;
     }
 
     @Override
-    public Value[] getDefaultValues() {
-        return defaultValues;
+    public String[] getAvailableQueryOperators() {
+        return queryOperators;
     }
 
     @Override
-    public void setDefaultValues(Value[] defaultValues) {
-        this.defaultValues = defaultValues;
+    public void setAvailableQueryOperators(String[] operators) {
+        checkNotNull(operators);
+        this.queryOperators = new String[operators.length];
+        System.arraycopy(operators, 0, this.queryOperators, 0, operators.length);
     }
 
     @Override
-    public void addDefaultValues(String value) throws RepositoryException {
-        if (defaultValues == null) {
-            defaultValues = new Value[] { createValue(value, getRequiredType()) };
-        } else {
-            Value[] values = new Value[defaultValues.length + 1];
-            System.arraycopy(defaultValues, 0, values, 0, defaultValues.length);
-            values[defaultValues.length] = createValue(value, getRequiredType());
-            defaultValues = values;
-        }
+    public String[] getValueConstraints() {
+        return valueConstraints; // no problem if modified by client
     }
 
     @Override
-    public String[] getValueConstraints() {
-        return valueConstraints;
+    public void setValueConstraints(String[] constraints) {
+        if (constraints == null) {
+            this.valueConstraints = null;
+        } else {
+            this.valueConstraints = new String[constraints.length];
+            System.arraycopy(
+                    constraints, 0, valueConstraints, 0, constraints.length);
+        }
     }
 
     @Override
-    public void setValueConstraints(String[] constraints) {
-        this.valueConstraints = constraints;
+    public Value[] getDefaultValues() {
+        return defaultValues; // no problem if modified by client
     }
 
     @Override
-    public void addValueConstraint(String constraint) {
-        if (valueConstraints == null) {
-            valueConstraints = new String[] { constraint };
+    public void setDefaultValues(Value[] values) {
+        if (values == null) {
+            this.defaultValues = null;
         } else {
-            String[] constraints = new String[valueConstraints.length + 1];
-            System.arraycopy(valueConstraints, 0, constraints, 0, valueConstraints.length);
-            constraints[valueConstraints.length] = constraint;
-            valueConstraints = constraints;
+            this.defaultValues = new Value[values.length];
+            System.arraycopy(values, 0, defaultValues, 0, values.length);
         }
     }
 
-}
\ No newline at end of file
+}

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ReadOnlyNodeTypeManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ReadOnlyNodeTypeManager.java?rev=1434620&r1=1434619&r2=1434620&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ReadOnlyNodeTypeManager.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ReadOnlyNodeTypeManager.java Thu Jan 17 11:25:57 2013
@@ -196,13 +196,14 @@ public abstract class ReadOnlyNodeTypeMa
     }
 
     @Override
-    public NodeTypeTemplate createNodeTypeTemplate() throws RepositoryException {
-        return new NodeTypeTemplateImpl(this, getNamePathMapper(), getValueFactory());
+    public NodeTypeTemplate createNodeTypeTemplate() {
+        return new NodeTypeTemplateImpl(getNamePathMapper());
     }
 
     @Override
-    public NodeTypeTemplate createNodeTypeTemplate(NodeTypeDefinition ntd) throws RepositoryException {
-        return new NodeTypeTemplateImpl(this, getNamePathMapper(), getValueFactory(), ntd);
+    public NodeTypeTemplate createNodeTypeTemplate(NodeTypeDefinition ntd)
+            throws ConstraintViolationException {
+        return new NodeTypeTemplateImpl(getNamePathMapper(), ntd);
     }
 
     @Override

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ReadWriteNodeTypeManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ReadWriteNodeTypeManager.java?rev=1434620&r1=1434619&r2=1434620&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ReadWriteNodeTypeManager.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ReadWriteNodeTypeManager.java Thu Jan 17 11:25:57 2013
@@ -16,10 +16,8 @@
  */
 package org.apache.jackrabbit.oak.plugins.nodetype;
 
-import java.io.Reader;
 import java.util.Arrays;
 import java.util.List;
-import java.util.Map;
 
 import javax.annotation.Nonnull;
 import javax.jcr.PropertyType;
@@ -32,14 +30,10 @@ import javax.jcr.nodetype.NodeType;
 import javax.jcr.nodetype.NodeTypeDefinition;
 import javax.jcr.nodetype.NodeTypeExistsException;
 import javax.jcr.nodetype.NodeTypeIterator;
-import javax.jcr.nodetype.NodeTypeTemplate;
 import javax.jcr.nodetype.PropertyDefinition;
 import javax.jcr.version.OnParentVersionAction;
 
 import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
-import org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader;
-import org.apache.jackrabbit.commons.cnd.ParseException;
 import org.apache.jackrabbit.commons.iterator.NodeTypeIteratorAdapter;
 import org.apache.jackrabbit.oak.api.CommitFailedException;
 import org.apache.jackrabbit.oak.api.Root;
@@ -67,7 +61,6 @@ import static org.apache.jackrabbit.JcrC
 import static org.apache.jackrabbit.JcrConstants.JCR_SUPERTYPES;
 import static org.apache.jackrabbit.JcrConstants.JCR_SYSTEM;
 import static org.apache.jackrabbit.JcrConstants.JCR_VALUECONSTRAINTS;
-import static org.apache.jackrabbit.JcrConstants.NT_BASE;
 import static org.apache.jackrabbit.JcrConstants.NT_CHILDNODEDEFINITION;
 import static org.apache.jackrabbit.JcrConstants.NT_NODETYPE;
 import static org.apache.jackrabbit.JcrConstants.NT_PROPERTYDEFINITION;
@@ -133,61 +126,6 @@ public abstract class ReadWriteNodeTypeM
     protected void refresh() throws RepositoryException {
     }
 
-    /**
-     * Utility method for registering node types from a CND format.
-     * @param cnd  reader for the CND
-     * @throws ParseException  if parsing the CND fails
-     * @throws RepositoryException  if registering the node types fails
-     */
-    public void registerNodeTypes(Reader cnd) throws ParseException, RepositoryException {
-        Root root = getWriteRoot();
-
-        CompactNodeTypeDefReader<NodeTypeTemplate, Map<String, String>> reader =
-                new CompactNodeTypeDefReader<NodeTypeTemplate, Map<String, String>>(
-                        cnd, null, new DefBuilderFactory(root.getTree("/")));
-
-        Map<String, NodeTypeTemplate> templates = Maps.newHashMap();
-        for (NodeTypeTemplate template : reader.getNodeTypeDefinitions()) {
-            templates.put(template.getName(), template);
-        }
-
-        for (NodeTypeTemplate template : templates.values()) {
-            // TODO: NT_BASE is the oak-name, while the template is an JCR level API which might have local namespace remapping
-            if (!template.isMixin() && !NT_BASE.equals(template.getName())) {
-                String[] supertypes = template.getDeclaredSupertypeNames();
-                if (supertypes.length == 0) {
-                    template.setDeclaredSuperTypeNames(new String[] {NT_BASE});
-                } else {
-                    // Check whether we need to add the implicit "nt:base" supertype
-                    boolean needsNtBase = true;
-                    for (String name : supertypes) {
-                        NodeTypeDefinition st = templates.get(name);
-                        if (st == null) {
-                            st = getNodeType(name);
-                        }
-                        if (st != null && !st.isMixin()) {
-                            needsNtBase = false;
-                        }
-                    }
-                    if (needsNtBase) {
-                        String[] withBase = new String[supertypes.length + 1];
-                        withBase[0] = NT_BASE;
-                        System.arraycopy(supertypes, 0, withBase, 1, supertypes.length);
-                        template.setDeclaredSuperTypeNames(withBase);
-                    }
-                }
-            }
-        }
-
-        try {
-            internalRegister(root, templates.values(), true);
-            root.commit();
-            refresh();
-        } catch (CommitFailedException e) {
-            throw new RepositoryException(e);
-        }
-    }
-
     //----------------------------------------------------< NodeTypeManager >---
 
     @Override

Modified: jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/TestContentLoader.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/TestContentLoader.java?rev=1434620&r1=1434619&r2=1434620&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/TestContentLoader.java (original)
+++ jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/TestContentLoader.java Thu Jan 17 11:25:57 2013
@@ -29,13 +29,14 @@ import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.Value;
 import javax.jcr.ValueFactory;
-import javax.jcr.nodetype.NodeTypeManager;
 
 import org.apache.jackrabbit.commons.JcrUtils;
+import org.apache.jackrabbit.commons.cnd.CndImporter;
 import org.apache.jackrabbit.commons.cnd.ParseException;
-import org.apache.jackrabbit.oak.plugins.nodetype.ReadWriteNodeTypeManager;
 import org.apache.jackrabbit.value.BinaryValue;
 
+import com.google.common.base.Charsets;
+
 public class TestContentLoader {
 
     /**
@@ -47,7 +48,7 @@ public class TestContentLoader {
         session.getWorkspace().getNamespaceRegistry().registerNamespace(
                 "test", "http://www.apache.org/jackrabbit/test");
 
-        registerTestNodeTypes(session.getWorkspace().getNodeTypeManager());
+        registerTestNodeTypes(session);
 
         Node data = getOrAddNode(session.getRootNode(), "testdata");
         addPropertyTestData(getOrAddNode(data, "property"));
@@ -64,13 +65,11 @@ public class TestContentLoader {
         session.save();
     }
 
-    private static void registerTestNodeTypes(NodeTypeManager ntm) throws RepositoryException, ParseException, IOException {
+    private static void registerTestNodeTypes(Session session) throws RepositoryException, ParseException, IOException {
         InputStream stream = TestContentLoader.class.getResourceAsStream("test_nodetypes.cnd");
         try {
-            if (!(ntm instanceof ReadWriteNodeTypeManager)) {
-                throw new IllegalArgumentException("Need ReadWriteNodeTypeManager");
-            }
-            ((ReadWriteNodeTypeManager)ntm).registerNodeTypes(new InputStreamReader(stream, "UTF-8"));
+            CndImporter.registerNodeTypes(
+                    new InputStreamReader(stream, Charsets.UTF_8), session);
         } finally {
             stream.close();
         }



Mime
View raw message