Skip to content

Commit

Permalink
Adding list of properties and a System-property-configurable baseURL …
Browse files Browse the repository at this point in the history
…to JMS events
  • Loading branch information
escowles committed May 30, 2014
1 parent c822f0e commit 5a4248c
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 13 deletions.
Expand Up @@ -52,6 +52,29 @@ public class DefaultMessageFactory implements JMSEventMessageFactory {
public static final String EVENT_TYPE_HEADER_NAME = REPOSITORY_NAMESPACE
+ "eventType";

public static final String BASE_URL_HEADER_NAME = REPOSITORY_NAMESPACE
+ "baseURL";

public static final String PROPERTIES_HEADER_NAME = REPOSITORY_NAMESPACE
+ "properties";

private String baseURL;

/**
* Set the baseURL clients should use to resolve paths to repository URIs
**/
public void setBaseURL( final String baseURL ) {
this.baseURL = baseURL;
System.out.println("baseURL: " + baseURL);
}

/**
* Get the baseURL clients should use to resolve paths to repository URIs
**/
public String getBaseURL() {
return baseURL;
}

@Override
public Message getMessage(final FedoraEvent jcrEvent,
final javax.jms.Session jmsSession) throws RepositoryException,
Expand All @@ -61,6 +84,8 @@ public Message getMessage(final FedoraEvent jcrEvent,
message.setStringProperty(IDENTIFIER_HEADER_NAME, jcrEvent.getPath());
message.setStringProperty(EVENT_TYPE_HEADER_NAME, getEventURIs( jcrEvent
.getTypes()));
message.setStringProperty(BASE_URL_HEADER_NAME, baseURL);
message.setStringProperty(PROPERTIES_HEADER_NAME, Joiner.on(',').join(jcrEvent.getProperties()));
return message;
}

Expand Down
Expand Up @@ -21,8 +21,10 @@
import static javax.jcr.observation.Event.NODE_REMOVED;
import static javax.jms.Session.AUTO_ACKNOWLEDGE;
import static org.fcrepo.jcr.FedoraJcrTypes.FEDORA_OBJECT;
import static org.fcrepo.jms.headers.DefaultMessageFactory.BASE_URL_HEADER_NAME;
import static org.fcrepo.jms.headers.DefaultMessageFactory.EVENT_TYPE_HEADER_NAME;
import static org.fcrepo.jms.headers.DefaultMessageFactory.IDENTIFIER_HEADER_NAME;
import static org.fcrepo.jms.headers.DefaultMessageFactory.PROPERTIES_HEADER_NAME;
import static org.fcrepo.jms.headers.DefaultMessageFactory.TIMESTAMP_HEADER_NAME;
import static org.fcrepo.kernel.RdfLexicon.REPOSITORY_NAMESPACE;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -169,10 +171,9 @@ public void testRemoval() throws RepositoryException, InterruptedException,
@Override
public void onMessage(final Message message) {
try {
LOGGER.debug(
"Received JMS message: {} with identifier: {}, timestamp: {}, and event type: {}",
message.getJMSMessageID(), getIdentifier(message),
getTimestamp(message), getEventTypes(message));
LOGGER.debug( "Received JMS message: {} with identifier: {}, timestamp: {}, event type: {}, properties: {},"
+ " and baseURL: {}", message.getJMSMessageID(), getIdentifier(message), getTimestamp(message),
getEventTypes(message), getProperties(message), getBaseURL(message));
} catch (final JMSException e) {
propagate(e);
}
Expand Down Expand Up @@ -220,4 +221,12 @@ private static Long getTimestamp(final Message msg) throws JMSException {
return msg.getLongProperty(TIMESTAMP_HEADER_NAME);
}

private static String getBaseURL(final Message msg) throws JMSException {
return msg.getStringProperty(BASE_URL_HEADER_NAME);
}

private static String getProperties(final Message msg) throws JMSException {
return msg.getStringProperty(PROPERTIES_HEADER_NAME);
}

}
3 changes: 2 additions & 1 deletion fcrepo-jms/src/test/resources/spring-test/headers-jms.xml
Expand Up @@ -19,6 +19,7 @@
<amq:connectionFactory id="connectionFactory"
brokerURL="vm://localhost?broker.persistent=false&amp;broker.useJmx=false&amp;broker.enableStatistics=false"/>

<bean class="org.fcrepo.jms.headers.DefaultMessageFactory"/>
<bean class="org.fcrepo.jms.headers.DefaultMessageFactory"
p:baseURL="http://localhost:8080/rest"/>

</beans>
Expand Up @@ -18,6 +18,8 @@
import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Multimaps.index;
import static org.slf4j.LoggerFactory.getLogger;
import static javax.jcr.observation.Event.PROPERTY_ADDED;
import static javax.jcr.observation.Event.PROPERTY_CHANGED;

import java.util.Iterator;

Expand Down Expand Up @@ -80,13 +82,17 @@ public FedoraEvent next() {
// because if
// there was no event at all, there would appear no entry in our
// Multimap under this key
final FedoraEvent fedoraEvent = new FedoraEvent(nodeSpecificEvents.next());
final Event firstEvent = nodeSpecificEvents.next();
final FedoraEvent fedoraEvent = new FedoraEvent(firstEvent);
addProperty( fedoraEvent, firstEvent );
while (nodeSpecificEvents.hasNext()) {
// add the type of the event in hand to the event we are
// building up to emit
// we could aggregate other information here if that seems
// useful
fedoraEvent.addType(nodeSpecificEvents.next().getType());
// add the event type and property name to the event we are building up to emit
// we could aggregate other information here if that seems useful
final Event otherEvent = nodeSpecificEvents.next();
try {
fedoraEvent.addType(otherEvent.getType());
addProperty( fedoraEvent, otherEvent );
} catch ( Exception ex ) { }
}
return fedoraEvent;
}
Expand All @@ -96,6 +102,16 @@ public void remove() {
// the underlying Multimap is immutable anyway
throw new UnsupportedOperationException();
}

private void addProperty( final FedoraEvent fedoraEvent, final Event e ) {
try {
if ( e.getType() == PROPERTY_ADDED || e.getType() == PROPERTY_CHANGED ) {
fedoraEvent.addProperty( e.getPath().substring(e.getPath().lastIndexOf("/") + 1) );
}
} catch (final RepositoryException ex) {
throw propagate(ex);
}
}
};
}

