Search in sources :

Example 31 with Repository

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

the class ValidNamesTest method setup.

@Before
public void setup() throws RepositoryException {
    Repository repo = createRepository(fixture);
    Session session = repo.login(getAdminCredentials());
    Node root = session.getRootNode();
    testNode = root.addNode(TEST_NODE);
    session.save();
    StringBuilder t = new StringBuilder();
    for (String prefix : session.getNamespacePrefixes()) {
        int l = t.length();
        if (prefix.length() > l) {
            t.append((char) (prefix.charAt(l) ^ 1));
        } else {
            t.append('x');
        }
    }
    unmappedNsPrefix = t.toString();
    for (String p : testNode.getSession().getNamespacePrefixes()) {
        if (p.length() != 0) {
            String u = testNode.getSession().getNamespaceURI(p);
            if (u.contains(":")) {
                testPrefix = p;
                testNsUri = u;
            }
        }
    }
    assertNotNull(testPrefix);
    assertNotNull(testNsUri);
}
Also used : Repository(javax.jcr.Repository) Node(javax.jcr.Node) Session(javax.jcr.Session) Before(org.junit.Before)

Example 32 with Repository

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

the class ValidNamesTest method tearDown.

@After
public void tearDown() throws RepositoryException {
    Session s = testNode.getSession();
    s.removeItem(TEST_PATH);
    s.save();
    Repository r = s.getRepository();
    s.logout();
    dispose(r);
}
Also used : Repository(javax.jcr.Repository) Session(javax.jcr.Session) After(org.junit.After)

Example 33 with Repository

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

the class OakOSGiRepositoryFactory method getRepository.

