Search in sources :

Example 1 with ClientRepositoryFactory

use of org.apache.jackrabbit.rmi.client.ClientRepositoryFactory 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 2 with ClientRepositoryFactory

use of org.apache.jackrabbit.rmi.client.ClientRepositoryFactory in project jackrabbit by apache.

the class ConnectToRmiServer method execute.

/**
 * {@inheritDoc}
 */
public boolean execute(Context ctx) throws Exception {
    String url = (String) ctx.get(this.urlKey);
    if (log.isDebugEnabled()) {
        log.debug("connecting to jcr-rmi server at " + url);
    }
    ClientRepositoryFactory factory = new ClientRepositoryFactory();
    Repository repository = factory.getRepository(url);
    CommandHelper.setRepository(ctx, repository, "jcr-rmi " + url);
    return false;
}
Also used : Repository(javax.jcr.Repository) ClientRepositoryFactory(org.apache.jackrabbit.rmi.client.ClientRepositoryFactory)

Aggregations

Repository (javax.jcr.Repository)2 ClientRepositoryFactory (org.apache.jackrabbit.rmi.client.ClientRepositoryFactory)2 InitialContext (javax.naming.InitialContext)1 LocalAdapterFactory (org.apache.jackrabbit.rmi.client.LocalAdapterFactory)1 RemoteRepository (org.apache.jackrabbit.rmi.remote.RemoteRepository)1