Search in sources :

Example 1 with Dom4JDriver

use of com.thoughtworks.xstream.io.xml.Dom4JDriver in project swiftmq-client by iitsoftware.

the class FileQueryPropsReply method createResult.

private void createResult(TextMessage message) throws JMSException {
    String s = message.getText();
    if (s != null) {
        XStream xStream = new XStream(new Dom4JDriver());
        StringReader reader = new StringReader(s);
        result = (Map<String, Map<String, Object>>) xStream.fromXML(reader);
    }
}
Also used : XStream(com.thoughtworks.xstream.XStream) Dom4JDriver(com.thoughtworks.xstream.io.xml.Dom4JDriver) StringReader(java.io.StringReader) Map(java.util.Map)

Example 2 with Dom4JDriver

use of com.thoughtworks.xstream.io.xml.Dom4JDriver in project swiftmq-client by iitsoftware.

the class FileQueryPropsReply method toMessage.

public Message toMessage() throws JMSException {
    TextMessage message = new TextMessageImpl();
    message.setIntProperty(ProtocolFactory.DUMPID_PROP, ProtocolFactory.FILEQUERYPROPS_REP);
    if (result != null && result.size() > 0) {
        XStream xStream = new XStream(new Dom4JDriver());
        StringWriter writer = new StringWriter();
        xStream.toXML(result, writer);
        message.setText(writer.toString());
    }
    return fillMessage(message);
}
Also used : StringWriter(java.io.StringWriter) XStream(com.thoughtworks.xstream.XStream) Dom4JDriver(com.thoughtworks.xstream.io.xml.Dom4JDriver) TextMessageImpl(com.swiftmq.jms.TextMessageImpl) TextMessage(javax.jms.TextMessage)

Example 3 with Dom4JDriver

use of com.thoughtworks.xstream.io.xml.Dom4JDriver in project swiftmq-ce by iitsoftware.

the class Exporter method execute.

public String[] execute(String[] context, Entity entity, String[] cmd) {
    if (cmd.length < 3)
        return new String[] { TreeCommands.ERROR, "Invalid command, please try '" + TreeCommands.EXPORT + " <queuename> <localdir> [-remove] [-xml] [-selector <selector>]" };
    int nMsgs = 0;
    try {
        String queueName = cmd[1];
        String localDir = cmd[2];
        boolean delete = false;
        boolean xml = false;
        String selector = null;
        if (cmd.length > 3) {
            StringBuffer selBuffer = null;
            for (int i = 3; i < cmd.length; i++) {
                if (selBuffer != null) {
                    if (selBuffer.length() > 0)
                        selBuffer.append(' ');
                    selBuffer.append(cmd[i]);
                } else if (cmd[i].equals("-delete"))
                    delete = true;
                else if (cmd[i].equals("-xml"))
                    xml = true;
                else if (cmd[i].equals("-selector"))
                    selBuffer = new StringBuffer();
                else
                    throw new Exception("Invalid option: " + cmd[i]);
            }
            if (selBuffer != null)
                selector = selBuffer.toString();
        }
        XStream xStream = null;
        File outputDir = new File(localDir);
        if (!outputDir.exists()) {
            if (!outputDir.mkdir())
                throw new Exception("Unable to create output directory: " + localDir);
        }
        if (xml) {
            xStream = new XStream(new Dom4JDriver());
            xStream.allowTypesByWildcard(new String[] { ".*" });
        }
        MessageSelector msel = null;
        if (selector != null) {
            msel = new MessageSelector(selector);
            msel.compile();
        }
        QueueReceiver receiver = ctx.queueManager.createQueueReceiver(queueName, null, msel);
        QueuePullTransaction pullTx = receiver.createTransaction(false);
        try {
            MessageEntry entry = null;
            while ((entry = pullTx.getMessage(0, msel)) != null) {
                MessageImpl msg = delete ? entry.getMessage() : copyMessage(entry.getMessage());
                msg.clearSwiftMQAllProps();
                store(queueName, xStream, outputDir, nMsgs++, msg);
                if (delete) {
                    pullTx.commit();
                    pullTx = receiver.createTransaction(false);
                }
            }
        } finally {
            try {
                pullTx.rollback();
            } catch (Exception e) {
            }
            try {
                receiver.close();
            } catch (Exception e) {
            }
        }
    } catch (Exception e) {
        return new String[] { TreeCommands.ERROR, e.getMessage() };
    }
    return new String[] { TreeCommands.INFO, nMsgs + " messages exported." };
}
Also used : XStream(com.thoughtworks.xstream.XStream) Dom4JDriver(com.thoughtworks.xstream.io.xml.Dom4JDriver) QueuePullTransaction(com.swiftmq.swiftlet.queue.QueuePullTransaction) QueueReceiver(com.swiftmq.swiftlet.queue.QueueReceiver) MessageEntry(com.swiftmq.swiftlet.queue.MessageEntry) MessageSelector(com.swiftmq.ms.MessageSelector) File(java.io.File) MessageImpl(com.swiftmq.jms.MessageImpl)

