summaryrefslogtreecommitdiff
path: root/qpid/tools/src/java/qpid-qmf2/src/main/java/org/apache/qpid/qmf2/console/MethodResult.java
blob: 8822e02752c952bffffd52c01bc2e2726a160b25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 *
 * 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.
 *
 */
package org.apache.qpid.qmf2.console;

import java.util.Map;

// QMF2 Imports
import org.apache.qpid.qmf2.common.QmfData;
import org.apache.qpid.qmf2.common.QmfException;

/**
 * The value(s) returned to the Console when the method call completes are represented by the MethodResult class.
 * <p>
 * The MethodResult class indicates whether the method call succeeded or not, and, on success, provides access to all
 * data returned by the method call.
 * <p>
 * Returned data is provided in QmfData map indexed by the name of the parameter. The QmfData map contains only those 
 * parameters that are classified as "output" by the SchemaMethod.
 * <p>
 * Should a method call result in a failure, this failure is indicated by the presence of an error object in
 * the MethodResult. This object is represented by a QmfException object, which contains a description of the
 * reason for the failure. There are no returned parameters when a method call fails.
 * <p>
 * Although not part of the QMF2 API I've made MethodResult extend QmfData so we can directly access the argument
 * or exception values of the MethodResult object, which tends to neaten up client code.
 *
 * @author Fraser Adams
 */
public final class MethodResult extends QmfData
{
    private QmfData _arguments = null;
    private QmfData _exception = null;

    /**
     * The main constructor, taking a java.util.Map as a parameter. In essence it "deserialises" its state from the Map.
     *
     * @param m the map used to construct the MethodResult.
     */
    @SuppressWarnings("unchecked")
    public MethodResult(final Map m)
    {
        super(m);
        _exception = this;
        String opcode = (m == null || !m.containsKey("qmf.opcode")) ? "none" : (String)m.get("qmf.opcode");
        if (m.size() == 0)
        { // Valid response from a method returning void
            _values = m;
            _arguments = this;
            _exception = null;
        }
        else if (opcode.equals("_method_response"))
        {
            Map args = (Map)m.get("_arguments");
            if (args != null)
            {
                _values = args;
                _arguments = this;
                _exception = null;
            }
        }
        else if (!opcode.equals("_exception"))
        {
            setValue("error_text", "Invalid response received, opcode: " + opcode);
        }
    }

    /**
     * Return true if the method call executed without error.
     * @return true if the method call executed without error.
     */
    public boolean succeeded()
    {
        return (_exception == null);
    }

    /**
     * Return the QmfData error object if method fails, else null.
     * @return the QmfData error object if method fails, else null.
     */
    public QmfData getException()
    {
        return _exception;
    }

    /**
     * Return a map of "name"=&lt;value&gt; pairs of all returned arguments.
     * @return a map of "name"=&lt;value&gt; pairs of all returned arguments.
     */
    public QmfData getArguments()
    {
        return _arguments;
    }

    /**
     * Return value of argument named "name".
     * @return value of argument named "name".
     */
    public Object getArgument(final String name)
    {
        if (_arguments == this)
        {
            return getValue(name);
        }
        return null;
    }

    /**
     * Return a QmfException object.
     * @return a QmfException object.
     * <p>
     * If the QmfData exception object contains a String property "error_text" or "message" return a QmfException object 
     * who's message is set to this value else return null;
     */
    public QmfException getQmfException()
    {
        if (_exception == this)
        {
            if (hasValue("error_text"))
            {
                return new QmfException(getStringValue("error_text"));
            }

            if (hasValue("message"))
            {
                return new QmfException(getStringValue("message"));
            }
        }
        return null;
    }
}