Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Jimmy Hawkins   on Feb 26 In Java Category.

  
Question Answered By: Vilhelm Fischer   on Feb 26

I have done some samples on Jackrabbit.... I found this particular of code in my old works. it works. I can send you whole of the samples if you need... I have followed a tutorial to find this.


package toorin.jcr.repo;
import javax.jcr.Repository;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.Node;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.apache.jackrabbit.core.jndi.RegistryHelper;
import org.apache.jackrabbit.value.StringValue;
import org.apache.jackrabbit.value.DateValue;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.Hashtable;
import java.util.Calendar;

public class RepoClientJNDI {
protected Log logger = LogFactory.getLog(getClass());
public String connect() throws Exception {
logger.info(" .'.'.'.'.'.'.'.'. Start .'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.' ");
StringBuffer result = new StringBuffer();
String configFile = "toorin_repository/repository.xml";
String repHomeDir = "toorin_repository";

Hashtable env = new Hashtable();
// JNDI Directory service...

env.put(Context.INITIAL_CONTEXT_FACTORY , "org.apache.jackrabbit.core.jndi.provider.DummyInitialContextFactory");
env.put(Context.PROVIDER_URL,"localhost");
InitialContext ctx = new InitialContext(env);
RegistryHelper.registerRepository(ctx,"toorin_repo" , configFile , repHomeDir , true);
Repository r = (Repository)ctx.lookup("toorin_repo");
Session session = r.login(new SimpleCredentials("username", "password".toCharArray()),null);

try {
/*
Root
\
\
hello
\
\
world --+ message : hello, world!
|
+ prop1 : it is a content.
*/
Node root = session.getRootNode();
Node n = root.addNode("blog"); // root node is here
n.setProperty("blogtitle", new StringValue("Chasing Jackrabbit article"));
n.setProperty("blogauthor", new StringValue("Joe Blogger"));
n.setProperty("blogdate", new DateValue(Calendar.getInstance()));
n.setProperty("blogtext", new StringValue("JCR is an interesting API to lo learn."));
/*
We can subsequently look up the nodes in the repository via any of
the query mechanisms supported by a repository. JCR compliant repositories
must support at least XPATH, but Jackrabbit also supports SQL. The following
code snippet finds and lists all blog entries written by "Joe Blogger:"
*/
session.save();
Node node = root.getNode("blog/");
result.append(node.getPath());
result.append(node.getProperty("message").getString());
result.append(node.getProperty("prop1").getString());
result.append(node.getProperty("prop2").getString());
root.getNode("hello").remove();
result.append("hello node ");
session.save();
} finally {
session.logout();
logger.info(" .'.'.'.'.'.'.'.'. End .'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.' ");
}
return result.toString();
}
}

Share: 

 

This Question has 7 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on JCR in java Or get search suggestion and latest updates.


Tagged: