/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (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.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */
package com.icesoft.net.messaging;

import com.icesoft.util.Properties;
import org.icepush.Configuration;

import java.io.InputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MessageServiceConfigurationProperties
implements MessageServiceConfiguration {
    private static final Logger LOGGER = Logger.getLogger(MessageServiceConfigurationProperties.class.getName());

    private static final String DEFAULT_DEFAULT_TOPIC_NAME = "icepush";
    private static final long DEFAULT_MESSAGE_MAX_DELAY = 100;
    private static final int DEFAULT_MESSAGE_MAX_LENGTH = 4 * 1024;
    private static final int DEFAULT_THREAD_POOL_SIZE = 15;

    protected Properties messageServiceConfigurationProperties =
        new Properties();
    protected Configuration configuration;

    public MessageServiceConfigurationProperties(final Configuration configuration) {
        this.configuration = configuration;
    }

    public MessageServiceConfigurationProperties(final InputStream inputStream, final Configuration configuration)
    throws IOException {
        messageServiceConfigurationProperties.load(inputStream);
        this.configuration = configuration;
    }

    public String get(final String name) {
        return messageServiceConfigurationProperties.getProperty(name, null);
    }

    public String getDefaultTopicName() {
        return
            configuration.getAttribute(
                DEFAULT_TOPIC_NAME,
                messageServiceConfigurationProperties.getProperty(DEFAULT_TOPIC_NAME, DEFAULT_DEFAULT_TOPIC_NAME));
    }

    public long getMessageMaxDelay() {
        return
            configuration.getAttributeAsLong(
                MESSAGE_MAX_DELAY,
                messageServiceConfigurationProperties.getLongProperty(MESSAGE_MAX_DELAY, DEFAULT_MESSAGE_MAX_DELAY));
    }

    public int getMessageMaxLength() {
        return
            configuration.getAttributeAsInteger(
                MESSAGE_MAX_LENGTH,
                messageServiceConfigurationProperties.getIntProperty(MESSAGE_MAX_LENGTH, DEFAULT_MESSAGE_MAX_LENGTH));
    }

    public int getThreadPoolSize() {
        return
            configuration.getAttributeAsInteger(
                THREAD_POOL_SIZE,
                messageServiceConfigurationProperties.getIntProperty(THREAD_POOL_SIZE, DEFAULT_THREAD_POOL_SIZE));
    }
}
