Search in sources :

Example 1 with NodeTypeDefDiff

use of org.apache.jackrabbit.spi.commons.nodetype.NodeTypeDefDiff in project jackrabbit by apache.

the class CompactNodeTypeDefTest method testCompactNodeTypeDef.

public void testCompactNodeTypeDef() throws Exception {
    // Read in node type def from test file
    Reader reader = new InputStreamReader(getClass().getClassLoader().getResourceAsStream(TEST_FILE));
    CompactNodeTypeDefReader<QNodeTypeDefinition, NamespaceMapping> cndReader = new CompactNodeTypeDefReader<QNodeTypeDefinition, NamespaceMapping>(reader, TEST_FILE, new QDefinitionBuilderFactory());
    List<QNodeTypeDefinition> ntdList1 = cndReader.getNodeTypeDefinitions();
    NamespaceMapping nsm = cndReader.getNamespaceMapping();
    NamePathResolver resolver = new DefaultNamePathResolver(nsm);
    // Put imported node type def back into CND form with CND writer
    StringWriter sw = new StringWriter();
    CompactNodeTypeDefWriter.write(ntdList1, nsm, resolver, sw);
    // Rerun the reader on the product of the writer
    cndReader = new CompactNodeTypeDefReader<QNodeTypeDefinition, NamespaceMapping>(new StringReader(sw.toString()), TEST_FILE, new QDefinitionBuilderFactory());
    List<QNodeTypeDefinition> ntdList2 = cndReader.getNodeTypeDefinitions();
    if (ntdList1.size() == 0 || ntdList1.size() != ntdList2.size()) {
        fail("Exported node type definition was not successfully read back in");
    } else {
        for (int k = 0; k < ntdList1.size(); k++) {
            QNodeTypeDefinition ntd1 = ntdList1.get(k);
            QNodeTypeDefinition ntd2 = ntdList2.get(k);
            NodeTypeDefDiff diff = NodeTypeDefDiff.create(ntd1, ntd2);
            if (diff.isModified() && !diff.isTrivial()) {
                fail("Exported node type definition was not successfully read back in. " + ntd2.getName() + "differs from original");
            }
        }
    }
}
Also used : NamePathResolver(org.apache.jackrabbit.spi.commons.conversion.NamePathResolver) DefaultNamePathResolver(org.apache.jackrabbit.spi.commons.conversion.DefaultNamePathResolver) InputStreamReader(java.io.InputStreamReader) NamespaceMapping(org.apache.jackrabbit.spi.commons.namespace.NamespaceMapping) CompactNodeTypeDefReader(org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition) StringWriter(java.io.StringWriter) QDefinitionBuilderFactory(org.apache.jackrabbit.spi.commons.nodetype.QDefinitionBuilderFactory) StringReader(java.io.StringReader) DefaultNamePathResolver(org.apache.jackrabbit.spi.commons.conversion.DefaultNamePathResolver) CompactNodeTypeDefReader(org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader) NodeTypeDefDiff(org.apache.jackrabbit.spi.commons.nodetype.NodeTypeDefDiff)

Example 2 with NodeTypeDefDiff

use of org.apache.jackrabbit.spi.commons.nodetype.NodeTypeDefDiff in project jackrabbit by apache.

the class NodeTypeRegistry method reregisterNodeType.

/**
     * Internal implementation of {@link #reregisterNodeType(QNodeTypeDefinition)}.
     *
     * @param ntd node type definition
     * @param external whether this invocation should be considered external
     * @return the new effective node type
     * @throws NoSuchNodeTypeException if <code>ntd</code> refers to an
     *                                 unknown node type
     * @throws InvalidNodeTypeDefException if the node type definition
     *                                     is invalid
     * @throws RepositoryException if another error occurs
     */
private EffectiveNodeType reregisterNodeType(QNodeTypeDefinition ntd, boolean external) throws NoSuchNodeTypeException, InvalidNodeTypeDefException, RepositoryException {
    EffectiveNodeType entNew;
    synchronized (this) {
        Name name = ntd.getName();
        if (!registeredNTDefs.containsKey(name)) {
            throw new NoSuchNodeTypeException(name.toString());
        }
        if (builtInNTDefs.contains(name)) {
            throw new RepositoryException(name.toString() + ": can't reregister built-in node type.");
        }
        /**
             * validate new node type definition
             */
        ntd = checkNtBaseSubtyping(ntd, registeredNTDefs);
        validateNodeTypeDef(ntd, entCache, registeredNTDefs, nsReg, false);
        /**
             * build diff of current and new definition and determine type of change
             */
        QNodeTypeDefinition ntdOld = registeredNTDefs.get(name);
        NodeTypeDefDiff diff = NodeTypeDefDiff.create(ntdOld, ntd);
        if (!diff.isModified()) {
            // the definition has not been modified, there's nothing to do here...
            return getEffectiveNodeType(name);
        }
        // make sure existing content would not conflict
        // with new node type definition
        checkForConflictingContent(ntd, diff);
        /**
             * re-register node type definition and update caches &
             * notify listeners on re-registration
             */
        internalUnregister(name);
        // remove old node type definition from store
        customNTDefs.remove(name);
        entNew = internalRegister(ntd);
        // add new node type definition to store
        customNTDefs.add(ntd);
        // persist node type definitions
        persistCustomNodeTypeDefs(customNTDefs);
        // notify listeners
        notifyReRegistered(name);
    }
    // inform cluster if this is not an external invocation
    if (!external && eventChannel != null) {
        eventChannel.reregistered(ntd);
    }
    return entNew;
}
Also used : QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition) RepositoryException(javax.jcr.RepositoryException) Name(org.apache.jackrabbit.spi.Name) NoSuchNodeTypeException(javax.jcr.nodetype.NoSuchNodeTypeException) NodeTypeDefDiff(org.apache.jackrabbit.spi.commons.nodetype.NodeTypeDefDiff)

Aggregations

QNodeTypeDefinition (org.apache.jackrabbit.spi.QNodeTypeDefinition)2 NodeTypeDefDiff (org.apache.jackrabbit.spi.commons.nodetype.NodeTypeDefDiff)2 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 RepositoryException (javax.jcr.RepositoryException)1 NoSuchNodeTypeException (javax.jcr.nodetype.NoSuchNodeTypeException)1 CompactNodeTypeDefReader (org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader)1 Name (org.apache.jackrabbit.spi.Name)1 DefaultNamePathResolver (org.apache.jackrabbit.spi.commons.conversion.DefaultNamePathResolver)1 NamePathResolver (org.apache.jackrabbit.spi.commons.conversion.NamePathResolver)1 NamespaceMapping (org.apache.jackrabbit.spi.commons.namespace.NamespaceMapping)1 QDefinitionBuilderFactory (org.apache.jackrabbit.spi.commons.nodetype.QDefinitionBuilderFactory)1