Skip to content

Commit

Permalink
ModeShape REST API no up alongside Fedora
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Jan 23, 2013
1 parent dbc1e7a commit c04de5d
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 30 deletions.
75 changes: 48 additions & 27 deletions src/main/java/org/fcrepo/ffmodeshapeprototype/Bootstrap.java
Expand Up @@ -2,6 +2,8 @@

import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import javax.jcr.RepositoryException;
import javax.servlet.ServletContext;
Expand All @@ -11,6 +13,7 @@
import org.infinispan.schematic.document.ParsingException;
import org.modeshape.common.SystemFailureException;
import org.modeshape.common.collection.Problems;
import org.modeshape.common.i18n.TextI18n;
import org.modeshape.common.logging.Logger;
import org.modeshape.jcr.ConfigurationException;
import org.modeshape.jcr.JcrRepository;
Expand All @@ -24,6 +27,8 @@

public class Bootstrap implements ServletContextListener {
ServletContext context;

private static ModeShapeEngine engine = new ModeShapeEngine();
private static JcrRepository repository = null;

private static Workspace ws = null;
Expand All @@ -39,60 +44,76 @@ public void contextInitialized(ServletContextEvent contextEvent) {
context = contextEvent.getServletContext();
try {
if (initialized == false)
initializeEngine();
initializeRepo();
initialized = true;
} catch (RepositoryException e) {
throw new java.lang.RuntimeException(e);
}
}

public void contextDestroyed(ServletContextEvent contextEvent) {

Future<Boolean> future = engine.shutdown();
try {
if (future.get()) { // optional, but blocks until engine is
// completely
// shutdown or interrupted
logger.debug("Shut down ModeShape safely");
}
} catch (InterruptedException e) {
logger.error(e, new TextI18n(
"Could not shut down ModeShape safely!"));
e.printStackTrace();
} catch (ExecutionException e) {
logger.error(e, new TextI18n(
"Could not shut down ModeShape safely!"));
e.printStackTrace();
}
}

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

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

public static Configuration getFreemarker() throws RepositoryException {
if (freemarker == null)
initializeEngine();
initializeRepo();
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.");
private static void initializeRepo() throws RepositoryException {
engine.start();
if (!engine.getRepositoryNames().contains("repo")) {
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);
} catch (FileNotFoundException ex) {
logger.error(ex, null);
}

repository = engine.deploy(repository_config);

} 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");
} else {
repository = engine.getRepository("repo");
}
engine.start();
repository = engine.deploy(repository_config);
final JcrRepository repository = Bootstrap.getRepository();

if (repository == null) {
throw new RepositoryException("Missing repository?");
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/org/fcrepo/ffmodeshapeprototype/FedoraObjects.java
Expand Up @@ -6,6 +6,7 @@
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.ws.rs.DELETE;
Expand All @@ -16,6 +17,7 @@
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import org.modeshape.common.logging.Logger;
import org.modeshape.jcr.ConfigurationException;

import com.google.common.collect.ImmutableMap;
Expand All @@ -25,6 +27,8 @@
@Path("/objects")
public class FedoraObjects extends AbstractResource {

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

public FedoraObjects() throws ConfigurationException, RepositoryException,
IOException {
super();
Expand All @@ -40,7 +44,12 @@ public Response ingestAndMint() throws RepositoryException {
@Path("/{pid}")
public Response ingest(@PathParam("pid") final String pid)
throws RepositoryException {

final Session session = ws.getSession();
logger.debug("Working in repository: "
+ session.getRepository().getDescriptor(
Repository.REP_NAME_DESC));
logger.debug("Working in workspace: " + ws.getName());
final Node root = session.getRootNode();

if (session.hasPermission("/" + pid, "add_node")) {
Expand All @@ -63,7 +72,11 @@ public Response getObjectInXML(@PathParam("pid") final String pid)
throws RepositoryException, IOException, TemplateException {

final Session session = ws.getSession();

logger.debug("Working in repository: "
+ session.getRepository().getDescriptor(
"custom.rep.name"));
logger.debug("Working in workspace: " + ws.getName());

if (session.nodeExists("/" + pid)) {
final Node obj = session.getNode("/" + pid);
PropertyIterator i = obj.getProperties();
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/my_repository.json
@@ -1,6 +1,6 @@
{
"name" : "repo",
"jndiName" : "",
"jndiName" : "java:/fedora",
"workspaces" : {
"predefined" : ["fedora"],
"default" : "fedora",
Expand Down
24 changes: 24 additions & 0 deletions src/main/webapp/WEB-INF/web.xml
Expand Up @@ -4,6 +4,25 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<!-- engaging the ModeShape REST API -->

<context-param>
<param-name>org.modeshape.jcr.URL</param-name>
<param-value>file:/my_repository.json</param-value>
</context-param>

<context-param>
<param-name>org.modeshape.jcr.RepositoryName</param-name>
<param-value>repo</param-value>
</context-param>

<!-- Required parameter for ModeShape REST - should not be modified
<context-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>org.modeshape.web.jcr.rest.JcrApplication</param-value>
</context-param> -->

<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
Expand All @@ -20,6 +39,11 @@
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>

<!-- Required parameter for ModeShape REST - should not be modified -->
<listener>
<listener-class>org.modeshape.web.jcr.ModeShapeJcrDeployer</listener-class>
</listener>

<listener>
<listener-class>org.fcrepo.ffmodeshapeprototype.Bootstrap</listener-class>
</listener>
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/my_repository.json
@@ -1,6 +1,6 @@
{
"name" : "repo",
"jndiName" : "",
"jndiName" : "java:jcr/local/fedora",
"workspaces" : {
"predefined" : ["fedora"],
"default" : "fedora",
Expand Down

0 comments on commit c04de5d

Please sign in to comment.