1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.johnzon.websocket.internal.jsr; |
20 | |
|
21 | |
import java.util.Collections; |
22 | |
import java.util.Map; |
23 | |
import java.util.concurrent.ConcurrentHashMap; |
24 | |
import javax.json.Json; |
25 | |
import javax.json.JsonReaderFactory; |
26 | |
import javax.json.JsonWriterFactory; |
27 | |
import javax.servlet.ServletContextEvent; |
28 | |
import javax.servlet.ServletContextListener; |
29 | |
import javax.servlet.annotation.WebListener; |
30 | |
|
31 | |
@WebListener |
32 | 0 | public class FactoryLocator implements ServletContextListener { |
33 | 0 | private static final Map<ClassLoader, JsonReaderFactory> READER_FACTORY_BY_LOADER = new ConcurrentHashMap<ClassLoader, JsonReaderFactory>(); |
34 | 0 | private static final Map<ClassLoader, JsonWriterFactory> WRITER_FACTORY_BY_LOADER = new ConcurrentHashMap<ClassLoader, JsonWriterFactory>(); |
35 | 0 | private static final String READER_ATTRIBUTE = FactoryLocator.class.getName() + ".readerFactory"; |
36 | 0 | private static final String WRITER_ATTRIBUTE = FactoryLocator.class.getName() + ".writerFactory"; |
37 | |
|
38 | |
@Override |
39 | |
public void contextInitialized(final ServletContextEvent servletContextEvent) { |
40 | 0 | final ClassLoader classLoader = servletContextEvent.getServletContext().getClassLoader(); |
41 | |
|
42 | 0 | final JsonReaderFactory reader = newReadFactory(); |
43 | 0 | READER_FACTORY_BY_LOADER.put(classLoader, reader); |
44 | 0 | servletContextEvent.getServletContext().setAttribute(READER_ATTRIBUTE, reader); |
45 | |
|
46 | 0 | final JsonWriterFactory writer = newWriterFactory(); |
47 | 0 | WRITER_FACTORY_BY_LOADER.put(classLoader, writer); |
48 | 0 | servletContextEvent.getServletContext().setAttribute(WRITER_ATTRIBUTE, reader); |
49 | 0 | } |
50 | |
|
51 | |
@Override |
52 | |
public void contextDestroyed(final ServletContextEvent servletContextEvent) { |
53 | 0 | final ClassLoader classLoader = servletContextEvent.getServletContext().getClassLoader(); |
54 | 0 | READER_FACTORY_BY_LOADER.remove(classLoader); |
55 | 0 | WRITER_FACTORY_BY_LOADER.remove(classLoader); |
56 | 0 | } |
57 | |
|
58 | |
public static JsonReaderFactory readerLocate() { |
59 | 0 | ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
60 | 0 | if (loader == null) { |
61 | 0 | loader = FactoryLocator.class.getClassLoader(); |
62 | |
} |
63 | 0 | final JsonReaderFactory factory = READER_FACTORY_BY_LOADER.get(loader); |
64 | 0 | if (factory == null) { |
65 | 0 | return newReadFactory(); |
66 | |
} |
67 | 0 | return factory; |
68 | |
} |
69 | |
|
70 | |
public static JsonWriterFactory writerLocate() { |
71 | 0 | ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
72 | 0 | if (loader == null) { |
73 | 0 | loader = FactoryLocator.class.getClassLoader(); |
74 | |
} |
75 | 0 | final JsonWriterFactory factory = WRITER_FACTORY_BY_LOADER.get(loader); |
76 | 0 | if (factory == null) { |
77 | 0 | return newWriterFactory(); |
78 | |
} |
79 | 0 | return factory; |
80 | |
} |
81 | |
|
82 | |
private static JsonReaderFactory newReadFactory() { |
83 | 0 | return Json.createReaderFactory(Collections.<String, Object>emptyMap()); |
84 | |
} |
85 | |
|
86 | |
private static JsonWriterFactory newWriterFactory() { |
87 | 0 | return Json.createWriterFactory(Collections.<String, Object>emptyMap()); |
88 | |
} |
89 | |
} |