Author: markt Date: Thu Nov 29 19:28:44 2012 New Revision: 1415330 URL: http://svn.apache.org/viewvc?rev=1415330&view=rev Log: WebSocket 1.0 implementation part 5 of many Extract the POJO method mapping. Cache it and start to think about caching the path parameter mapping too.
Added: tomcat/trunk/java/org/apache/tomcat/websocket/PojoMethodMapping.java Modified: tomcat/trunk/java/org/apache/tomcat/websocket/ServerContainerImpl.java tomcat/trunk/java/org/apache/tomcat/websocket/WsEndpointPojo.java Added: tomcat/trunk/java/org/apache/tomcat/websocket/PojoMethodMapping.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/PojoMethodMapping.java?rev=1415330&view=auto ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/websocket/PojoMethodMapping.java (added) +++ tomcat/trunk/java/org/apache/tomcat/websocket/PojoMethodMapping.java Thu Nov 29 19:28:44 2012 @@ -0,0 +1,79 @@ +/* + * 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 java.lang.reflect.Method; + +import javax.websocket.WebSocketClose; +import javax.websocket.WebSocketError; +import javax.websocket.WebSocketOpen; + +public class PojoMethodMapping { + + private final Method onOpen; + private final Method onClose; + private final Method onError; + + public PojoMethodMapping(Class<?> clazzPojo, String path) { + Method open = null; + Method close = null; + Method error = null; + Method[] methods = clazzPojo.getMethods(); + for (int i = 0; i < methods.length; i++) { + if (open == null && + methods[i].getAnnotation(WebSocketOpen.class) != null) { + open = methods[i]; + } else if (close == null && + methods[i].getAnnotation(WebSocketClose.class) != null) { + close = methods[i]; + } else if (error == null && + methods[i].getAnnotation(WebSocketError.class) != null) { + error = methods[i]; + } + } + this.onOpen = open; + this.onClose = close; + this.onError = error; + } + + public Method getOnOpen() { + return onOpen; + } + + public Object[] getOnOpenArgs(String path) { + // TODO Auto-generated method stub + return null; + } + + public Method getOnClose() { + return onClose; + } + + public Object[] getOnCloseArgs(String path) { + // TODO Auto-generated method stub + return null; + } + + public Method getOnError() { + return onError; + } + + public Object[] getOnErrorArgs(String path) { + // TODO Auto-generated method stub + return null; + } +} Modified: tomcat/trunk/java/org/apache/tomcat/websocket/ServerContainerImpl.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/ServerContainerImpl.java?rev=1415330&r1=1415329&r2=1415330&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/websocket/ServerContainerImpl.java (original) +++ tomcat/trunk/java/org/apache/tomcat/websocket/ServerContainerImpl.java Thu Nov 29 19:28:44 2012 @@ -78,6 +78,9 @@ public class ServerContainerImpl extends private Map<String, Class<?>> pojoMap = new ConcurrentHashMap<>(); + private Map<Class<?>, PojoMethodMapping> pojoMethodMap = + new ConcurrentHashMap<>(); + private ServerContainerImpl() { // Hide default constructor @@ -146,6 +149,7 @@ public class ServerContainerImpl extends } pojoMap.put(path.substring(0, path.length() - 2), pojo); + pojoMethodMap.put(pojo, new PojoMethodMapping(pojo, path)); addWsServletMapping(path); } @@ -170,10 +174,15 @@ public class ServerContainerImpl extends return ep; } + // TODO Need to cache the pojoMethodMapping too Class<?> clazzPojo = pojoMap.get(servletPath); if (clazzPojo != null) { - Endpoint ep = new WsEndpointPojo(clazzPojo, servletPath); - return ep; + PojoMethodMapping mapping = pojoMethodMap.get(clazzPojo); + if (mapping != null) { + Endpoint ep = new WsEndpointPojo(clazzPojo, + mapping, servletPath); + return ep; + } } throw new IllegalStateException( Modified: tomcat/trunk/java/org/apache/tomcat/websocket/WsEndpointPojo.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/WsEndpointPojo.java?rev=1415330&r1=1415329&r2=1415330&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/websocket/WsEndpointPojo.java (original) +++ tomcat/trunk/java/org/apache/tomcat/websocket/WsEndpointPojo.java Thu Nov 29 19:28:44 2012 @@ -24,19 +24,20 @@ import javax.websocket.DefaultServerConf import javax.websocket.Endpoint; import javax.websocket.EndpointConfiguration; import javax.websocket.Session; -import javax.websocket.WebSocketClose; -import javax.websocket.WebSocketError; -import javax.websocket.WebSocketOpen; public class WsEndpointPojo extends Endpoint { private final Object pojo; private final EndpointConfiguration config; private final Method onOpen; + private final Object[] onOpenArgs; private final Method onClose; + private final Object[] onCloseArgs; private final Method onError; + private final Object[] onErrorArgs; - public WsEndpointPojo(Class<?> clazzPojo, String path) + public WsEndpointPojo(Class<?> clazzPojo, PojoMethodMapping methodMapping, + String path) throws InstantiationException, IllegalAccessException { this.pojo = clazzPojo.newInstance(); this.config = new DefaultServerConfiguration(path) { @@ -45,29 +46,28 @@ public class WsEndpointPojo extends Endp public boolean checkOrigin(String originHeaderValue) { return true; } - }; - // TODO - Don't want to have to do this on every connection - Method open = null; - Method close = null; - Method error = null; - Method[] methods = clazzPojo.getMethods(); - for (int i = 0; i < methods.length; i++) { - if (open == null && - methods[i].getAnnotation(WebSocketOpen.class) != null) { - open = methods[i]; - } else if (close == null && - methods[i].getAnnotation(WebSocketClose.class) != null) { - close = methods[i]; - } else if (error == null && - methods[i].getAnnotation(WebSocketError.class) != null) { - error = methods[i]; - } + onOpen = methodMapping.getOnOpen(); + if (onOpen == null) { + onOpenArgs = null; + } else { + onOpenArgs = methodMapping.getOnOpenArgs(path); + } + + onClose = methodMapping.getOnClose(); + if (onClose == null) { + onCloseArgs = null; + } else { + onCloseArgs = methodMapping.getOnCloseArgs(path); + } + + onError = methodMapping.getOnError(); + if (onError == null) { + onErrorArgs = null; + } else { + onErrorArgs = methodMapping.getOnErrorArgs(path); } - this.onOpen = open; - this.onClose = close; - this.onError = error; } @Override @@ -77,10 +77,10 @@ public class WsEndpointPojo extends Endp @Override public void onOpen(Session session) { + // TODO Insert the session into the method args if (onOpen != null) { - try { - onOpen.invoke(pojo, session); + onOpen.invoke(pojo, onOpenArgs); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { // TODO Auto-generated catch block @@ -93,7 +93,7 @@ public class WsEndpointPojo extends Endp public void onClose(CloseReason closeReason) { if (onClose != null) { try { - onClose.invoke(pojo, (Object[]) null); + onClose.invoke(pojo, onCloseArgs); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { // TODO Auto-generated catch block @@ -106,7 +106,8 @@ public class WsEndpointPojo extends Endp public void onError(Throwable throwable) { if (onError != null) { try { - onError.invoke(pojo, throwable); + // TODO Insert throwable + onError.invoke(pojo, onErrorArgs); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { // TODO Auto-generated catch block --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org