@SuppressWarnings("unchecked")
public Repository getRepository(Map parameters) throws RepositoryException {
    if (parameters == null || !parameters.containsKey(REPOSITORY_HOME)) {
        //Required param missing so repository cannot be created
        return null;
    }
    Map config = new HashMap();
    config.putAll(parameters);
    PojoServiceRegistry registry = initializeServiceRegistry(config);
    BundleActivator activator = getApplicationActivator(config);
    try {
        activator.start(registry.getBundleContext());
    } catch (Exception e) {
        log.warn("Error occurred while starting activator {}", activator.getClass(), e);
    }
    //Future which would be used to notify when repository is ready
    // to be used
    SettableFuture<Repository> repoFuture = SettableFuture.create();
    new RunnableJobTracker(registry.getBundleContext());
    int timeoutInSecs = PropertiesUtil.toInteger(config.get(REPOSITORY_TIMEOUT_IN_SECS), DEFAULT_TIMEOUT);
    //Start the tracker for repository creation
    new RepositoryTracker(registry, activator, repoFuture, timeoutInSecs);
    // where OSGi runtime fails to start due to bugs (like cycles)
    try {
        return repoFuture.get(timeoutInSecs, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RepositoryException("Repository initialization was interrupted");
    } catch (ExecutionException e) {
        throw new RepositoryException(e);
    } catch (TimeoutException e) {
        try {
            if (PropertiesUtil.toBoolean(config.get(REPOSITORY_SHUTDOWN_ON_TIMEOUT), true)) {
                shutdown(registry, timeoutInSecs);
                log.info("OSGi container shutdown after waiting for repository initialization for {} sec", timeoutInSecs);
            } else {
                log.warn("[{}] found to be false. Container is not stopped", REPOSITORY_SHUTDOWN_ON_TIMEOUT);
            }
        } catch (BundleException be) {
            log.warn("Error occurred while shutting down the service registry (due to " + "startup timeout) backing the Repository ", be);
        }
        throw new RepositoryException("Repository could not be started in " + timeoutInSecs + " seconds", e);
    }
}
Also used : HashMap(java.util.HashMap) BundleActivator(org.osgi.framework.BundleActivator) RepositoryException(javax.jcr.RepositoryException) TimeoutException(java.util.concurrent.TimeoutException) RepositoryException(javax.jcr.RepositoryException) BundleException(org.osgi.framework.BundleException) ExecutionException(java.util.concurrent.ExecutionException) PojoServiceRegistry(org.apache.felix.connect.launch.PojoServiceRegistry) Repository(javax.jcr.Repository) JackrabbitRepository(org.apache.jackrabbit.api.JackrabbitRepository) BundleException(org.osgi.framework.BundleException) ExecutionException(java.util.concurrent.ExecutionException) HashMap(java.util.HashMap) Map(java.util.Map) TimeoutException(java.util.concurrent.TimeoutException)

Example 34 with Repository

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

the class JsonIndexCommand method openSession.

public static Session openSession(NodeStore nodeStore) throws RepositoryException {
    if (nodeStore == null) {
        return null;
    }
    StatisticsProvider statisticsProvider = StatisticsProvider.NOOP;
    Oak oak = new Oak(nodeStore).with(ManagementFactory.getPlatformMBeanServer());
    oak.getWhiteboard().register(StatisticsProvider.class, statisticsProvider, Collections.emptyMap());
    LuceneIndexProvider provider = createLuceneIndexProvider();
    oak.with((QueryIndexProvider) provider).with((Observer) provider).with(createLuceneIndexEditorProvider());
    Jcr jcr = new Jcr(oak);
    Repository repository = jcr.createRepository();
    return repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) Repository(javax.jcr.Repository) Observer(org.apache.jackrabbit.oak.spi.commit.Observer) Oak(org.apache.jackrabbit.oak.Oak) Jcr(org.apache.jackrabbit.oak.jcr.Jcr) LuceneIndexProvider(org.apache.jackrabbit.oak.plugins.index.lucene.LuceneIndexProvider) StatisticsProvider(org.apache.jackrabbit.oak.stats.StatisticsProvider)

Example 35 with Repository

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

the class UpgradeOldSegmentTest method upgradeFrom10.

@Test
public void upgradeFrom10() throws Exception {
    File testFolder = new File(new File("target"), UpgradeOldSegmentTest.class.getSimpleName());
    FileUtils.deleteDirectory(testFolder);
    File oldRepo = new File(testFolder, "test-repo-1.0");
    oldRepo.mkdirs();
    try (InputStream in = UpgradeOldSegmentTest.class.getResourceAsStream("/test-repo-1.0.zip")) {
        Util.unzip(in, oldRepo);
    }
    SegmentTarNodeStoreContainer newRepoContainer = new SegmentTarNodeStoreContainer();
    OakUpgrade.main("segment-old:" + oldRepo.getPath(), newRepoContainer.getDescription());
    Repository repo = new Jcr(newRepoContainer.open()).createRepository();
    Session s = repo.login(new SimpleCredentials("admin", "admin".toCharArray()));
    Node myType = s.getNode("/jcr:system/jcr:nodeTypes/test:MyType");
    assertEquals(2, Iterators.size(myType.getNodes("jcr:propertyDefinition")));
    NodeTypeManager ntMgr = s.getWorkspace().getNodeTypeManager();
    assertTrue(ntMgr.hasNodeType("test:MyType"));
    NodeType nt = ntMgr.getNodeType("test:MyType");
    PropertyDefinition[] pDefs = nt.getDeclaredPropertyDefinitions();
    assertEquals(2, pDefs.length);
    for (PropertyDefinition pd : pDefs) {
        String name = pd.getName();
        if (name.equals("test:mandatory")) {
            assertTrue(pd.isMandatory());
        } else if (name.equals("test:optional")) {
            assertFalse(pd.isMandatory());
        } else {
            fail("Unexpected property definition: " + name);
        }
    }
    // flip mandatory flag for test:mandatory
    String cnd = "<'test'='http://www.apache.org/jackrabbit/test'>\n" + "[test:MyType] > nt:unstructured\n" + " - test:mandatory (string)\n" + " - test:optional (string)";
    CndImporter.registerNodeTypes(new StringReader(cnd), s, true);
    myType = s.getNode("/jcr:system/jcr:nodeTypes/test:MyType");
    assertEquals(2, Iterators.size(myType.getNodes("jcr:propertyDefinition")));
    nt = ntMgr.getNodeType("test:MyType");
    pDefs = nt.getDeclaredPropertyDefinitions();
    assertEquals(2, pDefs.length);
    for (PropertyDefinition pd : pDefs) {
        String name = pd.getName();
        if (name.equals("test:mandatory")) {
            assertFalse(pd.isMandatory());
        } else if (name.equals("test:optional")) {
            assertFalse(pd.isMandatory());
        } else {
            fail("Unexpected property definition: " + name);
        }
    }
    s.logout();
    if (repo instanceof JackrabbitRepository) {
        ((JackrabbitRepository) repo).shutdown();
    }
    newRepoContainer.close();
    newRepoContainer.clean();
    deleteRecursive(testFolder);
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) InputStream(java.io.InputStream) Node(javax.jcr.Node) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) SimpleCredentials(javax.jcr.SimpleCredentials) Repository(javax.jcr.Repository) JackrabbitRepository(org.apache.jackrabbit.api.JackrabbitRepository) SegmentTarNodeStoreContainer(org.apache.jackrabbit.oak.upgrade.cli.container.SegmentTarNodeStoreContainer) NodeType(javax.jcr.nodetype.NodeType) Jcr(org.apache.jackrabbit.oak.jcr.Jcr) StringReader(java.io.StringReader) JackrabbitRepository(org.apache.jackrabbit.api.JackrabbitRepository) File(java.io.File) Session(javax.jcr.Session) Test(org.junit.Test)

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