1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.fourthline.cling.model.action;
17
18 import org.fourthline.cling.model.meta.Action;
19 import org.fourthline.cling.model.meta.ActionArgument;
20 import org.fourthline.cling.model.meta.Service;
21 import org.fourthline.cling.model.profile.ClientInfo;
22 import org.fourthline.cling.model.types.InvalidValueException;
23
24 import java.util.Collections;
25 import java.util.LinkedHashMap;
26 import java.util.Map;
27
28
29
30
31
32
33 public class ActionInvocation<S extends Service> {
34
35 final protected Action<S> action;
36 final protected ClientInfo clientInfo;
37
38
39
40 protected Map<String, ActionArgumentValue<S>> input = new LinkedHashMap<>();
41 protected Map<String, ActionArgumentValue<S>> output = new LinkedHashMap<>();
42
43 protected ActionException failure = null;
44
45 public ActionInvocation(Action<S> action) {
46 this(action, null, null, null);
47 }
48
49 public ActionInvocation(Action<S> action,
50 ClientInfo clientInfo) {
51 this(action, null, null, clientInfo);
52 }
53
54 public ActionInvocation(Action<S> action,
55 ActionArgumentValue<S>[] input) {
56 this(action, input, null, null);
57 }
58
59 public ActionInvocation(Action<S> action,
60 ActionArgumentValue<S>[] input,
61 ClientInfo clientInfo) {
62 this(action, input, null, clientInfo);
63 }
64
65 public ActionInvocation(Action<S> action,
66 ActionArgumentValue<S>[] input,
67 ActionArgumentValue<S>[] output) {
68 this(action, input, output, null);
69 }
70
71 public ActionInvocation(Action<S> action,
72 ActionArgumentValue<S>[] input,
73 ActionArgumentValue<S>[] output,
74 ClientInfo clientInfo) {
75 if (action == null) {
76 throw new IllegalArgumentException("Action can not be null");
77 }
78 this.action = action;
79
80 setInput(input);
81 setOutput(output);
82
83 this.clientInfo = clientInfo;
84 }
85
86 public ActionInvocation(ActionException failure) {
87 this.action = null;
88 this.input = null;
89 this.output = null;
90 this.failure = failure;
91 this.clientInfo = null;
92 }
93
94 public Action<S> getAction() {
95 return action;
96 }
97
98 public ActionArgumentValue<S>[] getInput() {
99 return input.values().toArray(new ActionArgumentValue[input.size()]);
100 }
101
102 public ActionArgumentValue<S> getInput(String argumentName) {
103 return getInput(getInputArgument(argumentName));
104 }
105
106 public ActionArgumentValue<S> getInput(ActionArgument<S> argument) {
107 return input.get(argument.getName());
108 }
109
110 public Map<String, ActionArgumentValue<S>> getInputMap() {
111 return Collections.unmodifiableMap(input);
112 }
113
114 public ActionArgumentValue<S>[] getOutput() {
115 return output.values().toArray(new ActionArgumentValue[output.size()]);
116 }
117
118 public ActionArgumentValue<S> getOutput(String argumentName) {
119 return getOutput(getOutputArgument(argumentName));
120 }
121
122 public Map<String, ActionArgumentValue<S>> getOutputMap() {
123 return Collections.unmodifiableMap(output);
124 }
125
126 public ActionArgumentValue<S> getOutput(ActionArgument<S> argument) {
127 return output.get(argument.getName());
128 }
129
130 public void setInput(String argumentName, Object value) throws InvalidValueException {
131 setInput(new ActionArgumentValue(getInputArgument(argumentName), value));
132 }
133
134 public void setInput(ActionArgumentValue<S> value) {
135 input.put(value.getArgument().getName(), value);
136 }
137
138 public void setInput(ActionArgumentValue<S>[] input) {
139 if (input == null) return;
140 for (ActionArgumentValue<S> argumentValue : input) {
141 this.input.put(argumentValue.getArgument().getName(), argumentValue);
142 }
143 }
144
145 public void setOutput(String argumentName, Object value) throws InvalidValueException {
146 setOutput(new ActionArgumentValue(getOutputArgument(argumentName), value));
147 }
148
149 public void setOutput(ActionArgumentValue<S> value){
150 output.put(value.getArgument().getName(), value);
151 }
152
153 public void setOutput(ActionArgumentValue<S>[] output) {
154 if (output == null) return;
155 for (ActionArgumentValue<S> argumentValue : output) {
156 this.output.put(argumentValue.getArgument().getName(), argumentValue);
157 }
158 }
159
160 protected ActionArgument<S> getInputArgument(String name) {
161 ActionArgument<S> argument = getAction().getInputArgument(name);
162 if (argument == null) throw new IllegalArgumentException("Argument not found: " + name);
163 return argument;
164 }
165
166 protected ActionArgument<S> getOutputArgument(String name) {
167 ActionArgument<S> argument = getAction().getOutputArgument(name);
168 if (argument == null) throw new IllegalArgumentException("Argument not found: " + name);
169 return argument;
170 }
171
172
173
174
175 public ActionException getFailure() {
176 return failure;
177 }
178
179 public void setFailure(ActionException failure) {
180 this.failure = failure;
181 }
182
183
184
185
186 public ClientInfo getClientInfo() {
187 return clientInfo;
188 }
189
190 @Override
191 public String toString() {
192 return "(" + getClass().getSimpleName() + ") " + getAction();
193 }
194 }