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;
20  
21  import java.util.concurrent.ConcurrentHashMap;
22  
23  import org.eclipse.jetty.websocket.api.WebSocketPolicy;
24  import org.eclipse.jetty.websocket.api.annotations.WebSocket;
25  
26  public class JettyAnnotatedImpl implements EventDriverImpl
27  {
28      private ConcurrentHashMap<Class<?>, JettyAnnotatedMetadata> cache = new ConcurrentHashMap<>();
29  
30      @Override
31      public EventDriver create(Object websocket, WebSocketPolicy policy)
32      {
33          Class<?> websocketClass = websocket.getClass();
34          synchronized (this)
35          {
36              JettyAnnotatedMetadata metadata = cache.get(websocketClass);
37              if (metadata == null)
38              {
39                  JettyAnnotatedScanner scanner = new JettyAnnotatedScanner();
40                  metadata = scanner.scan(websocketClass);
41                  cache.put(websocketClass,metadata);
42              }
43              return new JettyAnnotatedEventDriver(policy,websocket,metadata);
44          }
45      }
46  
47      @Override
48      public String describeRule()
49      {
50          return "class is annotated with @" + WebSocket.class.getName();
51      }
52  
53      @Override
54      public boolean supports(Object websocket)
55      {
56          WebSocket anno = websocket.getClass().getAnnotation(WebSocket.class);
57          return (anno != null);
58      }
59  
60      @Override
61      public String toString()
62      {
63          return String.format("%s [cache.count=%d]",this.getClass().getSimpleName(),cache.size());
64      }
65  }