Skip to content

Commit

Permalink
Better formatting for Atom messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Feb 2, 2013
1 parent fab4d2d commit db852c7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
27 changes: 17 additions & 10 deletions src/main/java/org/fcrepo/modeshape/observer/JMSTopicPublisher.java
Expand Up @@ -6,6 +6,7 @@
import static javax.jcr.observation.Event.PROPERTY_ADDED;
import static javax.jcr.observation.Event.PROPERTY_CHANGED;
import static javax.jcr.observation.Event.PROPERTY_REMOVED;
import static org.apache.abdera.model.Text.Type.TEXT;

import java.io.IOException;
import java.io.StringWriter;
Expand All @@ -27,7 +28,7 @@

import org.apache.abdera.Abdera;
import org.apache.abdera.model.Entry;
import org.apache.abdera.model.Text.Type;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -49,7 +50,7 @@ public class JMSTopicPublisher {
private ActiveMQConnectionFactory connectionFactory;

private Connection connection;
private Session session;
private Session jmsSession;
private MessageProducer producer;

final static private Abdera abdera = new Abdera();
Expand All @@ -62,14 +63,20 @@ public class JMSTopicPublisher {
@Subscribe
public void publishJCREvent(Event jcrEvent) throws JMSException,
RepositoryException, IOException {

Entry entry = abdera.newEntry();
entry.setTitle(operationsMappings.getFedoraMethodType(jcrEvent),
Type.TEXT).setBaseUri("http://localhost:8080/rest");
entry.addCategory("xsd:string", jcrEvent.getPath(), "fedora-types:pid");

entry.setTitle(operationsMappings.getFedoraMethodType(jcrEvent), TEXT)
.setBaseUri("http://localhost:8080/rest");

String path = jcrEvent.getPath();
String pid = path.substring(path.lastIndexOf('/') + 1, path.length());
entry.addCategory("xsd:string", pid, "fedora-types:pid");

StringWriter writer = new StringWriter();
entry.writeTo(writer);
String atomMessage = writer.toString();
producer.send(session.createTextMessage(atomMessage));
producer.send(jmsSession.createTextMessage(atomMessage));

logger.debug("Put event: \n" + atomMessage + "\n onto JMS.");
}
Expand All @@ -83,24 +90,24 @@ public void acquireConnections() throws JMSException, LoginException,

connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(session.createTopic("fedora"));
jmsSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = jmsSession.createProducer(jmsSession.createTopic("fedora"));
eventBus.register(this);
}

@PreDestroy
public void releaseConnections() throws JMSException {
operationsMappings.session.logout();
producer.close();
session.close();
jmsSession.close();
connection.close();
eventBus.unregister(this);
}

final private class OperationsMappings {

// this actor will never mutate the state of the repo,
// so we keep the session live for efficiency
// so we keep the jmsSession live for efficiency
private javax.jcr.Session session;

public String getFedoraMethodType(Event jcrEvent)
Expand Down
Expand Up @@ -73,7 +73,7 @@ public void testNodeCreation() throws LoginException, RepositoryException {
path = cat.getTerm();
}
}
assertEquals("Got wrong pid!", "/test1", path);
assertEquals("Got wrong pid!", "test1", path);
assertEquals("Got wrong method!", "ingest", entry.getTitle());
}

Expand Down

0 comments on commit db852c7

Please sign in to comment.