Skip to content

Commit 196992f

Browse files
committed
Fix issue with WebSocketContainer(Provider)
1 parent e335ad3 commit 196992f

File tree

9 files changed

+202
-6
lines changed

9 files changed

+202
-6
lines changed

cdt-examples/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<dependency>
2626
<groupId>com.github.kklisura.cdt</groupId>
2727
<artifactId>cdt-java-client</artifactId>
28-
<version>1.3.0</version>
28+
<version>1.3.1-SNAPSHOT</version>
2929
</dependency>
3030

3131
<dependency>

cdt-java-client/Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,10 @@ build:
2626

2727
deploy:
2828
# Deploying
29-
GPG_TTY=$$(tty) $(MVN) clean compile deploy -P release
29+
GPG_TTY=$$(tty) $(MVN) clean compile deploy -P release
30+
31+
snapshot:
32+
mvn versions:set -DnewVersion=${version}-SNAPSHOT
33+
34+
release:
35+
mvn versions:set -DnewVersion=${version}

cdt-java-client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.github.kklisura.cdt</groupId>
66
<artifactId>cdt-java-client</artifactId>
7-
<version>1.3.0</version>
7+
<version>1.3.1-SNAPSHOT</version>
88
<packaging>jar</packaging>
99

1010
<name>cdt-java-client</name>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.github.kklisura.cdt.services.factory;
2+
3+
/*-
4+
* #%L
5+
* cdt-java-client
6+
* %%
7+
* Copyright (C) 2018 Kenan Klisura
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
import javax.websocket.WebSocketContainer;
24+
25+
/**
26+
* WebSocketContainer factory creates and returns a WebSocketContainer.
27+
*
28+
* @author Kenan Klisura
29+
*/
30+
@FunctionalInterface
31+
public interface WebSocketContainerFactory {
32+
/**
33+
* Returns a WebSocket container.
34+
*
35+
* @return Web socket container.
36+
*/
37+
WebSocketContainer getWebSocketContainer();
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.github.kklisura.cdt.services.factory.impl;
2+
3+
/*-
4+
* #%L
5+
* cdt-java-client
6+
* %%
7+
* Copyright (C) 2018 Kenan Klisura
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
import com.github.kklisura.cdt.services.factory.WebSocketContainerFactory;
24+
import javax.websocket.WebSocketContainer;
25+
import org.glassfish.tyrus.client.ClientManager;
26+
import org.glassfish.tyrus.container.grizzly.client.GrizzlyClientContainer;
27+
28+
/**
29+
* Default WebSocketContainer factory creates a WebSocketContainer from GrizzlyContainerProvider.
30+
*
31+
* @author Kenan Klisura
32+
*/
33+
public class DefaultWebSocketContainerFactory implements WebSocketContainerFactory {
34+
@Override
35+
public WebSocketContainer getWebSocketContainer() {
36+
return ClientManager.createClient(GrizzlyClientContainer.class.getName());
37+
}
38+
}

cdt-java-client/src/main/java/com/github/kklisura/cdt/services/impl/WebSocketServiceImpl.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
* #L%
2121
*/
2222

23+
import static com.github.kklisura.cdt.services.utils.ConfigurationUtils.systemProperty;
24+
2325
import com.github.kklisura.cdt.services.WebSocketService;
2426
import com.github.kklisura.cdt.services.exceptions.WebSocketServiceException;
27+
import com.github.kklisura.cdt.services.factory.WebSocketContainerFactory;
28+
import com.github.kklisura.cdt.services.factory.impl.DefaultWebSocketContainerFactory;
2529
import java.io.IOException;
2630
import java.net.URI;
2731
import java.util.function.Consumer;
28-
import javax.websocket.ContainerProvider;
2932
import javax.websocket.DeploymentException;
3033
import javax.websocket.Endpoint;
3134
import javax.websocket.EndpointConfig;
@@ -41,10 +44,15 @@
4144
* @author Kenan Klisura
4245
*/
4346
public class WebSocketServiceImpl implements WebSocketService {
47+
public static final String WEB_SOCKET_CONTAINER_FACTORY_PROPERTY =
48+
"com.github.kklisura.cdt.services.config.webSocketContainerFactory";
49+
50+
private static final String DEFAULT_WEB_SOCKET_CONTAINER_FACTORY =
51+
DefaultWebSocketContainerFactory.class.getName();
52+
4453
private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketServiceImpl.class);
4554

46-
private static final WebSocketContainer WEB_SOCKET_CONTAINER =
47-
ContainerProvider.getWebSocketContainer();
55+
private static final WebSocketContainer WEB_SOCKET_CONTAINER = getWebSocketContainer();
4856

4957
private Session session;
5058

@@ -139,4 +147,39 @@ public void close() {
139147
LOGGER.error("Failed closing ws session on {}...", session.getRequestURI(), e);
140148
}
141149
}
150+
151+
/**
152+
* Returns a WebSocketContainer retrieved from class defined in system property
153+
* com.github.kklisura.cdt.services.config.webSocketContainerProvider. The default value for this
154+
* property is GrizzlyContainerProvider class FQN.
155+
*
156+
* @return WebSocketContainer.
157+
*/
158+
@SuppressWarnings("unchecked")
159+
public static WebSocketContainer getWebSocketContainer() {
160+
String containerFactoryClassName =
161+
systemProperty(WEB_SOCKET_CONTAINER_FACTORY_PROPERTY, DEFAULT_WEB_SOCKET_CONTAINER_FACTORY);
162+
if (containerFactoryClassName == null || containerFactoryClassName.isEmpty()) {
163+
throw new RuntimeException(WEB_SOCKET_CONTAINER_FACTORY_PROPERTY + " property not set");
164+
}
165+
166+
try {
167+
Class<WebSocketContainerFactory> containerFactoryClass =
168+
(Class<WebSocketContainerFactory>) Class.forName(containerFactoryClassName);
169+
170+
if (WebSocketContainerFactory.class.isAssignableFrom(containerFactoryClass)) {
171+
WebSocketContainerFactory containerFactory = containerFactoryClass.newInstance();
172+
return containerFactory.getWebSocketContainer();
173+
}
174+
175+
throw new RuntimeException(
176+
containerFactoryClassName
177+
+ " does not implement com.github.kklisura.cdt.services.factory.WebSocketContainerFactory interface.");
178+
} catch (ClassNotFoundException e) {
179+
throw new RuntimeException(containerFactoryClassName + " class not found.", e);
180+
} catch (IllegalAccessException | InstantiationException e) {
181+
throw new RuntimeException(
182+
"Could not create instance of " + containerFactoryClassName + " class");
183+
}
184+
}
142185
}

