Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
New DefaultMessageFactory using only headers
- Loading branch information
Showing
2 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
fcrepo-jms/src/main/java/org/fcrepo/jms/headers/DefaultMessageFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
|
||
package org.fcrepo.jms.headers; | ||
|
||
import static org.fcrepo.kernel.RdfLexicon.REPOSITORY_NAMESPACE; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.jcr.RepositoryException; | ||
import javax.jcr.Session; | ||
import javax.jcr.observation.Event; | ||
import javax.jms.JMSException; | ||
import javax.jms.Message; | ||
|
||
import org.fcrepo.jms.observer.JMSEventMessageFactory; | ||
import org.fcrepo.kernel.utils.EventType; | ||
|
||
/** | ||
* Generates JMS {@link Message}s composed entirely of headers, based entirely | ||
* on information found in the JCR {@link Event} that triggers publication. | ||
* | ||
* @author ajs6f | ||
* @date Dec 2, 2013 | ||
*/ | ||
public class DefaultMessageFactory implements JMSEventMessageFactory { | ||
|
||
public static final String TIMESTAMP_HEADER_NAME = REPOSITORY_NAMESPACE | ||
+ "timestamp"; | ||
|
||
public static final String IDENTIFIER_HEADER_NAME = REPOSITORY_NAMESPACE | ||
+ "identifier"; | ||
|
||
public static final String EVENT_TYPE_HEADER_NAME = REPOSITORY_NAMESPACE | ||
+ "eventType"; | ||
|
||
@Override | ||
public Message getMessage(final Event jcrEvent, final Session jcrSession, | ||
final javax.jms.Session jmsSession) throws RepositoryException, | ||
IOException, JMSException { | ||
final Message message = jmsSession.createMessage(); | ||
message.setLongProperty(TIMESTAMP_HEADER_NAME, jcrEvent.getDate()); | ||
message.setStringProperty(IDENTIFIER_HEADER_NAME, jcrEvent.getPath()); | ||
message.setStringProperty(EVENT_TYPE_HEADER_NAME, getEventURI(jcrEvent | ||
.getType())); | ||
return message; | ||
} | ||
|
||
private static String getEventURI(final int type) { | ||
return REPOSITORY_NAMESPACE + EventType.valueOf(type).toString(); | ||
} | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
fcrepo-jms/src/test/java/org/fcrepo/jms/headers/DefaultMessageFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
|
||
package org.fcrepo.jms.headers; | ||
|
||
import static javax.jcr.observation.Event.NODE_ADDED; | ||
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.TIMESTAMP_HEADER_NAME; | ||
import static org.fcrepo.kernel.RdfLexicon.REPOSITORY_NAMESPACE; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.when; | ||
import static org.mockito.MockitoAnnotations.initMocks; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.jcr.RepositoryException; | ||
import javax.jcr.observation.Event; | ||
import javax.jms.JMSException; | ||
import javax.jms.Message; | ||
import javax.jms.Session; | ||
|
||
import org.apache.activemq.command.ActiveMQObjectMessage; | ||
import org.fcrepo.kernel.utils.EventType; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
|
||
public class DefaultMessageFactoryTest { | ||
|
||
@Mock | ||
private Session mockSession; | ||
|
||
@Mock | ||
private Event mockEvent; | ||
|
||
private DefaultMessageFactory testDefaultMessageFactory; | ||
|
||
@Before | ||
public void setUp() throws JMSException { | ||
initMocks(this); | ||
when(mockSession.createMessage()).thenReturn( | ||
new ActiveMQObjectMessage()); | ||
testDefaultMessageFactory = new DefaultMessageFactory(); | ||
} | ||
|
||
@Test | ||
public void testBuildMessage() throws RepositoryException, IOException, | ||
JMSException { | ||
final Long testDate = 46647758568747L; | ||
when(mockEvent.getDate()).thenReturn(testDate); | ||
final String testPath = "super/calli/fragi/listic"; | ||
when(mockEvent.getPath()).thenReturn(testPath); | ||
final Integer testType = NODE_ADDED; | ||
final String testReturnType = | ||
REPOSITORY_NAMESPACE + EventType.valueOf(NODE_ADDED).toString(); | ||
when(mockEvent.getType()).thenReturn(testType); | ||
final Message testMessage = | ||
testDefaultMessageFactory.getMessage(mockEvent, null, mockSession); | ||
assertEquals("Got wrong date in message!", testDate, (Long) testMessage | ||
.getLongProperty(TIMESTAMP_HEADER_NAME)); | ||
assertEquals("Got wrong identifier in message!", testPath, testMessage | ||
.getStringProperty(IDENTIFIER_HEADER_NAME)); | ||
assertEquals("Got wrong type in message!", testReturnType, testMessage | ||
.getStringProperty(EVENT_TYPE_HEADER_NAME)); | ||
} | ||
|
||
} |