package org.apache.jackrabbit.firsthops;
import java.io.File;
import java.io.FileInputStream;
import java.util.Calendar;
import java.util.Hashtable;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.Value;
import javax.jcr.Workspace;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.jackrabbit.core.jndi.RegistryHelper;
import org.apache.jackrabbit.core.jndi.provider.DummyInitialContextFactory;
import org.apache.jackrabbit.value.BinaryImpl;
import sun.net.www.MimeTable;
@SuppressWarnings("restriction")
public class TestJNDI {
/**
* @param args
*/
public static void main(String[] args) {
String configFile = "repository.xml";
String repHomeDir = "repository";
Hashtable<String, Object> env = new Hashtable<String, Object>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
DummyInitialContextFactory.class.getName());
env.put(Context.PROVIDER_URL, "localhost");
try {
InitialContext ctx = new InitialContext(env);
RegistryHelper.registerRepository(ctx, "repo", configFile, repHomeDir,
true);
Repository r = (Repository) ctx.lookup("repo");
SimpleCredentials cred = new SimpleCredentials("admin","admin".toCharArray());
Session session = r.login(cred, null);
Node rn = session.getRootNode();
Workspace ws = session.getWorkspace();
ws.getNamespaceRegistry().registerNamespace("wiki","http://localhost/wiki/1.0");
Node encyclopedia = rn.addNode("wiki:encyclopedia");
Node p = encyclopedia.addNode("wiki:entry");
p.setProperty("wiki:title", toStringValue("rose"));
p.setProperty("wiki:content",toStringValue("A rose is a flowering shrub."));
p.setProperty("wiki:category", new Value[] { toStringValue("flower"),toStringValue("plant"), toStringValue("rose") });
Node n = encyclopedia.addNode("wiki:entry");
n.setProperty("wiki:title", toStringValue("Shakespeare"));
n.setProperty("wiki:content",toStringValue("A famous poet who likes roses."));
n.setProperty("wiki:category", toStringValue("poet"));
// Save
session.save();
// Query
QueryManager qm = ws.getQueryManager();
Query q = qm.createQuery("//wiki:encyclopedia/wiki:entry[@wiki:title = 'rose']",Query.XPATH);// deprecated
QueryResult result = q.execute();// Query
NodeIterator it = result.getNodes();
// Iterator
while (it.hasNext()) {
Node entry = it.nextNode();
entry.getUUID();
// print
System.out.println(entry.toString());
}
// save file
File file = new File("f:\\2012-03-07_111233.txt");
MimeTable mt = MimeTable.getDefaultTable();
String mimeType = mt.getContentTypeFor(file.getName());
if (mimeType == null) {
mimeType = "application/octet-stream";
}
Node fileNode = rn.addNode(file.getName(), "nt:file");
Node resNode = fileNode.addNode("jcr:content", "nt:resource");
resNode.setProperty("jcr:mimeType", mimeType);
resNode.setProperty("jcr:encoding", "");
// stream
// deprecated
// resNode.setProperty("jcr:data", new FileInputStream(file));
// let Binary change to FileInputStream
Binary fileBinary = new BinaryImpl(new FileInputStream(file));
resNode.setProperty("jcr:data", fileBinary);
Calendar lastModified = Calendar.getInstance();
lastModified.setTimeInMillis(file.lastModified());
resNode.setProperty("jcr:lastModified", lastModified);
// save
session.save();
// Iterator print
printAll(rn);
} catch (Exception e) {
e.printStackTrace();
}
}
private static org.apache.jackrabbit.value.StringValue toStringValue(
String str) {
return new org.apache.jackrabbit.value.StringValue(str);
}
private static void printAll(Node node) throws RepositoryException {
printFormat(node.toString());
PropertyIterator propertys = node.getProperties();
while (propertys.hasNext()) {
Property entry = propertys.nextProperty();
if (entry.isMultiple()) {
Value[] values = entry.getValues();
if (values == null) {
continue;
}
for (Value v : values) {
printFormat(v.getString());
}
} else {
printFormat(entry.getValue().getString());
}
}
NodeIterator entries = node.getNodes();
while (entries.hasNext()) {
Node entry = entries.nextNode();
printAll(entry);
}
}
private static void printFormat(Object str) {
System.out.println("####################:" + str);
}
}
import java.io.File;
import java.io.FileInputStream;
import java.util.Calendar;
import java.util.Hashtable;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.Value;
import javax.jcr.Workspace;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.jackrabbit.core.jndi.RegistryHelper;
import org.apache.jackrabbit.core.jndi.provider.DummyInitialContextFactory;
import org.apache.jackrabbit.value.BinaryImpl;
import sun.net.www.MimeTable;
@SuppressWarnings("restriction")
public class TestJNDI {
/**
* @param args
*/
public static void main(String[] args) {
String configFile = "repository.xml";
String repHomeDir = "repository";
Hashtable<String, Object> env = new Hashtable<String, Object>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
DummyInitialContextFactory.class.getName());
env.put(Context.PROVIDER_URL, "localhost");
try {
InitialContext ctx = new InitialContext(env);
RegistryHelper.registerRepository(ctx, "repo", configFile, repHomeDir,
true);
Repository r = (Repository) ctx.lookup("repo");
SimpleCredentials cred = new SimpleCredentials("admin","admin".toCharArray());
Session session = r.login(cred, null);
Node rn = session.getRootNode();
Workspace ws = session.getWorkspace();
ws.getNamespaceRegistry().registerNamespace("wiki","http://localhost/wiki/1.0");
Node encyclopedia = rn.addNode("wiki:encyclopedia");
Node p = encyclopedia.addNode("wiki:entry");
p.setProperty("wiki:title", toStringValue("rose"));
p.setProperty("wiki:content",toStringValue("A rose is a flowering shrub."));
p.setProperty("wiki:category", new Value[] { toStringValue("flower"),toStringValue("plant"), toStringValue("rose") });
Node n = encyclopedia.addNode("wiki:entry");
n.setProperty("wiki:title", toStringValue("Shakespeare"));
n.setProperty("wiki:content",toStringValue("A famous poet who likes roses."));
n.setProperty("wiki:category", toStringValue("poet"));
// Save
session.save();
// Query
QueryManager qm = ws.getQueryManager();
Query q = qm.createQuery("//wiki:encyclopedia/wiki:entry[@wiki:title = 'rose']",Query.XPATH);// deprecated
QueryResult result = q.execute();// Query
NodeIterator it = result.getNodes();
// Iterator
while (it.hasNext()) {
Node entry = it.nextNode();
entry.getUUID();
System.out.println(entry.toString());
}
// save file
File file = new File("f:\\2012-03-07_111233.txt");
MimeTable mt = MimeTable.getDefaultTable();
String mimeType = mt.getContentTypeFor(file.getName());
if (mimeType == null) {
mimeType = "application/octet-stream";
}
Node fileNode = rn.addNode(file.getName(), "nt:file");
Node resNode = fileNode.addNode("jcr:content", "nt:resource");
resNode.setProperty("jcr:mimeType", mimeType);
resNode.setProperty("jcr:encoding", "");
// stream
// deprecated
// resNode.setProperty("jcr:data", new FileInputStream(file));
// let Binary change to FileInputStream
Binary fileBinary = new BinaryImpl(new FileInputStream(file));
resNode.setProperty("jcr:data", fileBinary);
Calendar lastModified = Calendar.getInstance();
lastModified.setTimeInMillis(file.lastModified());
resNode.setProperty("jcr:lastModified", lastModified);
// save
session.save();
// Iterator print
printAll(rn);
} catch (Exception e) {
e.printStackTrace();
}
}
private static org.apache.jackrabbit.value.StringValue toStringValue(
String str) {
return new org.apache.jackrabbit.value.StringValue(str);
}
private static void printAll(Node node) throws RepositoryException {
printFormat(node.toString());
PropertyIterator propertys = node.getProperties();
while (propertys.hasNext()) {
Property entry = propertys.nextProperty();
if (entry.isMultiple()) {
Value[] values = entry.getValues();
if (values == null) {
continue;
}
for (Value v : values) {
printFormat(v.getString());
}
} else {
printFormat(entry.getValue().getString());
}
}
NodeIterator entries = node.getNodes();
while (entries.hasNext()) {
Node entry = entries.nextNode();
printAll(entry);
}
}
private static void printFormat(Object str) {
System.out.println("####################:" + str);
}
}