View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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.jsr356;
20  
21  import java.util.Map;
22  import java.util.concurrent.ConcurrentHashMap;
23  
24  import javax.websocket.Encoder;
25  import javax.websocket.EndpointConfig;
26  
27  import org.eclipse.jetty.util.log.Log;
28  import org.eclipse.jetty.util.log.Logger;
29  import org.eclipse.jetty.websocket.jsr356.metadata.EncoderMetadata;
30  import org.eclipse.jetty.websocket.jsr356.metadata.EncoderMetadataSet;
31  
32  /**
33   * Represents all of the declared {@link Encoder}s that the Container is aware of.
34   */
35  public class EncoderFactory implements Configurable
36  {
37      public static class Wrapper implements Configurable
38      {
39          private final Encoder encoder;
40          private final EncoderMetadata metadata;
41  
42          private Wrapper(Encoder encoder, EncoderMetadata metadata)
43          {
44              this.encoder = encoder;
45              this.metadata = metadata;
46          }
47  
48          public Encoder getEncoder()
49          {
50              return encoder;
51          }
52  
53          public EncoderMetadata getMetadata()
54          {
55              return metadata;
56          }
57  
58          @Override
59          public void init(EndpointConfig config)
60          {
61              this.encoder.init(config);
62          }
63      }
64  
65      private static final Logger LOG = Log.getLogger(EncoderFactory.class);
66  
67      private final EncoderMetadataSet metadatas;
68      private EncoderFactory parentFactory;
69      private Map<Class<?>, Wrapper> activeWrappers;
70  
71      public EncoderFactory(EncoderMetadataSet metadatas)
72      {
73          this.metadatas = metadatas;
74          this.activeWrappers = new ConcurrentHashMap<>();
75      }
76  
77      public EncoderFactory(EncoderMetadataSet metadatas, EncoderFactory parentFactory)
78      {
79          this(metadatas);
80          this.parentFactory = parentFactory;
81      }
82  
83      public Encoder getEncoderFor(Class<?> type)
84      {
85          Wrapper wrapper = getWrapperFor(type);
86          if (wrapper == null)
87          {
88              return null;
89          }
90          return wrapper.encoder;
91      }
92  
93      public EncoderMetadata getMetadataFor(Class<?> type)
94      {
95          LOG.debug("getMetadataFor({})",type);
96          EncoderMetadata metadata = metadatas.getMetadataByType(type);
97  
98          if (metadata != null)
99          {
100             return metadata;
101         }
102 
103         if (parentFactory != null)
104         {
105             return parentFactory.getMetadataFor(type);
106         }
107 
108         return null;
109     }
110 
111     public Wrapper getWrapperFor(Class<?> type)
112     {
113         synchronized (activeWrappers)
114         {
115             Wrapper wrapper = activeWrappers.get(type);
116 
117             // Try parent (if needed)
118             if ((wrapper == null) && (parentFactory != null))
119             {
120                 wrapper = parentFactory.getWrapperFor(type);
121             }
122 
123             if (wrapper == null)
124             {
125                 // Attempt to create Wrapper on demand
126                 EncoderMetadata metadata = metadatas.getMetadataByType(type);
127                 if (metadata == null)
128                 {
129                     return null;
130                 }
131                 wrapper = newWrapper(metadata);
132                 // track wrapper
133                 activeWrappers.put(type,wrapper);
134             }
135 
136             return wrapper;
137         }
138     }
139 
140     @Override
141     public void init(EndpointConfig config)
142     {
143         LOG.debug("init({})",config);
144 
145         // Instantiate all declared encoders
146         for (EncoderMetadata metadata : metadatas)
147         {
148             Wrapper wrapper = newWrapper(metadata);
149             activeWrappers.put(metadata.getObjectType(),wrapper);
150         }
151 
152         // Initialize all encoders
153         for (Wrapper wrapper : activeWrappers.values())
154         {
155             wrapper.encoder.init(config);
156         }
157     }
158 
159     private Wrapper newWrapper(EncoderMetadata metadata)
160     {
161         Class<? extends Encoder> encoderClass = metadata.getCoderClass();
162         try
163         {
164             Encoder encoder = encoderClass.newInstance();
165             return new Wrapper(encoder,metadata);
166         }
167         catch (InstantiationException | IllegalAccessException e)
168         {
169             throw new IllegalStateException("Unable to instantiate Encoder: " + encoderClass.getName());
170         }
171     }
172 }