org.modeshape.jcr
Class JcrRepositoryFactory

java.lang.Object
  extended by org.modeshape.jcr.JcrRepositoryFactory
All Implemented Interfaces:
RepositoryFactory

@ThreadSafe
public class JcrRepositoryFactory
extends Object
implements RepositoryFactory

Service provider for the JCR2 RepositoryFactory interface. This class provides a single public method, getRepository(Map), that allows for a runtime link to a ModeShape JCR repository.

The canonical way to get a reference to this class is to use the ServiceLocator:

 String configUrl = ... ; // URL that points to your configuration file
 Map params = Collections.singletonMap(JcrRepositoryFactory.URL, configUrl);
 Repository repository;
 
 for (RepositoryFactory factory : ServiceLoader.load(RepositoryFactory.class)) {
     repository = factory.getRepository(parameters);
     if (repository != null) break;
 }
 
It is also possible to instantiate this class directly.
 RepositoryFactory repoFactory = new JcrRepositoryFactory();    
 String configUrl = ... ; // URL that points to your configuration file
 Map params = Collections.singletonMap(JcrRepositoryFactory.URL, configUrl);
 
 Repository repository = repoFactory.getRepository(params);]]>
 

The configUrl used in the sample above should point to a configuration file (e.g., file:src/test/resources/configRepository.xml?repositoryName=MyRepository) OR a Repositories instance stored in the JNDI tree (e.g., jndi://name/of/Repositories/resource?repositoryName=MyRepository).

See Also:
getRepository(Map), RepositoryFactory.getRepository(Map)

Field Summary
static String REPOSITORY_NAME_PARAM
          The name of the URL parameter that specifies the repository name.
static String URL
          The name of the key for the ModeShape JCR URL in the parameter map
 
Constructor Summary
JcrRepositoryFactory()
           
 
Method Summary
 Repositories getRepositories(String jcrUrl)
          Returns the Repositories instance referenced by the jcrUrl parameter.
 Repository getRepository(Map parameters)
          Returns a reference to the appropriate repository for the given parameter map, if one exists.
 Repository getRepository(String jcrUrl, String repositoryName)
          Returns the repository with the given name from the Repositories referenced by jcrUrl if the engine and the named repository exist, null otherwise.
 Set<String> getRepositoryNames(String jcrUrl)
          Returns the repository names in the JcrEngine referenced by the jcrUrl parameter.
 void shutdown()
          Begin the shutdown process for all the JcrEngine JcrEngines created by calls to RepositoryFactory.getRepository(Map).
 boolean shutdown(long timeout, TimeUnit unit)
          Begin the shutdown process for all the JcrEngine JcrEngines created by calls to RepositoryFactory.getRepository(Map).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

URL

public static final String URL
The name of the key for the ModeShape JCR URL in the parameter map

See Also:
Constant Field Values

REPOSITORY_NAME_PARAM

public static final String REPOSITORY_NAME_PARAM
The name of the URL parameter that specifies the repository name.

See Also:
Constant Field Values
Constructor Detail

JcrRepositoryFactory

public JcrRepositoryFactory()
Method Detail

getRepository

public Repository getRepository(Map parameters)
Returns a reference to the appropriate repository for the given parameter map, if one exists. Although the parameters map can have any number of entries, this method only considers the entry with the key JcrRepositoryFactory#URL.

The value of this key is treated as a URL with the format PROTOCOL://PATH[?repositoryName=REPOSITORY_NAME] where PROTOCOL is "jndi" or "file", PATH is the JNDI name of the JcrEngine or the path to the configuration file, and REPOSITORY_NAME is the name of the repository to return if there is more than one JCR repository in the given JcrEngine or configuration file.

Specified by:
getRepository in interface RepositoryFactory
Parameters:
parameters - a map of parameters to use to look up the repository; may be null
Returns:
the repository specified by the value of the entry with key URL, or null if any of the following are true:
  • the parameters map is empty; or,
  • there is no parameter with the URL; or,
  • the value for the URL key is null or cannot be parsed into a ModeShape JCR URL; or,
  • the ModeShape JCR URL is parseable but does not point to a JcrEngine (in the JNDI tree) or a configuration file (in the classpath or file system); or,
  • or there is an error starting up the JcrEngine with the given configuration information.

shutdown

public void shutdown()
Description copied from interface: RepositoryFactory
Begin the shutdown process for all the JcrEngine JcrEngines created by calls to RepositoryFactory.getRepository(Map).

Calling #getRepository(Map) with a file-based URL parameter causes a new JcrEngine to be instantiated and started. Any JcrEngine created in this manner must be stored by the RepositoryFactory implementation. Invoking this method iteratively invokes the shutdown() method on each JcrEngine.

This method merely initiates the shutdown process for each JcrEngine. There is no guarantee that the shutdown process will have completed prior to this method returning. The RepositoryFactory.shutdown(long, TimeUnit) method provides the ability to wait until all engines are shutdown or the given time elapses.

Invoking this method does not preclude creating new JcrEngines with future calls to RepositoryFactory.getRepository(Map). Any caller using this method as part of an application shutdown process should take care to cease invocations of RepositoryFactory.getRepository(Map) prior to invoking this method.


shutdown

public boolean shutdown(long timeout,
                        TimeUnit unit)
                 throws InterruptedException
Description copied from interface: RepositoryFactory
Begin the shutdown process for all the JcrEngine JcrEngines created by calls to RepositoryFactory.getRepository(Map).

Calling #getRepository(Map) with a file-based URL parameter causes a new JcrEngine to be instantiated and started. Any JcrEngine created in this manner must be stored by the RepositoryFactory implementation. Invoking this method iteratively invokes the shutdown() method on each JcrEngine and then iteratively invokes the awaitTermination(long, TimeUnit) method to await termination.

Although this method initiates the shutdown process for each JcrEngine and invokes the awaitTermination method, there is still no guarantee that the shutdown process will have completed prior to this method returning. It is possible for the time required to shutdown one or more of the engines to exceed the provided time window.

Invoking this method does not preclude creating new JcrEngines with future calls to RepositoryFactory.getRepository(Map). Any caller using this method as part of an application shutdown process should take care to cease invocations of RepositoryFactory.getRepository(Map) prior to invoking this method.

Parameters:
timeout - the maximum time per engine to allow for shutdown
unit - the time unit of the timeout argument
Returns:
true if all engines completely shut down and false if the timeout elapsed before it was shut down completely
Throws:
InterruptedException - if interrupted while waiting

getRepository

public Repository getRepository(String jcrUrl,
                                String repositoryName)
                         throws RepositoryException
Returns the repository with the given name from the Repositories referenced by jcrUrl if the engine and the named repository exist, null otherwise.

If the jcrUrl parameter contains a valid, ModeShape-compatible URL for a JcrEngine that has not yet been started, that JcrEngine will be created and started as a side effect of this method.

If the repositoryName parameter is null, the repository name specified in the jcrUrl parameter will be used instead. If no repository name is specified in the jcrUrl parameter and the JcrEngine has exactly one repository, that repository will be used. Otherwise, this method will return null.

Parameters:
jcrUrl - the ModeShape-compatible URL that specifies the Repositories to be used; may not be null
repositoryName - the name of the repository to return; may be null
Returns:
the Repository with the given name from the Repositories referenced by jcrUrl if the engine exists and can be started (or is already started), and the named repository exists or null is provided as the repositoryName and the given engine has exactly one repository or the jcrUrl specifies a repository. If any of these conditions do not hold, null is returned.
Throws:
RepositoryException - if the named repository exists but cannot be accessed
See Also:
JcrEngine.getRepository(String)

getRepositoryNames

public Set<String> getRepositoryNames(String jcrUrl)
Returns the repository names in the JcrEngine referenced by the jcrUrl parameter.

If the jcrUrl parameter contains a valid, ModeShape-compatible URL for a JcrEngine that has not yet been started, that JcrEngine will be created and started as a side effect of this method.

Parameters:
jcrUrl - the ModeShape-compatible URL that specifies the JcrEngine to be used; may not be null
Returns:
the set of repository names in the given engine referred to by the jcrUrl parameter if that engine exists and it can be started (or is already started), otherwise null

getRepositories

public Repositories getRepositories(String jcrUrl)
Description copied from interface: RepositoryFactory
Returns the Repositories instance referenced by the jcrUrl parameter.

If the jcrUrl parameter contains a valid, ModeShape-compatible URL for a Repositories instance that has not yet been started, that Repositories instance will be created and started as a side effect of this method.

Parameters:
jcrUrl - the ModeShape-compatible URL that specifies the JcrEngine to be used; may not be null
Returns:
the Repositories instance specified by the given url if one exists, otherwise null


Copyright © 2008-2010 JBoss, a division of Red Hat. All Rights Reserved.