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 2019/03/04 11:43:48 UTC
[james-project] 14/16: JAMES-2664 Create a PreDeletionHook loader
This is an automated email from the ASF dual-hosted git repository.
btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 9198c349916895d691cfbb5130ffa5ccf8729097
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Mon Mar 4 11:32:46 2019 +0700
JAMES-2664 Create a PreDeletionHook loader
---
.../modules/mailbox/PreDeletionHookLoader.java | 25 +++++++++
.../modules/mailbox/PreDeletionHookLoaderImpl.java | 48 +++++++++++++++++
.../james/modules/mailbox/NoopPreDeletionHook.java | 32 ++++++++++++
.../mailbox/PreDeletionHookLoaderImplTest.java | 60 ++++++++++++++++++++++
4 files changed, 165 insertions(+)
diff --git a/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/PreDeletionHookLoader.java b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/PreDeletionHookLoader.java
new file mode 100644
index 0000000..5d0a003
--- /dev/null
+++ b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/PreDeletionHookLoader.java
@@ -0,0 +1,25 @@
+/****************************************************************
+ * 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.modules.mailbox;
+
+import org.apache.james.mailbox.extension.PreDeletionHook;
+
+public interface PreDeletionHookLoader {
+ PreDeletionHook createHook(PreDeletionHookConfiguration configuration) throws ClassNotFoundException;
+}
diff --git a/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/PreDeletionHookLoaderImpl.java b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/PreDeletionHookLoaderImpl.java
new file mode 100644
index 0000000..87b293d
--- /dev/null
+++ b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/PreDeletionHookLoaderImpl.java
@@ -0,0 +1,48 @@
+/****************************************************************
+ * 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.modules.mailbox;
+
+import org.apache.james.mailbox.extension.PreDeletionHook;
+import org.apache.james.utils.ExtendedClassLoader;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+
+public class PreDeletionHookLoaderImpl implements PreDeletionHookLoader {
+ private static final Logger LOGGER = LoggerFactory.getLogger(PreDeletionHookLoaderImpl.class);
+
+ private final Injector injector;
+ private final ExtendedClassLoader classLoader;
+
+ @Inject
+ PreDeletionHookLoaderImpl(Injector injector, ExtendedClassLoader classLoader) {
+ this.injector = injector;
+ this.classLoader = classLoader;
+ }
+
+ @Override
+ public PreDeletionHook createHook(PreDeletionHookConfiguration configuration) throws ClassNotFoundException {
+ String hookClass = configuration.getClazz();
+ LOGGER.info("Loading user registered mailbox listener {}", hookClass);
+ Class<PreDeletionHook> clazz = classLoader.locateClass(hookClass);
+ return injector.getInstance(clazz);
+ }
+}
diff --git a/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/NoopPreDeletionHook.java b/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/NoopPreDeletionHook.java
new file mode 100644
index 0000000..68c0db6
--- /dev/null
+++ b/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/NoopPreDeletionHook.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.modules.mailbox;
+
+import org.apache.james.mailbox.extension.PreDeletionHook;
+import org.reactivestreams.Publisher;
+
+import reactor.core.publisher.Mono;
+
+public class NoopPreDeletionHook implements PreDeletionHook {
+ @Override
+ public Publisher<Void> notifyDelete(DeleteOperation deleteOperation) {
+ return Mono.empty();
+ }
+}
diff --git a/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/PreDeletionHookLoaderImplTest.java b/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/PreDeletionHookLoaderImplTest.java
new file mode 100644
index 0000000..6c398f5
--- /dev/null
+++ b/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/PreDeletionHookLoaderImplTest.java
@@ -0,0 +1,60 @@
+/****************************************************************
+ * 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.modules.mailbox;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.FileNotFoundException;
+
+import org.apache.james.filesystem.api.FileSystem;
+import org.apache.james.utils.ExtendedClassLoader;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import com.google.inject.Guice;
+
+class PreDeletionHookLoaderImplTest {
+ private PreDeletionHookLoaderImpl testee;
+
+ @BeforeEach
+ void setUp() throws Exception {
+ FileSystem fileSystem = mock(FileSystem.class);
+ when(fileSystem.getFile(anyString()))
+ .thenThrow(new FileNotFoundException());
+
+ testee = new PreDeletionHookLoaderImpl(Guice.createInjector(), new ExtendedClassLoader(fileSystem));
+ }
+
+ @Test
+ void createHookShouldThrowWhenClassNotFound() {
+ assertThatThrownBy(() -> testee.createHook(PreDeletionHookConfiguration.forClass("invalid")))
+ .isInstanceOf(ClassNotFoundException.class);
+ }
+
+ @Test
+ void createHookShouldReturnAHookOfCreatedClass() throws Exception {
+ assertThat(testee.createHook(PreDeletionHookConfiguration.forClass(NoopPreDeletionHook.class.getName())))
+ .isInstanceOf(NoopPreDeletionHook.class);
+ }
+}
\ 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