View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
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   * Special executor for one action, the deprecated "query the value of the any state variable" action.
26   * 
27   * @author Christian Bauer
28   */
29  public class QueryStateVariableExecutor extends AbstractActionExecutor {
30      
31      @Override
32      protected void execute(ActionInvocation<LocalService> actionInvocation, Object serviceImpl) throws Exception {
33  
34          // Querying a state variable doesn't mean an actual "action" method on this instance gets invoked
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  }