Skip to content

Commit

Permalink
Moving the rest of initizalization code into Bootstrap.java in prepar…
Browse files Browse the repository at this point in the history
…ation for the introduction of DI
  • Loading branch information
ajs6f committed Jan 23, 2013
1 parent b645067 commit 6505162
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 160 deletions.
Expand Up @@ -5,7 +5,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Map;

import javax.jcr.Node;
Expand All @@ -17,11 +16,8 @@
import org.codehaus.jackson.map.ObjectMapper;
import org.fcrepo.ffmodeshapeprototype.identifiers.PidMinter;
import org.fcrepo.ffmodeshapeprototype.identifiers.UUIDPidMinter;
import org.modeshape.common.logging.Logger;
import org.modeshape.jcr.ConfigurationException;
import org.modeshape.jcr.JcrRepository;

import freemarker.ext.beans.BeansWrapper;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
Expand All @@ -30,51 +26,22 @@ public abstract class AbstractResource {

static final ObjectMapper mapper = new ObjectMapper();

protected static final Response four01 = Response.status(404).entity("401").build();
protected static final Response four04 = Response.status(404).entity("404").build();

private final Logger logger = Logger.getLogger(AbstractResource.class);
protected static final Response four01 = Response.status(404).entity("401")
.build();
protected static final Response four04 = Response.status(404).entity("404")
.build();

static protected Configuration freemarker = null;
static protected Workspace ws = null;

protected static PidMinter pidMinter = new UUIDPidMinter();

public AbstractResource() throws ConfigurationException,
RepositoryException, IOException {
if (ws == null) {

final JcrRepository repository = Bootstrap.getRepository();

if (repository == null) {
throw new RepositoryException("Missing repository?");
}
ws = repository.login().getWorkspace();

String[] workspaceNames = ws.getAccessibleWorkspaceNames();

if(!Arrays.asList(workspaceNames).contains("fedora")) {
ws.createWorkspace("fedora");
logger.debug("Created 'fedora' workspace.\n");
}

// switching to our new Fedora workspace
ws = repository.login("fedora").getWorkspace();
if(!Arrays.asList(ws.getNamespaceRegistry().getPrefixes()).contains("test")) {
ws.getNamespaceRegistry().registerNamespace("test", "test");
}

ws = Bootstrap.getWorkspace();
}

if (freemarker == null) {
freemarker = new Configuration();
logger.debug("Setting up Freemarker object wrapper");
BeansWrapper objWrapper = new BeansWrapper();
objWrapper.setExposureLevel(BeansWrapper.EXPOSE_ALL);
freemarker.setObjectWrapper(objWrapper);
// Specify the data source where the template files come from.
freemarker.setClassForTemplateLoading(this.getClass(),
"/freemarker");
freemarker = Bootstrap.getFreemarker();
}
}

Expand All @@ -96,8 +63,8 @@ protected Response deleteResource(final String path)
}

protected InputStream renderTemplate(final String templatename,
final Map<String, ?> map) throws RepositoryException,
IOException, TemplateException {
final Map<String, ?> map) throws RepositoryException, IOException,
TemplateException {

final Template template = freemarker.getTemplate(templatename);
final ByteArrayOutputStream out = new ByteArrayOutputStream();
Expand All @@ -107,5 +74,4 @@ protected InputStream renderTemplate(final String templatename,
return in;
}


}
182 changes: 116 additions & 66 deletions src/main/java/org/fcrepo/ffmodeshapeprototype/Bootstrap.java
@@ -1,5 +1,12 @@
package org.fcrepo.ffmodeshapeprototype;

import java.io.FileNotFoundException;
import java.util.Arrays;

import javax.jcr.RepositoryException;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.infinispan.schematic.document.ParsingException;
import org.modeshape.common.SystemFailureException;
Expand All @@ -9,70 +16,113 @@
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;
}
import org.modeshape.jcr.api.JcrTools;
import org.modeshape.jcr.api.Workspace;

import freemarker.ext.beans.BeansWrapper;
import freemarker.template.Configuration;

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

private static Workspace ws = null;
private static Configuration freemarker = null;
private static Boolean initialized = false;

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

private static final JcrTools jcrTools = new JcrTools();

public void contextInitialized(ServletContextEvent contextEvent) {
logger.debug("Context Created");
context = contextEvent.getServletContext();
try {
if (initialized == false)
initializeEngine();
initialized = true;
} 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;
}

public static Workspace getWorkspace() throws RepositoryException {
if (ws == null)
initializeEngine();
return ws;
}

public static Configuration getFreemarker() throws RepositoryException {
if (freemarker == null)
initializeEngine();
return freemarker;
}

private static void 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);
final JcrRepository repository = Bootstrap.getRepository();

if (repository == null) {
throw new RepositoryException("Missing repository?");
}

ws = repository.login().getWorkspace();

String[] workspaceNames = ws.getAccessibleWorkspaceNames();

if (!Arrays.asList(workspaceNames).contains("fedora")) {
ws.createWorkspace("fedora");
logger.debug("Created 'fedora' workspace.\n");
}

// switching to our new Fedora workspace
ws = repository.login("fedora").getWorkspace();
if (!Arrays.asList(ws.getNamespaceRegistry().getPrefixes()).contains(
"test")) {
ws.getNamespaceRegistry().registerNamespace("test", "test");
}
jcrTools.findOrCreateChild(ws.getSession().getRootNode(), "fedora");
logger.debug("Deployed Fedora repository.");

freemarker = new Configuration();
logger.debug("Setting up Freemarker object wrapper");
BeansWrapper objWrapper = new BeansWrapper();
objWrapper.setExposureLevel(BeansWrapper.EXPOSE_ALL);
freemarker.setObjectWrapper(objWrapper);
// Specify the data source where the template files come from.
freemarker.setClassForTemplateLoading(Bootstrap.class, "/freemarker");

}
}

0 comments on commit 6505162

Please sign in to comment.