diff --git a/api/.gitignore b/api/.gitignore new file mode 100644 index 0000000..c3a9b66 --- /dev/null +++ b/api/.gitignore @@ -0,0 +1,3 @@ +/.project +/.classpath +/.settings diff --git a/api/src/main/java/org/jboss/seam/jcr/annotations/JcrEventListener.java b/api/src/main/java/org/jboss/seam/jcr/annotations/JcrEventListener.java new file mode 100644 index 0000000..759700f --- /dev/null +++ b/api/src/main/java/org/jboss/seam/jcr/annotations/JcrEventListener.java @@ -0,0 +1,63 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright [2010], Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * 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.jboss.seam.jcr.annotations; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.jcr.observation.Event; +import javax.jcr.observation.ObservationManager; + +/** + * Represents that an event listener will be bound with this repository + * + * @see {@link ObservationManager#addEventListener(javax.jcr.observation.EventListener, int, String, boolean, String[], String[], boolean)} + * + * @author george + */ + +// TODO: Review all default parameters + +@Target({ TYPE, METHOD, PARAMETER, FIELD }) +@Retention(RUNTIME) +@Documented +public @interface JcrEventListener +{ + /** + * Constant for all events + */ + public static final int ALL_EVENTS = Event.NODE_ADDED | Event.NODE_MOVED | Event.NODE_REMOVED | Event.PERSIST | Event.PROPERTY_ADDED | Event.PROPERTY_CHANGED | Event.PROPERTY_REMOVED; + + String absPath() default "/"; + + boolean deep() default false; + + int eventTypes() default ALL_EVENTS; + + boolean noLocal() default false; + + String[] nodeTypeName() default {}; + + String[] uuid() default {}; + +} diff --git a/api/src/main/java/org/jboss/seam/jcr/annotations/events/NodeAdded.java b/api/src/main/java/org/jboss/seam/jcr/annotations/events/NodeAdded.java new file mode 100644 index 0000000..1b21b9b --- /dev/null +++ b/api/src/main/java/org/jboss/seam/jcr/annotations/events/NodeAdded.java @@ -0,0 +1,44 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright [2010], Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * 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.jboss.seam.jcr.annotations.events; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; +import javax.jcr.observation.Event; + +/** + * Qualifier for {@link Event#NODE_ADDED} events + * + * @author george + * + */ +@Qualifier +@Target({ TYPE, METHOD, PARAMETER, FIELD }) +@Retention(RUNTIME) +@Documented +public @interface NodeAdded +{ + +} diff --git a/api/src/main/java/org/jboss/seam/jcr/annotations/events/NodeMoved.java b/api/src/main/java/org/jboss/seam/jcr/annotations/events/NodeMoved.java new file mode 100644 index 0000000..c5c9ff0 --- /dev/null +++ b/api/src/main/java/org/jboss/seam/jcr/annotations/events/NodeMoved.java @@ -0,0 +1,44 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright [2010], Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * 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.jboss.seam.jcr.annotations.events; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; +import javax.jcr.observation.Event; + +/** + * Qualifier for {@link Event#NODE_MOVED} events + * + * @author george + * + */ +@Qualifier +@Target({ TYPE, METHOD, PARAMETER, FIELD }) +@Retention(RUNTIME) +@Documented +public @interface NodeMoved +{ + +} diff --git a/api/src/main/java/org/jboss/seam/jcr/annotations/events/NodeRemoved.java b/api/src/main/java/org/jboss/seam/jcr/annotations/events/NodeRemoved.java new file mode 100644 index 0000000..512d732 --- /dev/null +++ b/api/src/main/java/org/jboss/seam/jcr/annotations/events/NodeRemoved.java @@ -0,0 +1,44 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright [2010], Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * 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.jboss.seam.jcr.annotations.events; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; +import javax.jcr.observation.Event; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * Qualifier for {@link Event#NODE_REMOVED} events + * + * @author george + * + */ +@Qualifier +@Target({ TYPE, METHOD, PARAMETER, FIELD }) +@Retention(RUNTIME) +@Documented +public @interface NodeRemoved +{ + +} diff --git a/api/src/main/java/org/jboss/seam/jcr/annotations/events/Persist.java b/api/src/main/java/org/jboss/seam/jcr/annotations/events/Persist.java new file mode 100644 index 0000000..ae41d80 --- /dev/null +++ b/api/src/main/java/org/jboss/seam/jcr/annotations/events/Persist.java @@ -0,0 +1,44 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright [2010], Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * 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.jboss.seam.jcr.annotations.events; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; +import javax.jcr.observation.Event; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * Qualifier for {@link Event#PERSIST} events + * + * @author george + * + */ +@Qualifier +@Target({ TYPE, METHOD, PARAMETER, FIELD }) +@Retention(RUNTIME) +@Documented +public @interface Persist +{ + +} diff --git a/api/src/main/java/org/jboss/seam/jcr/annotations/events/PropertyAdded.java b/api/src/main/java/org/jboss/seam/jcr/annotations/events/PropertyAdded.java new file mode 100644 index 0000000..113a5c2 --- /dev/null +++ b/api/src/main/java/org/jboss/seam/jcr/annotations/events/PropertyAdded.java @@ -0,0 +1,45 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright [2010], Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * 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.jboss.seam.jcr.annotations.events; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; +import javax.jcr.observation.Event; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * Qualifier for {@link Event#PROPERTY_ADDED} events + * + * @author george + * + */ + +@Qualifier +@Target({ TYPE, METHOD, PARAMETER, FIELD }) +@Retention(RUNTIME) +@Documented +public @interface PropertyAdded +{ + +} diff --git a/api/src/main/java/org/jboss/seam/jcr/annotations/events/PropertyChanged.java b/api/src/main/java/org/jboss/seam/jcr/annotations/events/PropertyChanged.java new file mode 100644 index 0000000..4600e35 --- /dev/null +++ b/api/src/main/java/org/jboss/seam/jcr/annotations/events/PropertyChanged.java @@ -0,0 +1,44 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright [2010], Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * 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.jboss.seam.jcr.annotations.events; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; +import javax.jcr.observation.Event; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * Qualifier for {@link Event#PROPERTY_CHANGED} events + * + * @author george + * + */ +@Qualifier +@Target({ TYPE, METHOD, PARAMETER, FIELD }) +@Retention(RUNTIME) +@Documented +public @interface PropertyChanged +{ + +} diff --git a/api/src/main/java/org/jboss/seam/jcr/annotations/events/PropertyRemoved.java b/api/src/main/java/org/jboss/seam/jcr/annotations/events/PropertyRemoved.java new file mode 100644 index 0000000..7e09fee --- /dev/null +++ b/api/src/main/java/org/jboss/seam/jcr/annotations/events/PropertyRemoved.java @@ -0,0 +1,44 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright [2010], Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * 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.jboss.seam.jcr.annotations.events; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; +import javax.jcr.observation.Event; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * Qualifier for {@link Event#PROPERTY_REMOVED} events + * + * @author george + * + */ +@Qualifier +@Target({ TYPE, METHOD, PARAMETER, FIELD }) +@Retention(RUNTIME) +@Documented +public @interface PropertyRemoved +{ + +} diff --git a/combined/.gitignore b/combined/.gitignore new file mode 100644 index 0000000..c3a9b66 --- /dev/null +++ b/combined/.gitignore @@ -0,0 +1,3 @@ +/.project +/.classpath +/.settings diff --git a/dist/.gitignore b/dist/.gitignore new file mode 100644 index 0000000..541e887 --- /dev/null +++ b/dist/.gitignore @@ -0,0 +1,2 @@ +/.project +/.settings diff --git a/docs/.gitignore b/docs/.gitignore new file mode 100644 index 0000000..b75532f --- /dev/null +++ b/docs/.gitignore @@ -0,0 +1,3 @@ +/.project +/.settings +/.classpath diff --git a/impl/.gitignore b/impl/.gitignore new file mode 100644 index 0000000..ebbbd5f --- /dev/null +++ b/impl/.gitignore @@ -0,0 +1,3 @@ +/.classpath +/.project +/.settings diff --git a/impl/src/main/java/org/jboss/seam/jcr/events/JcrCDIEventListener.java b/impl/src/main/java/org/jboss/seam/jcr/events/JcrCDIEventListener.java new file mode 100644 index 0000000..a49d9cc --- /dev/null +++ b/impl/src/main/java/org/jboss/seam/jcr/events/JcrCDIEventListener.java @@ -0,0 +1,93 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright [2010], Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * 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.jboss.seam.jcr.events; + +import java.lang.annotation.Annotation; + +import javax.inject.Inject; +import javax.inject.Singleton; +import javax.jcr.observation.Event; +import javax.jcr.observation.EventIterator; +import javax.jcr.observation.EventListener; + +/** + * JCR {@link EventListener} for CDI + * + * @author george + * + */ +@Singleton +public final class JcrCDIEventListener implements EventListener +{ + + /** + * Producer of CDI Events + */ + @Inject + private javax.enterprise.event.Event eventManager; + + /** + * Fired by the JCR spec + */ + @Override + public void onEvent(EventIterator events) + { + while (events.hasNext()) + { + Event event = events.nextEvent(); + Annotation qualifier = getQualifierByEventType(event.getType()); + eventManager.select(qualifier).fire(event); + } + } + + /** + * Returns the qualifier by the event type + * + * @param eventType + * @return + */ + Annotation getQualifierByEventType(int eventType) + { + Annotation qualifier; + switch (eventType) + { + case Event.NODE_ADDED: + qualifier = NodeAddedLiteral.INSTANCE; + break; + case Event.NODE_MOVED: + qualifier = NodeMovedLiteral.INSTANCE; + break; + case Event.NODE_REMOVED: + qualifier = NodeRemovedLiteral.INSTANCE; + break; + case Event.PERSIST: + qualifier = PersistLiteral.INSTANCE; + break; + case Event.PROPERTY_ADDED: + qualifier = PropertyAddedLiteral.INSTANCE; + break; + case Event.PROPERTY_CHANGED: + qualifier = PropertyChangedLiteral.INSTANCE; + break; + case Event.PROPERTY_REMOVED: + qualifier = PropertyRemovedLiteral.INSTANCE; + break; + default: + throw new IllegalArgumentException("Event type unrecognized: " + eventType); + } + return qualifier; + } +} diff --git a/impl/src/main/java/org/jboss/seam/jcr/events/NodeAddedLiteral.java b/impl/src/main/java/org/jboss/seam/jcr/events/NodeAddedLiteral.java new file mode 100644 index 0000000..cef1a19 --- /dev/null +++ b/impl/src/main/java/org/jboss/seam/jcr/events/NodeAddedLiteral.java @@ -0,0 +1,27 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright [2010], Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * 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.jboss.seam.jcr.events; + +import javax.enterprise.util.AnnotationLiteral; + +import org.jboss.seam.jcr.annotations.events.NodeAdded; + +public class NodeAddedLiteral extends AnnotationLiteral implements NodeAdded +{ + + public static final NodeAdded INSTANCE = new NodeAddedLiteral(); + +} diff --git a/impl/src/main/java/org/jboss/seam/jcr/events/NodeMovedLiteral.java b/impl/src/main/java/org/jboss/seam/jcr/events/NodeMovedLiteral.java new file mode 100644 index 0000000..ba099ae --- /dev/null +++ b/impl/src/main/java/org/jboss/seam/jcr/events/NodeMovedLiteral.java @@ -0,0 +1,34 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright [2010], Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * 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.jboss.seam.jcr.events; + +import javax.enterprise.util.AnnotationLiteral; + +import org.jboss.seam.jcr.annotations.events.NodeMoved; + +/** + * @author george + * + */ +public class NodeMovedLiteral extends AnnotationLiteral implements NodeMoved +{ + + /** + * + */ + public static final NodeMoved INSTANCE = new NodeMovedLiteral(); + +} diff --git a/impl/src/main/java/org/jboss/seam/jcr/events/NodeRemovedLiteral.java b/impl/src/main/java/org/jboss/seam/jcr/events/NodeRemovedLiteral.java new file mode 100644 index 0000000..506bf55 --- /dev/null +++ b/impl/src/main/java/org/jboss/seam/jcr/events/NodeRemovedLiteral.java @@ -0,0 +1,34 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright [2010], Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * 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.jboss.seam.jcr.events; + +import javax.enterprise.util.AnnotationLiteral; + +import org.jboss.seam.jcr.annotations.events.NodeRemoved; + +/** + * @author george + * + */ +public class NodeRemovedLiteral extends AnnotationLiteral implements NodeRemoved +{ + + /** + * + */ + public static final NodeRemoved INSTANCE = new NodeRemovedLiteral(); + +} diff --git a/impl/src/main/java/org/jboss/seam/jcr/events/PersistLiteral.java b/impl/src/main/java/org/jboss/seam/jcr/events/PersistLiteral.java new file mode 100644 index 0000000..7d3e1e4 --- /dev/null +++ b/impl/src/main/java/org/jboss/seam/jcr/events/PersistLiteral.java @@ -0,0 +1,29 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright [2010], Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * 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.jboss.seam.jcr.events; + +import javax.enterprise.util.AnnotationLiteral; + +import org.jboss.seam.jcr.annotations.events.Persist; + +public class PersistLiteral extends AnnotationLiteral implements Persist +{ + + private static final long serialVersionUID = 1L; + + public static final Persist INSTANCE = new PersistLiteral(); + +} diff --git a/impl/src/main/java/org/jboss/seam/jcr/events/PropertyAddedLiteral.java b/impl/src/main/java/org/jboss/seam/jcr/events/PropertyAddedLiteral.java new file mode 100644 index 0000000..5687146 --- /dev/null +++ b/impl/src/main/java/org/jboss/seam/jcr/events/PropertyAddedLiteral.java @@ -0,0 +1,27 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright [2010], Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * 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.jboss.seam.jcr.events; + +import javax.enterprise.util.AnnotationLiteral; + +import org.jboss.seam.jcr.annotations.events.PropertyAdded; + +public class PropertyAddedLiteral extends AnnotationLiteral implements PropertyAdded +{ + + public static final PropertyAdded INSTANCE = new PropertyAddedLiteral(); + +} diff --git a/impl/src/main/java/org/jboss/seam/jcr/events/PropertyChangedLiteral.java b/impl/src/main/java/org/jboss/seam/jcr/events/PropertyChangedLiteral.java new file mode 100644 index 0000000..767bb03 --- /dev/null +++ b/impl/src/main/java/org/jboss/seam/jcr/events/PropertyChangedLiteral.java @@ -0,0 +1,27 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright [2010], Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * 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.jboss.seam.jcr.events; + +import javax.enterprise.util.AnnotationLiteral; + +import org.jboss.seam.jcr.annotations.events.PropertyChanged; + +public class PropertyChangedLiteral extends AnnotationLiteral implements PropertyChanged +{ + + public static final PropertyChanged INSTANCE = new PropertyChangedLiteral(); + +} diff --git a/impl/src/main/java/org/jboss/seam/jcr/events/PropertyRemovedLiteral.java b/impl/src/main/java/org/jboss/seam/jcr/events/PropertyRemovedLiteral.java new file mode 100644 index 0000000..553abf5 --- /dev/null +++ b/impl/src/main/java/org/jboss/seam/jcr/events/PropertyRemovedLiteral.java @@ -0,0 +1,27 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright [2010], Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * 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.jboss.seam.jcr.events; + +import javax.enterprise.util.AnnotationLiteral; + +import org.jboss.seam.jcr.annotations.events.PropertyRemoved; + +public class PropertyRemovedLiteral extends AnnotationLiteral implements PropertyRemoved +{ + + public static final PropertyRemoved INSTANCE = new PropertyRemovedLiteral(); + +} diff --git a/impl/src/main/java/org/jboss/seam/jcr/producers/RepositorySessionProducer.java b/impl/src/main/java/org/jboss/seam/jcr/producers/RepositorySessionProducer.java index 422c25c..4b34631 100644 --- a/impl/src/main/java/org/jboss/seam/jcr/producers/RepositorySessionProducer.java +++ b/impl/src/main/java/org/jboss/seam/jcr/producers/RepositorySessionProducer.java @@ -19,80 +19,114 @@ import java.util.Collections; import java.util.Map; import java.util.ServiceLoader; + import javax.enterprise.inject.Any; import javax.enterprise.inject.Disposes; +import javax.enterprise.inject.Instance; import javax.enterprise.inject.Produces; import javax.enterprise.inject.spi.InjectionPoint; import javax.jcr.Repository; import javax.jcr.RepositoryException; import javax.jcr.RepositoryFactory; import javax.jcr.Session; +import javax.jcr.observation.EventListener; +import javax.jcr.observation.ObservationManager; + import org.jboss.logging.Logger; +import org.jboss.seam.jcr.annotations.JcrEventListener; import org.jboss.seam.jcr.annotations.JcrRepository; +import org.jboss.seam.jcr.events.JcrCDIEventListener; /** * Produces {@link Repository} and {@link Session} objects * * @author johnament */ -public class RepositorySessionProducer { +public class RepositorySessionProducer +{ + + private final Logger logger = Logger.getLogger(RepositorySessionProducer.class); - private final Logger logger = Logger.getLogger(RepositorySessionProducer.class); + /** + * Produces a {@link Repository} based on {@link JcrRepository} + * + * TODO: we should add in support SeamManaged. + * + * @param ip + * @return + * @throws RepositoryException + */ + @Produces + @JcrRepository + public Repository produceJcrRepository(InjectionPoint ip) throws RepositoryException + { + JcrRepository jcrRepo = ip.getAnnotated().getAnnotation(JcrRepository.class); + return findRepository(jcrRepo); + } - /** - * Produces a {@link Repository} based on {@link JcrRepository} - * - * TODO: we should add in support SeamManaged. - * - * @param ip - * @return - * @throws RepositoryException - */ - @Produces - @JcrRepository - public Repository produceJcrRepository(InjectionPoint ip) throws RepositoryException { - JcrRepository jcrRepo = ip.getAnnotated().getAnnotation(JcrRepository.class); - return findRepository(jcrRepo); - } + /** + * Creates a logged in session to the repository. Currently only supports + * anonymous access. + * + * TODO: we should add in support SeamManaged. + * + * @param ip + * @return anoymous session + * @throws RepositoryException if anything not compliant to the JCR + * implementation occurs + */ + @Produces + @JcrRepository + public Session produceJcrRepositorySession(InjectionPoint ip, Instance eventListenerInstance) throws RepositoryException + { + JcrRepository jcrRepo = ip.getAnnotated().getAnnotation(JcrRepository.class); + Repository repo = findRepository(jcrRepo); + Session session = repo.login(); + // TODO: Find a better way of doing this + registerListener(ip, eventListenerInstance, session); + return session; + } - /** - * Creates a logged in session to the repository. Currently only supports - * anonymous access. - * - * TODO: we should add in support SeamManaged. - * - * @param ip - * @return anoymous session - * @throws RepositoryException - * if anything not compliant to the JCR implementation occurs - */ - @Produces - @JcrRepository - public Session produceJcrRepositorySession(InjectionPoint ip) throws RepositoryException { - JcrRepository jcrRepo = ip.getAnnotated().getAnnotation(JcrRepository.class); - Repository repo = findRepository(jcrRepo); - return repo.login(); - } + /** + * Registers this {@link EventListener} into an {@link ObservationManager} + * using the supplied config + * + * @param ann + * @param obsManager + * @throws RepositoryException + */ + private void registerListener(InjectionPoint ip, Instance eventListenerInstance, Session session) throws RepositoryException + { + JcrEventListener ann = ip.getAnnotated().getAnnotation(JcrEventListener.class); + if (ann != null) + { + session.getWorkspace().getObservationManager().addEventListener(eventListenerInstance.get(), ann.eventTypes(), ann.absPath(), ann.deep(), ann.uuid(), ann.nodeTypeName(), ann.noLocal()); + } + } - private Repository findRepository(JcrRepository jcrRepo) throws RepositoryException { - Map parameters = Collections.singletonMap(jcrRepo.name(), jcrRepo.value()); - Repository repository = null; - System.out.println(parameters); - for (RepositoryFactory factory : ServiceLoader.load(RepositoryFactory.class)) { - System.out.println(factory.getClass().getCanonicalName()); - repository = factory.getRepository(parameters); - if (repository != null) - break; - } - return repository; - } + private Repository findRepository(JcrRepository jcrRepo) throws RepositoryException + { + Map parameters = Collections.singletonMap(jcrRepo.name(), jcrRepo.value()); + Repository repository = null; + for (RepositoryFactory factory : ServiceLoader.load(RepositoryFactory.class)) + { + repository = factory.getRepository(parameters); + if (repository != null) + break; + } + return repository; + } - public void cleanSession(@Disposes @Any Session session) { - try { - session.save(); - session.logout(); - } catch (RepositoryException e) { - logger.error("Error saving session", e); - } - } + public void cleanSession(@Disposes @Any Session session) + { + try + { + session.save(); + session.logout(); + } + catch (RepositoryException e) + { + logger.error("Error saving session", e); + } + } } diff --git a/impl/src/test/java/org/jboss/seam/jcr/events/EventCounterListener.java b/impl/src/test/java/org/jboss/seam/jcr/events/EventCounterListener.java new file mode 100644 index 0000000..4062c28 --- /dev/null +++ b/impl/src/test/java/org/jboss/seam/jcr/events/EventCounterListener.java @@ -0,0 +1,82 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright [2010], Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * 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.jboss.seam.jcr.events; + +import javax.enterprise.event.Observes; +import javax.inject.Singleton; +import javax.jcr.observation.Event; + +import org.apache.commons.collections.Bag; +import org.apache.commons.collections.bag.HashBag; +import org.jboss.seam.jcr.annotations.events.NodeAdded; +import org.jboss.seam.jcr.annotations.events.NodeMoved; +import org.jboss.seam.jcr.annotations.events.NodeRemoved; +import org.jboss.seam.jcr.annotations.events.Persist; +import org.jboss.seam.jcr.annotations.events.PropertyAdded; +import org.jboss.seam.jcr.annotations.events.PropertyChanged; +import org.jboss.seam.jcr.annotations.events.PropertyRemoved; + +/** + * Used on test cases + * + * @author George Gastaldi + * + */ +@Singleton +public class EventCounterListener +{ + private Bag counter = new HashBag(); + + public void onNodeAdded(@Observes @NodeAdded Event event) + { + counter.add(event.getType()); + } + + public void onNodeRemoved(@Observes @NodeRemoved Event event) + { + counter.add(event.getType()); + } + + public void onNodeMoved(@Observes @NodeMoved Event event) + { + counter.add(event.getType()); + } + + public void onPersist(@Observes @Persist Event event) + { + counter.add(event.getType()); + } + + public void onPropertyAdded(@Observes @PropertyAdded Event event) + { + counter.add(event.getType()); + } + + public void onPropertyRemoved(@Observes @PropertyRemoved Event event) + { + counter.add(event.getType()); + } + + public void onPropertyChanged(@Observes @PropertyChanged Event event) + { + counter.add(event.getType()); + } + + public int getCountForType(int type) + { + return counter.getCount(type); + } +} \ No newline at end of file diff --git a/impl/src/test/java/org/jboss/seam/jcr/events/JcrCDIEventListenerTest.java b/impl/src/test/java/org/jboss/seam/jcr/events/JcrCDIEventListenerTest.java new file mode 100644 index 0000000..6bec3d5 --- /dev/null +++ b/impl/src/test/java/org/jboss/seam/jcr/events/JcrCDIEventListenerTest.java @@ -0,0 +1,107 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright [2010], Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * 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.jboss.seam.jcr.events; + +import static org.junit.Assert.assertEquals; + +import javax.inject.Inject; +import javax.jcr.Node; +import javax.jcr.Repository; +import javax.jcr.RepositoryException; +import javax.jcr.Session; +import javax.jcr.SimpleCredentials; +import javax.jcr.observation.Event; + +import org.jboss.arquillian.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.seam.jcr.annotations.JcrRepository; +import org.jboss.seam.jcr.producers.RepositorySessionProducer; +import org.jboss.shrinkwrap.api.ArchivePaths; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * Test case for {@link JcrCDIEventListener} + * + * NOTE: This test case works only with jackrabbit, so be sure to run with + * -Pjackrabbit + * + * @author George Gastaldi + * + */ +@RunWith(Arquillian.class) +public class JcrCDIEventListenerTest +{ + + @Inject + @JcrRepository(name = "org.apache.jackrabbit.repository.home", value = "target") + private Repository repository; + + @Inject + private EventCounterListener counter; + + @Inject + private JcrCDIEventListener listener; + + private Session session; + + @Deployment + public static JavaArchive createArchive() + { + return ShrinkWrap.create(JavaArchive.class).addPackage(JcrCDIEventListener.class.getPackage()).addPackage(RepositorySessionProducer.class.getPackage()).addManifestResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml")); + } + + /** + * Jackrabbit only allows save operations on authenticated users. + * + * TODO:Refactor to be injected with credentials + * + * @throws Exception + */ + @Before + public void setUp() throws Exception + { + session = repository.login(new SimpleCredentials("user", "pass".toCharArray())); + session.getWorkspace().getObservationManager().addEventListener(listener, 127, "/", true, null, null, false); + } + + @Test + public void testOnEventAdded() throws RepositoryException + { + try + { + // Perform SUT + Node root = session.getRootNode(); + Node hello = root.addNode("helloWorld"); + hello.setProperty("message", "Hello, World!"); + session.save(); + } + finally + { + // This is when the observers are fired + session.logout(); + } + + // Check that node was added + assertEquals(1, counter.getCountForType(Event.NODE_ADDED)); + // Properties jcr:primaryType and message added + assertEquals(2, counter.getCountForType(Event.PROPERTY_ADDED)); + } +} diff --git a/impl/src/test/java/org/jboss/seam/jcr/producers/RepositorySessionProducerTest.java b/impl/src/test/java/org/jboss/seam/jcr/producers/RepositorySessionProducerTest.java index 3624757..5148b78 100644 --- a/impl/src/test/java/org/jboss/seam/jcr/producers/RepositorySessionProducerTest.java +++ b/impl/src/test/java/org/jboss/seam/jcr/producers/RepositorySessionProducerTest.java @@ -20,6 +20,7 @@ import javax.inject.Inject; import javax.jcr.Repository; +import javax.jcr.Session; import org.jboss.arquillian.api.Deployment; import org.jboss.arquillian.junit.Arquillian; @@ -48,6 +49,10 @@ public class RepositorySessionProducerTest @JcrRepository(name = "org.apache.jackrabbit.repository.home", value = "target") private Repository repository; + @Inject + @JcrRepository(name = "org.apache.jackrabbit.repository.home", value = "target") + private Session session; + @Deployment public static JavaArchive createArchive() { @@ -60,4 +65,9 @@ public void testProduceJcrRepository() assertNotNull("JCR Repository should have been injected", repository); } + @Test + public void testProduceSession() + { + assertNotNull("JCR Session should have been injected", session); + } }