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());
}
}
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);
}
}
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);
}
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;
}
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();
}
Aggregations