Search in sources :

Example 36 with QNodeTypeDefinition

use of org.apache.jackrabbit.spi.QNodeTypeDefinition in project jackrabbit by apache.

the class TestAll method testWrite.

/**
     * Test for writing a node type definition file. Writing is tested
     * by writing and re-reading the test node types using an internal
     * byte array. The resulting node type map is then compared to the
     * original test node types.
     *
     * @throws IOException on IO errors
     * @throws RepositoryException on repository errors
     */
public void testWrite() throws IOException, RepositoryException {
    try {
        ByteArrayOutputStream xml = new ByteArrayOutputStream();
        NodeTypeWriter.write(xml, types, registry);
        byte[] bytes = xml.toByteArray();
        QNodeTypeDefinition[] output = NodeTypeReader.read(new ByteArrayInputStream(bytes));
        assertTrue("write output", Arrays.equals(types, output));
    } catch (InvalidNodeTypeDefException e) {
        fail(e.getMessage());
    }
}
Also used : QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition) InvalidNodeTypeDefException(org.apache.jackrabbit.core.nodetype.InvalidNodeTypeDefException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 37 with QNodeTypeDefinition

use of org.apache.jackrabbit.spi.QNodeTypeDefinition in project jackrabbit by apache.

the class RepositoryCopier method copyNodeTypes.

private void copyNodeTypes() throws RepositoryException {
    NodeTypeRegistry sourceRegistry = source.getNodeTypeRegistry();
    NodeTypeRegistry targetRegistry = target.getNodeTypeRegistry();
    logger.info("Copying registered node types");
    Collection<Name> existing = Arrays.asList(targetRegistry.getRegisteredNodeTypes());
    Collection<QNodeTypeDefinition> register = new ArrayList<QNodeTypeDefinition>();
    for (Name name : sourceRegistry.getRegisteredNodeTypes()) {
        // TODO: what about modified node types?
        if (!existing.contains(name)) {
            register.add(sourceRegistry.getNodeTypeDef(name));
        }
    }
    try {
        targetRegistry.registerNodeTypes(register);
    } catch (InvalidNodeTypeDefException e) {
        throw new RepositoryException("Unable to copy node types", e);
    }
}
Also used : QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition) InvalidNodeTypeDefException(org.apache.jackrabbit.core.nodetype.InvalidNodeTypeDefException) ArrayList(java.util.ArrayList) NodeTypeRegistry(org.apache.jackrabbit.core.nodetype.NodeTypeRegistry) RepositoryException(javax.jcr.RepositoryException) Name(org.apache.jackrabbit.spi.Name)

Example 38 with QNodeTypeDefinition

use of org.apache.jackrabbit.spi.QNodeTypeDefinition in project jackrabbit-oak by apache.

the class RepositoryUpgrade method copyNodeTypes.

private void copyNodeTypes(NodeTypeManager ntMgr, ValueFactory valueFactory) throws RepositoryException {
    NodeTypeRegistry sourceRegistry = source.getNodeTypeRegistry();
    List<NodeTypeTemplate> templates = Lists.newArrayList();
    for (Name name : sourceRegistry.getRegisteredNodeTypes()) {
        String oakName = getOakName(name);
        // skip built-in nodetypes (OAK-1235)
        if (!ntMgr.hasNodeType(oakName)) {
            QNodeTypeDefinition def = sourceRegistry.getNodeTypeDef(name);
            templates.add(createNodeTypeTemplate(valueFactory, ntMgr, oakName, def));
        }
    }
    ntMgr.registerNodeTypes(templates.toArray(new NodeTypeTemplate[templates.size()]), true);
}
Also used : QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) NodeTypeRegistry(org.apache.jackrabbit.core.nodetype.NodeTypeRegistry) Name(org.apache.jackrabbit.spi.Name)

Example 39 with QNodeTypeDefinition

use of org.apache.jackrabbit.spi.QNodeTypeDefinition in project jackrabbit by apache.

the class DefinitionValidator method validateNodeTypeDefs.

/**
     * Validate each QNodeTypeDefinition present in the given collection.
     *
     * @param ntDefs
     * @param validatedDefs
     * @return Map mapping the definition to the resulting effective nodetype
     * @throws InvalidNodeTypeDefinitionException
     * @throws RepositoryException
     */
