Repository: james-project Updated Branches: refs/heads/master c94d35e22 -> b99d96654
JAMES-2416 Share EntityManagerFactory provider between guice products Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/b99d9665 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/b99d9665 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/b99d9665 Branch: refs/heads/master Commit: b99d96654dc085c953cac401c2f0f92f4de46acd Parents: e8e9803 Author: Antoine Duprat <[email protected]> Authored: Thu Jun 7 18:33:50 2018 +0200 Committer: Matthieu Baechler <[email protected]> Committed: Mon Jun 18 11:40:26 2018 +0200 ---------------------------------------------------------------------- .../james/modules/data/JPAConfiguration.java | 107 +++++++++++++++++++ .../modules/data/JPAEntityManagerModule.java | 78 ++++++++++++++ .../java/org/apache/james/JPAConfiguration.java | 107 ------------------- .../james/modules/mailbox/JPAMailboxModule.java | 49 +-------- .../james/TestJPAConfigurationModule.java | 1 + ...JPAConfigurationModuleWithSqlValidation.java | 1 + .../org/apache/james/JPAJamesServerMain.java | 6 +- 7 files changed, 191 insertions(+), 158 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/b99d9665/server/container/guice/jpa-common-guice/src/main/java/org/apache/james/modules/data/JPAConfiguration.java ---------------------------------------------------------------------- diff --git a/server/container/guice/jpa-common-guice/src/main/java/org/apache/james/modules/data/JPAConfiguration.java b/server/container/guice/jpa-common-guice/src/main/java/org/apache/james/modules/data/JPAConfiguration.java new file mode 100644 index 0000000..89e07fd --- /dev/null +++ b/server/container/guice/jpa-common-guice/src/main/java/org/apache/james/modules/data/JPAConfiguration.java @@ -0,0 +1,107 @@ +/**************************************************************** + * 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.data; + +import org.apache.james.backends.jpa.JPAConstants; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; + +public class JPAConfiguration { + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private String driverName; + private String driverURL; + private boolean testOnBorrow; + private int validationQueryTimeoutSec = JPAConstants.VALIDATION_NO_TIMEOUT; + private String validationQuery; + + + public Builder driverName(String driverName) { + this.driverName = driverName; + return this; + } + + public Builder driverURL(String driverURL) { + this.driverURL = driverURL; + return this; + } + + public Builder testOnBorrow(boolean testOnBorrow) { + this.testOnBorrow = testOnBorrow; + return this; + } + + public Builder validationQueryTimeoutSec(int validationQueryTimeoutSec) { + this.validationQueryTimeoutSec = validationQueryTimeoutSec; + return this; + } + + public Builder validationQuery(String validationQuery) { + this.validationQuery = validationQuery; + return this; + } + + public JPAConfiguration build() { + Preconditions.checkNotNull(driverName); + Preconditions.checkNotNull(driverURL); + return new JPAConfiguration(driverName, driverURL, testOnBorrow, validationQueryTimeoutSec, validationQuery); + } + } + + private final String driverName; + private final String driverURL; + private final boolean testOnBorrow; + private final int validationQueryTimeoutSec; + private final String validationQuery; + + @VisibleForTesting + JPAConfiguration(String driverName, String driverURL, boolean testOnBorrow, int validationQueryTimeoutSec, String validationQuery) { + this.driverName = driverName; + this.driverURL = driverURL; + this.testOnBorrow = testOnBorrow; + this.validationQueryTimeoutSec = validationQueryTimeoutSec; + this.validationQuery = validationQuery; + } + + public String getDriverName() { + return driverName; + } + + public String getDriverURL() { + return driverURL; + } + + public boolean isTestOnBorrow() { + return testOnBorrow; + } + + public int getValidationQueryTimeoutSec() { + return validationQueryTimeoutSec; + } + + public String getValidationQuery() { + return validationQuery; + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/b99d9665/server/container/guice/jpa-common-guice/src/main/java/org/apache/james/modules/data/JPAEntityManagerModule.java ---------------------------------------------------------------------- diff --git a/server/container/guice/jpa-common-guice/src/main/java/org/apache/james/modules/data/JPAEntityManagerModule.java b/server/container/guice/jpa-common-guice/src/main/java/org/apache/james/modules/data/JPAEntityManagerModule.java new file mode 100644 index 0000000..76f518f --- /dev/null +++ b/server/container/guice/jpa-common-guice/src/main/java/org/apache/james/modules/data/JPAEntityManagerModule.java @@ -0,0 +1,78 @@ +/**************************************************************** + * 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.data; + +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import javax.inject.Singleton; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.PropertiesConfiguration; +import org.apache.james.backends.jpa.JPAConstants; +import org.apache.james.utils.PropertiesProvider; + +import com.google.common.base.Joiner; +import com.google.inject.AbstractModule; +import com.google.inject.Provides; + +public class JPAEntityManagerModule extends AbstractModule { + + @Override + protected void configure() { + } + + @Provides + @Singleton + public EntityManagerFactory provideEntityManagerFactory(JPAConfiguration jpaConfiguration) { + HashMap<String, String> properties = new HashMap<>(); + + properties.put("openjpa.ConnectionDriverName", jpaConfiguration.getDriverName()); + properties.put("openjpa.ConnectionURL", jpaConfiguration.getDriverURL()); + + List<String> connectionFactoryProperties = new ArrayList<>(); + connectionFactoryProperties.add("TestOnBorrow=" + jpaConfiguration.isTestOnBorrow()); + if (jpaConfiguration.getValidationQueryTimeoutSec() > 0) { + connectionFactoryProperties.add("ValidationTimeout=" + jpaConfiguration.getValidationQueryTimeoutSec() * 1000); + } + if (jpaConfiguration.getValidationQuery() != null) { + connectionFactoryProperties.add("ValidationSQL='" + jpaConfiguration.getValidationQuery() + "'"); + } + properties.put("openjpa.ConnectionFactoryProperties", Joiner.on(", ").join(connectionFactoryProperties)); + + return Persistence.createEntityManagerFactory("Global", properties); + } + + @Provides + @Singleton + JPAConfiguration provideConfiguration(PropertiesProvider propertiesProvider) throws FileNotFoundException, ConfigurationException { + PropertiesConfiguration dataSource = propertiesProvider.getConfiguration("james-database"); + return JPAConfiguration.builder() + .driverName(dataSource.getString("database.driverClassName")) + .driverURL(dataSource.getString("database.url")) + .testOnBorrow(dataSource.getBoolean("datasource.testOnBorrow", false)) + .validationQueryTimeoutSec(dataSource.getInt("datasource.validationQueryTimeoutSec", JPAConstants.VALIDATION_NO_TIMEOUT)) + .validationQuery(dataSource.getString("datasource.validationQuery", null)) + .build(); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/b99d9665/server/container/guice/jpa-guice/src/main/java/org/apache/james/JPAConfiguration.java ---------------------------------------------------------------------- diff --git a/server/container/guice/jpa-guice/src/main/java/org/apache/james/JPAConfiguration.java b/server/container/guice/jpa-guice/src/main/java/org/apache/james/JPAConfiguration.java deleted file mode 100644 index 62e96c3..0000000 --- a/server/container/guice/jpa-guice/src/main/java/org/apache/james/JPAConfiguration.java +++ /dev/null @@ -1,107 +0,0 @@ -/**************************************************************** - * 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; - -import org.apache.james.backends.jpa.JPAConstants; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Preconditions; - -public class JPAConfiguration { - - public static Builder builder() { - return new Builder(); - } - - public static class Builder { - private String driverName; - private String driverURL; - private boolean testOnBorrow; - private int validationQueryTimeoutSec = JPAConstants.VALIDATION_NO_TIMEOUT; - private String validationQuery; - - - public Builder driverName(String driverName) { - this.driverName = driverName; - return this; - } - - public Builder driverURL(String driverURL) { - this.driverURL = driverURL; - return this; - } - - public Builder testOnBorrow(boolean testOnBorrow) { - this.testOnBorrow = testOnBorrow; - return this; - } - - public Builder validationQueryTimeoutSec(int validationQueryTimeoutSec) { - this.validationQueryTimeoutSec = validationQueryTimeoutSec; - return this; - } - - public Builder validationQuery(String validationQuery) { - this.validationQuery = validationQuery; - return this; - } - - public JPAConfiguration build() { - Preconditions.checkNotNull(driverName); - Preconditions.checkNotNull(driverURL); - return new JPAConfiguration(driverName, driverURL, testOnBorrow, validationQueryTimeoutSec, validationQuery); - } - } - - private final String driverName; - private final String driverURL; - private final boolean testOnBorrow; - private final int validationQueryTimeoutSec; - private final String validationQuery; - - @VisibleForTesting - JPAConfiguration(String driverName, String driverURL, boolean testOnBorrow, int validationQueryTimeoutSec, String validationQuery) { - this.driverName = driverName; - this.driverURL = driverURL; - this.testOnBorrow = testOnBorrow; - this.validationQueryTimeoutSec = validationQueryTimeoutSec; - this.validationQuery = validationQuery; - } - - public String getDriverName() { - return driverName; - } - - public String getDriverURL() { - return driverURL; - } - - public boolean isTestOnBorrow() { - return testOnBorrow; - } - - public int getValidationQueryTimeoutSec() { - return validationQueryTimeoutSec; - } - - public String getValidationQuery() { - return validationQuery; - } - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/b99d9665/server/container/guice/jpa-guice/src/main/java/org/apache/james/modules/mailbox/JPAMailboxModule.java ---------------------------------------------------------------------- diff --git a/server/container/guice/jpa-guice/src/main/java/org/apache/james/modules/mailbox/JPAMailboxModule.java b/server/container/guice/jpa-guice/src/main/java/org/apache/james/modules/mailbox/JPAMailboxModule.java index fb8caba..91da90e 100644 --- a/server/container/guice/jpa-guice/src/main/java/org/apache/james/modules/mailbox/JPAMailboxModule.java +++ b/server/container/guice/jpa-guice/src/main/java/org/apache/james/modules/mailbox/JPAMailboxModule.java @@ -18,21 +18,10 @@ ****************************************************************/ package org.apache.james.modules.mailbox; -import java.io.FileNotFoundException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; - import javax.inject.Singleton; -import javax.persistence.EntityManagerFactory; -import javax.persistence.Persistence; -import org.apache.commons.configuration.ConfigurationException; -import org.apache.commons.configuration.PropertiesConfiguration; -import org.apache.james.JPAConfiguration; import org.apache.james.adapter.mailbox.store.UserRepositoryAuthenticator; import org.apache.james.adapter.mailbox.store.UserRepositoryAuthorizator; -import org.apache.james.backends.jpa.JPAConstants; import org.apache.james.mailbox.MailboxManager; import org.apache.james.mailbox.MailboxPathLocker; import org.apache.james.mailbox.SubscriptionManager; @@ -62,10 +51,9 @@ import org.apache.james.mailbox.store.mail.UidProvider; import org.apache.james.mailbox.store.mail.model.DefaultMessageId; import org.apache.james.mailbox.store.quota.ListeningCurrentQuotaUpdater; import org.apache.james.modules.Names; +import org.apache.james.modules.data.JPAEntityManagerModule; import org.apache.james.utils.MailboxManagerDefinition; -import org.apache.james.utils.PropertiesProvider; -import com.google.common.base.Joiner; import com.google.inject.AbstractModule; import com.google.inject.Inject; import com.google.inject.Provides; @@ -79,6 +67,7 @@ public class JPAMailboxModule extends AbstractModule { protected void configure() { install(new JpaQuotaModule()); install(new JPAQuotaSearchModule()); + install(new JPAEntityManagerModule()); bind(JPAMailboxSessionMapperFactory.class).in(Scopes.SINGLETON); bind(OpenJPAMailboxManager.class).in(Scopes.SINGLETON); @@ -131,38 +120,4 @@ public class JPAMailboxModule extends AbstractModule { super("jpa-mailboxmanager", manager); } } - - @Provides - @Singleton - public EntityManagerFactory provideEntityManagerFactory(JPAConfiguration jpaConfiguration) { - HashMap<String, String> properties = new HashMap<>(); - - properties.put("openjpa.ConnectionDriverName", jpaConfiguration.getDriverName()); - properties.put("openjpa.ConnectionURL", jpaConfiguration.getDriverURL()); - - List<String> connectionFactoryProperties = new ArrayList<>(); - connectionFactoryProperties.add("TestOnBorrow=" + jpaConfiguration.isTestOnBorrow()); - if (jpaConfiguration.getValidationQueryTimeoutSec() > 0) { - connectionFactoryProperties.add("ValidationTimeout=" + jpaConfiguration.getValidationQueryTimeoutSec() * 1000); - } - if (jpaConfiguration.getValidationQuery() != null) { - connectionFactoryProperties.add("ValidationSQL='" + jpaConfiguration.getValidationQuery() + "'"); - } - properties.put("openjpa.ConnectionFactoryProperties", Joiner.on(", ").join(connectionFactoryProperties)); - - return Persistence.createEntityManagerFactory("Global", properties); - } - - @Provides - @Singleton - JPAConfiguration provideConfiguration(PropertiesProvider propertiesProvider) throws FileNotFoundException, ConfigurationException { - PropertiesConfiguration dataSource = propertiesProvider.getConfiguration("james-database"); - return JPAConfiguration.builder() - .driverName(dataSource.getString("database.driverClassName")) - .driverURL(dataSource.getString("database.url")) - .testOnBorrow(dataSource.getBoolean("datasource.testOnBorrow", false)) - .validationQueryTimeoutSec(dataSource.getInt("datasource.validationQueryTimeoutSec", JPAConstants.VALIDATION_NO_TIMEOUT)) - .validationQuery(dataSource.getString("datasource.validationQuery", null)) - .build(); - } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/b99d9665/server/container/guice/jpa-guice/src/test/java/org/apache/james/TestJPAConfigurationModule.java ---------------------------------------------------------------------- diff --git a/server/container/guice/jpa-guice/src/test/java/org/apache/james/TestJPAConfigurationModule.java b/server/container/guice/jpa-guice/src/test/java/org/apache/james/TestJPAConfigurationModule.java index 3fa53f6..e6694ab 100644 --- a/server/container/guice/jpa-guice/src/test/java/org/apache/james/TestJPAConfigurationModule.java +++ b/server/container/guice/jpa-guice/src/test/java/org/apache/james/TestJPAConfigurationModule.java @@ -24,6 +24,7 @@ import java.io.FileNotFoundException; import javax.inject.Singleton; import org.apache.commons.configuration.ConfigurationException; +import org.apache.james.modules.data.JPAConfiguration; import com.google.inject.AbstractModule; import com.google.inject.Provides; http://git-wip-us.apache.org/repos/asf/james-project/blob/b99d9665/server/container/guice/jpa-guice/src/test/java/org/apache/james/TestJPAConfigurationModuleWithSqlValidation.java ---------------------------------------------------------------------- diff --git a/server/container/guice/jpa-guice/src/test/java/org/apache/james/TestJPAConfigurationModuleWithSqlValidation.java b/server/container/guice/jpa-guice/src/test/java/org/apache/james/TestJPAConfigurationModuleWithSqlValidation.java index 04e7c9d..b562c4a 100644 --- a/server/container/guice/jpa-guice/src/test/java/org/apache/james/TestJPAConfigurationModuleWithSqlValidation.java +++ b/server/container/guice/jpa-guice/src/test/java/org/apache/james/TestJPAConfigurationModuleWithSqlValidation.java @@ -24,6 +24,7 @@ import java.io.FileNotFoundException; import javax.inject.Singleton; import org.apache.commons.configuration.ConfigurationException; +import org.apache.james.modules.data.JPAConfiguration; import com.google.inject.AbstractModule; import com.google.inject.Provides; http://git-wip-us.apache.org/repos/asf/james-project/blob/b99d9665/server/container/guice/jpa-smtp-common/src/main/java/org/apache/james/JPAJamesServerMain.java ---------------------------------------------------------------------- diff --git a/server/container/guice/jpa-smtp-common/src/main/java/org/apache/james/JPAJamesServerMain.java b/server/container/guice/jpa-smtp-common/src/main/java/org/apache/james/JPAJamesServerMain.java index 7a329f7..7defa50 100644 --- a/server/container/guice/jpa-smtp-common/src/main/java/org/apache/james/JPAJamesServerMain.java +++ b/server/container/guice/jpa-smtp-common/src/main/java/org/apache/james/JPAJamesServerMain.java @@ -19,9 +19,8 @@ package org.apache.james; -import javax.persistence.EntityManagerFactory; - import org.apache.james.modules.data.JPADataModule; +import org.apache.james.modules.data.JPAEntityManagerModule; import org.apache.james.modules.protocols.ProtocolHandlerModule; import org.apache.james.modules.protocols.SMTPServerModule; import org.apache.james.modules.server.ActiveMQQueueModule; @@ -34,7 +33,6 @@ import org.apache.james.modules.server.NoJwtModule; import org.apache.james.modules.server.RawPostDequeueDecoratorModule; import org.apache.james.modules.server.WebAdminServerModule; import org.apache.james.server.core.configuration.Configuration; -import org.apache.openjpa.persistence.OpenJPAPersistence; import com.google.inject.Module; import com.google.inject.util.Modules; @@ -52,8 +50,8 @@ public class JPAJamesServerMain { new DefaultProcessorsConfigurationProviderModule()); public static final Module JPA_SERVER_MODULE = Modules.combine( + new JPAEntityManagerModule(), new JPADataModule(), - (binder) -> binder.bind(EntityManagerFactory.class).toProvider(OpenJPAPersistence::getEntityManagerFactory), new ActiveMQQueueModule(), new RawPostDequeueDecoratorModule(), new ElasticSearchMetricReporterModule()); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
