aboutsummaryrefslogtreecommitdiffstats
path: root/jrt/src/com/yahoo/jrt/Method.java
blob: 18affe35b6ae80b9b65e69e3b1d2aa49b4eef484 (plain) (blame)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jrt;


import com.yahoo.security.tls.Capability;
import com.yahoo.security.tls.CapabilitySet;

/**
 * <p>A Method encapsulates the reflective information about a single RPC
 * method.</p>
 *
 * <p>Method parameters and return values are declared with type
 * strings. A <i>type string</i> denotes the concatenation of type
 * identifiers where a string is used to represent a sequence of
 * types. For example 'ii' is the type string for two 32-bit integers,
 * while 'iss' is the type string for a single 32-bit integer followed
 * by two strings. The complete list of type identifiers can be found
 * in the {@link Value} class.</p>
 *
 * <p>The type strings associated with actual method parameters and
 * return values may only contain valid type identifiers. However,
 * when you specify the parameters accepted by- or returned from a RPC
 * method via the Method constructor, '*' may be used as the last
 * character in the type string. Ending a type string specification
 * with '*' means that additional values are optional and may have any
 * type. This feature can also be used with the {@link
 * Request#checkReturnTypes Request.checkReturnTypes} method when
 * verifying return types.</p>
 *
 * @see Supervisor#addMethod
 **/
public class Method {

    private final MethodHandler            handler;

    private String name;
    private String paramTypes;
    private String returnTypes;

    private String   desc;
    private String[] paramName;
    private String[] paramDesc;
    private String[] returnName;
    private String[] returnDesc;

    private static final RequireCapabilitiesFilter defaultFilter = RequireCapabilitiesFilter.unclassified();
    private RequestAccessFilter filter = defaultFilter;

    private static final String undocumented = "???";


    private void init(String name, String paramTypes, String returnTypes) {
        this.name = name;
        this.paramTypes = paramTypes;
        this.returnTypes = returnTypes;
        desc = undocumented;
        paramName = new String[this.paramTypes.length()];
        paramDesc = new String[this.paramTypes.length()];
        for (int i = 0; i < this.paramTypes.length(); i++) {
            paramName[i] = undocumented;
            paramDesc[i] = undocumented;
        }
        returnName = new String[this.returnTypes.length()];
        returnDesc = new String[this.returnTypes.length()];
        for (int i = 0; i < this.returnTypes.length(); i++) {
            returnName[i] = undocumented;
            returnDesc[i] = undocumented;
        }
    }

    /**
     * Create a new Method. The parameters define the name of the
     * method, the parameter types, the return value types and also
     * the handler for the method. Please refer to the {@link Method}
     * class description for an explanation of type strings.
     *
     * @param name method name
     * @param paramTypes a type string defining the parameter types
     * @param returnTypes a type string defining the return value types
     * @param handler the handler for this RPC method
     *
     * @throws MethodCreateException if the handler is <i>null</i>.
     **/
    public Method(String name, String paramTypes, String returnTypes,
                  MethodHandler handler) {

        this.handler = handler;
        if (this.handler == null) {
            throw new MethodCreateException("Handler is null");
        }
        init(name, paramTypes, returnTypes);
    }

    /**
     * Obtain the name of this method
     *
     * @return method name
     **/
    String name() {
        return name;
    }

    /**
     * Obtain the parameter types of this method
     *
     * @return parameter types
     **/
    String paramTypes() {
        return paramTypes;
    }

    /**
     * Obtain the return value types of this method
     *
     * @return return value types
     **/
    String returnTypes() {
        return returnTypes;
    }

    /**
     * Describe this method. This adds documentation that can be
     * obtained through remote reflection.
     *
     * @return this Method, to allow chaining
     **/
    public Method methodDesc(String desc) {
        this.desc = desc;
        return this;
    }

    /**
     * Obtain the method description
     *
     * @return method description
     **/
    String methodDesc() {
        return desc;
    }

    /**
     * Describe a parameter of this method. This adds documentation
     * that can be obtained through remote reflection.
     *
     * @return this Method, to allow chaining
     * @param index the parameter index
     * @param name the parameter name
     * @param desc the parameter description
     **/
    public Method paramDesc(int index, String name, String desc) {
        paramName[index] = name;
        paramDesc[index] = desc;
        return this;
    }

    public Method requestAccessFilter(RequestAccessFilter filter) { verifyNoFilterAssigned(); this.filter = filter; return this; }
    public Method requireCapabilities(Capability... capabilities) { return requireCapabilities(CapabilitySet.of(capabilities)); }
    public Method requireCapabilities(CapabilitySet capabilities) {
        verifyNoFilterAssigned();
        filter = new RequireCapabilitiesFilter(capabilities);
        return this;
    }

    private void verifyNoFilterAssigned() { if (filter != null && filter != defaultFilter) throw new IllegalStateException(); }

    public RequestAccessFilter requestAccessFilter() { return filter; }

    /**
     * Obtain the name of a parameter
     *
     * @return parameter name
     * @param index parameter index
     **/
    String paramName(int index) {
        return paramName[index];
    }

    /**
     * Obtain the description of a parameter
     *
     * @return parameter description
     * @param index parameter index
     **/
    String paramDesc(int index) {
        return paramDesc[index];
    }

    /**
     * Describe a return value of this method. This adds documentation
     * that can be obtained through remote reflection.
     *
     * @return this Method, to allow chaining
     * @param index the return value index
     * @param name the return value name
     * @param desc the return value description
     **/
    public Method returnDesc(int index, String name, String desc) {
        returnName[index] = name;
        returnDesc[index] = desc;
        return this;
    }

    /**
     * Obtain the name of a return value
     *
     * @return return value name
     * @param index return value index
     **/
    String returnName(int index) {
        return returnName[index];
    }

    /**
     * Obtain the description of a return value
     *
     * @return return value description
     * @param index return value index
     **/
    String returnDesc(int index) {
        return returnDesc[index];
    }

    /**
     * Check whether the parameters of the given request satisfies the
     * parameters of this method.
     *
     * @return true if the parameters of the given request satisfies
     * the parameters of this method
     * @param req a request
     **/
    boolean checkParameters(Request req) {
        return req.parameters().satisfies(paramTypes);
    }

    /**
     * Check whether the return values of the given request satisfies
     * the return values of this method.
     *
     * @return true if the return values of the given request satisfies
     * the return values of this method
     * @param req a request
     **/
    boolean checkReturnValues(Request req) {
        return req.returnValues().satisfies(returnTypes);
    }

    /**
     * Invoke this method. The given request holds the parameters and
     * will be given as parameter to the handler method.
     *
     * @param req the request causing this invocation
     **/
    void invoke(Request req) {
        try {
            handler.invoke(req);
        } catch (Exception e) {
            req.setError(ErrorCode.METHOD_FAILED, e.toString());
        }
    }

    @Override
    public String toString() {
        return "method " + name + "(" + paramTypes + ")" + ( returnTypes.length()>0 ? ": " + returnTypes : "");
    }

}