You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2018/12/18 02:11:20 UTC

[2/5] james-project git commit: MAILBOX-364 API for EventBus

MAILBOX-364 API for EventBus

The EventBus solves multiple concerns:
 - Making the subscriber responsible of their registration - and unregistration
 - Allow to register on multiple entities (MailboxId, User, etc...)


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/421bd1f4
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/421bd1f4
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/421bd1f4

Branch: refs/heads/master
Commit: 421bd1f451342fb4283bf95faad85d6a7c8067c0
Parents: eeafbf4
Author: Benoit Tellier <bt...@linagora.com>
Authored: Wed Dec 12 09:50:41 2018 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Tue Dec 18 09:09:57 2018 +0700

----------------------------------------------------------------------
 mailbox/api/pom.xml                             |  4 ++
 .../apache/james/mailbox/events/EventBus.java   | 41 ++++++++++++++
 .../org/apache/james/mailbox/events/Group.java  | 37 ++++++++++++
 .../mailbox/events/GroupAlreadyRegistered.java  | 29 ++++++++++
 .../events/MailboxIdRegistrationKey.java        | 51 +++++++++++++++++
 .../james/mailbox/events/Registration.java      | 24 ++++++++
 .../james/mailbox/events/RegistrationKey.java   | 23 ++++++++
 .../apache/james/mailbox/events/GroupTest.java  | 59 ++++++++++++++++++++
 .../events/MailboxIdRegistrationKeyTest.java    | 32 +++++++++++
 9 files changed, 300 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/421bd1f4/mailbox/api/pom.xml
----------------------------------------------------------------------
diff --git a/mailbox/api/pom.xml b/mailbox/api/pom.xml
index 4986f47..024675d 100644
--- a/mailbox/api/pom.xml
+++ b/mailbox/api/pom.xml
@@ -79,6 +79,10 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>io.projectreactor</groupId>
+            <artifactId>reactor-core</artifactId>
+        </dependency>
+        <dependency>
             <groupId>nl.jqno.equalsverifier</groupId>
             <artifactId>equalsverifier</artifactId>
             <scope>test</scope>

