Package org.apache.qpid.commands

Source Code of org.apache.qpid.commands.Commandviewcontent

/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you 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.
*
*/
/*
*
* Copyright (c) 2006 The Apache Software Foundation
*
* 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.apache.qpid.commands;

import org.apache.qpid.commands.objects.QueueObject;
import org.apache.qpid.utils.JMXinfo;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.openmbean.TabularData;
import javax.management.openmbean.CompositeData;
import java.util.*;
import java.nio.charset.Charset;

/**
* Created by IntelliJ IDEA.
* User: lahiru
* Date: Aug 10, 2008
* Time: 12:19:16 PM
* To change this template use File | Settings | File Templates.
*/
public class Commandviewcontent extends Command {
    private String object;
    private String name;
    private String vhost;
    private int number = 0;
    private QueueObject objname;
    private MBeanServerConnection mbsc;
    private String method1;
    private ObjectName queue;

    public Commandviewcontent(JMXinfo info, String name) {
        super(info, name);
        this.mbsc = info.getmbserverconnector();
        this.objname = new QueueObject(mbsc);
        this.method1 = "viewMessageContent";

    }

    public void viewcontent() {
        Set set = null;
        Object temp[] = {null};
        objname.setQueryString(this.object, this.name, this.vhost);
        set = objname.returnObjects();
        String temp_header = "", header = "", message_data = "", encoding = null;

        if (objname.getSet().size() != 0) {
            Iterator it = set.iterator();
            this.queue = (ObjectName) it.next();
            try {
                if (objname.getmessagecount(this.queue) == 0) {
                    echo("Selected Queue doesn't contain any messages");
                    return;
                }
                if (this.number == 0) {
                    echo("You haven't selected a MessageId Please use -id and give a message id");
                    echo("Or run view command with same arguemnts to view messageId list for the queue");
                }

//                if(objname.getmessagecount(this.queue) < this.number)
//                {
//                    echo("Given number is Greater than the Queue Depth");
//                    return;
//                }
                else {
                    Object[] params = {this.number};
                    String[] signature = {new String("long")};
                    CompositeData data = (CompositeData) this.mbsc.invoke(queue, this.method1, params, signature);
                    List<String> itemNames = new
                            ArrayList<String>(data.getCompositeType().keySet());
                    for (int j = 0; j < itemNames.size(); j++) {
                        temp_header = "";
                        temp_header = itemNames.get(j);
                        while (temp_header.length() < 15)
                            temp_header = " " + temp_header;

                        header = header + temp_header + "|";
                    }
                    echo(header);
                    encoding = String.valueOf(data.get(itemNames.get(2)));   // set the encoding at the beginning because encoding comes later in the loop
                    if (encoding == null || encoding.length() == 0) {
                        encoding = Charset.defaultCharset().name();

                    }
                    for (int j = 0; j < itemNames.size(); j++) {
                        temp_header = "";
                        if (j != 1) {
                            temp_header = String.valueOf(data.get(itemNames.get(j)));
                            while (temp_header.length() < 15)
                                temp_header = " " + temp_header;
                        } else {
                            Byte[] arrayItems = (Byte[]) data.get(itemNames.get(j));
                            byte[] byteArray = new byte[arrayItems.length];
                            for (int i = 0; i < arrayItems.length; i++) {
                                byteArray[i] = arrayItems[i];
                                temp_header = new String(byteArray, encoding);
                                while (temp_header.length() < 15)
                                    temp_header = " " + temp_header;
                            }
                        }
                        message_data = message_data + temp_header + "|";


                    }
                    echo(message_data);
//                    Object keys[] = data.keySet().toArray();
//                    CompositeData comdata = data.get(keys);
//                    for(int i=0;i < keys.length;i++)
//                    {
//                        System.out.println(keys[i].toString());
//                        CompositeData cdata = data.get(temp);
//                        System.out.println(cdata.toString());
//                        temp[0] = null;
//                    }
//                    TabularType tabular = data.getTabularType();
////                    System.out.println(tabular.toString());
//                   List info = tabular.getIndexNames();
//                   Iterator it1 = info.iterator();
//
//                    do{
//                            String temp1 = (String)it1.next();
//                        System.out.println(temp1);
//                    }while(it1.hasNext());

                }
            } catch (Exception ex) {
                echo("Given MessageId is invalid, There's no message with the given messageId");
                ex.printStackTrace();
                return;
            }

        } else {
            if (isname()) {

                echo("The Queue you have specified is not in the current broker");
                echo("");
            } else {
                printusage();
            }
        }
    }

    public void execute() {
        /* In here you it's easy to handle any number of otpions which are going to add with the list command which works
        with main option object or o
         */

        if (checkoptionsetting("object") || checkoptionsetting("o")) {
            String object = optionchecker("object");
            if (object == null) {
                object = optionchecker("o");
            }
            if (object.compareToIgnoreCase("queue") == 0)
                setobject(object);
            else {
                unrecognizeoption();
                echo("This command is only applicable for delete command so please start with queue");
            }
            if (checkoptionsetting("name") || checkoptionsetting("n")) {
                String name = optionchecker("name");
                if (name == null)
                    name = optionchecker("n");

                setname(name);
            }
            if (checkoptionsetting("virtualhost") || checkoptionsetting("v")) {
                String vhost = optionchecker("virtualhost");
                if (vhost == null)
                    vhost = optionchecker("v");
                setvhost(vhost);
            }
            if (checkoptionsetting("messageid") || checkoptionsetting("id")) {
                String number = optionchecker("id");
                if (number == null)
                    number = optionchecker("id");

                setnumber(removeSpaces(number));
            }
            this.viewcontent();
        } else if (checkoptionsetting("h") || checkoptionsetting("help"))
            printusage();
        else
            unrecognizeoption();
    }

    public void printusage() {
        echo("");
        echo("Usage:viewcontent [OPTION] ... [OBJECT TYPE]...\n");
        echo("view the information about given number of messages from the given queue object\n");
        echo("To specify the desired queue you have to give the virtualhost name and the queue name with following commands\n");
        echo("Where possible options include:\n");
        echo("        -v      --virtualhost Give the virtuallhost name of the desired queue");
        echo("        -n      --name        Give the queue name of the desired queue you want to view messages");
        echo("        -id                   Give the messageId of the required message you want to view the content");
        echo("        -h      --help        Display the help and back to the qpid-cli prompt\n");

    }

    private void setobject(String object) {
        this.object = object;
    }

    private void setname(String name) {
        this.name = name;
    }

    private boolean isname() {
        if (this.name == null)
            return false;

        else
            return true;
    }

    public void setvhost(String vhost) {
        this.vhost = vhost;
    }

    public String getvhost() {
        return this.vhost;
    }

    private void setnumber(String number) {
        Integer i = new Integer(number);
        this.number = i.intValue();
    }

    private static String removeSpaces(String s) {
        StringTokenizer st = new StringTokenizer(s, " ", false);
        String t = "";
        while (st.hasMoreElements()) t += st.nextElement();
        return t;
    }

    public String getname() {
        return this.name;
    }

    public String getobject() {
        return this.object;
    }

    public int getnumber() {
        return this.number;
    }


}
TOP

Related Classes of org.apache.qpid.commands.Commandviewcontent

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.