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.ValidationError;
19  import org.fourthline.cling.model.ValidationException;
20  import org.fourthline.cling.model.types.ServiceId;
21  import org.fourthline.cling.model.types.ServiceType;
22  
23  import java.net.URI;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  /**
28   * The metadata of a service discovered on a remote device.
29   * <p>
30   * Includes the URI's for getting the service's descriptor, calling its
31   * actions, and subscribing to events.
32   * </p>
33   * 
34   * @author Christian Bauer
35   */
36  public class RemoteService extends Service<RemoteDevice, RemoteService> {
37  
38      final private URI descriptorURI;
39      final private URI controlURI;
40      final private URI eventSubscriptionURI;
41  
42      public RemoteService(ServiceType serviceType, ServiceId serviceId,
43                           URI descriptorURI, URI controlURI, URI eventSubscriptionURI) throws ValidationException {
44          this(serviceType, serviceId, descriptorURI, controlURI, eventSubscriptionURI, null, null);
45      }
46  
47      public RemoteService(ServiceType serviceType, ServiceId serviceId,
48                           URI descriptorURI, URI controlURI, URI eventSubscriptionURI,
49                           Action<RemoteService>[] actions, StateVariable<RemoteService>[] stateVariables) throws ValidationException {
50          super(serviceType, serviceId, actions, stateVariables);
51  
52          this.descriptorURI = descriptorURI;
53          this.controlURI = controlURI;
54          this.eventSubscriptionURI = eventSubscriptionURI;
55  
56          List<ValidationError> errors = validateThis();
57          if (errors.size() > 0) {
58              throw new ValidationException("Validation of device graph failed, call getErrors() on exception", errors);
59          }
60      }
61  
62      @Override
63      public Action getQueryStateVariableAction() {
64          return new QueryStateVariableAction(this);
65      }
66  
67      public URI getDescriptorURI() {
68          return descriptorURI;
69      }
70  
71      public URI getControlURI() {
72          return controlURI;
73      }
74  
75      public URI getEventSubscriptionURI() {
76          return eventSubscriptionURI;
77      }
78  
79      public List<ValidationError> validateThis() {
80          List<ValidationError> errors = new ArrayList<>();
81  
82          if (getDescriptorURI() == null) {
83              errors.add(new ValidationError(
84                      getClass(),
85                      "descriptorURI",
86                      "Descriptor location (SCPDURL) is required"
87              ));
88          }
89  
90          if (getControlURI() == null) {
91              errors.add(new ValidationError(
92                      getClass(),
93                      "controlURI",
94                      "Control URL is required"
95              ));
96          }
97  
98          if (getEventSubscriptionURI() == null) {
99              errors.add(new ValidationError(
100                     getClass(),
101                     "eventSubscriptionURI",
102                     "Event subscription URL is required"
103             ));
104         }
105 
106         return errors;
107     }
108 
109     @Override
110     public String toString() {
111         return "(" + getClass().getSimpleName() + ") Descriptor: " + getDescriptorURI();
112     }
113 
114 }