Search in sources :

Example 1 with RemoteRepository

use of org.apache.jackrabbit.rmi.remote.RemoteRepository in project jackrabbit by apache.

the class RepositoryStubImpl method getRepository.

@Override
public synchronized Repository getRepository() throws RepositoryStubException {
    if (repository == null) {
        try {
            Repository repo = super.getRepository();
            principal = findKnownPrincipal(repo);
            RemoteAdapterFactory raf = new ServerAdapterFactory();
            remote = raf.getRemoteRepository(repo);
            // Make sure that the remote reference survives serialization
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(buffer);
            oos.writeObject(RemoteObject.toStub(remote));
            oos.close();
            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
            LocalAdapterFactory laf = new ClientAdapterFactory();
            repository = laf.getRepository((RemoteRepository) ois.readObject());
        } catch (Exception e) {
            throw new RepositoryStubException(e);
        }
    }
    return repository;
}
Also used : Repository(javax.jcr.Repository) RemoteRepository(org.apache.jackrabbit.rmi.remote.RemoteRepository) ByteArrayInputStream(java.io.ByteArrayInputStream) RemoteRepository(org.apache.jackrabbit.rmi.remote.RemoteRepository) RepositoryStubException(org.apache.jackrabbit.test.RepositoryStubException) RemoteAdapterFactory(org.apache.jackrabbit.rmi.server.RemoteAdapterFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ClientAdapterFactory(org.apache.jackrabbit.rmi.client.ClientAdapterFactory) ObjectOutputStream(java.io.ObjectOutputStream) LocalAdapterFactory(org.apache.jackrabbit.rmi.client.LocalAdapterFactory) ServerAdapterFactory(org.apache.jackrabbit.rmi.server.ServerAdapterFactory) RepositoryStubException(org.apache.jackrabbit.test.RepositoryStubException) RepositoryException(javax.jcr.RepositoryException) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with RemoteRepository

use of org.apache.jackrabbit.rmi.remote.RemoteRepository 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 3 with RemoteRepository

use of org.apache.jackrabbit.rmi.remote.RemoteRepository in project jackrabbit by apache.

the class ConnectToJNDIServer method execute.

/**
	 * {@inheritDoc}
	 */
public boolean execute(Context ctx) throws Exception {
    String url = (String) ctx.get(this.urlKey);
    if (log.isDebugEnabled()) {
        log.debug("connecting to jndi server at " + url);
    }
    InitialContext iCtx = new InitialContext();
    ClientAdapterFactory adapter = new ClientAdapterFactory();
    RemoteRepository remote = (RemoteRepository) iCtx.lookup(url);
    Repository repo = adapter.getRepository(remote);
    CommandHelper.setRepository(ctx, repo, "jndi " + url);
    return false;
}
Also used : Repository(javax.jcr.Repository) RemoteRepository(org.apache.jackrabbit.rmi.remote.RemoteRepository) RemoteRepository(org.apache.jackrabbit.rmi.remote.RemoteRepository) ClientAdapterFactory(org.apache.jackrabbit.rmi.client.ClientAdapterFactory) InitialContext(javax.naming.InitialContext)

Aggregations

Repository (javax.jcr.Repository)3 RemoteRepository (org.apache.jackrabbit.rmi.remote.RemoteRepository)3 InitialContext (javax.naming.InitialContext)2 ClientAdapterFactory (org.apache.jackrabbit.rmi.client.ClientAdapterFactory)2 LocalAdapterFactory (org.apache.jackrabbit.rmi.client.LocalAdapterFactory)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 RepositoryException (javax.jcr.RepositoryException)1 ClientRepositoryFactory (org.apache.jackrabbit.rmi.client.ClientRepositoryFactory)1 RemoteAdapterFactory (org.apache.jackrabbit.rmi.server.RemoteAdapterFactory)1 ServerAdapterFactory (org.apache.jackrabbit.rmi.server.ServerAdapterFactory)1 RepositoryStubException (org.apache.jackrabbit.test.RepositoryStubException)1