Repository: james-project Updated Branches: refs/heads/master 84fb8f6ca -> 6a8b56bf0
MAILBOX-291 Adding a MaxQuotaManager JPA implementation Contributed by Lai and Mai from PNV Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/8fabac55 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/8fabac55 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/8fabac55 Branch: refs/heads/master Commit: 8fabac55ae0df6a2781c0e2dc90d81872716b592 Parents: 84fb8f6 Author: benwa <btell...@linagora.com> Authored: Wed Mar 29 14:09:19 2017 +0700 Committer: benwa <btell...@linagora.com> Committed: Fri Mar 31 08:45:49 2017 +0700 ---------------------------------------------------------------------- mailbox/jpa/pom.xml | 13 ++- .../jpa/quota/JPAPerUserMaxQuotaManager.java | 112 +++++++++++++++++++ .../jpa/quota/model/MaxDefaultMessageCount.java | 50 +++++++++ .../jpa/quota/model/MaxDefaultStorage.java | 50 +++++++++ .../jpa/quota/model/MaxUserMessageCount.java | 48 ++++++++ .../mailbox/jpa/quota/model/MaxUserStorage.java | 49 ++++++++ .../src/main/resources/META-INF/persistence.xml | 4 + .../james/mailbox/jpa/JPAMailboxFixture.java | 18 +++ .../jpa/quota/JPAPerUserMaxQuotaTest.java | 41 +++++++ 9 files changed, 384 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/8fabac55/mailbox/jpa/pom.xml ---------------------------------------------------------------------- diff --git a/mailbox/jpa/pom.xml b/mailbox/jpa/pom.xml index 5be3d75..cc085c7 100644 --- a/mailbox/jpa/pom.xml +++ b/mailbox/jpa/pom.xml @@ -131,7 +131,18 @@ <property> <name>metaDataFactory</name> <value> - jpa(Types=org.apache.james.mailbox.jpa.mail.model.JPAUserFlag;org.apache.james.mailbox.jpa.mail.model.JPAMailbox;org.apache.james.mailbox.jpa.mail.model.openjpa.AbstractJPAMessage;org.apache.james.mailbox.jpa.mail.model.openjpa.JPAEncryptedMessage;org.apache.james.mailbox.jpa.mail.model.openjpa.JPAMessage;org.apache.james.mailbox.jpa.mail.model.openjpa.JPAStreamingMessage;org.apache.james.mailbox.jpa.mail.model.JPAProperty;org.apache.james.mailbox.jpa.user.model.JPASubscription) + jpa(Types=org.apache.james.mailbox.jpa.mail.model.JPAUserFlag; + org.apache.james.mailbox.jpa.mail.model.JPAMailbox; + org.apache.james.mailbox.jpa.mail.model.openjpa.AbstractJPAMessage; + org.apache.james.mailbox.jpa.mail.model.openjpa.JPAEncryptedMessage; + org.apache.james.mailbox.jpa.mail.model.openjpa.JPAMessage; + org.apache.james.mailbox.jpa.mail.model.openjpa.JPAStreamingMessage; + org.apache.james.mailbox.jpa.mail.model.JPAProperty; + org.apache.james.mailbox.jpa.user.model.JPASubscription; + org.apache.james.mailbox.jpa.quota.model.MaxDefaultMessageCount; + org.apache.james.mailbox.jpa.quota.model.MaxDefaultStorage + org.apache.james.mailbox.jpa.quota.model.MaxUserMessageCount + org.apache.james.mailbox.jpa.quota.model.MaxUserStorage) </value> </property> </toolProperties> http://git-wip-us.apache.org/repos/asf/james-project/blob/8fabac55/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/JPAPerUserMaxQuotaManager.java ---------------------------------------------------------------------- diff --git a/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/JPAPerUserMaxQuotaManager.java b/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/JPAPerUserMaxQuotaManager.java new file mode 100644 index 0000000..d93e0d6 --- /dev/null +++ b/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/JPAPerUserMaxQuotaManager.java @@ -0,0 +1,112 @@ +/**************************************************************** + * 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.jpa.quota; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; + +import org.apache.james.mailbox.exception.MailboxException; +import org.apache.james.mailbox.jpa.quota.model.MaxDefaultMessageCount; +import org.apache.james.mailbox.jpa.quota.model.MaxDefaultStorage; +import org.apache.james.mailbox.jpa.quota.model.MaxUserMessageCount; +import org.apache.james.mailbox.jpa.quota.model.MaxUserStorage; +import org.apache.james.mailbox.model.Quota; +import org.apache.james.mailbox.model.QuotaRoot; +import org.apache.james.mailbox.quota.MaxQuotaManager; + +import com.google.common.base.Function; +import com.google.common.base.Optional; + +public class JPAPerUserMaxQuotaManager implements MaxQuotaManager { + + private final EntityManager entityManager; + + public JPAPerUserMaxQuotaManager(EntityManagerFactory entityManagerFactory) { + entityManager = entityManagerFactory.createEntityManager(); + } + + @Override + public void setMaxStorage(QuotaRoot quotaRoot, long maxStorageQuota) throws MailboxException { + entityManager.getTransaction().begin(); + entityManager.merge(new MaxUserStorage(quotaRoot.getValue(), maxStorageQuota)); + entityManager.getTransaction().commit(); + } + + @Override + public void setMaxMessage(QuotaRoot quotaRoot, long maxMessageCount) throws MailboxException { + entityManager.getTransaction().begin(); + entityManager.merge(new MaxUserMessageCount(quotaRoot.getValue(), maxMessageCount)); + entityManager.getTransaction().commit(); + } + + @Override + public void setDefaultMaxStorage(long defaultMaxStorage) throws MailboxException { + entityManager.getTransaction().begin(); + entityManager.merge(new MaxDefaultStorage(defaultMaxStorage)); + entityManager.getTransaction().commit(); + } + + @Override + public void setDefaultMaxMessage(long defaultMaxMessageCount) throws MailboxException { + entityManager.getTransaction().begin(); + entityManager.merge(new MaxDefaultMessageCount(defaultMaxMessageCount)); + entityManager.getTransaction().commit(); + } + + @Override + public long getDefaultMaxStorage() throws MailboxException { + MaxDefaultStorage storedValue = entityManager.find(MaxDefaultStorage.class, MaxDefaultStorage.DEFAULT_KEY); + + if (storedValue == null) { + return Quota.UNLIMITED; + } + return storedValue.getValue(); + } + + @Override + public long getDefaultMaxMessage() throws MailboxException { + MaxDefaultMessageCount storedValue = entityManager.find(MaxDefaultMessageCount.class, MaxDefaultMessageCount.DEFAULT_KEY); + + if (storedValue == null) { + return Quota.UNLIMITED; + } + return storedValue.getValue(); + } + + @Override + public long getMaxStorage(QuotaRoot quotaRoot) throws MailboxException { + MaxUserStorage storedValue = entityManager.find(MaxUserStorage.class, quotaRoot.getValue()); + + if (storedValue == null) { + return getDefaultMaxStorage(); + } + return storedValue.getValue(); + } + + @Override + public long getMaxMessage(QuotaRoot quotaRoot) throws MailboxException { + MaxUserMessageCount storedValue = entityManager.find(MaxUserMessageCount.class, quotaRoot.getValue()); + + if (storedValue == null) { + return getDefaultMaxMessage(); + } + return storedValue.getValue(); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/8fabac55/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/model/MaxDefaultMessageCount.java ---------------------------------------------------------------------- diff --git a/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/model/MaxDefaultMessageCount.java b/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/model/MaxDefaultMessageCount.java new file mode 100644 index 0000000..1091939 --- /dev/null +++ b/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/model/MaxDefaultMessageCount.java @@ -0,0 +1,50 @@ +/**************************************************************** + * 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.jpa.quota.model; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity(name = "MaxDefaultMessageCount") +@Table(name = "JAMES_MAX_DEFAULT_MESSAGE_COUNT") +public class MaxDefaultMessageCount { + public static final String DEFAULT_KEY = "default_key"; + + @Id + @Column(name = "QUOTAROOT_ID") + private String quotaRoot = DEFAULT_KEY; + + @Column(name = "VALUE") + private long value; + + public MaxDefaultMessageCount(long value) { + this.quotaRoot = DEFAULT_KEY; + this.value = value; + } + + public MaxDefaultMessageCount() { + } + + public long getValue() { + return value; + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/8fabac55/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/model/MaxDefaultStorage.java ---------------------------------------------------------------------- diff --git a/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/model/MaxDefaultStorage.java b/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/model/MaxDefaultStorage.java new file mode 100644 index 0000000..f51ffce --- /dev/null +++ b/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/model/MaxDefaultStorage.java @@ -0,0 +1,50 @@ +/**************************************************************** + * 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.jpa.quota.model; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity(name="MaxDefaultStorage") +@Table(name="JAMES_MAX_DEFAULT_STORAGE") +public class MaxDefaultStorage { + public static final String DEFAULT_KEY = "default_key"; + + @Id + @Column(name = "QUOTAROOT_ID") + private String quotaRoot = DEFAULT_KEY; + + @Column(name="VALUE") + private long value; + + public MaxDefaultStorage(long value) { + this.quotaRoot = DEFAULT_KEY; + this.value = value; + } + + public MaxDefaultStorage() { + } + + public long getValue() { + return value; + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/8fabac55/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/model/MaxUserMessageCount.java ---------------------------------------------------------------------- diff --git a/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/model/MaxUserMessageCount.java b/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/model/MaxUserMessageCount.java new file mode 100644 index 0000000..3c7653f --- /dev/null +++ b/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/model/MaxUserMessageCount.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.mailbox.jpa.quota.model; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity(name="MaxUserMessageCount") +@Table(name="JAMES_MAX_USER_MESSAGE_COUNT") +public class MaxUserMessageCount { + @Id + @Column(name = "QUOTAROOT_ID") + private String quotaRoot; + + @Column(name = "VALUE") + private long value; + + public MaxUserMessageCount(String quotaRoot, long value) { + this.quotaRoot = quotaRoot; + this.value = value; + } + + public MaxUserMessageCount() { + } + + public long getValue() { + return value; + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/8fabac55/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/model/MaxUserStorage.java ---------------------------------------------------------------------- diff --git a/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/model/MaxUserStorage.java b/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/model/MaxUserStorage.java new file mode 100644 index 0000000..222fb81 --- /dev/null +++ b/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/quota/model/MaxUserStorage.java @@ -0,0 +1,49 @@ +/**************************************************************** + * 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.jpa.quota.model; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity(name = "MaxUserStorage") +@Table(name = "JAMES_MAX_USER_STORAGE") +public class MaxUserStorage { + + @Id + @Column(name = "QUOTAROOT_ID") + private String quotaRoot; + + @Column(name = "VALUE") + private long value; + + public MaxUserStorage(String quotaRoot, long value) { + this.quotaRoot = quotaRoot; + this.value = value; + } + + public MaxUserStorage() { + } + + public long getValue() { + return value; + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/8fabac55/mailbox/jpa/src/main/resources/META-INF/persistence.xml ---------------------------------------------------------------------- diff --git a/mailbox/jpa/src/main/resources/META-INF/persistence.xml b/mailbox/jpa/src/main/resources/META-INF/persistence.xml index 3ec5f22..70082c1 100644 --- a/mailbox/jpa/src/main/resources/META-INF/persistence.xml +++ b/mailbox/jpa/src/main/resources/META-INF/persistence.xml @@ -30,6 +30,10 @@ <class>org.apache.james.mailbox.jpa.mail.model.openjpa.JPAMailboxMessage</class> <class>org.apache.james.mailbox.jpa.mail.model.JPAProperty</class> <class>org.apache.james.mailbox.jpa.user.model.JPASubscription</class> + <class>org.apache.james.mailbox.jpa.quota.model.MaxDefaultMessageCount</class> + <class>org.apache.james.mailbox.jpa.quota.model.MaxDefaultStorage</class> + <class>org.apache.james.mailbox.jpa.quota.model.MaxUserMessageCount</class> + <class>org.apache.james.mailbox.jpa.quota.model.MaxUserStorage</class> <properties> <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/> <property name="openjpa.jdbc.MappingDefaults" value="ForeignKeyDeleteAction=cascade, JoinForeignKeyDeleteAction=cascade"/> http://git-wip-us.apache.org/repos/asf/james-project/blob/8fabac55/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAMailboxFixture.java ---------------------------------------------------------------------- diff --git a/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAMailboxFixture.java b/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAMailboxFixture.java index f73d0d1..8beecb5 100644 --- a/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAMailboxFixture.java +++ b/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAMailboxFixture.java @@ -25,6 +25,10 @@ import org.apache.james.mailbox.jpa.mail.model.JPAProperty; import org.apache.james.mailbox.jpa.mail.model.JPAUserFlag; import org.apache.james.mailbox.jpa.mail.model.openjpa.AbstractJPAMailboxMessage; import org.apache.james.mailbox.jpa.mail.model.openjpa.JPAMailboxMessage; +import org.apache.james.mailbox.jpa.quota.model.MaxDefaultMessageCount; +import org.apache.james.mailbox.jpa.quota.model.MaxDefaultStorage; +import org.apache.james.mailbox.jpa.quota.model.MaxUserMessageCount; +import org.apache.james.mailbox.jpa.quota.model.MaxUserStorage; import org.apache.james.mailbox.jpa.user.model.JPASubscription; public interface JPAMailboxFixture { @@ -37,9 +41,23 @@ public interface JPAMailboxFixture { JPAMailboxAnnotation.class, JPASubscription.class}; + Class<?>[] QUOTA_PERSISTANCE_CLASSES = new Class[] { + MaxDefaultMessageCount.class, + MaxDefaultStorage.class, + MaxUserMessageCount.class, + MaxUserStorage.class + }; + String[] MAILBOX_TABLE_NAMES = new String[] {"JAMES_MAIL_USERFLAG", "JAMES_MAIL_PROPERTY", "JAMES_MAILBOX_ANNOTATION", "JAMES_MAILBOX", "JAMES_MAIL"}; + + String[] QUOTA_TABLES_NAMES = new String[] { + "JAMES_MAX_DEFAULT_MESSAGE_COUNT", + "JAMES_MAX_DEFAULT_STORAGE", + "JAMES_MAX_USER_MESSAGE_COUNT", + "JAMES_MAX_USER_STORAGE" + }; } http://git-wip-us.apache.org/repos/asf/james-project/blob/8fabac55/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/quota/JPAPerUserMaxQuotaTest.java ---------------------------------------------------------------------- diff --git a/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/quota/JPAPerUserMaxQuotaTest.java b/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/quota/JPAPerUserMaxQuotaTest.java new file mode 100644 index 0000000..baed8c9 --- /dev/null +++ b/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/quota/JPAPerUserMaxQuotaTest.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.jpa.quota; + +import org.apache.james.backends.jpa.JpaTestCluster; +import org.apache.james.mailbox.jpa.JPAMailboxFixture; +import org.apache.james.mailbox.quota.MaxQuotaManager; +import org.apache.james.mailbox.store.quota.GenericMaxQuotaManagerTest; +import org.junit.After; + +public class JPAPerUserMaxQuotaTest extends GenericMaxQuotaManagerTest { + + private static final JpaTestCluster JPA_TEST_CLUSTER = JpaTestCluster.create(JPAMailboxFixture.QUOTA_PERSISTANCE_CLASSES); + + @Override + protected MaxQuotaManager provideMaxQuotaManager() { + return new JPAPerUserMaxQuotaManager(JPA_TEST_CLUSTER.getEntityManagerFactory()); + } + + @After + public void cleanUp() { + JPA_TEST_CLUSTER.clear(JPAMailboxFixture.QUOTA_TABLES_NAMES); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org