Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ded82019de4dc85cdad27954bfa25d674c0309b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//
//  ========================================================================
//  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.websocket.jsr356.metadata;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.jetty.websocket.api.InvalidWebSocketException;

/**
 * An durable collection of {@link CoderMetadata}.
 * <p>
 * This is a write-only collection, and cannot be modified once initialized.
 * 
 * @param <T>
 *            The type of coder ({@link javax.websocket.Decoder} or {@link javax.websocket.Encoder}
 * @param <M>
 *            The metadata for the coder
 */
public abstract class CoderMetadataSet<T, M extends CoderMetadata<T>> implements Iterable<M>
{
    /**
     * Collection of metadatas
     */
    private final List<M> metadatas;
    /**
     * Collection of declared Coder classes
     */
    private final List<Class<? extends T>> coders;
    /**
     * Mapping of supported Type to metadata list index
     */
    private final Map<Class<?>, Integer> typeMap;
    /**
     * Mapping of Coder class to list of supported metadata
     */
    private final Map<Class<? extends T>, List<Integer>> implMap;

    protected CoderMetadataSet()
    {
        metadatas = new ArrayList<>();
        coders = new ArrayList<>();
        typeMap = new ConcurrentHashMap<>();
        implMap = new ConcurrentHashMap<>();
    }

    public void add(Class<? extends T> coder)
    {
        List<M> metadatas = discover(coder);
        trackMetadata(metadatas);
    }

    public List<M> addAll(Class<? extends T>[] coders)
    {
        List<M> metadatas = new ArrayList<>();

        for (Class<? extends T> coder : coders)
        {
            metadatas.addAll(discover(coder));
        }

        trackMetadata(metadatas);
        return metadatas;
    }

    public List<M> addAll(List<Class<? extends T>> coders)
    {
        List<M> metadatas = new ArrayList<>();

        for (Class<? extends T> coder : coders)
        {
            metadatas.addAll(discover(coder));
        }

        trackMetadata(metadatas);
        return metadatas;
    }

    /**
     * Coder Specific discovery of Metadata for a specific coder.
     * 
     * @param coder
     *            the coder to discover metadata in.
     * @return the list of metadata discovered
     * @throws InvalidWebSocketException
     *             if unable to discover some metadata. Sucha as: a duplicate {@link CoderMetadata#getObjectType()} encountered, , or if unable to find the
     *             concrete generic class reference for the coder, or if the provided coder is not valid per spec.
     */
    protected abstract List<M> discover(Class<? extends T> coder);

    public Class<? extends T> getCoder(Class<?> type)
    {
        M metadata = getMetadataByType(type);
        if (metadata == null)
        {
            return null;
        }
        return metadata.getCoderClass();
    }

    public List<Class<? extends T>> getList()
    {
        return coders;
    }

    public List<M> getMetadataByImplementation(Class<? extends T> clazz)
    {
        List<Integer> indexes = implMap.get(clazz);
        if (indexes == null)
        {
            return null;
        }
        List<M> ret = new ArrayList<>();
        for (Integer idx : indexes)
        {
            ret.add(metadatas.get(idx));
        }
        return ret;
    }

    public M getMetadataByType(Class<?> type)
    {
        Integer idx = typeMap.get(type);
        if (idx == null)
        {
            // Quick lookup failed, try slower lookup via isAssignable instead
            idx = getMetadataByAssignableType(type);
            if (idx != null)
            {
                // add new entry map
                typeMap.put(type,idx);
            }
        }

        // If idx is STILL null, we've got no match
        if (idx == null)
        {
            return null;
        }
        return metadatas.get(idx);
    }

    private Integer getMetadataByAssignableType(Class<?> type)
    {
        for (Map.Entry<Class<?>, Integer> entry : typeMap.entrySet())
        {
            if (entry.getKey().isAssignableFrom(type))
            {
                return entry.getValue();
            }
        }

        return null;
    }

    @Override
    public Iterator<M> iterator()
    {
        return metadatas.iterator();
    }

    @Override
    public String toString()
    {
        StringBuilder builder = new StringBuilder();
        builder.append(this.getClass().getSimpleName());
        builder.append("[metadatas=");
        builder.append(metadatas.size());
        builder.append(",coders=");
        builder.append(coders.size());
        builder.append("]");
        return builder.toString();
    }

    protected void trackMetadata(List<M> metadatas)
    {
        for (M metadata : metadatas)
        {
            trackMetadata(metadata);
        }
    }

    protected void trackMetadata(M metadata)
    {
        synchronized (metadatas)
        {
            // Validate
            boolean duplicate = false;

            // Is this metadata already declared?
            if (metadatas.contains(metadata))
            {
                duplicate = true;
            }

            // Is this type already declared?
            Class<?> type = metadata.getObjectType();
            if (typeMap.containsKey(type))
            {
                duplicate = true;
            }

            if (duplicate)
            {
                StringBuilder err = new StringBuilder();
                err.append("Duplicate decoder for type: ");
                err.append(type);
                err.append(" (class ").append(metadata.getCoderClass().getName());

                // Get prior one
                M dup = getMetadataByType(type);
                err.append(" duplicates ");
                err.append(dup.getCoderClass().getName());
                err.append(")");
                throw new IllegalStateException(err.toString());
            }

            // Track
            Class<? extends T> coderClass = metadata.getCoderClass();
            int newidx = metadatas.size();
            metadatas.add(metadata);
            coders.add(coderClass);
            typeMap.put(type,newidx);

            List<Integer> indexes = implMap.get(coderClass);
            if (indexes == null)
            {
                indexes = new ArrayList<>();
            }
            if (indexes.contains(newidx))
            {
                // possible duplicate, TODO: how?
            }
            indexes.add(newidx);
            implMap.put(coderClass,indexes);
        }
    }
}

Back to the top