All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.james.container.spring.mailbox.MaxQuotaConfigurationReader Maven / Gradle / Ivy

There is a newer version: 3.8.1
Show newest version
/****************************************************************
 * 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.container.spring.mailbox;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.james.core.quota.QuotaCount;
import org.apache.james.core.quota.QuotaSize;
import org.apache.james.lifecycle.api.Configurable;
import org.apache.james.mailbox.exception.MailboxException;
import org.apache.james.mailbox.model.QuotaRoot;
import org.apache.james.mailbox.quota.MaxQuotaManager;
import org.apache.james.mailbox.quota.QuotaRootResolver;

public class MaxQuotaConfigurationReader implements Configurable {

    private final MaxQuotaManager maxQuotaManager;
    private final QuotaRootResolver quotaRootResolver;

    public MaxQuotaConfigurationReader(MaxQuotaManager maxQuotaManager, QuotaRootResolver quotaRootResolver) {
        this.maxQuotaManager = maxQuotaManager;
        this.quotaRootResolver = quotaRootResolver;
    }

    @Override
    public void configure(HierarchicalConfiguration config) throws ConfigurationException {
        Long globalMaxMessage = config.configurationAt("maxQuotaManager").getLong("globalMaxMessage", null);
        Long globalMaxStorage = config.configurationAt("maxQuotaManager").getLong("globalMaxStorage", null);
        Map maxMessage = parseMaxMessageConfiguration(config, "maxMessage");
        Map maxStorage = parseMaxMessageConfiguration(config, "maxStorage");
        try {
            configureGlobalValues(globalMaxMessage, globalMaxStorage);
            configureQuotaRootSpecificValues(maxMessage, maxStorage);
        } catch (MailboxException e) {
            throw new ConfigurationException("Exception caught while configuring max quota manager", e);
        }
    }

    private  Map parseMaxMessageConfiguration(HierarchicalConfiguration config, String entry) {
        List maxMessageConfiguration = config.configurationAt("maxQuotaManager").configurationsAt(entry);
        Map result = new HashMap<>();
        for (HierarchicalConfiguration conf : maxMessageConfiguration) {
            result.put(conf.getString("quotaRoot"), conf.getLong("value"));
        }
        return result;
    }

    private void configureGlobalValues(Long globalMaxMessage, Long globalMaxStorage) throws MailboxException {
        if (globalMaxMessage != null) {
            maxQuotaManager.setGlobalMaxMessage(QuotaCount.count(globalMaxMessage));
        }
        if (globalMaxStorage != null) {
            maxQuotaManager.setGlobalMaxStorage(QuotaSize.size(globalMaxStorage));
        }
    }

    private void configureQuotaRootSpecificValues(Map maxMessage, Map maxStorage) throws MailboxException {
        for (Map.Entry entry : maxMessage.entrySet()) {
            maxQuotaManager.setMaxMessage(toQuotaRoot(entry.getKey()), QuotaCount.count(entry.getValue()));
        }
        for (Map.Entry entry : maxStorage.entrySet()) {
            maxQuotaManager.setMaxStorage(toQuotaRoot(entry.getKey()), QuotaSize.size(entry.getValue()));
        }
    }

    private QuotaRoot toQuotaRoot(String serializedKey) throws MailboxException {
        return quotaRootResolver.fromString(serializedKey);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy