There are not many resources on the net that shows how to setup up JackRabbit with database. At least I couldn’t find it. There are many reasons you would want to choose to implement the storage in database rather than file system. Well I’m not going to talk about that here. You read an interesting discussion on TheServerSide.
I’m listing here the bare minium Java Code and the Repository Configuration XML required. The sample code creates root node and the few child nodes. The sample code also shows how to setup property and attachments at different nodes.
Few Notes
- RegistryHelper.registerRepository – Takes a parameter for filesystem folder name. We are storing our content in database, but for some reason we still need to provide the local file system folder it stores nothing but the workspace.xml
- If you got you repository configuration incorrect for the first time, you need to go and clean up the workspaces xml file in the filesystem folder. JackRabbit caches the workspace xml file and does not overwrite you new configuration
Java Code
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.Hashtable; import javax.jcr.Node; import javax.jcr.Repository; import javax.jcr.RepositoryException; import javax.jcr.Session; import javax.jcr.SimpleCredentials; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import org.apache.jackrabbit.core.jndi.RegistryHelper; public class MyContentRepository { public static void main(String[] args) { try { String configFile = "myrepository.xml"; String repHomeDir = "myrepository"; Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.jackrabbit.core.jndi" + ".provider.DummyInitialContextFactory"); env.put(Context.PROVIDER_URL, "localhost"); InitialContext ctx = new InitialContext( env ); // we are storing our content in database, but for some reason // we still need to provide the local file system folder // it stores nothing but the workspace.xml RegistryHelper.registerRepository(ctx, "myrepository", configFile, repHomeDir, true); Repository repository = (Repository) ctx.lookup("myrepository"); // You need to login otherwise you will get access denied exception Session session = repository.login(new SimpleCredentials("username", "password".toCharArray())); // Create Nodes Node root = session.getRootNode(); Node client = root.addNode("client"); Node clientNo = client.addNode("clientNo"); Node clientDocument = clientNo.addNode("clientDoc", "nt:file"); Node resource = clientDocument.addNode("jcr:content", "nt:resource"); // Store some properties clientNo.setProperty("Number", "1234"); // Store document File file = new File("C:\\clientdocument.pdf"); InputStream in = new FileInputStream(file); resource.setProperty("jcr:mimeType", "application/pdf"); resource.setProperty("jcr:data", in); resource.setProperty("jcr:lastModified", System.currentTimeMillis()); session.save(); // use this uuid to find your content String uuid = resource.getUUID(); session.save(); } catch (NamingException e) { e.printStackTrace(); } catch (RepositoryException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
<?xml version="1.0"?> <!DOCTYPE Repository PUBLIC "-//The Apache Software Foundation//DTD Jackrabbit 1.4//EN" "http://jackrabbit.apache.org/dtd/repository-1.4.dtd"> <Repository> <FileSystem class="org.apache.jackrabbit.core.fs.local.LocalFileSystem"> <param name="path" value="${rep.home}/repository"/> </FileSystem> <Security appName="Jackrabbit"> <AccessManager class="org.apache.jackrabbit.core.security.SimpleAccessManager"> </AccessManager> <LoginModule class="org.apache.jackrabbit.core.security.SimpleLoginModule"> <param name="anonymousId" value="anonymous"/> </LoginModule> </Security> <Workspaces rootPath="${rep.home}/workspaces" defaultWorkspace="default"/> <Workspace name="${wsp.name}"> <FileSystem class="org.apache.jackrabbit.core.fs.local.LocalFileSystem"> <param name="path" value="${wsp.home}"/> </FileSystem> <PersistenceManager class="org.apache.jackrabbit.core.persistence.bundle.MySqlPersistenceManager"> <param name="url" value="jdbc:mysql://localhost:3306/jrschema"/> <param name="user" value="jruser"/> <param name="password" value="jrpassword"/> <param name="schema" value="mysql"/> <param name="schemaObjectPrefix" value="jr_wksp_"/> </PersistenceManager> </Workspace> <Versioning rootPath="${rep.home}/version"> <FileSystem class="org.apache.jackrabbit.core.fs.local.LocalFileSystem"> <param name="path" value="${rep.home}/version" /> </FileSystem> <PersistenceManager class="org.apache.jackrabbit.core.persistence.bundle.MySqlPersistenceManager"> <param name="url" value="jdbc:mysql://localhost:3306/jrschema"/> <param name="user" value="jruser"/> <param name="password" value="jrpassword"/> <param name="schema" value="mysql"/> <param name="schemaObjectPrefix" value="jr_version_"/> </PersistenceManager> </Versioning> </Repository>