Skip to content

Instantly share code, notes, and snippets.

@rhauch
Created July 26, 2011 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhauch/1105781 to your computer and use it in GitHub Desktop.
Save rhauch/1105781 to your computer and use it in GitHub Desktop.
JCR Test
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.jcr.PropertyType;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.nodetype.NoSuchNodeTypeException;
import javax.jcr.nodetype.NodeType;
import javax.jcr.nodetype.NodeTypeManager;
import javax.jcr.nodetype.NodeTypeTemplate;
import javax.jcr.nodetype.PropertyDefinitionTemplate;
import org.modeshape.common.collection.Problem;
import org.modeshape.jcr.JcrConfiguration;
import org.modeshape.jcr.JcrEngine;
import org.xml.sax.SAXException;
public class JCRTest {
private static JcrEngine engine;
public static void main( String[] args ) {
try {
startEngine();
try {
Session session = getSession();
Workspace workspace = session.getWorkspace();
getOrCreateNodeType(workspace);
} catch (RepositoryException e) {
e.printStackTrace();
}
// Try getting the new node type with a different session ...
try {
Session session = getSession();
Workspace workspace = session.getWorkspace();
getNodeType(workspace);
} catch (RepositoryException e) {
e.printStackTrace();
}
} finally {
shutdownEngine();
}
// Start a new engine and look for our node type ...
try {
startEngine();
try {
Session session = getSession();
Workspace workspace = session.getWorkspace();
getNodeType(workspace);
} catch (RepositoryException e) {
e.printStackTrace();
}
} finally {
shutdownEngine();
}
}
private static void startEngine() {
if (engine != null) return; // already started
try {
JcrConfiguration config = new JcrConfiguration().loadFrom("src/main/resources/modeshape-config.xml");
engine = config.build();
engine.start();
if (engine.getProblems().hasProblems()) {
for (Problem problem : engine.getProblems()) {
System.err.println(problem.getMessageString());
}
throw new RuntimeException("Could not start due to problems");
}
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
private static void shutdownEngine() {
if (engine == null) return; // not yet started
// Shutdown the engine ...
try {
engine.shutdownAndAwaitTermination(5, TimeUnit.SECONDS);
System.out.println("Successfully shut down ModeShape engine");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
engine = null;
}
}
private static Session getSession() throws RepositoryException {
Repository repository = engine.getRepository("Repo");
Session session = repository.login(); // Logs in with a guest session - works by default.
return session;
}
/**
* Example to add nodetype
*
* @param workspace
*/
private static void getNodeType( Workspace workspace ) {
NodeTypeManager mgr;
try {
mgr = workspace.getNodeTypeManager();
mgr.getAllNodeTypes();
NodeType nodeType = mgr.getNodeType("StandardArticle");
System.out.println("Found node type \"" + nodeType.getName() + "\"");
} catch (RepositoryException e) {
e.printStackTrace();
}
}
/**
* Example to create a node type Article
*
* @param workspace
*/
// TODO method to create node type
@SuppressWarnings( "unchecked" )
private static void getOrCreateNodeType( Workspace workspace ) {
// Obtain the ModeShape-specific node type manager ...
NodeTypeManager nodeTypeManager;
try {
nodeTypeManager = workspace.getNodeTypeManager();
// Check if it's already there ...
try {
NodeType newNodeType = nodeTypeManager.getNodeType("StandardArticle");
System.out.println("Found existing node type \"" + newNodeType.getName() + "\"");
} catch (NoSuchNodeTypeException e) {
// Declare a mixin node type named "searchable" (with no namespace)
NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
nodeType.setName("StandardArticle");
// nodeType.setMixin(true);
// Add a property named "headline"
PropertyDefinitionTemplate headline = nodeTypeManager.createPropertyDefinitionTemplate();
headline.setName("headline");
headline.setMandatory(true);
headline.setRequiredType(PropertyType.STRING);
nodeType.getPropertyDefinitionTemplates().add(headline);
// Add a property named "teaser"
PropertyDefinitionTemplate teaser = nodeTypeManager.createPropertyDefinitionTemplate();
teaser.setName("teaser");
teaser.setMandatory(true);
teaser.setRequiredType(PropertyType.STRING);
nodeType.getPropertyDefinitionTemplates().add(teaser);
// Add a property named "body"
PropertyDefinitionTemplate body = nodeTypeManager.createPropertyDefinitionTemplate();
body.setName("body");
body.setMandatory(true);
body.setRequiredType(PropertyType.STRING);
// Register the custom node type
NodeType createdNodeType = nodeTypeManager.registerNodeType(nodeType, true);
System.out.println("node type name:" + createdNodeType.getName());
// Check that it's still there ...
NodeType newNodeType = nodeTypeManager.getNodeType("StandardArticle");
System.out.println("Created node type \"" + newNodeType.getName() + "\"");
}
} catch (RepositoryException e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns:mode="http://www.modeshape.org/1.0"
xmlns:jcr="http://www.jcp.org/jcr/1.0"
xmlns:nt="http://www.jcp.org/jcr/nt/1.0">
<!-- Define the sources from which content is made available. -->
<mode:sources jcr:primaryType="nt:unstructured">
<mode:source jcr:name="Store" mode:classname="org.modeshape.connector.store.jpa.JpaSource"
mode:model="Simple"
mode:dialect="org.hibernate.dialect.HSQLDialect"
mode:driverClassName="org.hsqldb.jdbcDriver"
mode:username="sa"
mode:password=""
mode:url="jdbc:hsqldb:target/jcr-test-db/db"
mode:maximumConnectionsInPool="2"
mode:referentialIntegrityEnforced="true"
mode:largeValueSizeInBytes="150"
mode:retryLimit="3"
mode:compressData="true"
mode:predefinedWorkspaceNames="default, otherWorkspace, system"
mode:showSql="false"
mode:autoGenerateSchema="disable"
mode:creatingWorkspacesAllowed="true"
mode:defaultWorkspaceName="default"/>
</mode:sources>
<!-- JCR Repositories. This is required, with a separate repository for each JCR repository instance. -->
<mode:repositories>
<mode:repository jcr:name="Repo" mode:source="Store">
<mode:options jcr:primaryType="options" >
<!-- Explicitly specify the "system" workspace in the "SystemStore" source. -->
<mode:option jcr:name="systemSourceName" mode:value="system@Store"/>
</mode:options>
</mode:repository>
</mode:repositories>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment