You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jackrabbit.apache.org by "Brian E. Lavender" <br...@brie.com> on 2021/09/08 05:43:23 UTC

Re: which jar files to copy to Tomcat lib folder

I wrote my own factory class and I can now access a remote RMI repository
using my webapp and the JNDI lookup with Tomcat. I am guessing that the
built-in ClientRepositoryFactory doesn't work. I will have to look at it

org.apache.jackrabbit.rmi.client.ClientRepositoryFactory

I used the following to write my factory class.

https://tomcat.apache.org/tomcat-9.0-doc/jndi-resources-howto.html

Here is my code. I didn't copy any jars into the tomcat lib folder.

```java
public class MyRepositoryFactory implements ObjectFactory {

  public Object getObjectInstance(Object obj,
      Name name2, Context nameCtx, Hashtable environment)
      throws NamingException {

      // Acquire an instance of our specified bean class
      Repository repository = null;
      String location = new String();

      // Customize the bean properties from our attributes
      Reference ref = (Reference) obj;
      Enumeration addrs = ref.getAll();
      while (addrs.hasMoreElements()) {
          RefAddr addr = (RefAddr) addrs.nextElement();
          String name = addr.getType();
          String value = (String) addr.getContent();
          if (name.equals("url")) {
              try {                  
                  repository = new URLRemoteRepository(value);
              } catch (MalformedURLException e) {
                  throw new NamingException("Invalid 'url' value " + value);
              } 
          }
      }

      // Return the customized instance
      return (repository);

  }

}
```

```java
public class SessionFactory {

    public static Session getSession() throws RepositoryException, NamingException {
        //org.apache.jackrabbit.rmi.client.ClientRepositoryFactory a;
        
        
        Context initial = new InitialContext();
        Context context = (Context) initial.lookup("java:comp/env");
        Repository repository = (Repository) context.lookup("jcr/Repository");

        Session session = repository.login(
                new SimpleCredentials("admin", "admin".toCharArray()));
        return session;
    }
}
```

context.xml placed in META-INF folder 
```xml
<?xml version="1.0" encoding="UTF-8"?>

<!-- The contents of this file will be loaded for each web application -->
<Context path="/jcrweb07/" >
<Resource name="jcr/Repository" auth="Container"
    type="javax.jcr.Repository"
    factory="gov.ca.brea.jcrweb07.MyRepositoryFactory"
    url="http://localhost:8081/rmi"/>

</Context>
```

listing.jsp

```jsp
%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="javax.jcr.Node"%>
<%@page import="javax.jcr.Session"%>
<%@page import="gov.ca.brea.jcrweb07.SessionFactory"%>
<%@page import="javax.jcr.NodeIterator"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JackRabbit Repository</title>
    </head>
    <body>
        <h1>This is what is in the JackRabbit Repository Two</h1>
        <%!
private void printContents(Node n, JspWriter out, String padding) throws Exception {
        if (n.getName().equals("jcr:system")) {
            return;
        }
  out.println(padding + n.getPath() + "(" + n.getPrimaryNodeType().getName() + ")");
  NodeIterator it = n.getNodes();
  while (it.hasNext()) {
    printContents(it.nextNode(), out, padding + " ");
  }
}
%>
<%
Session jcrSession = SessionFactory.getSession();
Node root = jcrSession.getRootNode();
%>
<pre>
<%
printContents(root, out, "");
%>
</pre>
    </body>
</html>
```





