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

org.apache.james.mailets.configuration.MailetContainer Maven / Gradle / Ivy

/****************************************************************
 * 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.mailets.configuration;

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

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;

public class MailetContainer implements SerializableAsXml {

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {

        public static final int DEFAULT_THREAD_COUNT = 5;
        public static final String DEFAULT_POSTMASTER = "postmaster@localhost";
        private Optional postmaster;
        private Optional threads;
        private Map processors;

        private Builder() {
            processors = new HashMap<>();
            threads = Optional.empty();
            postmaster = Optional.empty();
        }

        public Builder postmaster(String postmaster) {
            this.postmaster = Optional.of(postmaster);
            return this;
        }

        public Builder threads(int threads) {
            this.threads = Optional.of(threads);
            return this;
        }

        public Builder putProcessor(ProcessorConfiguration processorConfiguration) {
            this.processors.put(processorConfiguration.getState(), processorConfiguration);
            return this;
        }

        public Builder putProcessor(ProcessorConfiguration.Builder processorConfiguration) {
            return this.putProcessor(processorConfiguration.build());
        }

        public MailetContainer build() {
            String postmaster = this.postmaster.orElse(DEFAULT_POSTMASTER);
            int threads = this.threads.orElse(DEFAULT_THREAD_COUNT);
            Preconditions.checkState(!Strings.isNullOrEmpty(postmaster), "'postmaster' is mandatory");
            Preconditions.checkState(threads > 0, "'threads' should be greater than 0");
            return new MailetContainer(postmaster, threads,
                ImmutableList.copyOf(processors.values()));
        }
    }

    private final String postmaster;
    private final int threads;
    private final List processors;

    private MailetContainer(String postmaster, int threads, List processors) {
        this.postmaster = postmaster;
        this.threads = threads;
        this.processors = processors;
    }

    public String getPostmaster() {
        return postmaster;
    }

    public int getThreads() {
        return threads;
    }

    public List getProcessors() {
        return processors;
    }

    @Override
    public String serializeAsXml() {
        StringBuilder builder = new StringBuilder();
        builder.append("\n")
            .append("\n")
            .append("").append(getPostmaster()).append("").append("\n")
            .append("").append(getThreads()).append("").append("\n")
            .append("\n");
        for (ProcessorConfiguration processorConfiguration : getProcessors()) {
            builder.append(processorConfiguration.serializeAsXml());
        }
        builder.append("\n")
            .append("");
        return builder.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy