Search in sources :

Example 36 with Repository

use of javax.jcr.Repository in project jackrabbit-oak by apache.

the class RepositoryAccessServlet method getRepositoryByJNDI.

/**
     * Checks if the repository is available via JNDI and returns it.
     * @return the repository or <code>null</code>
     * @throws ServletException if this servlet is not properly configured.
     */
private Repository getRepositoryByJNDI() throws ServletException {
    BootstrapConfig config = getConfig();
    if (!config.getJndiConfig().isValid() || !config.getJndiConfig().enabled()) {
        return null;
    }
    // acquire via JNDI
    String repositoryName = config.getRepositoryName();
    InitialContext ctx = getInitialContext();
    if (ctx == null) {
        return null;
    }
    try {
        Repository r = (Repository) ctx.lookup(repositoryName);
        log.info("Acquired repository via JNDI.");
        return r;
    } catch (NamingException e) {
        log.error("Error while retrieving repository using JNDI (name={})", repositoryName, e);
        return null;
    }
}
Also used : Repository(javax.jcr.Repository) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext)

Example 37 with Repository

use of javax.jcr.Repository in project jackrabbit-oak by apache.

the class RemotingInitializer method webDavServlet.

@Bean
public ServletRegistrationBean webDavServlet() {
    ServletRegistrationBean bean = new ServletRegistrationBean(new SimpleWebdavServlet() {

        @Override
        public Repository getRepository() {
            return repository;
        }

        @Override
        public ServletContext getServletContext() {
            return RemotingInitializer.this.getServletContext();
        }
    }, "/repository/*");
    bean.addInitParameter(SimpleWebdavServlet.INIT_PARAM_RESOURCE_PATH_PREFIX, "/repository");
    bean.addInitParameter(SimpleWebdavServlet.INIT_PARAM_RESOURCE_CONFIG, "/remoting/webdav-config.xml");
    return bean;
}
Also used : Repository(javax.jcr.Repository) ServletRegistrationBean(org.springframework.boot.context.embedded.ServletRegistrationBean) SimpleWebdavServlet(org.apache.jackrabbit.webdav.simple.SimpleWebdavServlet) ServletContext(javax.servlet.ServletContext) ServletRegistrationBean(org.springframework.boot.context.embedded.ServletRegistrationBean) Bean(org.springframework.context.annotation.Bean)

Example 38 with Repository

use of javax.jcr.Repository in project jackrabbit-oak by apache.

the class ConcurrentAddNodesClusterIT method addNodesConcurrent2.

@Ignore("OAK-1807")
@Test
public void addNodesConcurrent2() throws Exception {
    final Thread mainThread = Thread.currentThread();
    for (int i = 0; i < NUM_CLUSTER_NODES; i++) {
        DocumentMK mk = new DocumentMK.Builder().setMongoDB(createConnection().getDB()).setClusterId(i + 1).open();
        mks.add(mk);
    }
    final Map<String, Exception> exceptions = Collections.synchronizedMap(new HashMap<String, Exception>());
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean stop = new AtomicBoolean();
    final UncaughtExceptionHandler ueh = new UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            RuntimeException r = new RuntimeException("Exception in thread " + t.getName(), e);
            r.printStackTrace();
        }
    };
    for (int i = 0; i < mks.size(); i++) {
        DocumentMK mk = mks.get(i);
        final Repository repo = new Jcr(mk.getNodeStore()).createRepository();
        repos.add(repo);
        for (int w = 0; w <= WORKER_COUNT; w++) {
            final String name = "Worker-" + (i + 1) + "-" + (w + 1);
            final Runnable r = new Runnable() {

                final Session session = createAdminSession(repo);

                int count = 0;

                @Override
                public void run() {
                    try {
                        Uninterruptibles.awaitUninterruptibly(latch);
                        session.refresh(false);
                        Node node = session.getRootNode().addNode(name + count++, "oak:Unstructured");
                        for (int j = 0; j < NODE_COUNT && !stop.get(); j++) {
                            node.addNode("node" + j);
                            session.save();
                        }
                    } catch (RepositoryException e) {
                        RuntimeException r = new RuntimeException("Exception in thread " + name, e);
                        r.printStackTrace();
                        exceptions.put(Thread.currentThread().getName(), r);
                        stop.set(true);
                        mainThread.interrupt();
                    } finally {
                        session.logout();
                    }
                }
            };
            //Last runnable would be a long running one
            Runnable runnable = r;
            if (w == WORKER_COUNT) {
                runnable = new Runnable() {

                    @Override
                    public void run() {
                        while (!stop.get()) {
                            r.run();
                        }
                    }
                };
            }
            Thread t = new Thread(runnable);
            t.setName(name);
            t.setUncaughtExceptionHandler(ueh);
            workers.add(t);
        }
    }
    for (Thread t : workers) {
        t.start();
    }
    latch.countDown();
    TimeUnit.MINUTES.sleep(10);
    stop.set(true);
    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) Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) CountDownLatch(java.util.concurrent.CountDownLatch) RepositoryException(javax.jcr.RepositoryException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Repository(javax.jcr.Repository) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) HashMap(java.util.HashMap) Map(java.util.Map) Session(javax.jcr.Session) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 39 with Repository

