View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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.extensions;
20  
21  import org.eclipse.jetty.io.ByteBufferPool;
22  import org.eclipse.jetty.util.StringUtil;
23  import org.eclipse.jetty.websocket.api.WebSocketException;
24  import org.eclipse.jetty.websocket.api.WebSocketPolicy;
25  import org.eclipse.jetty.websocket.api.extensions.Extension;
26  import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
27  import org.eclipse.jetty.websocket.api.extensions.ExtensionFactory;
28  
29  public class WebSocketExtensionFactory extends ExtensionFactory
30  {
31      private WebSocketPolicy policy;
32      private ByteBufferPool bufferPool;
33  
34      public WebSocketExtensionFactory(WebSocketPolicy policy, ByteBufferPool bufferPool)
35      {
36          super();
37          this.policy = policy;
38          this.bufferPool = bufferPool;
39      }
40  
41      @Override
42      public Extension newInstance(ExtensionConfig config)
43      {
44          if (config == null)
45          {
46              return null;
47          }
48  
49          String name = config.getName();
50          if (StringUtil.isBlank(name))
51          {
52              return null;
53          }
54  
55          Class<? extends Extension> extClass = getExtension(name);
56          if (extClass == null)
57          {
58              return null;
59          }
60  
61          try
62          {
63              Extension ext = extClass.newInstance();
64              if (ext instanceof AbstractExtension)
65              {
66                  AbstractExtension aext = (AbstractExtension)ext;
67                  aext.setPolicy(policy);
68                  aext.setBufferPool(bufferPool);
69                  aext.setConfig(config);
70              }
71              return ext;
72          }
73          catch (InstantiationException | IllegalAccessException e)
74          {
75              throw new WebSocketException("Cannot instantiate extension: " + extClass,e);
76          }
77      }
78  }