Example 4 with Dom4JDriver

use of com.thoughtworks.xstream.io.xml.Dom4JDriver in project swiftmq-ce by iitsoftware.

the class Importer method execute.

public String[] execute(String[] context, Entity entity, String[] cmd) {
    if (cmd.length < 3)
        return new String[] { TreeCommands.ERROR, "Invalid command, please try '" + TreeCommands.IMPORT + " <localdir> <queuename> [-newheader] [-delete] [-filter <regex>]" };
    int nMsgs = 0;
    try {
        String localDir = cmd[1];
        String queueName = cmd[2];
        boolean delete = false;
        boolean newId = false;
        String filter = null;
        if (cmd.length > 3) {
            StringBuffer selBuffer = null;
            for (int i = 3; i < cmd.length; i++) {
                if (selBuffer != null)
                    selBuffer.append(cmd[i]);
                else if (cmd[i].equals("-delete"))
                    delete = true;
                else if (cmd[i].equals("-newid"))
                    newId = true;
                else if (cmd[i].equals("-filter"))
                    selBuffer = new StringBuffer();
                else
                    throw new Exception("Invalid option: " + cmd[i]);
            }
            if (selBuffer != null)
                filter = selBuffer.toString();
        }
        String idPrefix = null;
        if (newId) {
            StringBuffer b = new StringBuffer(SwiftletManager.getInstance().getRouterName());
            b.append('/');
            b.append(IdGenerator.getInstance().nextId('/'));
            b.append('/');
            idPrefix = b.toString();
        }
        XStream xStream = null;
        File inputDir = new File(localDir);
        if (!inputDir.exists())
            throw new Exception("Input directory doesn't exists: " + localDir);
        xStream = new XStream(new Dom4JDriver());
        xStream.allowTypesByWildcard(new String[] { ".*" });
        QueueSender sender = ctx.queueManager.createQueueSender(queueName, null);
        try {
            MessageImpl msg = null;
            File[] files = null;
            files = inputDir.listFiles(new RegexFilter(filter) {

                public boolean accept(File file, String s) {
                    return regex == null || s.matches(regex);
                }
            });
            if (files != null && files.length > 0) {
                Arrays.sort(files);
                for (int i = 0; i < files.length; i++) {
                    msg = null;
                    if (!files[i].isDirectory()) {
                        if (files[i].getName().endsWith(".xml")) {
                            BufferedReader bufferedReader = new BufferedReader(new FileReader(files[i]));
                            msg = (MessageImpl) xStream.fromXML(bufferedReader);
                            bufferedReader.close();
                        } else if (files[i].getName().endsWith(".message")) {
                            DataStreamInputStream dis = new DataStreamInputStream(new BufferedInputStream(new FileInputStream(files[i])));
                            msg = MessageImpl.createInstance(dis.readInt());
                            msg.readContent(dis);
                            dis.close();
                        }
                        if (msg != null) {
                            if (newId) {
                                StringBuffer b = new StringBuffer(idPrefix);
                                b.append(nMsgs);
                                msg.setJMSMessageID(b.toString());
                            }
                            QueuePushTransaction pushTx = sender.createTransaction();
                            pushTx.putMessage(msg);
                            pushTx.commit();
                            if (delete)
                                files[i].delete();
                            nMsgs++;
                        }
                    }
                }
            }
        } finally {
            try {
                sender.close();
            } catch (Exception e) {
            }
        }
    } catch (Exception e) {
        return new String[] { TreeCommands.ERROR, e.getMessage() };
    }
    return new String[] { TreeCommands.INFO, nMsgs + " messages imported." };
}
Also used : QueuePushTransaction(com.swiftmq.swiftlet.queue.QueuePushTransaction) DataStreamInputStream(com.swiftmq.tools.util.DataStreamInputStream) XStream(com.thoughtworks.xstream.XStream) Dom4JDriver(com.thoughtworks.xstream.io.xml.Dom4JDriver) QueueSender(com.swiftmq.swiftlet.queue.QueueSender) MessageImpl(com.swiftmq.jms.MessageImpl)

Aggregations

XStream (com.thoughtworks.xstream.XStream)4 Dom4JDriver (com.thoughtworks.xstream.io.xml.Dom4JDriver)4 MessageImpl (com.swiftmq.jms.MessageImpl)2 TextMessageImpl (com.swiftmq.jms.TextMessageImpl)1 MessageSelector (com.swiftmq.ms.MessageSelector)1 MessageEntry (com.swiftmq.swiftlet.queue.MessageEntry)1 QueuePullTransaction (com.swiftmq.swiftlet.queue.QueuePullTransaction)1 QueuePushTransaction (com.swiftmq.swiftlet.queue.QueuePushTransaction)1 QueueReceiver (com.swiftmq.swiftlet.queue.QueueReceiver)1 QueueSender (com.swiftmq.swiftlet.queue.QueueSender)1 DataStreamInputStream (com.swiftmq.tools.util.DataStreamInputStream)1 File (java.io.File)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Map (java.util.Map)1 TextMessage (javax.jms.TextMessage)1