Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #13 from futures/context-listener
Initialize the repository on servlet load, or clustering may get unhappy...

We will likely go on to DI to deal with this kind of problem, but let's start with what we've got!
  • Loading branch information
ajs6f committed Jan 20, 2013
2 parents e48993c + 2986adc commit 7fd73db
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 27 deletions.
5 changes: 1 addition & 4 deletions pom.xml
Expand Up @@ -41,7 +41,7 @@
<dependency>
<groupId>org.modeshape.bom</groupId>
<artifactId>modeshape-bom-embedded</artifactId>
<version>3.0.1.Final</version>
<version>3.1.0.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand All @@ -56,15 +56,12 @@
<scope>test</scope>
</dependency>


<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>test</scope>
</dependency>


<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Expand Up @@ -46,31 +46,12 @@ public abstract class AbstractResource {
public AbstractResource() throws ConfigurationException,
RepositoryException, IOException {
if (ws == null) {
RepositoryConfiguration repository_config = null;
try {
repository_config = RepositoryConfiguration
.read("my_repository.json");
Problems problems = repository_config.validate();

if (problems.hasErrors()) {
throw new ConfigurationException(problems,
"Problems starting the engine.");
}

} catch (ParsingException ex) {
logger.error(ex, null, (Object) null);
} catch (FileNotFoundException ex) {
logger.error(ex, null, (Object) null);
}

final ModeShapeEngine engine = new ModeShapeEngine();
final JcrRepository repository = Bootstrap.getRepository();

if (engine == null || repository_config == null) {
throw new SystemFailureException("Missing engine");
}
engine.start();
final JcrRepository repository = engine.deploy(repository_config);
logger.debug("Deployed repository.");
if (repository == null) {
throw new RepositoryException("Missing repository?");
}
ws = repository.login().getWorkspace();

String[] workspaceNames = ws.getAccessibleWorkspaceNames();
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/org/fcrepo/ffmodeshapeprototype/Bootstrap.java
@@ -0,0 +1,78 @@
package org.fcrepo.ffmodeshapeprototype;


import org.infinispan.schematic.document.ParsingException;
import org.modeshape.common.SystemFailureException;
import org.modeshape.common.collection.Problems;
import org.modeshape.common.logging.Logger;
import org.modeshape.jcr.ConfigurationException;
import org.modeshape.jcr.JcrRepository;
import org.modeshape.jcr.ModeShapeEngine;
import org.modeshape.jcr.RepositoryConfiguration;

import javax.jcr.RepositoryException;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.FileNotFoundException;

public class Bootstrap implements ServletContextListener{
ServletContext context;
private static JcrRepository repository;

private static final Logger logger = Logger.getLogger(AbstractResource.class);

public void contextInitialized(ServletContextEvent contextEvent) {
System.out.println("Context Created");
context = contextEvent.getServletContext();
try {
initializeEngine();
} catch (RepositoryException e) {
throw new java.lang.RuntimeException(e);
}
}

public void contextDestroyed(ServletContextEvent contextEvent) {
}

public static JcrRepository getRepository() throws RepositoryException {
if(repository == null) {
initializeEngine();
}

return repository;
}


private static JcrRepository initializeEngine() throws RepositoryException {
RepositoryConfiguration repository_config = null;
try {
repository_config = RepositoryConfiguration
.read("my_repository.json");
Problems problems = repository_config.validate();

if (problems.hasErrors()) {
throw new ConfigurationException(problems,
"Problems starting the engine.");
}

} catch (ParsingException ex) {
logger.error(ex, null, (Object) null);
} catch (FileNotFoundException ex) {
logger.error(ex, null, (Object) null);
}

final ModeShapeEngine engine = new ModeShapeEngine();

if (engine == null || repository_config == null) {
throw new SystemFailureException("Missing engine");
}
engine.start();
repository = engine.deploy(repository_config);


logger.debug("Deployed repository.");

return repository;
}
}
4 changes: 4 additions & 0 deletions src/main/webapp/WEB-INF/web.xml
Expand Up @@ -19,6 +19,10 @@
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>

<listener>
<listener-class>org.fcrepo.ffmodeshapeprototype.Bootstrap</listener-class>
</listener>
<servlet-mapping>
<servlet-name>Server</servlet-name>
<url-pattern>/*</url-pattern>
Expand Down

0 comments on commit 7fd73db

Please sign in to comment.