Search in sources :

Example 41 with Repository

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();
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) Repository(javax.jcr.Repository) DocumentMK(org.apache.jackrabbit.oak.plugins.document.DocumentMK) Node(javax.jcr.Node) Session(javax.jcr.Session) Test(org.junit.Test)

Example 42 with Repository

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();
}
Also used : DocumentMK(org.apache.jackrabbit.oak.plugins.document.DocumentMK) RepositoryException(javax.jcr.RepositoryException) SimpleCredentials(javax.jcr.SimpleCredentials) Repository(javax.jcr.Repository) HashMap(java.util.HashMap) Map(java.util.Map) Session(javax.jcr.Session) Test(org.junit.Test)

Example 43 with Repository

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();
    }
}
Also used : DocumentMK(org.apache.jackrabbit.oak.plugins.document.DocumentMK) RepositoryException(javax.jcr.RepositoryException) Repository(javax.jcr.Repository) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 44 with Repository

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());
}
Also used : HashMap(java.util.HashMap) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RepositoryException(javax.jcr.RepositoryException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) PathNotFoundException(javax.jcr.PathNotFoundException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Repository(javax.jcr.Repository) RepositoryFactory(javax.jcr.RepositoryFactory) HashMap(java.util.HashMap) Map(java.util.Map)

Example 45 with Repository

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();
    }
}
Also used : Repository(javax.jcr.Repository) GuestCredentials(javax.jcr.GuestCredentials) Session(javax.jcr.Session)

Aggregations

Repository (javax.jcr.Repository)111 Session (javax.jcr.Session)33 RepositoryException (javax.jcr.RepositoryException)28 SimpleCredentials (javax.jcr.SimpleCredentials)15 Node (javax.jcr.Node)14 Test (org.junit.Test)13 HashMap (java.util.HashMap)12 DocumentMK (org.apache.jackrabbit.oak.plugins.document.DocumentMK)10 SlingRepository (org.apache.sling.jcr.api.SlingRepository)10 JackrabbitRepository (org.apache.jackrabbit.api.JackrabbitRepository)9 Oak (org.apache.jackrabbit.oak.Oak)7 Jcr (org.apache.jackrabbit.oak.jcr.Jcr)7 IOException (java.io.IOException)6 Map (java.util.Map)6 InitialContext (javax.naming.InitialContext)6 NamingException (javax.naming.NamingException)6 ServletContext (javax.servlet.ServletContext)5 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)5 File (java.io.File)4 OutputStreamWriter (java.io.OutputStreamWriter)4