Author: markt
Date: Tue Feb 12 10:44:15 2013
New Revision: 1445095
URL: http://svn.apache.org/r1445095
Log:
ServiceLoader implementation
Added:
tomcat/trunk/java/org/apache/tomcat/websocket/WsContainerProvider.java
(with props)
tomcat/trunk/res/META-INF/tomcat-websocket.jar/services/javax.websocket.ContainerProvider
Modified:
tomcat/trunk/java/javax/websocket/ContainerProvider.java
Modified: tomcat/trunk/java/javax/websocket/ContainerProvider.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/java/javax/websocket/ContainerProvider.java?rev=1445095&r1=1445094&r2=1445095&view=diff
==============================================================================
--- tomcat/trunk/java/javax/websocket/ContainerProvider.java (original)
+++ tomcat/trunk/java/javax/websocket/ContainerProvider.java Tue Feb 12
10:44:15 2013
@@ -16,37 +16,45 @@
*/
package javax.websocket;
+import java.util.Iterator;
+import java.util.ServiceLoader;
+
/**
- * Provides access to the implementation. This version of the API is hard-coded
- * to use the Apache Tomcat WebSocket implementation.
+ * Use the {@link ServiceLoader} mechanism to provide instances of the
WebSocket
+ * client container.
*/
public abstract class ContainerProvider {
private static final String DEFAULT_PROVIDER_CLASS_NAME =
"org.apache.tomcat.websocket.WsWebSocketContainer";
- private static final Class<WebSocketContainer> clazz;
-
- static {
- try {
- clazz = (Class<WebSocketContainer>) Class.forName(
- DEFAULT_PROVIDER_CLASS_NAME);
- } catch (ClassNotFoundException e) {
- throw new IllegalArgumentException(e);
- }
- }
-
/**
- * Create a new ClientContainer used to create outgoing WebSocket
- * connections.
+ * Create a new container used to create outgoing WebSocket connections.
*/
public static WebSocketContainer getWebSocketContainer() {
WebSocketContainer result = null;
- try {
- result = clazz.newInstance();
- } catch (InstantiationException | IllegalAccessException e) {
- throw new IllegalArgumentException(e);
+
+ ServiceLoader<ContainerProvider> serviceLoader =
+ ServiceLoader.load(ContainerProvider.class);
+ Iterator<ContainerProvider> iter = serviceLoader.iterator();
+ while (result == null && iter.hasNext()) {
+ result = iter.next().getContainer(WebSocketContainer.class);
+ }
+
+ // Fall-back. Also used by unit tests
+ if (result == null) {
+ try {
+ Class<WebSocketContainer> clazz =
+ (Class<WebSocketContainer>) Class.forName(
+ DEFAULT_PROVIDER_CLASS_NAME);
+ result = clazz.newInstance();
+ } catch (ClassNotFoundException | InstantiationException |
+ IllegalAccessException e) {
+ // No options left. Just return null.
+ }
}
return result;
}
+
+ protected abstract <T> T getContainer(Class<T> containerClass);
}
Added: tomcat/trunk/java/org/apache/tomcat/websocket/WsContainerProvider.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/WsContainerProvider.java?rev=1445095&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/WsContainerProvider.java
(added)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/WsContainerProvider.java Tue
Feb 12 10:44:15 2013
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+package org.apache.tomcat.websocket;
+
+import javax.websocket.ContainerProvider;
+
+public class WsContainerProvider extends ContainerProvider {
+
+ @Override
+ protected <T> T getContainer(Class<T> containerClass) {
+ if (containerClass.isAssignableFrom(WsWebSocketContainer.class)) {
+ @SuppressWarnings("unchecked")
+ T result = (T) new WsWebSocketContainer();
+ return result;
+ } else {
+ // Not supported
+ return null;
+ }
+ }
+}
Propchange:
tomcat/trunk/java/org/apache/tomcat/websocket/WsContainerProvider.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
tomcat/trunk/res/META-INF/tomcat-websocket.jar/services/javax.websocket.ContainerProvider
URL:
http://svn.apache.org/viewvc/tomcat/trunk/res/META-INF/tomcat-websocket.jar/services/javax.websocket.ContainerProvider?rev=1445095&view=auto
==============================================================================
---
tomcat/trunk/res/META-INF/tomcat-websocket.jar/services/javax.websocket.ContainerProvider
(added)
+++
tomcat/trunk/res/META-INF/tomcat-websocket.jar/services/javax.websocket.ContainerProvider
Tue Feb 12 10:44:15 2013
@@ -0,0 +1 @@
+org.apache.tomcat.websocket.WsContainerProvider
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]