Skip to content

Commit

Permalink
FCREPO-1864: Remove jcr dependencies from EventType enum
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Dec 22, 2015
1 parent 7494305 commit 023bdaa
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 132 deletions.
Expand Up @@ -26,8 +26,8 @@
import javax.jms.Message;

import org.fcrepo.jms.observer.JMSEventMessageFactory;
import org.fcrepo.kernel.api.observer.EventType;
import org.fcrepo.kernel.api.observer.FedoraEvent;
import org.fcrepo.kernel.api.utils.EventType;

import org.slf4j.Logger;

Expand Down
Expand Up @@ -18,11 +18,6 @@
import static com.google.common.base.Throwables.propagate;
import static com.jayway.awaitility.Awaitility.await;
import static com.jayway.awaitility.Duration.ONE_SECOND;
import static javax.jcr.observation.Event.NODE_ADDED;
import static javax.jcr.observation.Event.NODE_REMOVED;
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 javax.jms.Session.AUTO_ACKNOWLEDGE;
import static org.fcrepo.jms.headers.DefaultMessageFactory.BASE_URL_HEADER_NAME;
import static org.fcrepo.jms.headers.DefaultMessageFactory.EVENT_TYPE_HEADER_NAME;
Expand Down Expand Up @@ -51,10 +46,10 @@

import org.fcrepo.kernel.api.exception.InvalidChecksumException;
import org.fcrepo.kernel.api.models.FedoraResource;
import org.fcrepo.kernel.api.observer.EventType;
import org.fcrepo.kernel.api.models.Container;
import org.fcrepo.kernel.api.services.BinaryService;
import org.fcrepo.kernel.api.services.ContainerService;
import org.fcrepo.kernel.api.utils.EventType;
import org.fcrepo.kernel.modeshape.rdf.impl.DefaultIdentifierTranslator;
import org.fcrepo.kernel.modeshape.rdf.impl.PropertiesRdfContext;

