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;
17  
18  import org.fourthline.cling.binding.xml.DeviceDescriptorBinder;
19  import org.fourthline.cling.binding.xml.ServiceDescriptorBinder;
20  import org.fourthline.cling.binding.xml.UDA10DeviceDescriptorBinderImpl;
21  import org.fourthline.cling.binding.xml.UDA10ServiceDescriptorBinderImpl;
22  import org.fourthline.cling.model.ModelUtil;
23  import org.fourthline.cling.model.Namespace;
24  import org.fourthline.cling.model.message.UpnpHeaders;
25  import org.fourthline.cling.model.meta.RemoteDeviceIdentity;
26  import org.fourthline.cling.model.meta.RemoteService;
27  import org.fourthline.cling.model.types.ServiceType;
28  import org.fourthline.cling.transport.impl.DatagramIOConfigurationImpl;
29  import org.fourthline.cling.transport.impl.DatagramIOImpl;
30  import org.fourthline.cling.transport.impl.GENAEventProcessorImpl;
31  import org.fourthline.cling.transport.impl.MulticastReceiverConfigurationImpl;
32  import org.fourthline.cling.transport.impl.MulticastReceiverImpl;
33  import org.fourthline.cling.transport.impl.NetworkAddressFactoryImpl;
34  import org.fourthline.cling.transport.impl.SOAPActionProcessorImpl;
35  import org.fourthline.cling.transport.impl.StreamClientConfigurationImpl;
36  import org.fourthline.cling.transport.impl.StreamClientImpl;
37  import org.fourthline.cling.transport.impl.StreamServerConfigurationImpl;
38  import org.fourthline.cling.transport.impl.StreamServerImpl;
39  import org.fourthline.cling.transport.spi.DatagramIO;
40  import org.fourthline.cling.transport.spi.DatagramProcessor;
41  import org.fourthline.cling.transport.spi.GENAEventProcessor;
42  import org.fourthline.cling.transport.spi.MulticastReceiver;
43  import org.fourthline.cling.transport.spi.NetworkAddressFactory;
44  import org.fourthline.cling.transport.spi.SOAPActionProcessor;
45  import org.fourthline.cling.transport.spi.StreamClient;
46  import org.fourthline.cling.transport.spi.StreamServer;
47  
48  import javax.annotation.PostConstruct;
49  import javax.enterprise.context.ApplicationScoped;
50  import javax.inject.Inject;
51  import java.util.concurrent.Executor;
52  import java.util.concurrent.ExecutorService;
53  import java.util.logging.Logger;
54  
55  /**
56   * Adapter for CDI environments.
57   *
58   * @author Christian Bauer
59   */
60  @ApplicationScoped
61  public class ManagedUpnpServiceConfiguration implements UpnpServiceConfiguration {
62  
63      private static Logger log = Logger.getLogger(DefaultUpnpServiceConfiguration.class.getName());
64  
65      // TODO: All of these fields should be injected so users can provide values through CDI
66  
67      private int streamListenPort;
68  
69      private ExecutorService defaultExecutorService;
70  
71      @Inject
72      protected DatagramProcessor datagramProcessor;
73  
74      private SOAPActionProcessor soapActionProcessor;
75      private GENAEventProcessor genaEventProcessor;
76  
77      private DeviceDescriptorBinder deviceDescriptorBinderUDA10;
78      private ServiceDescriptorBinder serviceDescriptorBinderUDA10;
79  
80      private Namespace namespace;
81  
82      @PostConstruct
83      public void init() {
84  
85          if (ModelUtil.ANDROID_RUNTIME) {
86              throw new Error("Unsupported runtime environment, use org.fourthline.cling.android.AndroidUpnpServiceConfiguration");
87          }
88  
89          this.streamListenPort = NetworkAddressFactoryImpl.DEFAULT_TCP_HTTP_LISTEN_PORT;
90  
91          defaultExecutorService = createDefaultExecutorService();
92  
93          soapActionProcessor = createSOAPActionProcessor();
94          genaEventProcessor = createGENAEventProcessor();
95  
96          deviceDescriptorBinderUDA10 = createDeviceDescriptorBinderUDA10();
97          serviceDescriptorBinderUDA10 = createServiceDescriptorBinderUDA10();
98  
99          namespace = createNamespace();
100     }
101 
102     public DatagramProcessor getDatagramProcessor() {
103         return datagramProcessor;
104     }
105 
106     public SOAPActionProcessor getSoapActionProcessor() {
107         return soapActionProcessor;
108     }
109 
110     public GENAEventProcessor getGenaEventProcessor() {
111         return genaEventProcessor;
112     }
113 
114     public StreamClient createStreamClient() {
115         return new StreamClientImpl(
116             new StreamClientConfigurationImpl(
117                 getSyncProtocolExecutorService()
118             )
119         );
120     }
121 
122     public MulticastReceiver createMulticastReceiver(NetworkAddressFactory networkAddressFactory) {
123         return new MulticastReceiverImpl(
124                 new MulticastReceiverConfigurationImpl(
125                         networkAddressFactory.getMulticastGroup(),
126                         networkAddressFactory.getMulticastPort()
127                 )
128         );
129     }
130 
131     public DatagramIO createDatagramIO(NetworkAddressFactory networkAddressFactory) {
132         return new DatagramIOImpl(new DatagramIOConfigurationImpl());
133     }
134 
135     public StreamServer createStreamServer(NetworkAddressFactory networkAddressFactory) {
136         return new StreamServerImpl(
137                 new StreamServerConfigurationImpl(
138                         networkAddressFactory.getStreamListenPort()
139                 )
140         );
141     }
142 
143     public Executor getMulticastReceiverExecutor() {
144         return getDefaultExecutorService();
145     }
146 
147     public Executor getDatagramIOExecutor() {
148         return getDefaultExecutorService();
149     }
150 
151     public ExecutorService getStreamServerExecutorService() {
152         return getDefaultExecutorService();
153     }
154 
155     public DeviceDescriptorBinder getDeviceDescriptorBinderUDA10() {
156         return deviceDescriptorBinderUDA10;
157     }
158 
159     public ServiceDescriptorBinder getServiceDescriptorBinderUDA10() {
160         return serviceDescriptorBinderUDA10;
161     }
162 
163     public ServiceType[] getExclusiveServiceTypes() {
164         return new ServiceType[0];
165     }
166 
167     /**
168      * @return Defaults to <code>false</code>.
169      */
170 	public boolean isReceivedSubscriptionTimeoutIgnored() {
171 		return false;
172 	}
173 
174     public UpnpHeaders getDescriptorRetrievalHeaders(RemoteDeviceIdentity identity) {
175         return null;
176     }
177 
178     public UpnpHeaders getEventSubscriptionHeaders(RemoteService service) {
179         return null;
180     }
181 
182     /**
183      * @return Defaults to 1000 milliseconds.
184      */
185     public int getRegistryMaintenanceIntervalMillis() {
186         return 1000;
187     }
188 
189     /**
190      * @return Defaults to zero, disabling ALIVE flooding.
191      */
192     public int getAliveIntervalMillis() {
193     	return 0;
194     }
195 
196     public Integer getRemoteDeviceMaxAgeSeconds() {
197         return null;
198     }
199 
200     public Executor getAsyncProtocolExecutor() {
201         return getDefaultExecutorService();
202     }
203 
204     public ExecutorService getSyncProtocolExecutorService() {
205         return getDefaultExecutorService();
206     }
207 
208     public Namespace getNamespace() {
209         return namespace;
210     }
211 
212     public Executor getRegistryMaintainerExecutor() {
213         return getDefaultExecutorService();
214     }
215 
216     public Executor getRegistryListenerExecutor() {
217         return getDefaultExecutorService();
218     }
219 
220     public NetworkAddressFactory createNetworkAddressFactory() {
221         return createNetworkAddressFactory(streamListenPort);
222     }
223 
224     public void shutdown() {
225         log.fine("Shutting down default executor service");
226         getDefaultExecutorService().shutdownNow();
227     }
228 
229     protected NetworkAddressFactory createNetworkAddressFactory(int streamListenPort) {
230         return new NetworkAddressFactoryImpl(streamListenPort);
231     }
232 
233     protected SOAPActionProcessor createSOAPActionProcessor() {
234         return new SOAPActionProcessorImpl();
235     }
236 
237     protected GENAEventProcessor createGENAEventProcessor() {
238         return new GENAEventProcessorImpl();
239     }
240 
241     protected DeviceDescriptorBinder createDeviceDescriptorBinderUDA10() {
242         return new UDA10DeviceDescriptorBinderImpl();
243     }
244 
245     protected ServiceDescriptorBinder createServiceDescriptorBinderUDA10() {
246         return new UDA10ServiceDescriptorBinderImpl();
247     }
248 
249     protected Namespace createNamespace() {
250         return new Namespace();
251     }
252 
253     protected ExecutorService getDefaultExecutorService() {
254         return defaultExecutorService;
255     }
256 
257     protected ExecutorService createDefaultExecutorService() {
258         return new DefaultUpnpServiceConfiguration.ClingExecutor();
259     }
260 }