On Tue, Aug 31, 2021 at 12:42:56PM -0700, Eric Norman wrote:
> Hi Brian,
> 
> If I were you, I would try to avoid using a Repository provided via JNDI
> unless you are expecting reuse the same configuration in multiple webapps
> in your container.
> 
> I would recommend that you first get the JCR remoting client working in a
> simple standalone java application, and then once that is working you can
> move the code and all the jars that were required for that into your war
> file.
> 
> For a standalone JCR remoting java application, I believe the minimum
> dependencies (for a maven project) would be something like this:
> 
>     <!-- JCR API -->
>     <dependency>
>       <groupId>javax.jcr</groupId>
>       <artifactId>jcr</artifactId>
>       <version>2.0</version>
>     </dependency>
>     <!-- All the Jackrabbit libraries needed for DavEx, plus JcrUtils -->
>     <dependency>
>       <groupId>org.apache.jackrabbit</groupId>
>       <artifactId>jackrabbit-jcr2dav</artifactId>
>       <version>2.20.3</version>
>     </dependency>
> 
> 
> And then from java code you can connect to the remote server with client
> code that looks something like this:
> 
> import javax.jcr.Node;
> import javax.jcr.Repository;
> import javax.jcr.RepositoryException;
> import javax.jcr.Session;
> import javax.jcr.SimpleCredentials;
> 
> import org.apache.jackrabbit.commons.JcrUtils;
> 
> public class TestJcrRemoting {
> 
>     public static void main(String[] args) {
>         Session jcrSession = null;
>         try {
>             Repository repo = JcrUtils.getRepository("
> http://localhost:8080/server");
> 
>             jcrSession = repo.login(new SimpleCredentials("your_username",
> "your_password".toCharArray()));
> 
>             Node rootNode = jcrSession.getRootNode();
>             System.out.println("root node is: " + rootNode);
>         } catch (RepositoryException e) {
>             e.printStackTrace();
>         } finally {
>             if (jcrSession != null) {
>                 jcrSession.logout();
>             }
>         }
>     }
> }
> 
> I created a simple test project locally to verify the JCR remoting over
> davex worked as I remembered, and with the minimal test project, the "mvn
> dependency:tree" output for that client project reported the following set
> of required jar files that gets you an idea of what you would have to
> include in your war file:
> 
> [INFO] sample:jcr-remoting-test:jar:0.0.1-SNAPSHOT
> [INFO] +- javax.jcr:jcr:jar:2.0:compile
> [INFO] \- org.apache.jackrabbit:jackrabbit-jcr2dav:jar:2.20.3:compile
> [INFO]    +- org.apache.jackrabbit:jackrabbit-jcr2spi:jar:2.20.3:compile
> [INFO]    |  +- org.apache.jackrabbit:oak-jackrabbit-api:jar:1.40.0:compile
> [INFO]    |  +- org.apache.jackrabbit:jackrabbit-spi:jar:2.20.3:compile
> [INFO]    |  +-
> org.apache.jackrabbit:jackrabbit-spi-commons:jar:2.20.3:compile
> [INFO]    |  +-
> org.apache.jackrabbit:jackrabbit-jcr-commons:jar:2.20.3:compile
> [INFO]    |  +- org.slf4j:slf4j-api:jar:1.7.30:compile
> [INFO]    |  +- commons-collections:commons-collections:jar:3.2.2:compile
> [INFO]    |  \- commons-io:commons-io:jar:2.8.0:compile
> [INFO]    \- org.apache.jackrabbit:jackrabbit-spi2dav:jar:2.20.3:compile
> [INFO]       +- org.apache.jackrabbit:jackrabbit-webdav:jar:2.20.3:compile
> [INFO]       |  +- org.apache.httpcomponents:httpcore:jar:4.4.14:compile
> [INFO]       |  +- org.apache.httpcomponents:httpclient:jar:4.5.13:compile
> [INFO]       |  |  +- commons-logging:commons-logging:jar:1.2:compile
> [INFO]       |  |  \- commons-codec:commons-codec:jar:1.11:compile
> [INFO]       |  \- org.slf4j:jcl-over-slf4j:jar:1.7.30:compile
> [INFO]       \- org.apache.httpcomponents:httpmime:jar:4.5.13:compile
> 
> 
> Hope that helps get you further..
> 
> Regards
> -Eric
> 
> On Tue, Aug 31, 2021 at 9:24 AM Brian E. Lavender <br...@brie.com> wrote:
> 
> > On Tue, Aug 31, 2021 at 08:04:53AM +0200, Julian Reschke wrote:
> > > Am 31.08.2021 um 02:09 schrieb Brian E. Lavender:
> > > > I am still attempting to connect my webapp to a remote repository
> > > > I still keep getting a null reference. :( And, the log
> > > > messages doesn't seem to indicate what I am missing.
> > > >
> > > > I am thinking that I need to copy the jars into the Tomcat lib folder
> > > > for my application to connect to a remote repository based upon the
> > > > following. What jars do I need besides the following?
> > > >
> > > > jackrabbit-jcr-rmi-2.20.3.jar
> > > >
> > > > https://jackrabbit.apache.org/jcr/repository-server-howto.html
> > > >
> > > > The instructions state the following.
> > > >
> > > > Place the JCR-RMI jar file and its dependencies (including the JCR API
> > > > jar) under $CATALINA_HOME/webapps/_your app_/WEB-INF/lib.
> > > >
> > > > Any ideas on this?
> > > >
> > > > Does someone have a sample project that has a context.xml that connects
> > > > to a remote repository?
> > >
> > > I can't help you with RMI, I have never used it. What I know though is
> > > that it hasn't got any attention for many years, and is known not to
> > > support certain operations.
> > >
> > > I would recommend connecting using WebDAV.
> >
> > How would I do that?
> >
> > Is there a jndi connection binding or something?
> >
> > Brian
> > --
> > Brian Lavender
> > http://www.brie.com/brian/
> >
> > "There are two ways of constructing a software design. One way is to
> > make it so simple that there are obviously no deficiencies. And the other
> > way is to make it so complicated that there are no obvious deficiencies."
> >
> > Professor C. A. R. Hoare
> > The 1980 Turing award lecture
> >

-- 
Brian Lavender
http://www.brie.com/brian/

"There are two ways of constructing a software design. One way is to
make it so simple that there are obviously no deficiencies. And the other
way is to make it so complicated that there are no obvious deficiencies."

Professor C. A. R. Hoare
The 1980 Turing award lecture