6 Replies Latest reply on Jun 27, 2011 7:08 PM by joedel

    How to connect to a Modeshape repository?

    joedel

      Hi,

      I've just started working with Modeshape, and I'm quite blocked. I have ModeShape 2.5.0 up and running on JBoss AS 6, and I want to start by writing a jsp snippet to access the content repository using the JCR API.

       

      The question is: How can I get an instance of the Modeshape repository to create a JCR session?

       

      I've gone through the documentation, and it says that the way to do it is using a RepositoryFactory, but I haven't found where to include the necessary parameters nor the syntax to write them.

       

      Can somebody, please, help me with this?

      Also, what is the name for the default Modeshape content repository?

       

      Thanks

        • 1. Re: How to connect to a Modeshape repository?
          bcarothers

          The canonical way to get a reference to a ModeShape repository is to write some code like this:

           

          String configFileName = // path to config file
          
          JcrConfiguration config = new JcrConfiguration();
          
          config.loadFrom(configFileName);
          
          JcrEngine engine = config.build();
          
          engine.start();
          
          if (engine.getProblems().hasProblems()) {
          
               System.err.println(engine.getProblems();
          
               System.exit(1);  // You wouldn't really quit here, but you get the picture
          
          }
          
          javax.jcr.Repository repo = engine.getRepository("the name of some repository that you specified in the config file");
          
          
          

           

          I typed that from memory, so apologies in advance for the syntax errors.  All that leaves is for you to set up your config file correctly.  There's a chapter in the Reference Guide here that covers all of the details with (what I hope are) some relevant examples.

           

          If you need a quick working example that uses the in-memory repository, use the config file here.  This creates a JCR repository called "Cars Repository", so you could get a reference to it with engine.getRepository("Car Repository").  Remember that none of your content will save over JVM restart, because you're using an in-memory repository.

           

          In case it helps, you can also call engine.getRepositoryNames() to get a list of all valid JCR repository names.

           

          Let us know how it turns out.

          1 of 1 people found this helpful
          • 2. Re: How to connect to a Modeshape repository?
            rhauch

            If you've deployed ModeShape to JBoss AS, then the engine is already running and your application just needs to look up the repository instances using the RepositoryFactory. A recent blog post of ours just featured how to do this:

             

            http://modeshape.wordpress.com/2011/06/20/finding-a-jcr-repository/

             

            The post talks about how the ModeShape RepositoryFactory needs one parameter, and shows how to use that. When ModeShape is deployed to the app server, it registers the engine in JNDI. That means you can use this URI for the "org.modeshape.jcr.URL" property:

             

                 jndi:jcr/local?repositoryName=MyRepository

             

            Hope that helps!

            1 of 1 people found this helpful
            • 3. Re: How to connect to a Modeshape repository?
              joedel

              Thank you Brian, your answer is really helpful to start a Modeshape repository, but I already have the engine running on JBoss AS and what I need is a way to connect to it.

              • 4. Re: How to connect to a Modeshape repository?
                joedel

                Thanks Randall.

                A couple of questions:

                1) What is the syntax to assingt the URI value to the property?

                2) In which config file should I add this property?

                 

                Thanks

                • 5. Re: How to connect to a Modeshape repository?
                  rhauch

                  1) What is the syntax to assingt the URI value to the property?

                   

                  If you've deployed ModeShape to JBoss AS and have not changed the name of the repository in the configuration file, then as shown in the blog post your "org.modeshape.jcr.URL" property should be "jndi:jcr/local?repositoryName=repository".

                   

                  2) In which config file should I add this property?

                   

                  This property does not go into ModeShape's configuration file, because it's not something that ModeShape will use. Remember that this parameter is supplied by your application to the RepositoryFactory method call, so you're application needs to define this property. If your application wants to put that into a file, then great. Your properties file would contain this one line:

                   

                  org.modeshape.jcr.URL = jndi:jcr/local?repositoryName=repository
                  

                   

                  and you can read this using techniques shown in the blog post. If your application is not going to use a property file, then you should add this to the 'parameters' map that's passed to the RepositoryFactory.getRepository(Map) method:

                   

                  parameters.put("org.modeshape.jcr.URL","jndi:jcr/local?repositoryName=repository");
                  
                  • 6. Re: How to connect to a Modeshape repository?
                    joedel

                    It works! (as you probably already knew it would)

                     

                    Thanks a lot.