public Map<QNodeTypeDefinition, EffectiveNodeType> validateNodeTypeDefs(Collection<QNodeTypeDefinition> ntDefs, Map<Name, QNodeTypeDefinition> validatedDefs) throws InvalidNodeTypeDefinitionException, RepositoryException {
    // tmp. map containing names/defs of validated nodetypes
    Map<Name, QNodeTypeDefinition> tmpMap = new HashMap<Name, QNodeTypeDefinition>(validatedDefs);
    for (QNodeTypeDefinition ntd : ntDefs) {
        tmpMap.put(ntd.getName(), ntd);
    }
    // map of nodetype definitions and effective nodetypes to be registered
    Map<QNodeTypeDefinition, EffectiveNodeType> ntMap = new HashMap<QNodeTypeDefinition, EffectiveNodeType>();
    List<QNodeTypeDefinition> list = new ArrayList<QNodeTypeDefinition>(ntDefs);
    // iterate over definitions until there are no more definitions with
    // unresolved (i.e. unregistered) dependencies or an error occurs;
    // number of validated nt's per iteration
    int count = -1;
    while (list.size() > 0 && count != 0) {
        count = 0;
        Iterator<QNodeTypeDefinition> iterator = list.iterator();
        while (iterator.hasNext()) {
            QNodeTypeDefinition ntd = iterator.next();
            // check if definition has unresolved dependencies
            /* Note: don't compared to 'registered' nodetypes since registr. is performed later on */
            Collection<Name> dependencies = ntd.getDependencies();
            if (tmpMap.keySet().containsAll(dependencies)) {
                EffectiveNodeType ent = validateNodeTypeDef(ntd, tmpMap);
                ntMap.put(ntd, ent);
                // remove it from list
                iterator.remove();
                // increase count
                count++;
            }
        }
    }
    if (list.size() > 0) {
        StringBuffer msg = new StringBuffer();
        msg.append("the following node types could not be registered because of unresolvable dependencies: ");
        Iterator<QNodeTypeDefinition> iterator = list.iterator();
        while (iterator.hasNext()) {
            msg.append(iterator.next().getName());
            msg.append(" ");
        }
        log.error(msg.toString());
        throw new InvalidNodeTypeDefinitionException(msg.toString());
    }
    return ntMap;
}
Also used : InvalidNodeTypeDefinitionException(javax.jcr.nodetype.InvalidNodeTypeDefinitionException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ValueConstraint(org.apache.jackrabbit.spi.commons.nodetype.constraint.ValueConstraint) QValueConstraint(org.apache.jackrabbit.spi.QValueConstraint) Name(org.apache.jackrabbit.spi.Name) QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition)

Example 40 with QNodeTypeDefinition

use of org.apache.jackrabbit.spi.QNodeTypeDefinition in project jackrabbit by apache.

the class NodeTypeCache method getDefinitions.

/**
     * Returns the <code>QNodeTypeDefinition</code>s for the given node type
     * names. The implementation is free to return additional definitions e.g.
     * dependencies.
     *
     * @param nodeTypeNames
     * @return
     * @throws javax.jcr.nodetype.NoSuchNodeTypeException
     * @throws RepositoryException
     */
public Iterator<QNodeTypeDefinition> getDefinitions(NodeTypeStorage storage, Name[] nodeTypeNames) throws NoSuchNodeTypeException, RepositoryException {
    List<QNodeTypeDefinition> nts = new ArrayList<QNodeTypeDefinition>();
    List<Name> missing = null;
    synchronized (nodeTypes) {
        for (int i = 0; i < nodeTypeNames.length; i++) {
            QNodeTypeDefinition def = nodeTypes.get(nodeTypeNames[i]);
            if (def == null) {
                if (missing == null) {
                    missing = new ArrayList<Name>();
                }
                missing.add(nodeTypeNames[i]);
            } else {
                nts.add(def);
            }
        }
    }
    if (missing != null) {
        Name[] ntNames = missing.toArray(new Name[missing.size()]);
        Iterator<QNodeTypeDefinition> it = storage.getDefinitions(ntNames);
        synchronized (nodeTypes) {
            while (it.hasNext()) {
                QNodeTypeDefinition def = it.next();
                nts.add(def);
                nodeTypes.put(def.getName(), def);
            }
        }
    }
    return nts.iterator();
}
Also used : QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition) ArrayList(java.util.ArrayList) Name(org.apache.jackrabbit.spi.Name)

Aggregations

QNodeTypeDefinition (org.apache.jackrabbit.spi.QNodeTypeDefinition)57 Name (org.apache.jackrabbit.spi.Name)21 ArrayList (java.util.ArrayList)13 RepositoryException (javax.jcr.RepositoryException)11 NoSuchNodeTypeException (javax.jcr.nodetype.NoSuchNodeTypeException)9 NodeType (javax.jcr.nodetype.NodeType)6 QNodeTypeDefinitionImpl (org.apache.jackrabbit.spi.commons.QNodeTypeDefinitionImpl)6 HashMap (java.util.HashMap)5 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)5 NodeTypeDefinition (javax.jcr.nodetype.NodeTypeDefinition)4 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)4 InvalidNodeTypeDefException (org.apache.jackrabbit.core.nodetype.InvalidNodeTypeDefException)4 QValueConstraint (org.apache.jackrabbit.spi.QValueConstraint)4 NameException (org.apache.jackrabbit.spi.commons.conversion.NameException)4 QNodeTypeDefinitionBuilder (org.apache.jackrabbit.spi.commons.nodetype.QNodeTypeDefinitionBuilder)4 CompactNodeTypeDefReader (org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader)3 NodeTypeRegistry (org.apache.jackrabbit.core.nodetype.NodeTypeRegistry)3 NamespaceMapping (org.apache.jackrabbit.spi.commons.namespace.NamespaceMapping)3 QDefinitionBuilderFactory (org.apache.jackrabbit.spi.commons.nodetype.QDefinitionBuilderFactory)3 QNodeDefinitionBuilder (org.apache.jackrabbit.spi.commons.nodetype.QNodeDefinitionBuilder)3