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.registry;
17  
18  import org.fourthline.cling.model.resource.Resource;
19  import org.fourthline.cling.model.ValidationException;
20  import org.fourthline.cling.model.meta.Device;
21  import org.fourthline.cling.model.gena.GENASubscription;
22  import org.fourthline.cling.model.types.DeviceType;
23  import org.fourthline.cling.model.types.ServiceType;
24  import org.fourthline.cling.model.types.UDN;
25  
26  import java.util.Arrays;
27  import java.util.Collection;
28  import java.util.HashSet;
29  import java.util.Set;
30  
31  /**
32   * Internal class, required by {@link RegistryImpl}.
33   *
34   * @author Christian Bauer
35   */
36  abstract class RegistryItems<D extends Device, S extends GENASubscription> {
37  
38      protected final RegistryImpl registry;
39  
40      protected final Set<RegistryItem<UDN, D>> deviceItems = new HashSet<>();
41      protected final Set<RegistryItem<String, S>> subscriptionItems = new HashSet<>();
42  
43      RegistryItems(RegistryImpl registry) {
44          this.registry = registry;
45      }
46  
47      Set<RegistryItem<UDN, D>> getDeviceItems() {
48          return deviceItems;
49      }
50  
51      Set<RegistryItem<String, S>> getSubscriptionItems() {
52          return subscriptionItems;
53      }
54  
55      abstract void add(D device);
56      abstract boolean remove(final D device);
57      abstract void removeAll();
58  
59      abstract void maintain();
60      abstract void shutdown();
61  
62      /**
63       * Returns root and embedded devices registered under the given UDN.
64       *
65       * @param udn A unique device name.
66       * @param rootOnly Set to true if only root devices (no embedded) should be searched
67       * @return Any registered root or embedded device under the given UDN, <tt>null</tt> if
68       *         no device with the given UDN has been registered.
69       */
70      D get(UDN udn, boolean rootOnly) {
71          for (RegistryItem<UDN, D> item : deviceItems) {
72              D device = item.getItem();
73              if (device.getIdentity().getUdn().equals(udn)) {
74                  return device;
75              }
76              if (!rootOnly) {
77                  D foundDevice = (D)item.getItem().findDevice(udn);
78                  if (foundDevice != null) return foundDevice;
79              }
80          }
81          return null;
82      }
83  
84      /**
85       * Returns all devices (root or embedded) with a compatible type.
86       * <p>
87       * This routine will check compatible versions, as described by the UDA.
88       * </p>
89       *
90       * @param deviceType The minimum device type required.
91       * @return Any registered root or embedded device with a compatible type.
92       */
93      Collection<D> get(DeviceType deviceType) {
94          Collection<D> devices = new HashSet<>();
95          for (RegistryItem<UDN, D> item : deviceItems) {
96              D[] d = (D[])item.getItem().findDevices(deviceType);
97              if (d != null) {
98                  devices.addAll(Arrays.asList(d));
99              }
100         }
101         return devices;
102     }
103 
104     /**
105      * Returns all devices (root or embedded) which have at least one matching service.
106      *
107      * @param serviceType The type of service to search for.
108      * @return Any registered root or embedded device with at least one matching service.
109      */
110     Collection<D> get(ServiceType serviceType) {
111         Collection<D> devices = new HashSet<>();
112         for (RegistryItem<UDN, D> item : deviceItems) {
113 
114             D[] d = (D[])item.getItem().findDevices(serviceType);
115             if (d != null) {
116                 devices.addAll(Arrays.asList(d));
117             }
118         }
119         return devices;
120     }
121 
122     Collection<D> get() {
123         Collection<D> devices = new HashSet<>();
124         for (RegistryItem<UDN, D> item : deviceItems) {
125             devices.add(item.getItem());
126         }
127         return devices;
128     }
129 
130     boolean contains(D device) {
131         return contains(device.getIdentity().getUdn());
132     }
133 
134     boolean contains(UDN udn) {
135         return deviceItems.contains(new RegistryItem<UDN, D>(udn));
136     }
137 
138     void addSubscription(S subscription) {
139 
140         RegistryItem<String, S> subscriptionItem =
141                 new RegistryItem<>(
142                         subscription.getSubscriptionId(),
143                         subscription,
144                         subscription.getActualDurationSeconds()
145                 );
146 
147         subscriptionItems.add(subscriptionItem);
148     }
149 
150     boolean updateSubscription(S subscription) {
151         if (removeSubscription(subscription)) {
152             addSubscription(subscription);
153             return true;
154         }
155         return false;
156     }
157 
158     boolean removeSubscription(S subscription) {
159         return subscriptionItems.remove(new RegistryItem<String, S>(subscription.getSubscriptionId()));
160     }
161 
162     S getSubscription(String subscriptionId) {
163         for (RegistryItem<String, S> registryItem : subscriptionItems) {
164             if (registryItem.getKey().equals(subscriptionId)) {
165                 return registryItem.getItem();
166             }
167         }
168         return null;
169     }
170 
171     Resource[] getResources(Device device) throws RegistrationException {
172         try {
173             return registry.getConfiguration().getNamespace().getResources(device);
174         } catch (ValidationException ex) {
175             throw new RegistrationException("Resource discover error: " + ex.toString(), ex);
176         }
177     }
178 }