http://git-wip-us.apache.org/repos/asf/james-project/blob/421bd1f4/mailbox/api/src/main/java/org/apache/james/mailbox/events/EventBus.java
----------------------------------------------------------------------
diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/events/EventBus.java b/mailbox/api/src/main/java/org/apache/james/mailbox/events/EventBus.java
new file mode 100644
index 0000000..02ea68f
--- /dev/null
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/events/EventBus.java
@@ -0,0 +1,41 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.apache.james.mailbox.events;
+
+import java.util.Set;
+
+import org.apache.james.mailbox.Event;
+import org.apache.james.mailbox.MailboxListener;
+
+import com.google.common.collect.ImmutableSet;
+
+import reactor.core.publisher.Mono;
+
+public interface EventBus {
+    Registration register(MailboxListener listener, RegistrationKey key);
+
+    Registration register(MailboxListener listener, Group group) throws GroupAlreadyRegistered;
+
+    Mono<Void> dispatch(Event event, Set<RegistrationKey> key);
+
+    default Mono<Void> dispatch(Event event, RegistrationKey key) {
+        return dispatch(event, ImmutableSet.of(key));
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/421bd1f4/mailbox/api/src/main/java/org/apache/james/mailbox/events/Group.java
----------------------------------------------------------------------
diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/events/Group.java b/mailbox/api/src/main/java/org/apache/james/mailbox/events/Group.java
new file mode 100644
index 0000000..33df511
--- /dev/null
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/events/Group.java
@@ -0,0 +1,37 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.apache.james.mailbox.events;
+
+import java.util.Objects;
+
+public class Group {
+    @Override
+    public final boolean equals(Object o) {
+        if (o == null) {
+            return false;
+        }
+        return this.getClass().equals(o.getClass());
+    }
+
+    @Override
+    public final int hashCode() {
+        return Objects.hash(getClass());
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/421bd1f4/mailbox/api/src/main/java/org/apache/james/mailbox/events/GroupAlreadyRegistered.java
----------------------------------------------------------------------
diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/events/GroupAlreadyRegistered.java b/mailbox/api/src/main/java/org/apache/james/mailbox/events/GroupAlreadyRegistered.java
new file mode 100644
index 0000000..710de75
--- /dev/null
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/events/GroupAlreadyRegistered.java
@@ -0,0 +1,29 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.apache.james.mailbox.events;
+
+public class GroupAlreadyRegistered extends RuntimeException {
+    private final Group group;
+
+    public GroupAlreadyRegistered(Group group) {
+        super(group.getClass().getCanonicalName() + " is already Registered and thus can not be used.");
+        this.group = group;
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/421bd1f4/mailbox/api/src/main/java/org/apache/james/mailbox/events/MailboxIdRegistrationKey.java
----------------------------------------------------------------------
diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/events/MailboxIdRegistrationKey.java b/mailbox/api/src/main/java/org/apache/james/mailbox/events/MailboxIdRegistrationKey.java
new file mode 100644
index 0000000..cc438a9
--- /dev/null
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/events/MailboxIdRegistrationKey.java
@@ -0,0 +1,51 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.apache.james.mailbox.events;
+
+import java.util.Objects;
+
+import org.apache.james.mailbox.model.MailboxId;
+
+public class MailboxIdRegistrationKey implements RegistrationKey {
+    private final MailboxId mailboxId;
+
+    public MailboxIdRegistrationKey(MailboxId mailboxId) {
+        this.mailboxId = mailboxId;
+    }
+
+    public MailboxId getMailboxId() {
+        return mailboxId;
+    }
+
+    @Override
+    public final boolean equals(Object o) {
+        if (o instanceof MailboxIdRegistrationKey) {
+            MailboxIdRegistrationKey that = (MailboxIdRegistrationKey) o;
+
+            return Objects.equals(this.mailboxId, that.mailboxId);
+        }
+        return false;
+    }
+
+    @Override
+    public final int hashCode() {
+        return Objects.hash(mailboxId);
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/421bd1f4/mailbox/api/src/main/java/org/apache/james/mailbox/events/Registration.java
----------------------------------------------------------------------
diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/events/Registration.java b/mailbox/api/src/main/java/org/apache/james/mailbox/events/Registration.java
new file mode 100644
index 0000000..c5a3a53
--- /dev/null
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/events/Registration.java
@@ -0,0 +1,24 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.apache.james.mailbox.events;
+
+public interface Registration {
+    void unregister();
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/421bd1f4/mailbox/api/src/main/java/org/apache/james/mailbox/events/RegistrationKey.java
----------------------------------------------------------------------
diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/events/RegistrationKey.java b/mailbox/api/src/main/java/org/apache/james/mailbox/events/RegistrationKey.java
new file mode 100644
index 0000000..82b6e31
--- /dev/null
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/events/RegistrationKey.java
@@ -0,0 +1,23 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.apache.james.mailbox.events;
+
+public interface RegistrationKey {
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/421bd1f4/mailbox/api/src/test/java/org/apache/james/mailbox/events/GroupTest.java
----------------------------------------------------------------------
diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/events/GroupTest.java b/mailbox/api/src/test/java/org/apache/james/mailbox/events/GroupTest.java
new file mode 100644
index 0000000..f954027
--- /dev/null
+++ b/mailbox/api/src/test/java/org/apache/james/mailbox/events/GroupTest.java
@@ -0,0 +1,59 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.apache.james.mailbox.events;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.jupiter.api.Test;
+
+import nl.jqno.equalsverifier.EqualsVerifier;
+
+class GroupTest {
+    static class GroupA extends Group {}
+    static class GroupB extends Group {}
+    static class GroupC extends GroupA {}
+
+    @Test
+    void shouldMatchBeanContract() {
+        EqualsVerifier.forClass(Group.class)
+            .usingGetClass()
+            .verify();
+    }
+
+    @Test
+    void equalsShouldReturnTrueOnSameClass() {
+        assertThat(new GroupA()).isEqualTo(new GroupA());
+    }
+
+    @Test
+    void equalsShouldReturnFalseOnDifferentClass() {
+        assertThat(new GroupA()).isNotEqualTo(new GroupB());
+    }
+
+    @Test
+    void equalsShouldReturnFalseOnSubClass() {
+        assertThat(new GroupA()).isNotEqualTo(new GroupC());
+    }
+
+    @Test
+    void equalsShouldReturnFalseOnParentClass() {
+        assertThat(new GroupC()).isNotEqualTo(new GroupA());
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/421bd1f4/mailbox/api/src/test/java/org/apache/james/mailbox/events/MailboxIdRegistrationKeyTest.java
----------------------------------------------------------------------
diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/events/MailboxIdRegistrationKeyTest.java b/mailbox/api/src/test/java/org/apache/james/mailbox/events/MailboxIdRegistrationKeyTest.java
new file mode 100644
index 0000000..f1e28e1
--- /dev/null
+++ b/mailbox/api/src/test/java/org/apache/james/mailbox/events/MailboxIdRegistrationKeyTest.java
@@ -0,0 +1,32 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.apache.james.mailbox.events;
+
+import org.junit.jupiter.api.Test;
+
+import nl.jqno.equalsverifier.EqualsVerifier;
+
+class MailboxIdRegistrationKeyTest {
+    @Test
+    void shouldRespectBeanContract() {
+        EqualsVerifier.forClass(MailboxIdRegistrationKey.class)
+            .verify();
+    }
+}
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org