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:45:17 UTC

Re: can't seem to access ClientRepository from Servlet

I did get access to a remote RMI repository working in my web application
using the following. This is a duplicate details that I put in my
other reply.


```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 Sun, Aug 29, 2021 at 11:59:45AM -0700, Brian E. Lavender wrote:
> Is the following still valid?
> 
> https://jackrabbit.apache.org/jcr/components/jackrabbit-jcr-rmi.html
> 
> Do I need to copy a jar into Tomcat lib folder?
> How about the stuff that goes into context.xml?
> Is the url part correct? Does a Repository type need to have an "url"
> setter for that to work? 
> 
> <Resource name="jcr/Repository" auth="Container"
>     type="javax.jcr.Repository"
>     factory="org.apache.jackrabbit.rmi.client.ClientRepositoryFactory"
>     url="..."/>
> 
> Brian
> 
> On Sun, Aug 29, 2021 at 10:58:11AM -0700, Brian E. Lavender wrote:
> > I started the standalone.jar on port 8081 with the following and now
> > I am trying to access it from a servlet.  Yet, I get an Exception
> > where it can't `getSession` in my SessionFactory class. I am not sure
> > what is going on. The one thing I checked is the looked so that it is
> > "jcr/repository". I also checked that the ClientRepositoryFactory class
> > is in my dependencies. Could it be the way I am creating the context.xml?
> > 
> 
> -- 
> 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