3 Replies Latest reply on Jan 16, 2012 6:41 PM by Randall Hauch

    NPE when starting Modshape REST Server with repository running as a service in JBoss AS

    Akram Ben Aissi Newbie

      Hi modeshapers,

       

      I am trying to deploying an extended version of the Modshape REST server.

      Following the documentation, I created a maven project with the following dependencies:

       

      <dependency>
        <groupId>org.modeshape</groupId>
        <artifactId>modeshape-jcr</artifactId>
      </dependency>
      <dependency>
        <groupId>org.modeshape</groupId>
        <artifactId>modeshape-web-jcr-rest</artifactId>
        </dependency>
      <!-- The JCR API -->
      <dependency>
        <groupId>javax.jcr</groupId>
        <artifactId>jcr</artifactId>
      </dependency>
      
      

       

      Then, I just get the original web.xml from modshape-rest.war which is supposed to use JNDI to locate the repositories.

      Modeshape is deployed as a service using the binaries provided for JBoss AS 5 and when using the original modshape-rest.war everithing works well.

       

      Here is a code snippet that I used to extend the server:

       

       

       

      public static Repository getRepository(String repositoryName) throws RepositoryException {
       Map<String, String> parameters = new HashMap<String, String>();
       String configUrlKey = "org.modeshape.jcr.URL";
       String configUrlKey2 = "org.modeshape.web.jcr.JCR_URL";
       String configUrl = "jndi:jcr/local?repositoryName=" + repositoryName;
       parameters.put(configUrlKey, configUrl);
       parameters.put(configUrlKey2, configUrl);
      
       for (RepositoryFactory factory : ServiceLoader.load(RepositoryFactory.class)) {
       Repository repository = factory.getRepository(parameters);
       if (repository != null) {
       return repository;
       }
       }
       throw new RepositoryException();
       }
      


       

       

      and

      public class RenameService {
       @GET
       @Path("/rename")
       @Produces("application/json")
       public String rename(@Context HttpServletRequest request/*, @PathParam("repositoryName") String rawRepositoryName*/)
       throws JSONException, RepositoryException {
       Repository repository = RepositoryUtils.getRepository("repository");
       String login = "admin";
       char[] password = login.toCharArray();
       Credentials credentials = new SimpleCredentials(login, password);
       Session session = repository.login(credentials, "default");
       Node node = session.getNode("passwd");
       session.move(node.getPath(), node.getParent().getPath() + "/aaaa");
       // Don't forget - not necessarily here at this place:
       // node.getSession().save();
       return "OK";
       }
      
      }
      

      And when trying to acces the /rename URL I got the following stacktrace:

       

       

      java.lang.NullPointerException
       org.modeshape.web.jcr.spi.FactoryRepositoryProvider.getRepository(FactoryRepositoryProvider.java:71)
       org.modeshape.web.jcr.spi.FactoryRepositoryProvider.getSession(FactoryRepositoryProvider.java:111)
       org.modeshape.web.jcr.RepositoryFactory.getSession(RepositoryFactory.java:90)
       org.modeshape.web.jcr.rest.AbstractHandler.getSession(AbstractHandler.java:39)
       org.modeshape.web.jcr.rest.RepositoryHandler.getWorkspaces(RepositoryHandler.java:33)
       org.modeshape.web.jcr.rest.JcrResources.getWorkspaces(JcrResources.java:186)
       sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       java.lang.reflect.Method.invoke(Method.java:597)
       org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:124)
       org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:247)
       org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:212)
       org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:202)
       org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:441)
       org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:418)
       org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:111)
       org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:217)
       org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:159)
       javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
       org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
      

      It doesn't seem that my code is even called, but it looks like resteasy could not instanciate correctly.

       

      Question: is it the correct way to extend the REST server ?

       

      Any help would be really appreciated.

        • 1. Re: NPE when starting Modshape REST Server with repository running as a service in JBoss AS
          Randall Hauch Master

          The error is happening because the FactoryRepositoryProvider class is either getting a null RepositoryFactory or, more likely, the RepositoryFactory is returning null for the Repositories. This can happen when your WAR file contains copies of the JCR API class files and/or ModeShape API class files, while the service is using its own. Remember, in Java, if one class is loaded by two class loaders, those classes are not considered the same, so instances of one will not be instances of the other.

           

          Check your war file - I'll bet it contains the JAR file for the JCR API and/or the small ModeShape extensions to the JCR API. If so, remove the JARs from the WAR file, and try again. Also, compare what's in your WAR file with the 'modeshape-rest.war' file (which you can try to mirror).

           

          If that works, then mark those dependencies with a scope value of 'provided'. If that still doesn't work, you'll have to change how Maven is assembling your WAR file by using a custom assembly descriptor.

          • 2. Re: NPE when starting Modshape REST Server with repository running as a service in JBoss AS
            Akram Ben Aissi Newbie

            Thanks for your reply, you were completely right (as usual )

             

            You're right about classloader and classes, but in this case I should have received a ClassCastException not an NullPointerEcxeption, which is a probably a side effect.

             

            Anyway, your answer is correct.

            Here is the final dependencies that I used to make it work:

            <dependencies>
                    <dependency>
                        <groupId>javax.servlet</groupId>
                        <artifactId>servlet-api</artifactId>
                    </dependency>
                    <dependency>
                        <groupId>org.modeshape</groupId>
                        <artifactId>modeshape-web-jcr-rest</artifactId>
                    </dependency>
                    <dependency>
                        <groupId>org.modeshape</groupId>
                        <artifactId>modeshape-jcr-api</artifactId>
                        <scope>provided</scope>
                    </dependency>
                    <dependency>
                        <groupId>javax.jcr</groupId>
                        <artifactId>jcr</artifactId>
                        <scope>provided</scope>
                    </dependency>        <dependency>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <scope>test</scope>
                    </dependency>
                </dependencies>
            

             

            The documentation is very good, but it maybe requires these few updates to make cusotmization of the REST or Webdav servers straight forward.

            Thanks again and congratulation for the work done and the quality of documentation and support.

             

            I will keep using modshape and probably come and bother people with other questions.

            Greetings

            1 of 1 people found this helpful
            • 3. Re: NPE when starting Modshape REST Server with repository running as a service in JBoss AS
              Randall Hauch Master

              You're right about classloader and classes, but in this case I should have received a ClassCastException not an NullPointerEcxeption, which is a probably a side effect.

              We'll try to make this more apparent in 3.0, by checking whether the object's class name is one of the classes we expect, and if so reporting the error as a potential classpath problem.

               

              The documentation is very good, but it maybe requires these few updates to make cusotmization of the REST or Webdav servers straight forward.

              Thanks again and congratulation for the work done and the quality of documentation and support.

              Thanks for the feedback on our documentation. We do hope to improve the documentation in 3.0, and our docs will have a comment section where you can hightlight something that needs clarification.

              I will keep using modshape and probably come and bother people with other questions.

              Please do!