Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id: DefaultPayload.java 22 2009-05-18 17:20:44Z mitcje $
   * $URL: https://ikasaneip.svn.sourceforge.net/svnroot/ikasaneip/tags/ikasan-0.7.3/common/src/main/java/org/ikasan/common/component/DefaultPayload.java $
   * 
   * ====================================================================
   * Ikasan Enterprise Integration Platform
   * Copyright (c) 2003-2008 Mizuho International plc. and individual contributors as indicated
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the 
  * Free Software Foundation Europe e.V. Talstrasse 110, 40217 Dusseldorf, Germany 
  * or see the FSF site: http://www.fsfeurope.org/.
  * ====================================================================
  */
 package org.ikasan.common.component;
 
 
 
 // Imported log4j classes
 
 // standard payload interface
Default implementation of the Payload interface.

Author(s):
Ikasan Development Team
 
 public class DefaultPayload extends MetaData implements PayloadCloneable
 {
    
Serialise ID
 
     private static final long serialVersionUID = 1L;

    
The logger instance
 
     private static Logger logger = Logger.getLogger(DefaultPayload.class);

    
Actual content of the payload to be delivered
 
     private byte[] content;

    
Do not let anyone create a payload based on a no-argument default constructor
 
     @SuppressWarnings("unused")
     private DefaultPayload()
     {
         // empty
     }

    
Default constructor Creates a new instance of Payload with the empty data content.

Parameters:
name Name of the payload
spec Spec of the payload
srcSystem Src System of the payload
 
     public DefaultPayload(final String namefinal String specfinal String srcSystem)
     {
         this(namespecsrcSystemnew String("").getBytes()); //$NON-NLS-1$
     }

    
Creates a new instance of Payload with the specified data content.

Parameters:
name Name of the payload
spec Spec of the payload
srcSystem Src System of the payload
content Content of the payload
 
     public DefaultPayload(final String namefinal String specfinal String srcSystemfinal byte content[])
     {
         this. = null;
         this. = name;
         this. = spec;
         this. = srcSystem;
         this. = generateId();
        this. = generateTimestamp();
        this. = new Integer(..getLevel());
        this. = content;
        this. = ..toString();
        this. = Charset.defaultCharset().toString();
        this. = ;
        this. = this.calculateChecksum();
        this. = new Long(content.length);
        if (.isDebugEnabled()) .debug("Payload constructor created payload."); //$NON-NLS-1$
    }

    
This constructor has a serious flaw and does not create a copy of the payload. DO NOT USE THIS CONSTRUCTOR.

Deprecated:
- this is not a deep copy and simply returns a pointer to the same object.
Parameters:
payload The payload to clone off
    @Deprecated
    public DefaultPayload(final Payload payload)
    {
        this. = payload.getSchemaInstanceNSURI();
        this. = payload.getId();
        this. = payload.getPriority();
        this. = payload.getTimestamp();
        this. = payload.getTimestampFormat();
        this. = payload.getTimezone();
        this. = payload.getContent();
        this. = payload.getName();
        this. = payload.getSpec();
        this. = payload.getEncoding();
        this. = payload.getFormat();
        this. = payload.getCharset();
        this. = payload.getSize();
        this. = payload.getChecksumAlg();
        this. = payload.getChecksum();
        this. = payload.getSrcSystem();
        this. = payload.getTargetSystems();
        this. = payload.getProcessIds();
        if (.isDebugEnabled()) .debug("Payload constructor created payload."); //$NON-NLS-1$
    }

    
Setter for content. This setter by default sets the size and checksum of the particular payload.

Parameters:
content The content of the payload to set
    public void setContent(final byte[] content)
    {
        this. = content;
        if (.isDebugEnabled()) .debug("Content set to [" + this. + "]."); //$NON-NLS-1$//$NON-NLS-2$
        this.setChecksum();
        this. = new Long(content.length);
    }

    
Setter for content that allows to override the default functionality of setting the respective size and checksum automatically.

Parameters:
content The content to set
contentOnly If true, set the content only, otherwise set the content and adjust the size and checksum automatically.
    public void setContent(final byte[] contentboolean contentOnly)
    {
        if (contentOnly)
        {
            this. = content;
        }
        else
        {
            setContent(content);
        }
    }

    
Getter for content

Returns:
byte[]
    public byte[] getContent()
    {
        if (.isDebugEnabled())
        {
            .debug("Returning content [" + this. + "]."); //$NON-NLS-1$//$NON-NLS-2$
        }
        return this.;
    }

    
Utility setter for size. When this setter is used, the size is automatically set to the size of the content byte array. If the content is empty to null, the size is set to zero.
    @Override
    public void setSize()
    {
        if (this. != null && this..length > 0)
            this. = new Long(this..length);
        else
        {
            .warn("Payload content null or empty. Size set to [0]."); //$NON-NLS-1$
            this. = new Long(0L);
        }
        if (.isDebugEnabled()) .debug("Size set to [" + this. + "]."); //$NON-NLS-1$//$NON-NLS-2$
    }

    
Method used to calculate the checksum of the content byte array. By default, the checksum is calculated using the MD5 algorithm.

Returns:
The MD5 checksum of the content as a hexadecimal String.
    @Override
    protected String calculateChecksum()
    {
        String result = null;
        // Check whether the content is null
        if (this. == null)
        {
            if (.isDebugEnabled())
            {
                .debug("Content was null, returning a null checksum for the payload.");
            }
        }
        else
        {
            try
            {
                if (.isDebugEnabled())
                {
                    .debug("Calculating checksum for the payload.");
                }
                // we have content so lets get a checksum
                InputStream stream = new ByteArrayInputStream(this.);
                result = ChecksumUtils.getChecksum(streamthis.getChecksumAlg());
            }
            catch (IOException e)
            {
                .warn("Exception encountered when calculating payload " //$NON-NLS-1$
                        + "content checksum. Setting checksum to [null]."e); //$NON-NLS-1$
            }
            catch (NoSuchAlgorithmException e)
            {
                .warn("Exception encountered when calculating payload " //$NON-NLS-1$
                        + "content checksum. Setting checksum to [null]."e); //$NON-NLS-1$
            }
        }
        return result;
    }

    
Create a formatted string detailing the payload id of the incoming payload.

Returns:
String
    @Override
    public String idToString()
    {
        return "Payload Id[" + this.getId() + "] "//$NON-NLS-1$ //$NON-NLS-2$
    }

    
Creates a String representation of the Payload showing all the objects properties and an many characters of the content as specified in the cLength parameter. If the cLength is negative, the content is display from the end of the content string.

Parameters:
cLength Used to restrict the length of the payload to a specific number of chars.
Returns:
A formatted String representing the Payload object.
    @Override
    public String toString(int cLength)
    {
        StringBuilder sb = new StringBuilder(512);
        sb.append(super.toString());
        sb.append("Content  = [");//$NON-NLS-1$
        sb.append(new String(this.));
        sb.append("]\n");//$NON-NLS-1$
        return sb.toString();
    }

    
Wrapper method for toString(int length) which defaults length to the length of the content.

Returns:
A string representation of the complete payload (fields and content).
    @Override
    public String toString()
    {
        return toString(this..length);
    }

    
Test equality of two payload instances based on the lifetime identifier. Only the identifiers are compared as the payload content and associated attributes can change over the life of the payload, however, the originally assigned id never changes.

Parameters:
payload The payload to check against
Returns:
true if we can convert, else false
    public boolean equals(Payload payload)
    {
        if (this..equals(payload.getId()))
        {
            return true;
        }
        return false;
    }
    /*
     * (non-Javadoc)
     * 
     * @see org.ikasan.common.Payload#base64EncodePayload()
     */
    public void base64EncodePayload()
    {
        setContent(Base64.encodeBase64(getContent()));
        if (.isDebugEnabled())
        {
            .debug("Binary payload encoded to [" //$NON-NLS-1$ 
                    + ..toString() + "]"); //$NON-NLS-1$
        }
    }
    /*
     * (non-Javadoc)
     * 
     * @see org.ikasan.common.Payload#clone()
     */
    @Override
    public Payload clone() throws CloneNotSupportedException
    {
        Payload clone = (Payloadsuper.clone();
        // sort out non-cloneable objects
        if (this.getPriority() != null)
        {
            clone.setPriority(new Integer(this.getPriority()));
        }
        if (this.getTimestamp() != null)
        {
            clone.setTimestamp(new Long(this.getTimestamp()));
        }
        byte[] copiedContent = new byte[.];
        System.arraycopy(, 0, copiedContent, 0, .);
        clone.setContent(copiedContent);
        if (this.getSize() != null)
        {
            clone.setSize(new Long(this.getSize()));
        }
        clone.setChecksum();
        return clone;
    }
    /*
     * (non-Javadoc)
     * 
     * @see org.ikasan.common.Payload#spawn()
     */
    public Payload spawn() throws CloneNotSupportedException
    {
        Payload spawned = this.clone();
        // Stamp certain fields with new values
        spawned.setId(generateId());
        spawned.setTimestamp(generateTimestamp());
        return spawned;
    }
New to GrepCode? Check out our FAQ X