cdt-java-client/src/main/java/com/github/kklisura/cdt/services/utils/ConfigurationUtils.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,20 @@ public static long systemProperty(String name, long defaultValue) {
5555

5656
return defaultValue;
5757
}
58+
59+
/**
60+
* Returns name system property or default value if no property name exists or is invalid.
61+
*
62+
* @param name Environment name.
63+
* @param defaultValue Default value.
64+
* @return String value.
65+
*/
66+
public static String systemProperty(String name, String defaultValue) {
67+
String propertyValue = System.getProperty(name);
68+
if (propertyValue != null && !propertyValue.trim().isEmpty()) {
69+
return propertyValue.trim();
70+
}
71+
72+
return defaultValue;
73+
}
5874
}

cdt-java-client/src/test/java/com/github/kklisura/cdt/services/impl/WebSocketServiceImplTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
import static org.easymock.EasyMock.verify;
2727
import static org.junit.Assert.assertEquals;
2828
import static org.junit.Assert.assertNotNull;
29+
import static org.junit.Assert.assertNull;
2930
import static org.junit.Assert.assertTrue;
3031

3132
import com.github.kklisura.cdt.services.WebSocketService;
3233
import com.github.kklisura.cdt.services.exceptions.WebSocketServiceException;
34+
import com.github.kklisura.cdt.services.factory.WebSocketContainerFactory;
3335
import java.io.IOException;
3436
import java.net.URI;
3537
import java.util.HashMap;
@@ -42,10 +44,12 @@
4244
import javax.websocket.MessageHandler;
4345
import javax.websocket.OnMessage;
4446
import javax.websocket.Session;
47+
import javax.websocket.WebSocketContainer;
4548
import javax.websocket.server.ServerEndpoint;
4649
import org.easymock.Capture;
4750
import org.easymock.EasyMockRunner;
4851
import org.easymock.EasyMockSupport;
52+
import org.glassfish.tyrus.client.ClientManager;
4953
import org.glassfish.tyrus.server.Server;
5054
import org.junit.Test;
5155
import org.junit.runner.RunWith;
@@ -175,6 +179,36 @@ public void testAddMessageHandlerHandlerIsAdded() throws WebSocketServiceExcepti
175179
assertEquals(message, messageCapture.getValue());
176180
}
177181