Expand Down Expand Up @@ -95,15 +90,15 @@ public class HeadersJMSIT implements MessageListener {
private static final String testMeta = "/testMessageFromMetadata-" + randomUUID();

private static final String NODE_ADDED_EVENT_TYPE
= REPOSITORY_NAMESPACE + EventType.valueOf(NODE_ADDED).toString();
= REPOSITORY_NAMESPACE + EventType.NODE_ADDED;
private static final String NODE_REMOVED_EVENT_TYPE
= REPOSITORY_NAMESPACE + EventType.valueOf(NODE_REMOVED).toString();
= REPOSITORY_NAMESPACE + EventType.NODE_REMOVED;
private static final String PROP_ADDED_EVENT_TYPE
= REPOSITORY_NAMESPACE + EventType.valueOf(PROPERTY_ADDED).toString();
= REPOSITORY_NAMESPACE + EventType.PROPERTY_ADDED;
private static final String PROP_CHANGED_EVENT_TYPE
= REPOSITORY_NAMESPACE + EventType.valueOf(PROPERTY_CHANGED).toString();
= REPOSITORY_NAMESPACE + EventType.PROPERTY_CHANGED;
private static final String PROP_REMOVED_EVENT_TYPE
= REPOSITORY_NAMESPACE + EventType.valueOf(PROPERTY_REMOVED).toString();
= REPOSITORY_NAMESPACE + EventType.PROPERTY_REMOVED;

@Inject
private Repository repository;
Expand Down
Expand Up @@ -17,7 +17,6 @@

import static com.google.common.base.Strings.isNullOrEmpty;
import static java.util.Collections.singleton;
import static javax.jcr.observation.Event.NODE_ADDED;
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;
Expand All @@ -38,8 +37,9 @@
import javax.jms.Session;

import org.apache.activemq.command.ActiveMQObjectMessage;

import org.fcrepo.kernel.api.observer.EventType;
import org.fcrepo.kernel.api.observer.FedoraEvent;
import org.fcrepo.kernel.api.utils.EventType;

import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -102,8 +102,8 @@ private Message doTestBuildMessage(final String baseUrl, final String userAgent,
final String testUser = "testUser";
when(mockEvent.getUserID()).thenReturn(testUser);
when(mockEvent.getPath()).thenReturn(id);
final Set<EventType> testTypes = singleton(EventType.valueOf(NODE_ADDED));
final String testReturnType = REPOSITORY_NAMESPACE + EventType.valueOf(NODE_ADDED).toString();
final Set<EventType> testTypes = singleton(EventType.NODE_ADDED);
final String testReturnType = REPOSITORY_NAMESPACE + EventType.NODE_ADDED;
when(mockEvent.getTypes()).thenReturn(testTypes);
final String prop = "test-property";
when(mockEvent.getProperties()).thenReturn(singleton(prop));
Expand Down
@@ -0,0 +1,45 @@
/*
* Copyright 2015 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.kernel.api.observer;

/**
* A convenient abstraction over JCR's integer-typed events.
*
* @author ajs6f
* @since Feb 7, 2013
*/
public enum EventType {
NODE_ADDED("node added"),
NODE_REMOVED("node removed"),
PROPERTY_ADDED("property added"),
PROPERTY_REMOVED("property removed"),
PROPERTY_CHANGED("property changed"),
NODE_MOVED("node moved"),
PERSIST("persist");

private final String eventName;

EventType(final String eventName) {
this.eventName = eventName;
}

/**
* @return a human-readable name for this event
*/
public String getName() {
return this.eventName;
}
}
Expand Up @@ -17,8 +17,6 @@

import java.util.Set;

import org.fcrepo.kernel.api.utils.EventType;

/**
* A very simple abstraction to support downstream event-related machinery.
*
Expand Down

This file was deleted.

Expand Up @@ -13,10 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.kernel.api.utils;
package org.fcrepo.kernel.api.observer;

import static javax.jcr.observation.Event.NODE_ADDED;
import static org.fcrepo.kernel.api.utils.EventType.valueOf;
import static org.junit.Assert.assertEquals;

import org.junit.Test;
Expand All @@ -28,20 +26,8 @@
*/
public class EventTypeTest {

@Test
public void testGetEventName() {
assertEquals("node added", valueOf(NODE_ADDED).getName());
}

@Test(expected = IllegalArgumentException.class)
public void testBadEvent() {
valueOf(9999999);
}

@Test()
public void testValueOf() {
assertEquals(EventType.PERSIST, EventType.valueOf("PERSIST"));
}


}
Expand Up @@ -15,20 +15,25 @@
*/
package org.fcrepo.kernel.modeshape.observer;

import static com.google.common.base.Functions.forMap;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Sets.union;
import static org.fcrepo.kernel.api.utils.EventType.valueOf;
import static org.fcrepo.kernel.api.observer.EventType.NODE_ADDED;
import static org.fcrepo.kernel.api.observer.EventType.NODE_MOVED;
import static org.fcrepo.kernel.api.observer.EventType.NODE_REMOVED;
import static org.fcrepo.kernel.api.observer.EventType.PERSIST;
import static org.fcrepo.kernel.api.observer.EventType.PROPERTY_ADDED;
import static org.fcrepo.kernel.api.observer.EventType.PROPERTY_CHANGED;
import static org.fcrepo.kernel.api.observer.EventType.PROPERTY_REMOVED;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static java.util.Arrays.asList;
import static java.util.Collections.singleton;
import static java.util.stream.Collectors.toList;
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 java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.jcr.RepositoryException;
Expand All @@ -38,7 +43,10 @@
import org.fcrepo.kernel.api.observer.FedoraEvent;
import org.fcrepo.kernel.api.services.functions.HierarchicalIdentifierSupplier;
import org.fcrepo.kernel.api.services.functions.UniqueValueSupplier;
import org.fcrepo.kernel.api.utils.EventType;

import com.google.common.collect.ImmutableMap;

import org.fcrepo.kernel.api.observer.EventType;

/**
* A very simple abstraction to prevent event-driven machinery downstream from the repository from relying directly
Expand All @@ -49,13 +57,15 @@
*/
public class FedoraEventImpl implements FedoraEvent {

private Event e;
private final Event e;
private final String eventID;

private Set<EventType> eventTypes = new HashSet<>();
private Set<String> eventProperties = new HashSet<>();
private static final List<Integer> PROPERTY_TYPES = Arrays.asList(PROPERTY_ADDED, PROPERTY_CHANGED,
PROPERTY_REMOVED);
private final Set<EventType> eventTypes = new HashSet<>();
private final Set<String> eventProperties = new HashSet<>();

private static final List<Integer> PROPERTY_TYPES = asList(javax.jcr.observation.Event.PROPERTY_ADDED,
javax.jcr.observation.Event.PROPERTY_CHANGED,
javax.jcr.observation.Event.PROPERTY_REMOVED);

private static final UniqueValueSupplier pidMinter = new DefaultPathMinter();

Expand Down Expand Up @@ -143,7 +153,7 @@ public static String getPath(final Event e) {
path = e.getPath();
}
return path.replaceAll("/" + JCR_CONTENT, "");
} catch (RepositoryException e1) {
} catch (final RepositoryException e1) {
throw new RepositoryRuntimeException("Error getting event path!", e1);
}
}
Expand All @@ -163,7 +173,7 @@ public String getUserID() {
public String getUserData() {
try {
return e.getUserData();
} catch (RepositoryException e1) {
} catch (final RepositoryException e1) {
throw new RepositoryRuntimeException("Error getting event userData!", e1);
}
}
Expand All @@ -175,7 +185,7 @@ public String getUserData() {
public long getDate() {
try {
return e.getDate();
} catch (RepositoryException e1) {
} catch (final RepositoryException e1) {
throw new RepositoryRuntimeException("Error getting event date!", e1);
}
}
Expand All @@ -201,5 +211,24 @@ public String toString() {
.add("Date: ", getDate()).toString();
}

private static final Map<Integer, EventType> translation = ImmutableMap.<Integer, EventType>builder()
.put(javax.jcr.observation.Event.NODE_ADDED, NODE_ADDED)
.put(javax.jcr.observation.Event.NODE_REMOVED, NODE_REMOVED)
.put(javax.jcr.observation.Event.PROPERTY_ADDED, PROPERTY_ADDED)
.put(javax.jcr.observation.Event.PROPERTY_REMOVED, PROPERTY_REMOVED)
.put(javax.jcr.observation.Event.PROPERTY_CHANGED, PROPERTY_CHANGED)
.put(javax.jcr.observation.Event.NODE_MOVED, NODE_MOVED)
.put(javax.jcr.observation.Event.PERSIST, PERSIST).build();

/**
* Get the Fedora event type for a JCR type
*
* @param i the integer value of a JCR type
* @return EventType
*/
public static EventType valueOf(final Integer i) {
return forMap(translation).apply(i);
}

private static class DefaultPathMinter implements HierarchicalIdentifierSupplier { }
}
Expand Up @@ -34,7 +34,6 @@
import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.api.observer.FedoraEvent;
import org.fcrepo.kernel.api.observer.eventmappings.InternalExternalEventMapper;
import org.fcrepo.kernel.api.utils.EventType;
import org.fcrepo.kernel.modeshape.observer.FedoraEventImpl;

import org.slf4j.Logger;
Expand Down Expand Up @@ -103,7 +102,7 @@ public FedoraEvent next() {
// 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();
fedoraEvent.addType(EventType.valueOf(otherEvent.getType()));
fedoraEvent.addType(FedoraEventImpl.valueOf(otherEvent.getType()));
addProperty(fedoraEvent, otherEvent);
}
return fedoraEvent;
Expand Down

0 comments on commit 023bdaa

Please sign in to comment.