/*
 * 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 com.icesoft.util.PropertyException;
import com.icesoft.util.ServerUtility;

import java.util.Collections;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletContext;

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

    protected final Properties messageProperties;

    protected AbstractMessage(final ServletContext servletContext) {
        /*
         * According to Java Message Service 1.1 & 2.0:
         *
         * 3.5.2. Property values
         *
         *        Property values can be boolean, byte, short, int, long, float, double, and String.
         *
         * JBoss EAP 6.3.0 uses Object property values as well.
         */
        if (!ServerUtility.isJBoss7(servletContext)) {
            messageProperties =
                new Properties(
                    new IdentifierValidator(servletContext),
                    new Properties.PropertyValueValidator()
                );
        } else {
            messageProperties =
                new Properties(
                    new IdentifierValidator(servletContext),
                    new Properties.PropertyValueValidator() {
                        @Override
                        public boolean isValid(final Object object) {
                            return true;
                        }
                    }
                );
        }
    }

    public void clearProperties() {
        messageProperties.clear();
    }

    public boolean getBooleanProperty(final String name)
    throws PropertyException {
        return messageProperties.getBooleanProperty(name);
    }

    public byte getByteProperty(final String name)
    throws PropertyException {
        return messageProperties.getByteProperty(name);
    }

    public double getDoubleProperty(final String name)
    throws PropertyException {
        return messageProperties.getDoubleProperty(name);
    }

    public float getFloatProperty(final String name)
    throws PropertyException {
        return messageProperties.getFloatProperty(name);
    }

    public int getIntProperty(final String name)
    throws PropertyException {
        return messageProperties.getIntProperty(name);
    }

    public long getLongProperty(final String name)
    throws PropertyException {
        return messageProperties.getLongProperty(name);
    }

    public Object getObjectProperty(final String name) {
        return messageProperties.getObjectProperty(name);
    }

    public Enumeration getPropertyNames() {
        return Collections.enumeration(messageProperties.keySet());
    }

    public short getShortProperty(final String name)
    throws PropertyException {
        return messageProperties.getShortProperty(name);
    }

    public String getStringProperty(final String name) {
        return messageProperties.getStringProperty(name);
    }

    public boolean propertyExists(final String name) {
        return messageProperties.containsKey(name);
    }

    public void setBooleanProperty(final String name, final boolean value)
    throws PropertyException {
        messageProperties.setBooleanProperty(name, value);
    }

    public void setByteProperty(final String name, final byte value)
    throws PropertyException {
        messageProperties.setByteProperty(name, value);
    }

    public void setDoubleProperty(final String name, final double value)
    throws PropertyException {
        messageProperties.setDoubleProperty(name, value);
    }

    public void setFloatProperty(final String name, final float value)
    throws PropertyException {
        messageProperties.setFloatProperty(name, value);
    }

    public void setIntProperty(final String name, final int value)
    throws PropertyException {
        messageProperties.setIntProperty(name, value);
    }

    public void setLongProperty(final String name, final long value)
    throws PropertyException {
        messageProperties.setLongProperty(name, value);
    }

    public void setObjectProperty(final String name, final Object value)
    throws PropertyException {
        messageProperties.setObjectProperty(name, value);
    }

    public void setShortProperty(final String name, final short value)
    throws PropertyException {
        messageProperties.setShortProperty(name, value);
    }

    public void setStringProperty(final String name, final String value)
    throws PropertyException {
        messageProperties.setStringProperty(name, value);
    }

    public String toString() {
        StringBuffer _messageString = new StringBuffer();
        Enumeration _propertyNames = getPropertyNames();
        while (_propertyNames.hasMoreElements()) {
            String _propertyName = (String)_propertyNames.nextElement();
            _messageString.append(_propertyName).append(": ").append(getObjectProperty(_propertyName)).append("\r\n");
        }
        return _messageString.append("\r\n").toString();
    }
}
