/*
 * 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 java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletContext;

public abstract class AbstractMessageServiceAdapter
implements MessageServiceAdapter {
    private static final Logger LOGGER = Logger.getLogger(AbstractMessageServiceAdapter.class.getName());

    protected static final String MESSAGING_PROPERTIES = "com.icesoft.net.messaging.properties";

    protected final Map<String, Object> topicPublisherMap = new HashMap<String, Object>();
    protected final Map<String, Object> topicSubscriberMap = new HashMap<String, Object>();

    protected MessageServiceClient messageServiceClient;
    protected MessageServiceConfiguration messageServiceConfiguration;

    protected AbstractMessageServiceAdapter(final ServletContext servletContext)
    throws IllegalArgumentException {
        if (servletContext == null) {
            throw new IllegalArgumentException("servletContext is null");
        }
    }

    public MessageServiceClient getMessageServiceClient() {
        return messageServiceClient;
    }

    public MessageServiceConfiguration getMessageServiceConfiguration() {
        return messageServiceConfiguration;
    }

    public String[] getPublisherTopicNames() {
        return topicPublisherMap.keySet().toArray(new String[topicPublisherMap.size()]);
    }

    public String[] getSubscriberTopicNames() {
        return topicSubscriberMap.keySet().toArray(new String[topicSubscriberMap.size()]);
    }

    public boolean isPublishingOn(final String topicName) {
        return topicName != null && topicName.trim().length() != 0 && topicPublisherMap.containsKey(topicName);
    }

    public boolean isSubscribedTo(final String topicName) {
        return topicName != null && topicName.trim().length() != 0 && topicSubscriberMap.containsKey(topicName);
    }

    public void setMessageServiceClient(final MessageServiceClient messageServiceClient) {
        this.messageServiceClient = messageServiceClient;
    }
}