use of javax.jcr.Repository in project jackrabbit-oak by apache.

the class ConcurrentAddNodesClusterIT method addNodes2.

@Test
public void addNodes2() throws Exception {
    for (int i = 0; i < 3; 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);
    final DocumentMK mk3 = mks.get(2);
    Repository r1 = new Jcr(mk1.getNodeStore()).createRepository();
    repos.add(r1);
    Repository r2 = new Jcr(mk2.getNodeStore()).createRepository();
    repos.add(r2);
    Repository r3 = new Jcr(mk3.getNodeStore()).createRepository();
    repos.add(r3);
    Session s1 = r1.login(new SimpleCredentials("admin", "admin".toCharArray()));
    Session s2 = r2.login(new SimpleCredentials("admin", "admin".toCharArray()));
    Session s3 = r3.login(new SimpleCredentials("admin", "admin".toCharArray()));
    ensureIndex(s1.getRootNode(), PROP_NAME);
    runBackgroundOps(mk1);
    runBackgroundOps(mk2);
    runBackgroundOps(mk3);
    // begin test
    Node root2 = s2.getRootNode().addNode("testroot-Worker-2", "nt:unstructured");
    createNodes(root2, "testnode0");
    s2.save();
    createNodes(root2, "testnode1");
    runBackgroundOps(mk1);
    runBackgroundOps(mk3);
    // publish 'testroot-Worker-2/testnode0'
    runBackgroundOps(mk2);
    Node root3 = s3.getRootNode().addNode("testroot-Worker-3", "nt:unstructured");
    createNodes(root3, "testnode0");
    s2.save();
    createNodes(root2, "testnode2");
    // sees 'testroot-Worker-2/testnode0'
    runBackgroundOps(mk1);
    // sees 'testroot-Worker-2/testnode0'
    runBackgroundOps(mk3);
    // publish 'testroot-Worker-2/testnode1'
    runBackgroundOps(mk2);
    // subsequent read on mk3 will read already published docs from mk2
    s3.save();
    createNodes(root3, "testnode1");
    Node root1 = s1.getRootNode().addNode("testroot-Worker-1", "nt:unstructured");
    createNodes(root1, "testnode0");
    s2.save();
    createNodes(root2, "testnode3");
    runBackgroundOps(mk1);
    runBackgroundOps(mk3);
    runBackgroundOps(mk2);
    s1.save();
    createNodes(root1, "testnode1");
    s3.save();
    createNodes(root3, "testnode2");
    runBackgroundOps(mk1);
    s1.save();
    s1.logout();
    s2.logout();
    s3.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 40 with Repository

use of javax.jcr.Repository in project jackrabbit-oak by apache.

the class ConcurrentAddNodesClusterIT method after.

@After
public void after() throws Exception {
    workers.clear();
    for (Repository repo : repos) {
        dispose(repo);
    }
    repos.clear();
    for (DocumentMK mk : mks) {
        mk.dispose();
    }
    mks.clear();
    dropDB();
}
Also used : Repository(javax.jcr.Repository) DocumentMK(org.apache.jackrabbit.oak.plugins.document.DocumentMK) After(org.junit.After)

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