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

org.cometd.common.HashMapMessage Maven / Gradle / Ivy

package org.cometd.common;

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

import org.cometd.bayeux.Message;
import org.eclipse.jetty.util.ajax.JSON;

public class HashMapMessage extends HashMap implements Message.Mutable, JSON.Generator
{
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    private static JSON __json=new JSON()
    {
    };

    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    private static JSON __msgJSON=new JSON()
    {
        @Override
        protected Map newMap()
        {
            return new HashMapMessage();
        }

        @Override
        protected JSON contextFor(String field)
        {
            return __json;
        }
    };

    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    private static JSON __batchJSON=new JSON()
    {
        @Override
        protected Map newMap()
        {
            return new HashMapMessage();
        }

        @Override
        protected Object[] newArray(int size)
        {
            return new Message[size]; 
        }

        @Override
        protected JSON contextFor(String field)
        {
            return __json;
        }

        @Override
        protected JSON contextForArray()
        {
            return __msgJSON;
        }
    };
    
    private Message _associated;
    private boolean _lazy=false;
    

    /* ------------------------------------------------------------ */
    public HashMapMessage()
    {
    }
    

    /* ------------------------------------------------------------ */
    public void addJSON(StringBuffer buffer)
    {
        buffer.append(getJSON());
    }

    /* ------------------------------------------------------------ */
    @Override
    public void clear()
    {
        _lazy=false;
        super.clear();
    }

    /* ------------------------------------------------------------ */
    public Map getAdvice()
    {
        return (Map)get(ADVICE_FIELD);
    }

    /* ------------------------------------------------------------ */
    public Message getAssociated()
    {
        return _associated;
    }

    /* ------------------------------------------------------------ */
    public String getChannel()
    {
        return (String)get(CHANNEL_FIELD);
    }

    /* ------------------------------------------------------------ */
    public String getClientId()
    {
        return (String)get(CLIENT_FIELD);
    }

    /* ------------------------------------------------------------ */
    public Object getData()
    {
        return get(DATA_FIELD);
    }

    /* ------------------------------------------------------------ */
    public Map getDataAsMap()
    {
        return (Map)get(DATA_FIELD);
    }

    /* ------------------------------------------------------------ */
    public Map getExt()
    {
        return (Map)get(EXT_FIELD);
    }

    /* ------------------------------------------------------------ */
    public Object getId()
    {
        return (String)get(ID_FIELD);
    }

    /* ------------------------------------------------------------ */
    public String getJSON()
    {
        StringBuffer buf=new StringBuffer(__json.getStringBufferSize());
        synchronized(buf)
        {
            __json.appendMap(buf,this);
            return buf.toString();
        }
    }

    /* ------------------------------------------------------------ */
    public Map getAdvice(boolean create)
    {
        Map advice=getAdvice();
        if (create && advice==null)
        {
            advice = new HashMap();
            put(ADVICE_FIELD,advice);
        }
        return advice;
    }

    /* ------------------------------------------------------------ */
    public Map getDataAsMap(boolean create)
    {
        Map data=(Map)getData();
        if (create && data==null)
        {
            data = new HashMap();
            put(DATA_FIELD,data);
        }
        return data;
    }

    /* ------------------------------------------------------------ */
    public Map getExt(boolean create)
    {
        Object ext=getExt();
        if (ext==null && !create)
            return null;
        
        if (ext instanceof Map)
            return (Map)ext;

        if (ext instanceof JSON.Literal)
        {
            ext=__json.fromJSON(ext.toString());
            put(EXT_FIELD,ext);
            return (Map)ext;
        }

        ext=new HashMap();
        put(EXT_FIELD,ext);
        return (Map)ext;
    }

    /* ------------------------------------------------------------ */
    /**
     * Lazy messages are queued but do not wake up waiting clients.
     * 
     * @return true if message is lazy
     */
    public boolean isLazy()
    {
        return _lazy;
    }
    

    /* ------------------------------------------------------------ */
    /**
     * @return True if the message is for a Meta channel
     */
    public boolean isMeta()
    {
        return ChannelId.isMeta(getChannel());
    }

    /* ------------------------------------------------------------ */
    public boolean isSuccessful()
    {
        Boolean bool=(Boolean)get(Message.SUCCESSFUL_FIELD);
        return bool != null && bool.booleanValue();
    }
    
    /* ------------------------------------------------------------ */
    public void setAssociated(Message associated)
    {
        _associated=associated;
    }

    /* ------------------------------------------------------------ */
    /**
     * Lazy messages are queued but do not wake up waiting clients.
     * 
     * @param lazy
     *            true if message is lazy
     */
    public void setLazy(boolean lazy)
    {
        _lazy=lazy;
    }

    /* ------------------------------------------------------------ */
    public String toString()
    {
        return getJSON();
    }

    public void setChannel(String channelId)
    {
        put(CHANNEL_FIELD,channelId);
    }

    public void setClientId(String clientId)
    {
        put(CLIENT_FIELD,clientId);
    }

    public void setData(Object data)
    {
        put(DATA_FIELD,data);
    }

    public void setId(Object id)
    {
        put(ID_FIELD,id);
    }

    public void setSuccessful(boolean success)
    {
        put(SUCCESSFUL_FIELD,success?Boolean.TRUE:Boolean.FALSE);
    }


    public Message asImmutable()
    {
        return this;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy