View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.websocket.common.events.annotated;
20  
21  import java.io.InputStream;
22  import java.io.Reader;
23  import java.lang.reflect.Method;
24  
25  import org.eclipse.jetty.websocket.api.Session;
26  
27  /**
28   * Simple CallableMethod that manages the optional {@link Session} argument
29   */
30  public class OptionalSessionCallableMethod extends CallableMethod
31  {
32      private final boolean wantsSession;
33      private final boolean streaming;
34  
35      public OptionalSessionCallableMethod(Class<?> pojo, Method method)
36      {
37          super(pojo,method);
38  
39          boolean foundConnection = false;
40          boolean foundStreaming = false;
41  
42          if (paramTypes != null)
43          {
44              for (Class<?> paramType : paramTypes)
45              {
46                  if (Session.class.isAssignableFrom(paramType))
47                  {
48                      foundConnection = true;
49                  }
50                  if (Reader.class.isAssignableFrom(paramType) || InputStream.class.isAssignableFrom(paramType))
51                  {
52                      foundStreaming = true;
53                  }
54              }
55          }
56  
57          this.wantsSession = foundConnection;
58          this.streaming = foundStreaming;
59      }
60  
61      public void call(Object obj, Session connection, Object... args)
62      {
63          if (wantsSession)
64          {
65              Object fullArgs[] = new Object[args.length + 1];
66              fullArgs[0] = connection;
67              System.arraycopy(args,0,fullArgs,1,args.length);
68              call(obj,fullArgs);
69          }
70          else
71          {
72              call(obj,args);
73          }
74      }
75  
76      public boolean isSessionAware()
77      {
78          return wantsSession;
79      }
80  
81      public boolean isStreaming()
82      {
83          return streaming;
84      }
85  
86      @Override
87      public String toString()
88      {
89          return String.format("%s[%s]",this.getClass().getSimpleName(),method.toGenericString());
90      }
91  }