use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit-oak by apache.
the class RepositoryTest method importWithRegisteredType.
// OAK-2038
@Test
public void importWithRegisteredType() throws Exception {
Session session = getAdminSession();
NodeTypeManager ntMgr = getAdminSession().getWorkspace().getNodeTypeManager();
NodeTypeTemplate ntd = ntMgr.createNodeTypeTemplate();
ntd.setName("fooType");
PropertyDefinitionTemplate propDefTemplate = ntMgr.createPropertyDefinitionTemplate();
propDefTemplate.setName("fooProp");
propDefTemplate.setRequiredType(PropertyType.STRING);
ntd.getPropertyDefinitionTemplates().add(propDefTemplate);
ntMgr.registerNodeType(ntd, false);
Node node = session.getRootNode().addNode("node", "fooType");
node.setProperty("fooProp", "fooValue");
session.save();
ByteArrayOutputStream out = new ByteArrayOutputStream();
session.exportDocumentView("/node", out, true, false);
node.remove();
session.save();
session.getWorkspace().importXML("/", new ByteArrayInputStream(out.toByteArray()), IMPORT_UUID_CREATE_NEW);
session.save();
assertEquals("fooValue", session.getProperty("/node/fooProp").getString());
}
use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit-oak by apache.
the class CompatibilityIssuesTest method testBinaryCoercion.
@Test
public void testBinaryCoercion() throws RepositoryException, IOException {
Session session = getAdminSession();
// node type with default child-node type of to nt:base
String ntName = "binaryCoercionTest";
NodeTypeManager ntm = session.getWorkspace().getNodeTypeManager();
NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();
ntt.setName(ntName);
PropertyDefinitionTemplate propertyWithType = ntm.createPropertyDefinitionTemplate();
propertyWithType.setName("javaObject");
propertyWithType.setRequiredType(PropertyType.STRING);
PropertyDefinitionTemplate unnamed = ntm.createPropertyDefinitionTemplate();
unnamed.setName("*");
unnamed.setRequiredType(PropertyType.UNDEFINED);
List<PropertyDefinition> properties = ntt.getPropertyDefinitionTemplates();
properties.add(propertyWithType);
properties.add(unnamed);
ntm.registerNodeType(ntt, false);
Node node = session.getRootNode().addNode("testNodeForBinary", ntName);
ByteArrayOutputStream bos = serializeObject("testValue");
node.setProperty("javaObject", session.getValueFactory().createBinary(new ByteArrayInputStream(bos.toByteArray())));
Assert.assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(bos.toByteArray()), node.getProperty("javaObject").getStream()));
}
use of javax.jcr.nodetype.NodeTypeManager in project sling by apache.
the class NodeTypeConfigurationPrinter method printConfiguration.
/**
* {@inheritDoc}
*/
public void printConfiguration(PrintWriter pw, String mode) {
if (slingRepository != null) {
Session session = null;
try {
session = slingRepository.loginAdministrative(null);
NodeTypeManager ntm = session.getWorkspace().getNodeTypeManager();
NodeTypeIterator it = ntm.getAllNodeTypes();
List<NodeType> sortedTypes = sortTypes(it);
for (NodeType nt : sortedTypes) {
pw.printf("[%s]", nt.getName());
printSuperTypes(pw, nt);
if (nt.hasOrderableChildNodes()) {
pw.print(" orderable");
}
if (nt.isMixin()) {
pw.print(" mixin");
}
linebreak(pw, mode);
for (PropertyDefinition prop : nt.getPropertyDefinitions()) {
if (prop.getDeclaringNodeType() == nt) {
startBold(pw, mode);
}
pw.printf("- %s", prop.getName());
printDefaultValues(pw, prop);
if (prop.getName().equals(nt.getPrimaryItemName())) {
pw.print(" primary");
}
if (prop.isMandatory()) {
pw.print(" mandatory");
}
if (prop.isAutoCreated()) {
pw.print(" autocreated");
}
if (prop.isProtected()) {
pw.print(" protected");
}
if (prop.isMultiple()) {
pw.print(" multiple");
}
pw.printf(" %s", OnParentVersionAction.nameFromValue(prop.getOnParentVersion()));
printConstraints(pw, prop);
if (prop.getDeclaringNodeType() == nt) {
stopBold(pw, mode);
}
linebreak(pw, mode);
}
for (NodeDefinition child : nt.getChildNodeDefinitions()) {
if (child.getDeclaringNodeType() == nt) {
startBold(pw, mode);
}
pw.printf("+ %s", child.getName());
printRequiredChildTypes(pw, child);
if (child.getDefaultPrimaryType() != null) {
pw.printf(" = %s", child.getDefaultPrimaryType().getName());
}
if (child.isMandatory()) {
pw.print(" mandatory");
}
if (child.isAutoCreated()) {
pw.print(" autocreated");
}
if (child.isProtected()) {
pw.print(" protected");
}
if (child.allowsSameNameSiblings()) {
pw.print(" multiple");
}
pw.printf(" %s", OnParentVersionAction.nameFromValue(child.getOnParentVersion()));
if (child.getDeclaringNodeType() == nt) {
stopBold(pw, mode);
}
linebreak(pw, mode);
}
linebreak(pw, mode);
}
} catch (RepositoryException e) {
pw.println("Unable to output namespace mappings.");
e.printStackTrace(pw);
} finally {
if (session != null) {
session.logout();
}
}
} else {
pw.println("SlingRepository is not available.");
}
}
use of javax.jcr.nodetype.NodeTypeManager in project sling by apache.
the class NodeTypeDefinitionScanner method registerNodeTypes.
/**
* Registers node types found in classpath in JCR repository.
* @param session Session
* @param nodeTypeResources List of classpath resource URLs pointing to node type definitions
*/
private void registerNodeTypes(Session session, List<String> nodeTypeResources) throws RepositoryException {
ClassLoader classLoader = getClass().getClassLoader();
Workspace workspace = session.getWorkspace();
NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager();
NamespaceRegistry namespaceRegistry = workspace.getNamespaceRegistry();
ValueFactory valueFactory = session.getValueFactory();
// try registering node types multiple times because the exact order is not known
int iteration = 0;
List<String> remainingNodeTypeResources = new ArrayList<String>(nodeTypeResources);
while (!remainingNodeTypeResources.isEmpty()) {
registerNodeTypesAndRemoveSucceeds(remainingNodeTypeResources, classLoader, nodeTypeManager, namespaceRegistry, valueFactory, false);
iteration++;
if (iteration >= MAX_ITERATIONS) {
break;
}
}
if (!remainingNodeTypeResources.isEmpty()) {
registerNodeTypesAndRemoveSucceeds(remainingNodeTypeResources, classLoader, nodeTypeManager, namespaceRegistry, valueFactory, true);
}
}
use of javax.jcr.nodetype.NodeTypeManager in project archiva by apache.
the class JcrMetadataRepository method initialize.
public static void initialize(Session session) throws RepositoryException {
// TODO: consider using namespaces for facets instead of the current approach:
// (if used, check if actually called by normal injection)
// for ( String facetId : metadataFacetFactories.keySet() )
// {
// session.getWorkspace().getNamespaceRegistry().registerNamespace( facetId, facetId );
// }
Workspace workspace = session.getWorkspace();
NamespaceRegistry registry = workspace.getNamespaceRegistry();
if (!Arrays.asList(registry.getPrefixes()).contains("archiva")) {
registry.registerNamespace("archiva", "http://archiva.apache.org/jcr/");
}
NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager();
registerMixinNodeType(nodeTypeManager, JcrMetadataRepository.NAMESPACE_NODE_TYPE);
registerMixinNodeType(nodeTypeManager, JcrMetadataRepository.PROJECT_NODE_TYPE);
registerMixinNodeType(nodeTypeManager, JcrMetadataRepository.PROJECT_VERSION_NODE_TYPE);
registerMixinNodeType(nodeTypeManager, JcrMetadataRepository.ARTIFACT_NODE_TYPE);
registerMixinNodeType(nodeTypeManager, JcrMetadataRepository.FACET_NODE_TYPE);
registerMixinNodeType(nodeTypeManager, JcrMetadataRepository.DEPENDENCY_NODE_TYPE);
}
Aggregations