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.LocalService;
19 import org.fourthline.cling.model.meta.QueryStateVariableAction;
20 import org.fourthline.cling.model.meta.StateVariable;
21 import org.fourthline.cling.model.state.StateVariableAccessor;
22 import org.fourthline.cling.model.types.ErrorCode;
23
24
25
26
27
28
29 public class QueryStateVariableExecutor extends AbstractActionExecutor {
30
31 @Override
32 protected void execute(ActionInvocation<LocalService> actionInvocation, Object serviceImpl) throws Exception {
33
34
35 if (actionInvocation.getAction() instanceof QueryStateVariableAction) {
36 if (!actionInvocation.getAction().getService().isSupportsQueryStateVariables()) {
37 actionInvocation.setFailure(
38 new ActionException(ErrorCode.INVALID_ACTION, "This service does not support querying state variables")
39 );
40 } else {
41 executeQueryStateVariable(actionInvocation, serviceImpl);
42 }
43 } else {
44 throw new IllegalStateException(
45 "This class can only execute QueryStateVariableAction's, not: " + actionInvocation.getAction()
46 );
47 }
48 }
49
50 protected void executeQueryStateVariable(ActionInvocation<LocalService> actionInvocation, Object serviceImpl) throws Exception {
51
52 LocalService service = actionInvocation.getAction().getService();
53
54 String stateVariableName = actionInvocation.getInput("varName").toString();
55 StateVariable stateVariable = service.getStateVariable(stateVariableName);
56
57 if (stateVariable == null) {
58 throw new ActionException(
59 ErrorCode.ARGUMENT_VALUE_INVALID, "No state variable found: " + stateVariableName
60 );
61 }
62
63 StateVariableAccessor accessor;
64 if ((accessor = service.getAccessor(stateVariable.getName())) == null) {
65 throw new ActionException(
66 ErrorCode.ARGUMENT_VALUE_INVALID, "No accessor for state variable, can't read state: " + stateVariableName
67 );
68 }
69
70 try {
71 setOutputArgumentValue(
72 actionInvocation,
73 actionInvocation.getAction().getOutputArgument("return"),
74 accessor.read(stateVariable, serviceImpl).toString()
75 );
76 } catch (Exception ex) {
77 throw new ActionException(ErrorCode.ACTION_FAILED, ex.getMessage());
78 }
79 }
80
81 }