Skip to content

Commit

Permalink
Fix issue with WebSocketContainer(Provider)
Browse files Browse the repository at this point in the history
  • Loading branch information
kklisura committed Sep 11, 2018
1 parent e335ad3 commit 196992f
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cdt-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<dependency>
<groupId>com.github.kklisura.cdt</groupId>
<artifactId>cdt-java-client</artifactId>
<version>1.3.0</version>
<version>1.3.1-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
8 changes: 7 additions & 1 deletion cdt-java-client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ build:

deploy:
# Deploying
GPG_TTY=$$(tty) $(MVN) clean compile deploy -P release
GPG_TTY=$$(tty) $(MVN) clean compile deploy -P release

snapshot:
mvn versions:set -DnewVersion=${version}-SNAPSHOT

release:
mvn versions:set -DnewVersion=${version}
2 changes: 1 addition & 1 deletion cdt-java-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.github.kklisura.cdt</groupId>
<artifactId>cdt-java-client</artifactId>
<version>1.3.0</version>
<version>1.3.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>cdt-java-client</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.kklisura.cdt.services.factory;

/*-
* #%L
* cdt-java-client
* %%
* Copyright (C) 2018 Kenan Klisura
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import javax.websocket.WebSocketContainer;

/**
* WebSocketContainer factory creates and returns a WebSocketContainer.
*
* @author Kenan Klisura
*/
@FunctionalInterface
public interface WebSocketContainerFactory {
/**
* Returns a WebSocket container.
*
* @return Web socket container.
*/
WebSocketContainer getWebSocketContainer();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.kklisura.cdt.services.factory.impl;

/*-
* #%L
* cdt-java-client
* %%
* Copyright (C) 2018 Kenan Klisura
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import com.github.kklisura.cdt.services.factory.WebSocketContainerFactory;
import javax.websocket.WebSocketContainer;
import org.glassfish.tyrus.client.ClientManager;
import org.glassfish.tyrus.container.grizzly.client.GrizzlyClientContainer;

/**
* Default WebSocketContainer factory creates a WebSocketContainer from GrizzlyContainerProvider.
*
* @author Kenan Klisura
*/
public class DefaultWebSocketContainerFactory implements WebSocketContainerFactory {
@Override
public WebSocketContainer getWebSocketContainer() {
return ClientManager.createClient(GrizzlyClientContainer.class.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
* #L%
*/

import static com.github.kklisura.cdt.services.utils.ConfigurationUtils.systemProperty;

import com.github.kklisura.cdt.services.WebSocketService;
import com.github.kklisura.cdt.services.exceptions.WebSocketServiceException;
import com.github.kklisura.cdt.services.factory.WebSocketContainerFactory;
import com.github.kklisura.cdt.services.factory.impl.DefaultWebSocketContainerFactory;
import java.io.IOException;
import java.net.URI;
import java.util.function.Consumer;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
Expand All @@ -41,10 +44,15 @@
* @author Kenan Klisura
*/
public class WebSocketServiceImpl implements WebSocketService {
public static final String WEB_SOCKET_CONTAINER_FACTORY_PROPERTY =
"com.github.kklisura.cdt.services.config.webSocketContainerFactory";

private static final String DEFAULT_WEB_SOCKET_CONTAINER_FACTORY =
DefaultWebSocketContainerFactory.class.getName();

private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketServiceImpl.class);

private static final WebSocketContainer WEB_SOCKET_CONTAINER =
ContainerProvider.getWebSocketContainer();
private static final WebSocketContainer WEB_SOCKET_CONTAINER = getWebSocketContainer();

private Session session;

Expand Down Expand Up @@ -139,4 +147,39 @@ public void close() {
LOGGER.error("Failed closing ws session on {}...", session.getRequestURI(), e);
}
}

/**
* Returns a WebSocketContainer retrieved from class defined in system property
* com.github.kklisura.cdt.services.config.webSocketContainerProvider. The default value for this
* property is GrizzlyContainerProvider class FQN.
*
* @return WebSocketContainer.
*/
@SuppressWarnings("unchecked")
public static WebSocketContainer getWebSocketContainer() {
String containerFactoryClassName =
systemProperty(WEB_SOCKET_CONTAINER_FACTORY_PROPERTY, DEFAULT_WEB_SOCKET_CONTAINER_FACTORY);
if (containerFactoryClassName == null || containerFactoryClassName.isEmpty()) {
throw new RuntimeException(WEB_SOCKET_CONTAINER_FACTORY_PROPERTY + " property not set");
}

try {
Class<WebSocketContainerFactory> containerFactoryClass =
(Class<WebSocketContainerFactory>) Class.forName(containerFactoryClassName);

if (WebSocketContainerFactory.class.isAssignableFrom(containerFactoryClass)) {
WebSocketContainerFactory containerFactory = containerFactoryClass.newInstance();
return containerFactory.getWebSocketContainer();
}

throw new RuntimeException(
containerFactoryClassName
+ " does not implement com.github.kklisura.cdt.services.factory.WebSocketContainerFactory interface.");
} catch (ClassNotFoundException e) {
throw new RuntimeException(containerFactoryClassName + " class not found.", e);
} catch (IllegalAccessException | InstantiationException e) {
throw new RuntimeException(
"Could not create instance of " + containerFactoryClassName + " class");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,20 @@ public static long systemProperty(String name, long defaultValue) {

return defaultValue;
}

/**
* Returns name system property or default value if no property name exists or is invalid.
*
* @param name Environment name.
* @param defaultValue Default value.
* @return String value.
*/
public static String systemProperty(String name, String defaultValue) {
String propertyValue = System.getProperty(name);
if (propertyValue != null && !propertyValue.trim().isEmpty()) {
return propertyValue.trim();
}

return defaultValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import com.github.kklisura.cdt.services.WebSocketService;
import com.github.kklisura.cdt.services.exceptions.WebSocketServiceException;
import com.github.kklisura.cdt.services.factory.WebSocketContainerFactory;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
Expand All @@ -42,10 +44,12 @@
import javax.websocket.MessageHandler;
import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
import javax.websocket.server.ServerEndpoint;
import org.easymock.Capture;
import org.easymock.EasyMockRunner;
import org.easymock.EasyMockSupport;
import org.glassfish.tyrus.client.ClientManager;
import org.glassfish.tyrus.server.Server;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -175,6 +179,36 @@ public void testAddMessageHandlerHandlerIsAdded() throws WebSocketServiceExcepti
assertEquals(message, messageCapture.getValue());
}

@Test
public void testGetWebSocketContainerReturnsDefaultContainerFactory() {
System.setProperty(WebSocketServiceImpl.WEB_SOCKET_CONTAINER_FACTORY_PROPERTY, "");

WebSocketContainer webSocketContainer;

webSocketContainer = WebSocketServiceImpl.getWebSocketContainer();
assertTrue(webSocketContainer instanceof ClientManager);

System.setProperty(
WebSocketServiceImpl.WEB_SOCKET_CONTAINER_FACTORY_PROPERTY,
CustomWebSocketContainerFactory.class.getName());
webSocketContainer = WebSocketServiceImpl.getWebSocketContainer();
assertNull(webSocketContainer);
}

@Test(expected = RuntimeException.class)
public void testGetWebSocketContainerFailsOnUnknownFactoryClass() {
System.setProperty(WebSocketServiceImpl.WEB_SOCKET_CONTAINER_FACTORY_PROPERTY, "non-existing");
WebSocketServiceImpl.getWebSocketContainer();
}

@Test(expected = RuntimeException.class)
public void testGetWebSocketContainerFailsOnNonImplementingFactoryClass() {
System.setProperty(
WebSocketServiceImpl.WEB_SOCKET_CONTAINER_FACTORY_PROPERTY,
CustomNonImplementingWebSocketContainerFactory.class.getName());
WebSocketServiceImpl.getWebSocketContainer();
}

private static Server startServer() {
Server server;
while (true) {
Expand Down Expand Up @@ -209,4 +243,17 @@ public void onMessage(String message, Session session) throws IOException {
}
}
}

public static class CustomWebSocketContainerFactory implements WebSocketContainerFactory {
@Override
public WebSocketContainer getWebSocketContainer() {
return null;
}
}

public static class CustomNonImplementingWebSocketContainerFactory {
public WebSocketContainer getWebSocketContainer() {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,17 @@ public class ConfigurationUtilsTest {
public void testSystemProperty() {
final String propertyName = "testSystemProperty";
assertEquals(10, ConfigurationUtils.systemProperty(propertyName, 10));
assertEquals("10", ConfigurationUtils.systemProperty(propertyName, "10"));
System.setProperty(propertyName, "invalid-value");
assertEquals(10, ConfigurationUtils.systemProperty(propertyName, 10));
System.setProperty(propertyName, "123");
assertEquals(123, ConfigurationUtils.systemProperty(propertyName, 10));
assertEquals("123", ConfigurationUtils.systemProperty(propertyName, "10"));
System.setProperty(propertyName, "");
assertEquals("10", ConfigurationUtils.systemProperty(propertyName, "10"));
System.setProperty(propertyName, " ");
assertEquals("10", ConfigurationUtils.systemProperty(propertyName, "10"));
System.setProperty(propertyName, " 123 ");
assertEquals("123", ConfigurationUtils.systemProperty(propertyName, "10"));
}
}

0 comments on commit 196992f

Please sign in to comment.