JAMES-2418 Provide MailRepositoryUrlStore JPA implementation
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/0f7ec3c1 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/0f7ec3c1 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/0f7ec3c1 Branch: refs/heads/master Commit: 0f7ec3c131a211220424645d628f32600f3ed232 Parents: c2dadb2 Author: benwa <[email protected]> Authored: Fri Jun 8 13:28:14 2018 +0700 Committer: benwa <[email protected]> Committed: Thu Jun 14 15:16:59 2018 +0700 ---------------------------------------------------------------------- .../james/backends/jpa/TransactionRunner.java | 22 ++++++- .../james/backends/jpa/JpaTestCluster.java | 1 + .../src/main/resources/META-INF/persistence.xml | 1 + .../src/main/resources/META-INF/persistence.xml | 1 + .../src/main/resources/META-INF/persistence.xml | 1 + .../org/apache/james/JPAJamesServerTest.java | 3 +- server/data/data-jpa/pom.xml | 34 ++++++++-- .../jpa/JPAMailRepositoryUrlStore.java | 67 ++++++++++++++++++++ .../apache/james/mailrepository/jpa/JPAUrl.java | 65 +++++++++++++++++++ .../src/main/resources/META-INF/persistence.xml | 1 + .../jpa/JPAMailRepositoryUrlStoreExtension.java | 47 ++++++++++++++ .../jpa/JPAMailRepositoryUrlStoreTest.java | 28 ++++++++ 12 files changed, 263 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/0f7ec3c1/backends-common/jpa/src/main/java/org/apache/james/backends/jpa/TransactionRunner.java ---------------------------------------------------------------------- diff --git a/backends-common/jpa/src/main/java/org/apache/james/backends/jpa/TransactionRunner.java b/backends-common/jpa/src/main/java/org/apache/james/backends/jpa/TransactionRunner.java index 0b3c914..dae6f2e 100644 --- a/backends-common/jpa/src/main/java/org/apache/james/backends/jpa/TransactionRunner.java +++ b/backends-common/jpa/src/main/java/org/apache/james/backends/jpa/TransactionRunner.java @@ -20,6 +20,7 @@ package org.apache.james.backends.jpa; import java.util.function.Consumer; +import java.util.function.Function; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; @@ -32,6 +33,7 @@ import org.slf4j.LoggerFactory; public class TransactionRunner { private static final Logger LOGGER = LoggerFactory.getLogger(TransactionRunner.class); + public static final Function<PersistenceException, Object> IGNORE_EXCEPTION = e -> null; private final EntityManagerFactory entityManagerFactory; @@ -40,17 +42,35 @@ public class TransactionRunner { } public void run(Consumer<EntityManager> runnable) { + runAndRetrieveResult(entityManager -> { + runnable.accept(entityManager); + return null; + }, + IGNORE_EXCEPTION); + } + + public <T> T runAndRetrieveResult(Function<EntityManager, T> toResult) { + return runAndRetrieveResult(toResult, + e -> { + throw new RuntimeException(e); + }); + } + + public <T> T runAndRetrieveResult(Function<EntityManager, T> toResult, + Function<PersistenceException, T> errorHandler) { EntityManager entityManager = entityManagerFactory.createEntityManager(); EntityTransaction transaction = entityManager.getTransaction(); try { transaction.begin(); - runnable.accept(entityManager); + T result = toResult.apply(entityManager); transaction.commit(); + return result; } catch (PersistenceException e) { LOGGER.warn("Could not execute transaction", e); if (transaction.isActive()) { transaction.rollback(); } + return errorHandler.apply(e); } finally { entityManager.close(); } http://git-wip-us.apache.org/repos/asf/james-project/blob/0f7ec3c1/backends-common/jpa/src/test/java/org/apache/james/backends/jpa/JpaTestCluster.java ---------------------------------------------------------------------- diff --git a/backends-common/jpa/src/test/java/org/apache/james/backends/jpa/JpaTestCluster.java b/backends-common/jpa/src/test/java/org/apache/james/backends/jpa/JpaTestCluster.java index e845801..fc96085 100644 --- a/backends-common/jpa/src/test/java/org/apache/james/backends/jpa/JpaTestCluster.java +++ b/backends-common/jpa/src/test/java/org/apache/james/backends/jpa/JpaTestCluster.java @@ -22,6 +22,7 @@ package org.apache.james.backends.jpa; import java.util.HashMap; import java.util.List; import java.util.stream.Collectors; + import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; http://git-wip-us.apache.org/repos/asf/james-project/blob/0f7ec3c1/server/app/src/main/resources/META-INF/persistence.xml ---------------------------------------------------------------------- diff --git a/server/app/src/main/resources/META-INF/persistence.xml b/server/app/src/main/resources/META-INF/persistence.xml index 4688e39..6f68e20 100644 --- a/server/app/src/main/resources/META-INF/persistence.xml +++ b/server/app/src/main/resources/META-INF/persistence.xml @@ -33,6 +33,7 @@ <class>org.apache.james.mailbox.jpa.mail.model.JPAMailboxAnnotation</class> <class>org.apache.james.mailbox.jpa.user.model.JPASubscription</class> <class>org.apache.james.domainlist.jpa.model.JPADomain</class> + <class>org.apache.james.mailrepository.jpa.JPAUrl</class> <class>org.apache.james.user.jpa.model.JPAUser</class> <class>org.apache.james.rrt.jpa.model.JPARecipientRewrite</class> <class>org.apache.james.mailbox.jpa.quota.model.MaxDomainMessageCount</class> http://git-wip-us.apache.org/repos/asf/james-project/blob/0f7ec3c1/server/container/guice/jpa-guice/src/main/resources/META-INF/persistence.xml ---------------------------------------------------------------------- diff --git a/server/container/guice/jpa-guice/src/main/resources/META-INF/persistence.xml b/server/container/guice/jpa-guice/src/main/resources/META-INF/persistence.xml index 9f00fa9..fd1711d 100644 --- a/server/container/guice/jpa-guice/src/main/resources/META-INF/persistence.xml +++ b/server/container/guice/jpa-guice/src/main/resources/META-INF/persistence.xml @@ -32,6 +32,7 @@ <class>org.apache.james.mailbox.jpa.user.model.JPASubscription</class> <class>org.apache.james.domainlist.jpa.model.JPADomain</class> + <class>org.apache.james.mailrepository.jpa.JPAUrl</class> <class>org.apache.james.user.jpa.model.JPAUser</class> <class>org.apache.james.rrt.jpa.model.JPARecipientRewrite</class> http://git-wip-us.apache.org/repos/asf/james-project/blob/0f7ec3c1/server/container/guice/jpa-smtp/src/main/resources/META-INF/persistence.xml ---------------------------------------------------------------------- diff --git a/server/container/guice/jpa-smtp/src/main/resources/META-INF/persistence.xml b/server/container/guice/jpa-smtp/src/main/resources/META-INF/persistence.xml index d82c703..0656779 100644 --- a/server/container/guice/jpa-smtp/src/main/resources/META-INF/persistence.xml +++ b/server/container/guice/jpa-smtp/src/main/resources/META-INF/persistence.xml @@ -25,6 +25,7 @@ <persistence-unit name="Global" transaction-type="RESOURCE_LOCAL"> <class>org.apache.james.domainlist.jpa.model.JPADomain</class> + <class>org.apache.james.mailrepository.jpa.JPAUrl</class> <class>org.apache.james.user.jpa.model.JPAUser</class> <class>org.apache.james.rrt.jpa.model.JPARecipientRewrite</class> <properties> http://git-wip-us.apache.org/repos/asf/james-project/blob/0f7ec3c1/server/container/guice/jpa-smtp/src/test/java/org/apache/james/JPAJamesServerTest.java ---------------------------------------------------------------------- diff --git a/server/container/guice/jpa-smtp/src/test/java/org/apache/james/JPAJamesServerTest.java b/server/container/guice/jpa-smtp/src/test/java/org/apache/james/JPAJamesServerTest.java index 93a74f1..87962dd 100644 --- a/server/container/guice/jpa-smtp/src/test/java/org/apache/james/JPAJamesServerTest.java +++ b/server/container/guice/jpa-smtp/src/test/java/org/apache/james/JPAJamesServerTest.java @@ -31,6 +31,7 @@ import javax.persistence.EntityManagerFactory; import org.apache.james.backends.jpa.JpaTestCluster; import org.apache.james.domainlist.jpa.model.JPADomain; +import org.apache.james.mailrepository.jpa.JPAUrl; import org.apache.james.rrt.jpa.model.JPARecipientRewrite; import org.apache.james.server.core.configuration.Configuration; import org.apache.james.user.jpa.model.JPAUser; @@ -66,7 +67,7 @@ public class JPAJamesServerTest { .overrideWith( new TestJPAConfigurationModule(), (binder) -> binder.bind(EntityManagerFactory.class) - .toInstance(JpaTestCluster.create(JPAUser.class, JPADomain.class, JPARecipientRewrite.class) + .toInstance(JpaTestCluster.create(JPAUser.class, JPADomain.class, JPARecipientRewrite.class, JPAUrl.class) .getEntityManagerFactory())); } http://git-wip-us.apache.org/repos/asf/james-project/blob/0f7ec3c1/server/data/data-jpa/pom.xml ---------------------------------------------------------------------- diff --git a/server/data/data-jpa/pom.xml b/server/data/data-jpa/pom.xml index eab6334..7bb4925 100644 --- a/server/data/data-jpa/pom.xml +++ b/server/data/data-jpa/pom.xml @@ -49,6 +49,12 @@ </dependency> <dependency> <groupId>${project.groupId}</groupId> + <artifactId>james-server-data-api</artifactId> + <type>test-jar</type> + <scope>test</scope> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> <artifactId>james-server-data-library</artifactId> </dependency> <dependency> @@ -109,13 +115,23 @@ <scope>test</scope> </dependency> <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-engine</artifactId> <scope>test</scope> </dependency> <dependency> - <groupId>org.assertj</groupId> - <artifactId>assertj-core</artifactId> + <groupId>org.junit.platform</groupId> + <artifactId>junit-platform-launcher</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.junit.vintage</groupId> + <artifactId>junit-vintage-engine</artifactId> <scope>test</scope> </dependency> <dependency> @@ -137,7 +153,10 @@ <artifactId>openjpa-maven-plugin</artifactId> <version>2.4.2</version> <configuration> - <includes>org/apache/james/user/jpa/model/JPAUser.class,org/apache/james/rrt/jpa/model/JPARecipientRewrite.class,org/apache/james/domainlist/jpa/model/JPADomain.class</includes> + <includes>org/apache/james/user/jpa/model/JPAUser.class, + org/apache/james/rrt/jpa/model/JPARecipientRewrite.class, + org/apache/james/domainlist/jpa/model/JPADomain.class, + org/apache/james/mailrepository/jpa/JPAUrl.class</includes> <addDefaultConstructor>true</addDefaultConstructor> <enforcePropertyRestrictions>true</enforcePropertyRestrictions> <toolProperties> @@ -147,7 +166,10 @@ </property> <property> <name>metaDataFactory</name> - <value>jpa(Types=org.apache.james.user.jpa.model.JPAUser;org.apache.james.rrt.jpa.model.JPARecipientRewrite;org.apache.james.domainlist.jpa.model.JPADomain)</value> + <value>jpa(Types=org.apache.james.user.jpa.model.JPAUser; + org.apache.james.rrt.jpa.model.JPARecipientRewrite; + org.apache.james.domainlist.jpa.model.JPADomain; + org.apache.james.mailrepository.jpa.JPAUrl)</value> </property> </toolProperties> </configuration> http://git-wip-us.apache.org/repos/asf/james-project/blob/0f7ec3c1/server/data/data-jpa/src/main/java/org/apache/james/mailrepository/jpa/JPAMailRepositoryUrlStore.java ---------------------------------------------------------------------- diff --git a/server/data/data-jpa/src/main/java/org/apache/james/mailrepository/jpa/JPAMailRepositoryUrlStore.java b/server/data/data-jpa/src/main/java/org/apache/james/mailrepository/jpa/JPAMailRepositoryUrlStore.java new file mode 100644 index 0000000..576b774 --- /dev/null +++ b/server/data/data-jpa/src/main/java/org/apache/james/mailrepository/jpa/JPAMailRepositoryUrlStore.java @@ -0,0 +1,67 @@ +/**************************************************************** + * 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.mailrepository.jpa; + +import java.util.Set; + +import javax.inject.Inject; +import javax.persistence.EntityManagerFactory; + +import org.apache.james.backends.jpa.TransactionRunner; +import org.apache.james.mailrepository.api.MailRepositoryUrl; +import org.apache.james.mailrepository.api.MailRepositoryUrlStore; + +import com.github.steveash.guavate.Guavate; + +public class JPAMailRepositoryUrlStore implements MailRepositoryUrlStore { + private final TransactionRunner transactionRunner; + + @Inject + public JPAMailRepositoryUrlStore(EntityManagerFactory entityManagerFactory) { + this.transactionRunner = new TransactionRunner(entityManagerFactory); + } + + @Override + public void add(MailRepositoryUrl url) { + transactionRunner.run(entityManager -> + entityManager.merge(JPAUrl.from(url))); + } + + @Override + public Set<MailRepositoryUrl> list() { + return transactionRunner.runAndRetrieveResult(entityManager -> + entityManager + .createNamedQuery("listUrls", JPAUrl.class) + .getResultList() + .stream() + .map(JPAUrl::toMailRepositoryUrl) + .collect(Guavate.toImmutableSet())); + } + + @Override + public boolean contains(MailRepositoryUrl url) { + return transactionRunner.runAndRetrieveResult(entityManager -> + ! entityManager.createNamedQuery("getUrl", JPAUrl.class) + .setParameter("value", url.asString()) + .getResultList() + .isEmpty()); + } +} + http://git-wip-us.apache.org/repos/asf/james-project/blob/0f7ec3c1/server/data/data-jpa/src/main/java/org/apache/james/mailrepository/jpa/JPAUrl.java ---------------------------------------------------------------------- diff --git a/server/data/data-jpa/src/main/java/org/apache/james/mailrepository/jpa/JPAUrl.java b/server/data/data-jpa/src/main/java/org/apache/james/mailrepository/jpa/JPAUrl.java new file mode 100644 index 0000000..c775ad7 --- /dev/null +++ b/server/data/data-jpa/src/main/java/org/apache/james/mailrepository/jpa/JPAUrl.java @@ -0,0 +1,65 @@ +/**************************************************************** + * 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.mailrepository.jpa; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; +import javax.persistence.Table; + +import org.apache.james.mailrepository.api.MailRepositoryUrl; + +@Entity(name = "JamesMailRepos") +@Table(name = "JAMES_MAIL_REPOS") +@NamedQueries({ + @NamedQuery(name = "listUrls", query = "SELECT url FROM JamesMailRepos url"), + @NamedQuery(name = "getUrl", query = "SELECT url FROM JamesMailRepos url WHERE url.value=:value")}) +public class JPAUrl { + public static JPAUrl from(MailRepositoryUrl url) { + return new JPAUrl(url.asString()); + } + + @Id + @Column(name = "MAIL_REPO_NAME", nullable = false, length = 1024) + private String value; + + /** + * Default no-args constructor for JPA class enhancement. + * The constructor need to be public or protected to be used by JPA. + * See: http://docs.oracle.com/javaee/6/tutorial/doc/bnbqa.html + * Do not us this constructor, it is for JPA only. + */ + protected JPAUrl() { + } + + public JPAUrl(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + + public MailRepositoryUrl toMailRepositoryUrl() { + return MailRepositoryUrl.from(value); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/0f7ec3c1/server/data/data-jpa/src/main/resources/META-INF/persistence.xml ---------------------------------------------------------------------- diff --git a/server/data/data-jpa/src/main/resources/META-INF/persistence.xml b/server/data/data-jpa/src/main/resources/META-INF/persistence.xml index 14a6a4f..0e43be9 100644 --- a/server/data/data-jpa/src/main/resources/META-INF/persistence.xml +++ b/server/data/data-jpa/src/main/resources/META-INF/persistence.xml @@ -29,6 +29,7 @@ <class>org.apache.james.domainlist.jpa.model.JPADomain</class> <class>org.apache.james.user.jpa.model.JPAUser</class> <class>org.apache.james.rrt.jpa.model.JPARecipientRewrite</class> + <class>org.apache.james.mailrepository.jpa.JPAUrl</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/> http://git-wip-us.apache.org/repos/asf/james-project/blob/0f7ec3c1/server/data/data-jpa/src/test/java/org/apache/james/mailrepository/jpa/JPAMailRepositoryUrlStoreExtension.java ---------------------------------------------------------------------- diff --git a/server/data/data-jpa/src/test/java/org/apache/james/mailrepository/jpa/JPAMailRepositoryUrlStoreExtension.java b/server/data/data-jpa/src/test/java/org/apache/james/mailrepository/jpa/JPAMailRepositoryUrlStoreExtension.java new file mode 100644 index 0000000..402a5de --- /dev/null +++ b/server/data/data-jpa/src/test/java/org/apache/james/mailrepository/jpa/JPAMailRepositoryUrlStoreExtension.java @@ -0,0 +1,47 @@ +/**************************************************************** + * 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.mailrepository.jpa; + +import org.apache.james.backends.jpa.JpaTestCluster; +import org.apache.james.mailrepository.api.MailRepositoryUrlStore; +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ParameterContext; +import org.junit.jupiter.api.extension.ParameterResolutionException; +import org.junit.jupiter.api.extension.ParameterResolver; + +public class JPAMailRepositoryUrlStoreExtension implements ParameterResolver, AfterEachCallback{ + private static final JpaTestCluster JPA_TEST_CLUSTER = JpaTestCluster.create(JPAUrl.class); + + @Override + public void afterEach(ExtensionContext context) { + JPA_TEST_CLUSTER.clear("JAMES_MAIL_REPOS"); + } + + @Override + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { + return (parameterContext.getParameter().getType() == MailRepositoryUrlStore.class); + } + + @Override + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { + return new JPAMailRepositoryUrlStore(JPA_TEST_CLUSTER.getEntityManagerFactory()); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/0f7ec3c1/server/data/data-jpa/src/test/java/org/apache/james/mailrepository/jpa/JPAMailRepositoryUrlStoreTest.java ---------------------------------------------------------------------- diff --git a/server/data/data-jpa/src/test/java/org/apache/james/mailrepository/jpa/JPAMailRepositoryUrlStoreTest.java b/server/data/data-jpa/src/test/java/org/apache/james/mailrepository/jpa/JPAMailRepositoryUrlStoreTest.java new file mode 100644 index 0000000..5a58174 --- /dev/null +++ b/server/data/data-jpa/src/test/java/org/apache/james/mailrepository/jpa/JPAMailRepositoryUrlStoreTest.java @@ -0,0 +1,28 @@ +/**************************************************************** + * 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.mailrepository.jpa; + +import org.apache.james.mailrepository.api.MailRepositoryUrlStoreContract; +import org.junit.jupiter.api.extension.ExtendWith; + +@ExtendWith(JPAMailRepositoryUrlStoreExtension.class) +public class JPAMailRepositoryUrlStoreTest implements MailRepositoryUrlStoreContract { + +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
