tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1420948 - /tomcat/trunk/java/org/apache/tomcat/websocket/
Date Wed, 12 Dec 2012 20:26:41 GMT
Author: markt
Date: Wed Dec 12 20:26:38 2012
New Revision: 1420948

URL: http://svn.apache.org/viewvc?rev=1420948&view=rev
Log:
WebSocket 1.0 implementation part 13 of many
Complete first draft of @WebSocketMessage annotation handling

Added:
    tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsyncBase.java
      - copied, changed from r1420901, tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsync.java
    tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsyncBinary.java   (with
props)
    tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsyncString.java   (with
props)
    tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasicBase.java
      - copied, changed from r1420901, tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasic.java
    tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasicBinary.java   (with
props)
    tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasicPong.java   (with
props)
    tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasicString.java   (with
props)
Removed:
    tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsync.java
    tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasic.java
Modified:
    tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBase.java
    tomcat/trunk/java/org/apache/tomcat/websocket/PojoMethodMapping.java
    tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java

Copied: tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsyncBase.java (from
r1420901, tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsync.java)
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsyncBase.java?p2=tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsyncBase.java&p1=tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsync.java&r1=1420901&r2=1420948&rev=1420948&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsync.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsyncBase.java Wed Dec
12 20:26:38 2012
@@ -18,25 +18,42 @@ package org.apache.tomcat.websocket;
 
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.nio.ByteBuffer;
 
 import javax.websocket.MessageHandler;
+import javax.websocket.Session;
 