182+
@Test
183+
public void testGetWebSocketContainerReturnsDefaultContainerFactory() {
184+
System.setProperty(WebSocketServiceImpl.WEB_SOCKET_CONTAINER_FACTORY_PROPERTY, "");
185+
186+
WebSocketContainer webSocketContainer;
187+
188+
webSocketContainer = WebSocketServiceImpl.getWebSocketContainer();
189+
assertTrue(webSocketContainer instanceof ClientManager);
190+
191+
System.setProperty(
192+
WebSocketServiceImpl.WEB_SOCKET_CONTAINER_FACTORY_PROPERTY,
193+
CustomWebSocketContainerFactory.class.getName());
194+
webSocketContainer = WebSocketServiceImpl.getWebSocketContainer();
195+
assertNull(webSocketContainer);
196+
}
197+
198+
@Test(expected = RuntimeException.class)
199+
public void testGetWebSocketContainerFailsOnUnknownFactoryClass() {
200+
System.setProperty(WebSocketServiceImpl.WEB_SOCKET_CONTAINER_FACTORY_PROPERTY, "non-existing");
201+
WebSocketServiceImpl.getWebSocketContainer();
202+
}
203+
204+
@Test(expected = RuntimeException.class)
205+
public void testGetWebSocketContainerFailsOnNonImplementingFactoryClass() {
206+
System.setProperty(
207+
WebSocketServiceImpl.WEB_SOCKET_CONTAINER_FACTORY_PROPERTY,
208+
CustomNonImplementingWebSocketContainerFactory.class.getName());
209+
WebSocketServiceImpl.getWebSocketContainer();
210+
}
211+
178212
private static Server startServer() {
179213
Server server;
180214
while (true) {
@@ -209,4 +243,17 @@ public void onMessage(String message, Session session) throws IOException {
209243
}
210244
}
211245
}
246+
247+
public static class CustomWebSocketContainerFactory implements WebSocketContainerFactory {
248+
@Override
249+
public WebSocketContainer getWebSocketContainer() {
250+
return null;
251+
}
252+
}
253+
254+
public static class CustomNonImplementingWebSocketContainerFactory {
255+
public WebSocketContainer getWebSocketContainer() {
256+
return null;
257+
}
258+
}
212259
}

cdt-java-client/src/test/java/com/github/kklisura/cdt/services/utils/ConfigurationUtilsTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,17 @@ public class ConfigurationUtilsTest {
3434
public void testSystemProperty() {
3535
final String propertyName = "testSystemProperty";
3636
assertEquals(10, ConfigurationUtils.systemProperty(propertyName, 10));
37+
assertEquals("10", ConfigurationUtils.systemProperty(propertyName, "10"));
3738
System.setProperty(propertyName, "invalid-value");
3839
assertEquals(10, ConfigurationUtils.systemProperty(propertyName, 10));
3940
System.setProperty(propertyName, "123");
4041
assertEquals(123, ConfigurationUtils.systemProperty(propertyName, 10));
42+
assertEquals("123", ConfigurationUtils.systemProperty(propertyName, "10"));
43+
System.setProperty(propertyName, "");
44+
assertEquals("10", ConfigurationUtils.systemProperty(propertyName, "10"));
45+
System.setProperty(propertyName, " ");
46+
assertEquals("10", ConfigurationUtils.systemProperty(propertyName, "10"));
47+
System.setProperty(propertyName, " 123 ");
48+
assertEquals("123", ConfigurationUtils.systemProperty(propertyName, "10"));
4149
}
4250
}

0 commit comments

Comments
 (0)