Package org.mom4j.messaging

Source Code of org.mom4j.messaging.XcpAdapter

package org.mom4j.messaging;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mom4j.xcp.XcpException;
import org.mom4j.xcp.XcpRequest;
import org.mom4j.xcp.XcpRequestListener;
import javax.jms.Message;

import java.io.OutputStream;
import java.io.OutputStreamWriter;


public class XcpAdapter implements XcpRequestListener {
   
    private static Log log = LogFactory.getLog(XcpAdapter.class);

    private Console console;


    public XcpAdapter(Console c) {
        this.console = c;
    }


    private void register(String destination,
                          String consumerId,
                          String messageSelector)
        throws XcpException
    {
        try {
            this.console.register(destination, consumerId, messageSelector);
        } catch(javax.jms.JMSException ex) {
            log.error("failed to register messaging client", ex);
            throw new XcpException(ex.getMessage());
        }
    }


    private void registerDur(XcpMessage msg, XcpRequest req)
        throws XcpException
    {
        String consumerId = null;
        try {
            consumerId =
                this.console.registerDur(msg.getDestination(),
                                         msg.getSubscriberName(),
                                         msg.getConsumerId(),
                                         msg.getMessageSelector());
            XcpMessage ret =
                new XcpMessage(msg.getSessionId(),
                               XcpMessage.ACTION_REGISTER_DUR,
                               msg.getDestination(),
                               consumerId);
            OutputStream out = req.getOutputStream();
            OutputStreamWriter writer = new OutputStreamWriter(out);
            writer.write(ret.getTagOpen());
            writer.write(ret.getContent());
            writer.write(ret.getTagClosed());
            writer.flush();
            writer.close();
        } catch(Exception ex) {
            log.error("failed to register durable subscriber", ex);
            throw new XcpException(ex.getMessage());
        }
    }


    private void unregister(String destination, String consumerId)
        throws XcpException
    {
        try {
            this.console.unregister(destination, consumerId);
        } catch(javax.jms.JMSException ex) {
            log.error("Unregister failed", ex);
            throw new XcpException(ex.getMessage());
        }
    }


    private void unregisterDur(String subscriberName)
        throws XcpException
    {
        try {
            this.console.unregisterDur(subscriberName);
        } catch(javax.jms.JMSException ex) {
            log.error("UnregisterDur failed", ex);
            throw new XcpException(ex.getMessage());
        }
    }


    private void send(XcpMessage msg, boolean transacted)
        throws XcpException
    {
        String sid = msg.getSessionId();
        try {
            String dest = msg.getDestination();
            this.console.send(sid, dest, msg.getMessage(), transacted);
        } catch(Exception ex) {
            log.error("failed sending message", ex);
            throw new XcpException(ex.getMessage());
        }
    }


    private void receive(XcpRequest req, boolean transacted)
        throws XcpException
    {
        XcpMessage msg  = (XcpMessage)req.getRootElement();
        String     sid  = msg.getSessionId();
        String     dest = msg.getDestination();
        String     cid  = msg.getConsumerId();

        Message jmsmsg = null;

        try {
            jmsmsg = this.console.receive(sid, dest, cid, transacted);
            XcpMessage ret = new XcpMessage(sid, XcpMessage.ACTION_RECEIVE);
            if(jmsmsg != null) {
                ret.setMessage(jmsmsg);
            }
            OutputStream out = req.getOutputStream();
            OutputStreamWriter writer = new OutputStreamWriter(out);
            writer.write(ret.getTagOpen());
            writer.write(ret.getContent());
            writer.write(ret.getTagClosed());
            writer.flush();
            writer.close();
        } catch(Exception ex) {
            log.error("Receiving a message failed", ex);
            throw new XcpException(ex.getMessage());
        }
    }


    private void createQueue(XcpMessage msg)
        throws XcpException
    {
        try {
            this.console.createQueue(msg.getDestination());
        } catch(Exception ex) {
            log.error("createQueue failed", ex);
            throw new XcpException(ex.getMessage());
        }
    }


    private void createTopic(XcpMessage msg)
        throws XcpException
    {
        try {
            this.console.createTopic(msg.getDestination());
        } catch(Exception ex) {
            log.error("createTopic failed", ex);
            throw new XcpException(ex.getMessage());
        }
    }


    private void commit(String sid)
        throws XcpException
    {
        this.console.commit(sid);
    }


    private void rollback(String sid)
        throws XcpException
    {
        this.console.rollback(sid);
    }


    public void requestNotify(XcpRequest req)
        throws XcpException
    {
        XcpMessage msg = (XcpMessage)req.getRootElement();
        String action = msg.getAction();
       
        if(action.equals(XcpMessage.ACTION_SEND)) {
            this.send(msg, false);
        } else if(action.equals(XcpMessage.ACTION_SEND_TX)) {
            this.send(msg, true);
        } else if(action.equals(XcpMessage.ACTION_RECEIVE)) {
            this.receive(req, false);
        } else if(action.equals(XcpMessage.ACTION_RECEIVE_TX)) {
            this.receive(req, true);
        } else if(action.equals(XcpMessage.ACTION_CREATE_QUEUE)) {
            this.createQueue(msg);
        } else if(action.equals(XcpMessage.ACTION_CREATE_TOPIC)) {
            this.createTopic(msg);
        } else if(action.equals(XcpMessage.ACTION_COMMIT)) {
            this.commit(msg.getSessionId());
        } else if(action.equals(XcpMessage.ACTION_ROLLBACK)) {
            this.rollback(msg.getSessionId());
        } else if(action.equals(XcpMessage.ACTION_REGISTER)) {
            this.register(msg.getDestination(),
                          msg.getConsumerId(),
                          msg.getMessageSelector());
        } else if(action.equals(XcpMessage.ACTION_REGISTER_DUR)) {
            this.registerDur(msg, req);
        } else if(action.equals(XcpMessage.ACTION_UNREGISTER)) {
            this.unregister(msg.getDestination(), msg.getConsumerId());
        } else if(action.equals(XcpMessage.ACTION_UNREGISTER_DUR)) {
            this.unregisterDur(msg.getSubscriberName());
        } else {
            throw new XcpException("unknown action " + action);
        }
    }
       
}
TOP

Related Classes of org.mom4j.messaging.XcpAdapter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.