Jackrabbit configuration
April 16th, 2010
No comments
1. Download the jackrabbit jca from http://jackrabbit.apache.org/downloads.html 2. Deploy it on glassfish 3. Create a new Resource Adapter Configuration (Create a new Thread-pool first if you want) 4. Create a new Connector Connection Pool with the new Resource Adapter Configuration Add the following properties: homeDir <full path to where your repository is located> configFile <full path to where your repository config (repository.xml) is located> 5. Create the connector resource and name it 'jcr/repository' for example. |
You can now access your repository in the code. Below is a sample.
package com.famvdploeg.jackrabbit; import javax.annotation.Resource; import javax.ejb.Stateless; import javax.jcr.Node; import javax.jcr.Repository; import javax.jcr.Session; import javax.jcr.SimpleCredentials; /** * * @author wytze */ @Stateless public class JackrabbitManager { @Resource(mappedName="jcr/repository", type=javax.jcr.Repository.class) private Repository repository; public String getFromRepo() { try { Session session = repository.login(new SimpleCredentials("username", "password".toCharArray())); Node root = session.getRootNode(); Node hello = root.addNode("hello"); hello.setProperty("message", "Hello, World!"); session.save(); // Retrieve content Node node = root.getNode("hello"); System.out.println(node.getPath()); System.out.println(node.getProperty("message").getString()); root.getNode("hello").remove(); return "Created and removed!"; } catch (Exception ex) { return ex.getMessage(); } } } |
A possible Java EE 6 Servlet which you can use for WebDAV support. You will need jackrabbit-jcr-server for the base servlet. You can get it by building the source package with maven.
package com.famvdploeg.jackrabbit; import javax.annotation.PostConstruct; import javax.ejb.EJB; import javax.jcr.Repository; import javax.servlet.annotation.WebServlet; import javax.servlet.annotation.WebInitParam; /** * * @author wytze */ @WebServlet( name = "webdav", urlPatterns = "/repository/*", initParams = { @WebInitParam(name = "resource-path-prefix", value = "/repository"), @WebInitParam(name = "missing-auth-mapping", value = "admin:admin"), @WebInitParam(name = "resource-config", value = "/WEB-INF/config.xml") } ) public class SimpleWebdavServlet extends org.apache.jackrabbit.webdav.simple.SimpleWebdavServlet { @EJB private RepositoryFactory repositoryFactory; private Repository repository; @PostConstruct public void postConstruct() { repository = repositoryFactory.getRepository(); } @Override public Repository getRepository() { return repository; } } |
Possible extended configuration of the repository:
<!-- Store all items larger than 256 bytes in the FileDataStore --> <DataStore class="org.apache.jackrabbit.core.data.FileDataStore"> <param name="path" value="${rep.home}/repository/datastore"/> <param name="minRecordLength" value="256"/> </DataStore> |