Search in sources :

Example 46 with Repository

use of javax.jcr.Repository in project sling by apache.

the class DescriptorHelper method setDescriptor.

public static void setDescriptor(ResourceResolverFactory factory, String key, String value) throws Exception {
    ResourceResolver resourceResolver = factory.getServiceResourceResolver(null);
    try {
        Session session = resourceResolver.adaptTo(Session.class);
        if (session == null) {
            return;
        }
        Repository repo = session.getRepository();
        //<hack>
        //            Method setDescriptorMethod = repo.getClass().
        //                    getDeclaredMethod("setDescriptor", String.class, String.class);
        //            if (setDescriptorMethod!=null) {
        //                setDescriptorMethod.setAccessible(true);
        //                setDescriptorMethod.invoke(repo, key, value);
        //            } else {
        //                fail("could not get 'setDescriptor' method");
        //            }
        Method getDescriptorsMethod = repo.getClass().getDeclaredMethod("getDescriptors");
        if (getDescriptorsMethod == null) {
            fail("could not get 'getDescriptors' method");
        } else {
            getDescriptorsMethod.setAccessible(true);
            GenericDescriptors descriptors = (GenericDescriptors) getDescriptorsMethod.invoke(repo);
            SimpleValueFactory valueFactory = new SimpleValueFactory();
            descriptors.put(key, valueFactory.createValue(value), true, true);
        }
        //</hack>
        //<verify-hack>
        assertEquals(value, repo.getDescriptor(key));
    //</verify-hack>
    } finally {
        if (resourceResolver != null) {
            resourceResolver.close();
        }
    }
}
Also used : GenericDescriptors(org.apache.jackrabbit.oak.util.GenericDescriptors) Repository(javax.jcr.Repository) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Method(java.lang.reflect.Method) SimpleValueFactory(org.apache.jackrabbit.commons.SimpleValueFactory) Session(javax.jcr.Session)

Example 47 with Repository

use of javax.jcr.Repository in project sling by apache.

the class AbstractSlingRepository2 method login.

/**
     * Logs into the repository at the given {@code workspace} with the given
     * {@code credentials} and returns the session returned from
     * the repository.
     * <p>
     * This method logs in as a guest if {@code null} credentials are provided.
     * The method may be overwritten to implement a different behaviour as
     * indicated by the JCR specification for this method to use external
     * mechanisms to login instead of leveraging provided credentials.
     * <p>
     * This method may be overwritten.
     *
     * @param credentials The {@code Credentials} to use to login. If this is
     *            {@code null} JCR {@code GuestCredentials} are used to login.
     * @param workspace The workspace to access or {@code null} to access the
     *            {@link #getDefaultWorkspace() default workspace}
     * @throws LoginException If login is not possible
     * @throws NoSuchWorkspaceException if the desired workspace is not
     *             available
     * @throws RepositoryException If another error occurrs during login
     * @see #getNamespaceAwareSession(Session)
     */
@Override
public Session login(Credentials credentials, String workspace) throws LoginException, NoSuchWorkspaceException, RepositoryException {
    if (credentials == null) {
        credentials = new GuestCredentials();
    }
    // check the workspace
    if (workspace == null) {
        workspace = this.getDefaultWorkspace();
    }
    try {
        logger.debug("login: Logging in to workspace '" + workspace + "'");
        final Repository repository = this.getRepository();
        if (this.getRepository() == null) {
            throw new RepositoryException("Sling Repository not ready");
        }
        final Session session = repository.login(credentials, workspace);
        return session;
    } catch (final RuntimeException re) {
        // repository has already been shut down ...
        throw new RepositoryException(re.getMessage(), re);
    }
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) Repository(javax.jcr.Repository) RepositoryException(javax.jcr.RepositoryException) GuestCredentials(javax.jcr.GuestCredentials) Session(javax.jcr.Session)

Example 48 with Repository

use of javax.jcr.Repository in project sling by apache.

the class AbstractSlingRepository2 method getDescriptorValues.

@Override
public Value[] getDescriptorValues(String key) {
    Repository repo = getRepository();
    if (repo != null) {
        return repo.getDescriptorValues(key);
    }
    logger.error("getDescriptorValues: Repository not available");
    return null;
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) Repository(javax.jcr.Repository)

Example 49 with Repository

use of javax.jcr.Repository in project sling by apache.

the class RepositoryAccessor method getRepository.

