Skip to content

Commit

Permalink
Initialize the repository on servlet load, or clustering may get unha…
Browse files Browse the repository at this point in the history
…ppy.. i think
  • Loading branch information
cbeer committed Jan 19, 2013
1 parent 2cb454d commit 2986adc
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 26 deletions.
3 changes: 0 additions & 3 deletions pom.xml
Expand Up @@ -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 2986adc

Please sign in to comment.