-public class PojoMessageHandlerAsync<T> extends PojoMessageHandlerBase<T>
-        implements MessageHandler.Async<T> {
+public abstract class PojoMessageHandlerAsyncBase<T>
+        extends PojoMessageHandlerBase<T> implements MessageHandler.Async<T>
{
 
-    public PojoMessageHandlerAsync(Object pojo, Method method,
-            WsSession wsSession) {
-        super(pojo, method, wsSession);
+    private final int indexBoolean;
+
+    public PojoMessageHandlerAsyncBase(Object pojo, Method method,
+            Session session, Object[] params, int indexPayload,
+            boolean wrap, int indexBoolean, int indexSession) {
+        super(pojo, method, session, params, indexPayload, wrap,
+                indexSession);
+        this.indexBoolean = indexBoolean;
     }
 
 
     @Override
     public void onMessage(T message, boolean last) {
-        Object[] params = null; // TODO insert message, last and session into
-                                // params
+        Object[] parameters = params.clone();
+        if (indexBoolean != -1) {
+            parameters[indexBoolean] = Boolean.valueOf(last);
+        }
+        if (indexSession != -1) {
+            parameters[indexSession] = session;
+        }
+        if (unwrap) {
+            parameters[indexPayload] = ((ByteBuffer) message).array();
+        } else {
+            parameters[indexPayload] = message;
+        }
         Object result;
         try {
-            result = method.invoke(pojo, params);
+            result = method.invoke(pojo, parameters);
         } catch (IllegalAccessException | InvocationTargetException e) {
             throw new IllegalArgumentException();
         }

Added: tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsyncBinary.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsyncBinary.java?rev=1420948&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsyncBinary.java (added)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsyncBinary.java Wed Dec
12 20:26:38 2012
@@ -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 java.lang.reflect.Method;
+import java.nio.ByteBuffer;
+
+import javax.websocket.Session;
+
+
+public class PojoMessageHandlerAsyncBinary
+        extends PojoMessageHandlerAsyncBase<ByteBuffer>{
+
+    public PojoMessageHandlerAsyncBinary(Object pojo, Method method,
+            Session session, Object[] params, int indexPayload, boolean wrap,
+            int indexBoolean, int indexSession) {
+        super(pojo, method, session, params, indexPayload, wrap, indexBoolean,
+                indexSession);
+    }
+}

Propchange: tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsyncBinary.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsyncString.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsyncString.java?rev=1420948&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsyncString.java (added)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsyncString.java Wed Dec
12 20:26:38 2012
@@ -0,0 +1,33 @@
+/*
+ * 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.Session;
+
+
+public class PojoMessageHandlerAsyncString
+        extends PojoMessageHandlerAsyncBase<String>{
+
+    public PojoMessageHandlerAsyncString(Object pojo, Method method,
+            Session session, Object[] params, int indexPayload, boolean wrap,
+            int indexBoolean, int indexSession) {
+        super(pojo, method, session, params, indexPayload, wrap, indexBoolean,
+                indexSession);
+    }
+}

Propchange: tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsyncString.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBase.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBase.java?rev=1420948&r1=1420947&r2=1420948&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBase.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBase.java Wed Dec 12 20:26:38
2012
@@ -21,33 +21,53 @@ import java.lang.reflect.Method;
 import java.nio.ByteBuffer;
 
 import javax.websocket.EncodeException;
+import javax.websocket.RemoteEndpoint;
+import javax.websocket.Session;
 
 public abstract class PojoMessageHandlerBase<T> {
 
     protected final Object pojo;
     protected final Method method;
-    protected final WsSession wsSession;
+    protected final Session session;
+    protected final Object[] params;
+    protected final int indexPayload;
+    protected final boolean unwrap;
+    protected final int indexSession;
 
 
     public PojoMessageHandlerBase(Object pojo, Method method,
-            WsSession wsSession) {
+            Session session, Object[] params, int indexPayload, boolean unwrap,
+            int indexSession) {
         this.pojo = pojo;
         this.method = method;
-        this.wsSession = wsSession;
+        this.session = session;
+        this.params = params;
+        this.indexPayload = indexPayload;
+        this.unwrap = unwrap;
+        this.indexSession = indexSession;
     }
 
 
     protected void processResult(Object result) {
+        if (result == null) {
+            return;
+        }
+
+        RemoteEndpoint remoteEndpoint = session.getRemote();
+        // TODO: Remove this once sendXxx is implemented?
+        if (remoteEndpoint == null) {
+            return;
+        }
+
         try {
             if (result instanceof String) {
-                wsSession.getRemote().sendString((String) result);
+                remoteEndpoint.sendString((String) result);
             } else if (result instanceof ByteBuffer) {
-                wsSession.getRemote().sendBytes((ByteBuffer) result);
+                remoteEndpoint.sendBytes((ByteBuffer) result);
             } else if (result instanceof byte[]) {
-                wsSession.getRemote().sendBytes(
-                        ByteBuffer.wrap((byte[]) result));
-            } else if (result != null) {
-                wsSession.getRemote().sendObject(result);
+                remoteEndpoint.sendBytes(ByteBuffer.wrap((byte[]) result));
+            } else {
+                remoteEndpoint.sendObject(result);
             }
         } catch (IOException | EncodeException ioe) {
             throw new IllegalStateException(ioe);

Copied: tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasicBase.java (from
r1420901, tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasic.java)
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasicBase.java?p2=tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasicBase.java&p1=tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasic.java&r1=1420901&r2=1420948&rev=1420948&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasic.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasicBase.java Wed Dec
12 20:26:38 2012
@@ -18,24 +18,36 @@ package org.apache.tomcat.websocket;
 
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.nio.ByteBuffer;
 
 import javax.websocket.MessageHandler;
+import javax.websocket.Session;
 
-public class PojoMessageHandlerBasic<T> extends PojoMessageHandlerBase<T>
-        implements MessageHandler.Basic<T> {
+public abstract class PojoMessageHandlerBasicBase<T>
+        extends PojoMessageHandlerBase<T> implements MessageHandler.Basic<T>
{
 
-    public PojoMessageHandlerBasic(Object pojo, Method method,
-            WsSession wsSession) {
-        super(pojo, method, wsSession);
+    public PojoMessageHandlerBasicBase(Object pojo, Method method,
+            Session session, Object[] params, int indexPayload,
+            boolean wrap, int indexSession) {
+        super(pojo, method, session, params, indexPayload, wrap,
+                indexSession);
     }
 
 
     @Override
     public void onMessage(T message) {
-        Object[] params = null; // TODO insert message and session into params
+        Object[] parameters = params.clone();
+        if (indexSession != -1) {
+            parameters[indexSession] = session;
+        }
+        if (unwrap) {
+            parameters[indexPayload] = ((ByteBuffer) message).array();
+        } else {
+            parameters[indexPayload] = message;
+        }
         Object result;
         try {
-            result = method.invoke(pojo, params);
+            result = method.invoke(pojo, parameters);
         } catch (IllegalAccessException | InvocationTargetException e) {
             throw new IllegalArgumentException();
         }

Added: tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasicBinary.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasicBinary.java?rev=1420948&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasicBinary.java (added)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasicBinary.java Wed Dec
12 20:26:38 2012
@@ -0,0 +1,32 @@
+/*
+ * 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 java.nio.ByteBuffer;
+
+import javax.websocket.Session;
+
+public class PojoMessageHandlerBasicBinary
+        extends PojoMessageHandlerBasicBase<ByteBuffer> {
+
+    public PojoMessageHandlerBasicBinary(Object pojo, Method method,
+            Session session, Object[] params, int indexPayload, boolean wrap,
+            int indexSession) {
+        super(pojo, method, session, params, indexPayload, wrap, indexSession);
+    }
+}

Propchange: tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasicBinary.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasicPong.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasicPong.java?rev=1420948&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasicPong.java (added)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasicPong.java Wed Dec
12 20:26:38 2012
@@ -0,0 +1,32 @@
+/*
+ * 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.PongMessage;
+import javax.websocket.Session;
+
+public class PojoMessageHandlerBasicPong
+        extends PojoMessageHandlerBasicBase<PongMessage> {
+
+    public PojoMessageHandlerBasicPong(Object pojo, Method method,
+            Session session, Object[] params, int indexPayload, boolean wrap,
+            int indexSession) {
+        super(pojo, method, session, params, indexPayload, wrap, indexSession);
+    }
+}

Propchange: tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasicPong.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasicString.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasicString.java?rev=1420948&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasicString.java (added)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasicString.java Wed Dec
12 20:26:38 2012
@@ -0,0 +1,31 @@
+/*
+ * 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.Session;
+
+public class PojoMessageHandlerBasicString
+        extends PojoMessageHandlerBasicBase<String> {
+
+    public PojoMessageHandlerBasicString(Object pojo, Method method,
+            Session session, Object[] params, int indexPayload, boolean wrap,
+            int indexSession) {
+        super(pojo, method, session, params, indexPayload, wrap, indexSession);
+    }
+}

Propchange: tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasicString.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 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=1420948&r1=1420947&r2=1420948&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/PojoMethodMapping.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/PojoMethodMapping.java Wed Dec 12 20:26:38
2012
@@ -18,11 +18,14 @@ package org.apache.tomcat.websocket;
 
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
+import java.nio.ByteBuffer;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
 import javax.websocket.MessageHandler;
+import javax.websocket.PongMessage;
 import javax.websocket.Session;
 import javax.websocket.WebSocketClose;
 import javax.websocket.WebSocketError;
@@ -51,6 +54,11 @@ public class PojoMethodMapping {
         Method open = null;
         Method close = null;
         Method error = null;
+        if (path.length() > mappingPath.length()) {
+            template = new UriTemplate(path.substring(mappingPath.length() - 2));
+        } else {
+            template = null;
+        }
         for (Method method : clazzPojo.getMethods()) {
             if (open == null &&
                     method.getAnnotation(WebSocketOpen.class) != null) {
@@ -62,17 +70,12 @@ public class PojoMethodMapping {
                     method.getAnnotation(WebSocketError.class) != null) {
                 error = method;
             } else if (method.getAnnotation(WebSocketMessage.class) != null) {
-                onMessage.add(new MessageMethod(method));
+                onMessage.add(new MessageMethod(method, template));
             }
         }
         this.onOpen = open;
         this.onClose = close;
         this.onError = error;
-        if (path.length() > mappingPath.length()) {
-            template = new UriTemplate(path.substring(mappingPath.length() - 2));
-        } else {
-            template = null;
-        }
         onOpenParams = getPathParams(onOpen, false);
         onCloseParams = getPathParams(onClose, false);
         onErrorParams = getPathParams(onError, true);
@@ -114,20 +117,12 @@ public class PojoMethodMapping {
             Session session) {
         Set<MessageHandler> result = new HashSet<>();
         for (MessageMethod messageMethod : onMessage) {
-            result.add(buildMessageHandler(messageMethod, pojo, pathInfo,
-                    session));
+            result.add(messageMethod.getMessageHandler(pojo, pathInfo,session));
         }
         return result;
     }
 
 
-    private static MessageHandler buildMessageHandler(
-            MessageMethod messageMethod, Object pojo, String pathInfo,
-            Session session) {
-        return null;
-    }
-
-
     private static PathParam[] getPathParams(Method m, boolean isError) {
         if (m == null) {
             return new PathParam[0];
@@ -220,20 +215,168 @@ public class PojoMethodMapping {
     private static class MessageMethod {
 
         private final Method m;
+        private final UriTemplate template;
+        private int indexString = -1;
+        private int indexByteArray = -1;
+        private int indexByteBuffer = -1;
+        private int indexPong = -1;
+        private int indexBoolean = -1;
+        private int indexSession = -1;
+        private Map<Integer,PathParam> indexPathParams = new HashMap<>();
+        private int indexPayload = -1;
 
 
-        public MessageMethod(Method m) {
+        public MessageMethod(Method m, UriTemplate template) {
             this.m = m;
-        }
+            this.template = template;
 
+            Class<?>[] types = m.getParameterTypes();
+            Annotation[][] paramsAnnotations = m.getParameterAnnotations();
 
-        public Method getMethod() {
-            return m;
+            for (int i = 0; i < types.length; i++) {
+                if (types[i] == String.class) {
+                    Annotation[] paramAnnotations = paramsAnnotations[i];
+                    for (Annotation paramAnnotation : paramAnnotations) {
+                        if (paramAnnotation.annotationType().equals(
+                                WebSocketPathParam.class)) {
+                            indexPathParams.put(
+                                    Integer.valueOf(i), new PathParam(types[i],
+                                            ((WebSocketPathParam) paramAnnotation).value()));
+                            break;
+                        }
+                    }
+                    if (indexString == -1) {
+                        indexString = i;
+                    } else {
+                        // TODO i18n
+                        throw new IllegalArgumentException();
+                    }
+                } else if (types[i] == boolean.class) {
+                    if (indexBoolean == -1) {
+                        indexBoolean = i;
+                    } else {
+                        // TODO i18n
+                        throw new IllegalArgumentException();
+                    }
+                } else if (types[i] == ByteBuffer.class) {
+                    if (indexByteBuffer == -1) {
+                        indexByteBuffer = i;
+                    } else {
+                        // TODO i18n
+                        throw new IllegalArgumentException();
+                    }
+                } else if (types[i] == byte[].class) {
+                    if (indexByteArray == -1) {
+                        indexByteArray = i;
+                    } else {
+                        // TODO i18n
+                        throw new IllegalArgumentException();
+                    }
+                } else if (types[i] == Session.class) {
+                    if (indexSession == -1) {
+                        indexSession = i;
+                    } else {
+                        // TODO i18n
+                        throw new IllegalArgumentException();
+                    }
+                } else if (types[i] == PongMessage.class) {
+                    if (indexPong == -1) {
+                        indexPong = i;
+                    } else {
+                        // TODO i18n
+                        throw new IllegalArgumentException();
+                    }
+                }
+            }
+            // Additional checks required
+            if (indexString != -1) {
+                indexPayload = indexString;
+            }
+            if (indexByteArray != -1) {
+                if (indexPayload != -1) {
+                    // TODO i18n
+                    throw new IllegalArgumentException();
+                } else {
+                    indexPayload = indexByteArray;
+                }
+            }
+            if (indexByteBuffer != -1) {
+                if (indexPayload != -1) {
+                    // TODO i18n
+                    throw new IllegalArgumentException();
+                } else {
+                    indexPayload = indexByteBuffer;
+                }
+            }
+            if (indexPong != -1) {
+                if (indexPayload != -1) {
+                    // TODO i18n
+                    throw new IllegalArgumentException();
+                } else {
+                    indexPayload = indexPong;
+                }
+            }
+            if (indexPayload == -1) {
+                // TODO i18n
+                throw new IllegalArgumentException();
+            }
+            if (indexPong != -1 && indexBoolean != -1) {
+                // TODO i18n
+                throw new IllegalArgumentException();
+            }
         }
 
 
-        public Object[] getParameters() {
-            return null;
+        public MessageHandler getMessageHandler(Object pojo, String pathInfo,
+                Session session) {
+            Object[] params = new Object[m.getParameterTypes().length];
+
+            Map<String,String> pathParams = template.match(pathInfo);
+
+            for (Map.Entry<Integer,PathParam> entry :
+                    indexPathParams.entrySet()) {
+                PathParam pathParam = entry.getValue();
+                String valueString = pathParams.get(pathParam.getName());
+                Object value = null;
+                if (valueString != null) {
+                    value = coerceToType(pathParam.getType(), valueString);
+                }
+                params[entry.getKey().intValue()] = value;
+            }
+
+            MessageHandler mh = null;
+            if (indexBoolean == -1) {
+                // Basic
+                if (indexString != -1) {
+                    mh = new PojoMessageHandlerBasicString(pojo, m,  session,
+                            params, indexString, false, indexSession);
+                } else if (indexByteArray != -1) {
+                    mh = new PojoMessageHandlerBasicBinary(pojo, m, session,
+                            params, indexByteArray, true, indexSession);
+                } else if (indexByteBuffer != -1) {
+                    mh = new PojoMessageHandlerBasicBinary(pojo, m, session,
+                            params, indexByteBuffer, false, indexSession);
+                } else {
+                    mh = new PojoMessageHandlerBasicPong(pojo, m, session,
+                            params, indexPong, false, indexSession);
+                }
+            } else {
+                // ASync
+                if (indexString != -1) {
+                    mh = new PojoMessageHandlerAsyncString(pojo, m, session,
+                            params, indexString, false, indexSession,
+                            indexBoolean);
+                } else if (indexByteArray != -1) {
+                    mh = new PojoMessageHandlerAsyncBinary(pojo, m, session,
+                            params, indexByteArray, true, indexSession,
+                            indexBoolean);
+                } else {
+                    mh = new PojoMessageHandlerAsyncBinary(pojo, m, session,
+                            params, indexByteBuffer, false, indexSession,
+                            indexBoolean);
+                }
+            }
+            return mh;
         }
     }
 }

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java?rev=1420948&r1=1420947&r2=1420948&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java Wed Dec 12 20:26:38 2012
@@ -17,6 +17,7 @@
 package org.apache.tomcat.websocket;
 
 import java.io.IOException;
+import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
 import java.net.URI;
 import java.nio.ByteBuffer;
@@ -58,24 +59,24 @@ public class WsSession implements Sessio
     @SuppressWarnings("unchecked")
     @Override
     public void addMessageHandler(MessageHandler listener) {
-        Type[] types = listener.getClass().getGenericInterfaces();
+        Type[] types = ((ParameterizedType) listener.getClass().getGenericSuperclass()).getActualTypeArguments();
         if (types.length != 1) {
             // TODO i18n
             throw new IllegalArgumentException();
         }
-        if (types[0].getClass().equals(String.class)) {
+        if (types[0].equals(String.class)) {
             if (textMessageHandler != null) {
                 // TODO i18n
                 throw new IllegalStateException();
             }
             textMessageHandler = listener;
-        } else if (types[0].getClass().equals(ByteBuffer.class)) {
+        } else if (types[0].equals(ByteBuffer.class)) {
             if (binaryMessageHandler != null) {
                 // TODO i18n
                 throw new IllegalStateException();
             }
             binaryMessageHandler = listener;
-        } else if (types[0].getClass().equals(PongMessage.class)) {
+        } else if (types[0].equals(PongMessage.class)) {
             if (pongMessageHandler != null) {
                 // TODO i18n
                 throw new IllegalStateException();



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message