-
1. Re: How to connect to a Modeshape repository?
bcarothers Jun 22, 2011 8:18 PM (in response to joedel)1 of 1 people found this helpfulThe 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.
-
2. Re: How to connect to a Modeshape repository?
rhauch Jun 23, 2011 9:40 AM (in response to joedel)1 of 1 people found this helpfulIf 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!
-
3. Re: How to connect to a Modeshape repository?
joedel Jun 23, 2011 11:46 AM (in response to bcarothers)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 Jun 23, 2011 11:48 AM (in response to rhauch)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 Jun 23, 2011 12:04 PM (in response to joedel)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 Jun 27, 2011 7:08 PM (in response to rhauch)It works! (as you probably already knew it would)
Thanks a lot.