use of javax.jcr.Repository in project jackrabbit-oak by apache.
the class ConcurrentAddNodesClusterIT method rebaseVisibility.
@Test
public void rebaseVisibility() throws Exception {
for (int i = 0; i < 2; i++) {
DocumentMK mk = new DocumentMK.Builder().setMongoDB(createConnection().getDB()).setAsyncDelay(0).setClusterId(i + 1).open();
mks.add(mk);
}
final DocumentMK mk1 = mks.get(0);
final DocumentMK mk2 = mks.get(1);
Repository r1 = new Jcr(mk1.getNodeStore()).createRepository();
repos.add(r1);
Repository r2 = new Jcr(mk2.getNodeStore()).createRepository();
repos.add(r2);
Session s1 = r1.login(new SimpleCredentials("admin", "admin".toCharArray()));
Session s2 = r2.login(new SimpleCredentials("admin", "admin".toCharArray()));
Node root1 = s1.getRootNode().addNode("session-1");
s1.save();
Node root2 = s2.getRootNode().addNode("session-2");
s2.save();
runBackgroundOps(mk1);
runBackgroundOps(mk2);
runBackgroundOps(mk1);
createNodes(root1, "nodes");
createNodes(root2, "nodes");
s2.save();
runBackgroundOps(mk2);
runBackgroundOps(mk1);
assertFalse(s1.getRootNode().hasNode("session-2/nodes"));
s1.refresh(true);
assertTrue(s1.getRootNode().hasNode("session-2/nodes"));
s1.logout();
s2.logout();
}
use of javax.jcr.Repository in project jackrabbit-oak by apache.
the class ConcurrentAddNodesClusterIT method addNodes.
@Test
public void addNodes() throws Exception {
for (int i = 0; i < 2; i++) {
DocumentMK mk = new DocumentMK.Builder().setMongoDB(createConnection().getDB()).setClusterId(i + 1).open();
mks.add(mk);
}
final DocumentMK mk1 = mks.get(0);
final DocumentMK mk2 = mks.get(1);
Repository r1 = new Jcr(mk1.getNodeStore()).createRepository();
repos.add(r1);
Repository r2 = new Jcr(mk2.getNodeStore()).createRepository();
repos.add(r2);
Session s1 = r1.login(new SimpleCredentials("admin", "admin".toCharArray()));
Session s2 = r2.login(new SimpleCredentials("admin", "admin".toCharArray()));
ensureIndex(s1.getRootNode(), PROP_NAME);
ensureIndex(s2.getRootNode(), PROP_NAME);
Map<String, Exception> exceptions = Collections.synchronizedMap(new HashMap<String, Exception>());
createNodes(s1, "testroot-1", 1, 1, exceptions);
createNodes(s2, "testroot-2", 1, 1, exceptions);
for (Map.Entry<String, Exception> entry : exceptions.entrySet()) {
throw entry.getValue();
}
s1.logout();
s2.logout();
}
use of javax.jcr.Repository in project jackrabbit-oak by apache.
the class ConcurrentAddNodesClusterIT method addNodesConcurrent.
@Test
public void addNodesConcurrent() throws Exception {
for (int i = 0; i < NUM_CLUSTER_NODES; i++) {
DocumentMK mk = new DocumentMK.Builder().setMongoDB(createConnection().getDB()).setClusterId(i + 1).open();
mks.add(mk);
}
Map<String, Exception> exceptions = Collections.synchronizedMap(new HashMap<String, Exception>());
for (int i = 0; i < mks.size(); i++) {
DocumentMK mk = mks.get(i);
Repository repo = new Jcr(mk.getNodeStore()).createRepository();
repos.add(repo);
workers.add(new Thread(new Worker(repo, exceptions), "Worker-" + (i + 1)));
}
for (Thread t : workers) {
t.start();
}
for (Thread t : workers) {
t.join();
}
for (Map.Entry<String, Exception> entry : exceptions.entrySet()) {
// System.out.println("exception in thread " + entry.getKey());
throw entry.getValue();
}
}
use of javax.jcr.Repository in project jackrabbit by apache.
the class JcrUtils method getRepository.
/**
* Looks up the available {@link RepositoryFactory repository factories}
* and returns the {@link Repository repository} that one of the factories
* returns for the given settings.
* <p>
* Note that unlike {@link RepositoryFactory#getRepository(Map)} this
* method will throw an exception instead of returning <code>null</code>
* if the given parameters can not be interpreted.
*
* @param parameters repository settings
* @return repository reference
* @throws RepositoryException if the repository can not be accessed,
* or if an appropriate repository factory
* is not available
*/
public static Repository getRepository(Map<String, String> parameters) throws RepositoryException {
String newline = System.getProperty("line.separator");
// Prepare the potential error message (JCR-2459)
StringBuilder log = new StringBuilder("Unable to access a repository");
if (parameters != null) {
log.append(" with the following settings:");
for (Map.Entry<String, String> entry : parameters.entrySet()) {
log.append(newline);
log.append(" ");
log.append(entry.getKey());
log.append(": ");
log.append(entry.getValue());
}
} else {
log.append(" with the default settings.");
}
// Use the query part of a repository URI as additional parameters
if (parameters != null && parameters.containsKey(JcrUtils.REPOSITORY_URI)) {
String uri = parameters.get(JcrUtils.REPOSITORY_URI);
try {
URI u = new URI(uri);
String query = u.getRawQuery();
if (query != null) {
Map<String, String> copy = new HashMap<String, String>(parameters);
for (String entry : query.split("&")) {
int i = entry.indexOf('=');
if (i != -1) {
copy.put(decode(entry.substring(0, i), "UTF-8"), decode(entry.substring(i + 1), "UTF-8"));
} else {
copy.put(decode(entry, "UTF-8"), Boolean.TRUE.toString());
}
}
copy.put(JcrUtils.REPOSITORY_URI, new URI(u.getScheme(), u.getRawAuthority(), u.getRawPath(), null, u.getRawFragment()).toASCIIString());
parameters = copy;
}
} catch (URISyntaxException e) {
log.append(newline);
log.append("Note that the given repository URI was invalid:");
log.append(newline);
log.append(" ").append(uri);
log.append(newline);
log.append(" ").append(e.getMessage());
} catch (UnsupportedEncodingException e) {
throw new RepositoryException("UTF-8 is not supported!", e);
}
}
// Iterate through the available RepositoryFactories, with logging
log.append(newline);
log.append("The following RepositoryFactory classes were consulted:");
Iterator<RepositoryFactory> iterator = ServiceRegistry.lookupProviders(RepositoryFactory.class);
while (iterator.hasNext()) {
RepositoryFactory factory = iterator.next();
log.append(newline);
log.append(" ");
log.append(factory.getClass().getName());
try {
Repository repository = factory.getRepository(parameters);
if (repository != null) {
// and just ignore the error message being built.
return repository;
} else {
log.append(": declined");
}
} catch (Exception e) {
log.append(": failed");
for (Throwable c = e; c != null; c = c.getCause()) {
log.append(newline);
log.append(" because of ");
log.append(c.getClass().getSimpleName());
log.append(": ");
log.append(c.getMessage());
}
}
}
log.append(newline);
log.append("Perhaps the repository you are trying" + " to access is not available at the moment.");
// detailed information we gathered during the above process.
throw new RepositoryException(log.toString());
}
use of javax.jcr.Repository in project jackrabbit by apache.
the class FirstHop method main.
/**
* The main entry point of the example application.
*
* @param args
* command line arguments (ignored)
* @throws Exception
* if an error occurs
*/
public static void main(String[] args) throws Exception {
Repository repository = JcrUtils.getRepository();
Session session = repository.login(new GuestCredentials());
try {
String user = session.getUserID();
String name = repository.getDescriptor(Repository.REP_NAME_DESC);
System.out.println("Logged in as " + user + " to a " + name + " repository.");
} finally {
session.logout();
}
}
Aggregations