|
4 | 4 | import java.io.InputStream; |
5 | 5 | import java.net.URL; |
6 | 6 |
|
| 7 | +import javax.servlet.ServletContext; |
7 | 8 | import javax.xml.XMLConstants; |
8 | 9 | import javax.xml.bind.JAXBContext; |
9 | 10 | import javax.xml.bind.JAXBElement; |
|
34 | 35 | public class JAXBPrettyConfigParser implements PrettyConfigParser |
35 | 36 | { |
36 | 37 |
|
| 38 | + private static final String JAXB_CONTEXT_ATTRIBUTE = JAXBPrettyConfigParser.class.getName() + ".JAXBContext"; |
| 39 | + |
37 | 40 | private static final String SCHEMA_LOCATION = "META-INF/ocpsoft-pretty-faces-3.3.1.xsd"; |
38 | 41 |
|
39 | 42 | private final static String JAXB_CLASS_PACKAGE = "com.ocpsoft.pretty.faces.config.types"; |
40 | | - |
41 | | - private final static JAXBContext jaxbContext; |
42 | 43 |
|
43 | 44 | private final SAXParserFactory saxParserFactory; |
44 | 45 |
|
| 46 | + private JAXBContext jaxbContext; |
| 47 | + |
45 | 48 | private Schema schema; |
46 | 49 |
|
47 | 50 | /** |
48 | | - * We do this expensive operations here. This should |
49 | | - * be OK as we do it once per ClassLoader. |
| 51 | + * Creates a new {@link JAXBPrettyConfigParser} |
50 | 52 | */ |
51 | | - static |
| 53 | + public JAXBPrettyConfigParser(ServletContext servletContext) |
52 | 54 | { |
53 | | - try |
54 | | - { |
55 | | - // build JAXBContext using the package we generate the classes to |
56 | | - jaxbContext = JAXBContext.newInstance(JAXB_CLASS_PACKAGE); |
57 | 55 |
|
58 | | - } |
59 | | - catch (JAXBException e) |
| 56 | + // try to find the cached JAXBContext |
| 57 | + jaxbContext = (JAXBContext) servletContext.getAttribute(JAXB_CONTEXT_ATTRIBUTE); |
| 58 | + |
| 59 | + // no existing JAXBContext? We will create a new one! |
| 60 | + if (jaxbContext == null) |
60 | 61 | { |
61 | | - throw new IllegalStateException(e); |
62 | | - } |
63 | | - } |
64 | | - |
65 | | - /** |
66 | | - * Creates a new {@link JAXBPrettyConfigParser} |
67 | | - */ |
68 | | - public JAXBPrettyConfigParser() |
69 | | - { |
70 | 62 |
|
| 63 | + try |
| 64 | + { |
| 65 | + // build JAXBContext using the package we generate the classes to |
| 66 | + jaxbContext = JAXBContext.newInstance(JAXB_CLASS_PACKAGE); |
| 67 | + |
| 68 | + // cache the JAXBContext using the ServletContext |
| 69 | + servletContext.setAttribute(JAXB_CONTEXT_ATTRIBUTE, jaxbContext); |
| 70 | + |
| 71 | + } |
| 72 | + catch (JAXBException e) |
| 73 | + { |
| 74 | + throw new IllegalStateException(e); |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + |
71 | 79 | // SAXParserFactory is NOT thread-safe |
72 | 80 | saxParserFactory = SAXParserFactory.newInstance(); |
73 | 81 | saxParserFactory.setNamespaceAware(true); |
|
0 commit comments