Search in sources :

Example 1 with RepositoryFactory

use of javax.jcr.RepositoryFactory 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 2 with RepositoryFactory

use of javax.jcr.RepositoryFactory in project jackrabbit by apache.

the class RepositoryFactoryTest method testGetFactory.

public void testGetFactory() {
    Iterator it = ServiceRegistry.lookupProviders(RepositoryFactory.class);
    if (it.hasNext()) {
        RepositoryFactory rf = (RepositoryFactory) it.next();
        assertTrue(rf instanceof RepositoryFactoryImpl);
    } else {
        fail();
    }
}
Also used : Iterator(java.util.Iterator) RepositoryFactory(javax.jcr.RepositoryFactory)

Example 3 with RepositoryFactory

use of javax.jcr.RepositoryFactory in project jackrabbit by apache.

the class RepositoryFactoryImpl method getRepository.

/**
     * Creates a JCR repository from the given <code>parameters</code>.
     * If either {@link #PARAM_REPOSITORY_SERVICE_FACTORY} or
     * {@link #PARAM_REPOSITORY_CONFIG} is present, this factory delegates
     * to {@code org.apache.jackrabbit.jcr2spi.Jcr2spiRepositoryFactory}.
     * Otherwise it delegates to
     * {@code org.apache.jackrabbit.core.RepositoryFactoryImpl}.
     *
     * @see RepositoryFactory#getRepository(java.util.Map)
     */
public Repository getRepository(@SuppressWarnings("rawtypes") Map parameters) throws RepositoryException {
    String repositoryFactoryName = parameters != null && (parameters.containsKey(PARAM_REPOSITORY_SERVICE_FACTORY) || parameters.containsKey(PARAM_REPOSITORY_CONFIG)) ? "org.apache.jackrabbit.jcr2spi.Jcr2spiRepositoryFactory" : "org.apache.jackrabbit.core.RepositoryFactoryImpl";
    Object repositoryFactory;
    try {
        Class<?> repositoryFactoryClass = Class.forName(repositoryFactoryName, true, Thread.currentThread().getContextClassLoader());
        repositoryFactory = repositoryFactoryClass.newInstance();
    } catch (Exception e) {
        throw new RepositoryException(e);
    }
    if (repositoryFactory instanceof RepositoryFactory) {
        return ((RepositoryFactory) repositoryFactory).getRepository(parameters);
    } else {
        throw new RepositoryException(repositoryFactory + " is not a RepositoryFactory");
    }
}
Also used : RepositoryException(javax.jcr.RepositoryException) RepositoryFactory(javax.jcr.RepositoryFactory) RepositoryException(javax.jcr.RepositoryException)

Aggregations

RepositoryFactory (javax.jcr.RepositoryFactory)3 RepositoryException (javax.jcr.RepositoryException)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 PathNotFoundException (javax.jcr.PathNotFoundException)1 Repository (javax.jcr.Repository)1