1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.fourthline.cling.mock;
17
18 import org.fourthline.cling.DefaultUpnpServiceConfiguration;
19 import org.fourthline.cling.transport.impl.NetworkAddressFactoryImpl;
20 import org.fourthline.cling.transport.spi.NetworkAddressFactory;
21
22 import javax.enterprise.inject.Alternative;
23 import java.net.Inet4Address;
24 import java.net.InetAddress;
25 import java.net.NetworkInterface;
26 import java.util.List;
27 import java.util.concurrent.AbstractExecutorService;
28 import java.util.concurrent.Executor;
29 import java.util.concurrent.ExecutorService;
30 import java.util.concurrent.TimeUnit;
31
32
33
34
35 @Alternative
36 public class MockUpnpServiceConfiguration extends DefaultUpnpServiceConfiguration {
37
38 final protected boolean maintainsRegistry;
39 final protected boolean multiThreaded;
40
41
42
43
44 public MockUpnpServiceConfiguration() {
45 this(false, false);
46 }
47
48
49
50
51 public MockUpnpServiceConfiguration(boolean maintainsRegistry) {
52 this(maintainsRegistry, false);
53 }
54
55 public MockUpnpServiceConfiguration(boolean maintainsRegistry, boolean multiThreaded) {
56 super(false);
57 this.maintainsRegistry = maintainsRegistry;
58 this.multiThreaded = multiThreaded;
59 }
60
61 public boolean isMaintainsRegistry() {
62 return maintainsRegistry;
63 }
64
65 public boolean isMultiThreaded() {
66 return multiThreaded;
67 }
68
69 @Override
70 protected NetworkAddressFactory createNetworkAddressFactory(int streamListenPort) {
71
72 return new NetworkAddressFactoryImpl(streamListenPort) {
73 @Override
74 protected boolean isUsableNetworkInterface(NetworkInterface iface) throws Exception {
75 return (iface.isLoopback());
76 }
77
78 @Override
79 protected boolean isUsableAddress(NetworkInterface networkInterface, InetAddress address) {
80 return (address.isLoopbackAddress() && address instanceof Inet4Address);
81 }
82
83 };
84 }
85
86 @Override
87 public Executor getRegistryMaintainerExecutor() {
88 if (isMaintainsRegistry()) {
89 return new Executor() {
90 public void execute(Runnable runnable) {
91 new Thread(runnable).start();
92 }
93 };
94 }
95 return getDefaultExecutorService();
96 }
97
98 @Override
99 protected ExecutorService getDefaultExecutorService() {
100 if (isMultiThreaded()) {
101 return super.getDefaultExecutorService();
102 }
103 return new AbstractExecutorService() {
104
105 boolean terminated;
106
107 public void shutdown() {
108 terminated = true;
109 }
110
111 public List<Runnable> shutdownNow() {
112 shutdown();
113 return null;
114 }
115
116 public boolean isShutdown() {
117 return terminated;
118 }
119
120 public boolean isTerminated() {
121 return terminated;
122 }
123
124 public boolean awaitTermination(long l, TimeUnit timeUnit) throws InterruptedException {
125 shutdown();
126 return terminated;
127 }
128
129 public void execute(Runnable runnable) {
130 runnable.run();
131 }
132 };
133 }
134
135 }