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

org.javalite.async.Command Maven / Gradle / Ivy

There is a newer version: 3.5-j11
Show newest version
/*
Copyright 2009-(CURRENT YEAR) Igor Polevoy

Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/


package org.javalite.async;

import com.thoughtworks.xstream.XStream;
import org.javalite.common.JsonHelper;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import static org.javalite.common.Collections.map;

/**
 * Super class of all commands. Only the method {@link #execute()} is to be provided by subclasses to
 * do real application work.
 *
 * @author Igor Polevoy on 4/4/15.
 */
public abstract class Command {

    private String jmsMessageId;

    private Map params = map("class", getClass().getSimpleName());

    private static final XStream X_STREAM = new XStream(new CDATAXppDriver());

    /**
     * Method used by framework to de-serialize a command from XML.
     *
     * @param commandXml XML representation of a command.
     *
     * @return new instance of a command initialized with data from commandXml  argument.
     */
    public static  T  fromXml(String commandXml) {
        return (T) X_STREAM.fromXML(commandXml);
    }



    /**
     * Subclasses are to provide implementation of this method to get application-specific work done.
     */
    public abstract void execute();

    /**
     * Serializes this object into XML. The output is used by {@link #fromXml(String)} to de-serialize a command
     * after it passes through messaging broker.
     *
     * @return XML representation of this instance.
     */
    public final String toXml() {
        return X_STREAM.toXML(this);
    }

    /**
     * Flattens(serializes, dehydrates, etc.) this instance to a binary representation.
     *
     * @return a binary representation
     * @throws IOException
     */
    public byte[] toBytes() throws IOException {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ZipOutputStream stream = new ZipOutputStream(bout);
        ZipEntry ze = new ZipEntry("async_message");
        stream.putNextEntry(ze);
        stream.write(toXml().getBytes());
        stream.flush();
        stream.close();
        return  bout.toByteArray();
    }

    @SuppressWarnings("unchecked")
    public static  T fromBytes(byte[] bytes) throws IOException {
        ZipInputStream zin  = new ZipInputStream(new ByteArrayInputStream(bytes));
        ZipEntry ze = zin.getNextEntry();
        if(ze == null){
            throw new AsyncException("something is seriously wrong with serialization");
        }
        ByteArrayOutputStream bout  = new ByteArrayOutputStream();
        byte[] buffer = new byte[2048];
        int len;
        while((len = zin.read(buffer)) != -1) {
            bout.write(buffer, 0, len);
        }
        return  (T) Command.fromXml(bout.toString());
    }

    public String getJMSMessageID() {
        return jmsMessageId;
    }

    public void setJMSMessageID(String jmsMessageId) {
        this.jmsMessageId = jmsMessageId;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy