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.mapper; |
20 | |
|
21 | |
import org.apache.johnzon.mapper.Mapper; |
22 | |
import org.apache.johnzon.mapper.MapperBuilder; |
23 | |
|
24 | |
import java.util.Map; |
25 | |
import java.util.concurrent.ConcurrentHashMap; |
26 | |
import javax.servlet.ServletContextEvent; |
27 | |
import javax.servlet.ServletContextListener; |
28 | |
import javax.servlet.annotation.WebListener; |
29 | |
|
30 | |
@WebListener |
31 | 0 | public class MapperLocator implements ServletContextListener { |
32 | 0 | private static final Map<ClassLoader, Mapper> MAPPER_BY_LOADER = new ConcurrentHashMap<ClassLoader, Mapper>(); |
33 | 0 | private static final String ATTRIBUTE = MapperLocator.class.getName() + ".mapper"; |
34 | |
|
35 | |
@Override |
36 | |
public void contextInitialized(final ServletContextEvent servletContextEvent) { |
37 | 0 | final Mapper build = newMapper(); |
38 | 0 | MAPPER_BY_LOADER.put(servletContextEvent.getServletContext().getClassLoader(), build); |
39 | 0 | servletContextEvent.getServletContext().setAttribute(ATTRIBUTE, build); |
40 | 0 | } |
41 | |
|
42 | |
@Override |
43 | |
public void contextDestroyed(final ServletContextEvent servletContextEvent) { |
44 | 0 | MAPPER_BY_LOADER.remove(servletContextEvent.getServletContext().getClassLoader()); |
45 | 0 | } |
46 | |
|
47 | |
public static Mapper locate() { |
48 | 0 | ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
49 | 0 | if (loader == null) { |
50 | 0 | loader = MapperLocator.class.getClassLoader(); |
51 | |
} |
52 | 0 | final Mapper mapper = MAPPER_BY_LOADER.get(loader); |
53 | 0 | if (mapper == null) { |
54 | 0 | return newMapper(); |
55 | |
} |
56 | 0 | return mapper; |
57 | |
} |
58 | |
|
59 | |
private static Mapper newMapper() { |
60 | 0 | return new MapperBuilder().build(); |
61 | |
} |
62 | |
} |