/**
     * First try to access the Repository via JNDI (unless jndiContext is null),
     * and if not successful try RMI.
     *
     * @param repositoryName JNDI name or RMI URL (must start with "rmi://") of
     *            the Repository
     * @param jndiContext if null, JNDI is not tried
     * @return a Repository, or null if not found
     */
public Repository getRepository(String repositoryName, Hashtable<String, Object> jndiContext) {
    Repository result = null;
    String tried = "";
    if (jndiContext == null || jndiContext.size() == 0) {
        log.info("jndiContext is null or empty, not trying JNDI");
    } else {
        log.debug("Trying to acquire Repository '" + repositoryName + "' via JNDI, context=" + jndiContext);
        tried += "JNDI ";
        final ClassLoader old = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
            InitialContext initialContext = new InitialContext(jndiContext);
            Object repoObject = initialContext.lookup(repositoryName);
            if (repoObject instanceof Repository) {
                result = (Repository) repoObject;
                log.info("Acquired Repository '" + repositoryName + "' via JNDI");
            } else if (repoObject instanceof RemoteRepository) {
                RemoteRepository remoteRepo = (RemoteRepository) repoObject;
                LocalAdapterFactory laf = getLocalAdapterFactory();
                result = laf.getRepository(remoteRepo);
                log.info("Acquired RemoteRepository '" + repositoryName + "' via JNDI");
            } else {
                log.info("Repository '" + repositoryName + "' acquired via JDNI " + "does not implement the required interfaces, class=" + repoObject.getClass().getName());
            }
        } catch (Throwable t) {
            log.info("Unable to acquire Repository '" + repositoryName + "' via JNDI, context=" + jndiContext, t);
        } finally {
            Thread.currentThread().setContextClassLoader(old);
        }
    }
    if (result == null) {
        if (repositoryName == null || !repositoryName.startsWith(RMI_PREFIX)) {
            log.info("Repository name does not start with '" + RMI_PREFIX + "', not trying RMI");
        } else {
            try {
                tried += "RMI ";
                log.debug("Trying to acquire Repository '" + repositoryName + "' via RMI");
                ClientRepositoryFactory crf = getClientRepositoryFactory();
                result = crf.getRepository(repositoryName);
                log.info("Acquired Repository '" + repositoryName + "' via RMI");
            } catch (Throwable t) {
                log.info("Unable to acquire Repository '" + repositoryName + "' via RMI", t);
            }
        }
    }
    if (result == null) {
        log.info("Unable to acquire Repository '" + repositoryName + "', tried " + tried);
    }
    return result;
}
Also used : Repository(javax.jcr.Repository) RemoteRepository(org.apache.jackrabbit.rmi.remote.RemoteRepository) RemoteRepository(org.apache.jackrabbit.rmi.remote.RemoteRepository) LocalAdapterFactory(org.apache.jackrabbit.rmi.client.LocalAdapterFactory) ClientRepositoryFactory(org.apache.jackrabbit.rmi.client.ClientRepositoryFactory) InitialContext(javax.naming.InitialContext)

Example 50 with Repository

use of javax.jcr.Repository in project sling by apache.

the class AbstractSlingRepositoryManager method initializeAndRegisterRepositoryService.

private void initializeAndRegisterRepositoryService() {
    Throwable t = null;
    try {
        log.debug("start: calling acquireRepository()");
        Repository newRepo = this.acquireRepository();
        if (newRepo != null) {
            // ensure we really have the repository
            log.debug("start: got a Repository");
            this.repository = newRepo;
            synchronized (this.repoInitLock) {
                this.masterSlingRepository = this.create(this.bundleContext.getBundle());
                log.debug("start: setting up Loader");
                this.loader = new Loader(this.masterSlingRepository, this.bundleContext);
                log.debug("start: calling SlingRepositoryInitializer");
                try {
                    executeRepositoryInitializers(this.masterSlingRepository);
                } catch (Throwable e) {
                    t = e;
                    log.error("Exception in a SlingRepositoryInitializer, SlingRepository service registration aborted", t);
                }
                log.debug("start: calling registerService()");
                this.repositoryService = registerService();
                log.debug("start: registerService() successful, registration=" + repositoryService);
            }
        }
    } catch (Throwable e) {
        // consider an uncaught problem an error
        log.error("start: Uncaught Throwable trying to access Repository, calling stopRepository()", e);
        t = e;
    } finally {
        if (t != null) {
            // repository might be partially started, stop anything left
            stop();
        }
    }
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) Repository(javax.jcr.Repository) Loader(org.apache.sling.jcr.base.internal.loader.Loader)

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