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.meta;
17  
18  import org.fourthline.cling.model.ModelUtil;
19  import org.fourthline.cling.model.ServiceManager;
20  import org.fourthline.cling.model.ValidationException;
21  import org.fourthline.cling.model.action.ActionExecutor;
22  import org.fourthline.cling.model.state.StateVariableAccessor;
23  import org.fourthline.cling.model.types.ServiceId;
24  import org.fourthline.cling.model.types.ServiceType;
25  
26  import java.util.HashMap;
27  import java.util.HashSet;
28  import java.util.Map;
29  import java.util.Set;
30  
31  /**
32   * The metadata of a service created on this host, by application code.
33   * <p>
34   * After instantiation {@link #setManager(org.fourthline.cling.model.ServiceManager)} must
35   * be called to bind the service metadata to the service implementation.
36   * </p>
37   *
38   * @author Christian Bauer
39   */
40  public class LocalService<T> extends Service<LocalDevice, LocalService> {
41  
42      final protected Map<Action, ActionExecutor> actionExecutors;
43      final protected Map<StateVariable, StateVariableAccessor> stateVariableAccessors;
44      final protected Set<Class> stringConvertibleTypes;
45      final protected boolean supportsQueryStateVariables;
46  
47      protected ServiceManager manager;
48  
49      public LocalService(ServiceType serviceType, ServiceId serviceId,
50                          Action[] actions, StateVariable[] stateVariables) throws ValidationException {
51          super(serviceType, serviceId, actions, stateVariables);
52          this.manager = null;
53          this.actionExecutors = new HashMap<>();
54          this.stateVariableAccessors = new HashMap<>();
55          this.stringConvertibleTypes = new HashSet<>();
56          this.supportsQueryStateVariables = true;
57      }
58  
59      public LocalService(ServiceType serviceType, ServiceId serviceId,
60                          Map<Action, ActionExecutor> actionExecutors,
61                          Map<StateVariable, StateVariableAccessor> stateVariableAccessors,
62                          Set<Class> stringConvertibleTypes,
63                          boolean supportsQueryStateVariables) throws ValidationException {
64  
65          super(serviceType, serviceId,
66                  actionExecutors.keySet().toArray(new Action[actionExecutors.size()]),
67                  stateVariableAccessors.keySet().toArray(new StateVariable[stateVariableAccessors.size()])
68          );
69  
70          this.supportsQueryStateVariables = supportsQueryStateVariables;
71          this.stringConvertibleTypes = stringConvertibleTypes;
72          this.stateVariableAccessors = stateVariableAccessors;
73          this.actionExecutors = actionExecutors;
74      }
75  
76      synchronized public void setManager(ServiceManager<T> manager) {
77          if (this.manager != null) {
78              throw new IllegalStateException("Manager is final");
79          }
80          this.manager = manager;
81      }
82  
83      synchronized public ServiceManager<T> getManager() {
84          if (manager == null) {
85              throw new IllegalStateException("Unmanaged service, no implementation instance available");
86          }
87          return manager;
88      }
89  
90      public boolean isSupportsQueryStateVariables() {
91          return supportsQueryStateVariables;
92      }
93  
94      public Set<Class> getStringConvertibleTypes() {
95          return stringConvertibleTypes;
96      }
97  
98      public boolean isStringConvertibleType(Object o) {
99          return o != null && isStringConvertibleType(o.getClass());
100     }
101 
102     public boolean isStringConvertibleType(Class clazz) {
103         return ModelUtil.isStringConvertibleType(getStringConvertibleTypes(), clazz);
104     }
105 
106     public StateVariableAccessor getAccessor(String stateVariableName) {
107         StateVariable sv;
108         return (sv = getStateVariable(stateVariableName)) != null ? getAccessor(sv) : null;
109     }
110 
111     public StateVariableAccessor getAccessor(StateVariable stateVariable) {
112         return stateVariableAccessors.get(stateVariable);
113     }
114 
115     public ActionExecutor getExecutor(String actionName) {
116         Action action;
117         return (action = getAction(actionName)) != null ? getExecutor(action) : null;
118     }
119 
120     public ActionExecutor getExecutor(Action action) {
121         return actionExecutors.get(action);
122     }
123 
124     @Override
125     public Action getQueryStateVariableAction() {
126         return getAction(QueryStateVariableAction.ACTION_NAME);
127     }
128 
129     @Override
130     public String toString() {
131         return super.toString()  + ", Manager: " + manager;
132     }
133 }