Coverage Report - org.apache.johnzon.websocket.internal.jsr.FactoryLocator
 
Classes in this File Line Coverage Branch Coverage Complexity
FactoryLocator
0 %
0/33
0 %
0/8
2
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements. See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership. The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License. You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied. See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 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  
 }