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