Expand Down
Expand Up @@ -49,6 +49,7 @@ public class FedoraEvent {
private Event e;

private Set<Integer> eventTypes = new HashSet<>();
private Set<String> eventProperties = new HashSet<>();

/**
* Wrap a JCR Event with our FedoraEvent decorators
Expand Down Expand Up @@ -76,6 +77,23 @@ public FedoraEvent addType(final Integer type) {
return this;
}

/**
* @return the property names of the underlying JCR property {@linkEvent}s
**/
public Set<String> getProperties() {
return eventProperties;
}

/**
* Add a property name to this event
* @param property property name
* @return this object for continued use
**/
public FedoraEvent addProperty( final String property ) {
eventProperties.add(property);
return this;
}

/**
* @return the path of the underlying JCR {@link Event}s
*/
Expand Down Expand Up @@ -129,7 +147,9 @@ public String toString() {
public String apply(final Integer type) {
return EventType.valueOf(type).getName();
}
}))).add("Path:", getPath()).add("Date: ", getDate()).add("Info:", getInfo()).toString();
}))).add("Event properties:",
Joiner.on(',').join(eventProperties)).add("Path:", getPath()).add("Date: ",
getDate()).add("Info:", getInfo()).toString();
} catch (final RepositoryException e) {
throw propagate(e);
}
Expand Down
3 changes: 2 additions & 1 deletion fcrepo-webapp/src/main/resources/spring/jms.xml
Expand Up @@ -19,6 +19,7 @@
p:config="classpath:/config/activemq.xml" p:start="true"/>

<!-- translates events into JMS header-only format-->
<bean class="org.fcrepo.jms.headers.DefaultMessageFactory"/>
<bean class="org.fcrepo.jms.headers.DefaultMessageFactory"
p:baseURL="${jms.baseURL:http://localhost:8080/rest}"/>

</beans>

0 comments on commit 5a4248c

Please sign in to comment.