How to use RMI to connect CRX from an external application

Question

How do I use RMI to connect to CRX from an external application ?

Answer, Resolution

The RMI connection listener is configured in web.xml of the crx web application. In a default installation, the war file is extracted to crx_install_dir/server/runtime/0/_crx/. You can find web.xml under crx_install_dir/server/runtime/0/_crx/WEB-INF/webxml.

Per default, RMI is not enabled, in order to enable it you need to:

  1. Edit web.xml and comment out the following lines under Repository Servlet
    <init-param>
      <param-name>rmi-port</param-name>
      <param-value>1234</param-value>
      <description>
      The RMI port for registering the repository in the RMI Registry.
      If equals 0, the default port is used. Omit this parameter to
      disable RMI server entirely.
      </description>
    </init-param>
    

  2. Restart CRX and make sure that your server is listening on defined port (you can use unix command : lsof -i)
  3. Write a java class that connects to CRX through RMI. Below is a very simple example showing how to login to the CRX and get a Crx Session:
    import javax.jcr.Repository;
    import javax.jcr.Session;
    import javax.jcr.SimpleCredentials;
    import org.apache.jackrabbit.rmi.client.ClientRepositoryFactory;
    
    /*
     * Simple CRX RMI test class that connects to the CRX and logs in as admin
     * 
     */
    
    public class RMITest {
    	
    	public static void main (String[] args){
    		
    		ClientRepositoryFactory factory = new ClientRepositoryFactory();
    		Repository repository;
    		String crxApplicationName = "crx";
    		String repoUrl = "//localhost:1234/"+crxApplicationName; 
    		String workspace = "crx.default";
    		String username = "admin";
    		char [] password = "admin".toCharArray();
    		try {
    			repository = factory.getRepository(repoUrl);
    			Session s = repository.login(new SimpleCredentials(username,password), workspace);
    			System.out.println("userId:"+s.getUserID());
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    

Note

Make sure that you have following jar files in place for this to work,

\crx-quickstart\server\lib\container\jcr-*.*.jar

\crx-quickstart\server\lib\server\slf4j-log4j12-*.*.jar

\crx-quickstart\server\lib\server\log4j-*.*.*.jar

\crx-quickstart\server\runtime\0\_crx\WEB-INF\lib\jackrabbit-jcr-rmi-*.*.*.jar

\crx-quickstart\server\runtime\0\_crx\WEB-INF\lib\crx-rmi-*.*.*.jar