Coverage Report - org.apache.myfaces.config.annotation.Tomcat7AnnotationLifecycleProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
Tomcat7AnnotationLifecycleProvider
0%
0/38
0%
0/14
3.167
 
 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.myfaces.config.annotation;
 20  
 
 21  
 import java.lang.reflect.InvocationTargetException;
 22  
 import java.util.Map;
 23  
 import java.util.WeakHashMap;
 24  
 import java.util.logging.Level;
 25  
 import java.util.logging.Logger;
 26  
 
 27  
 import javax.faces.FacesException;
 28  
 import javax.faces.context.ExternalContext;
 29  
 import javax.faces.context.FacesContext;
 30  
 import javax.naming.NamingException;
 31  
 
 32  
 import org.apache.myfaces.shared.util.ClassUtils;
 33  
 import org.apache.tomcat.InstanceManager;
 34  
 
 35  
 /**
 36  
  * An annotation lifecycle provider for Tomcat 7.
 37  
  */
 38  
 public class Tomcat7AnnotationLifecycleProvider implements
 39  
         DiscoverableLifecycleProvider, LifecycleProvider2
 40  
 {
 41  
 
 42  0
     private static Logger log = Logger.getLogger(TomcatAnnotationLifecycleProvider.class.getName());
 43  
     
 44  0
     private WeakHashMap<ClassLoader, InstanceManager> instanceManagers = null;
 45  
 
 46  
     public Tomcat7AnnotationLifecycleProvider(ExternalContext externalContext)
 47  0
     {
 48  0
         instanceManagers = new WeakHashMap<ClassLoader, InstanceManager>();
 49  0
     }
 50  
 
 51  
     public Object newInstance(String className)
 52  
             throws ClassNotFoundException, IllegalAccessException,
 53  
             InstantiationException, NamingException, InvocationTargetException
 54  
     {
 55  0
         Class<?> clazz = ClassUtils.classForName(className);
 56  0
         if (log.isLoggable(Level.FINEST))
 57  
         {
 58  0
             log.finest("Creating instance of " + className);
 59  
         }
 60  0
         Object object = clazz.newInstance();
 61  
 
 62  0
         return object;
 63  
     }
 64  
 
 65  
     public void destroyInstance(Object instance)
 66  
             throws IllegalAccessException, InvocationTargetException
 67  
     {
 68  0
         InstanceManager manager = instanceManagers
 69  
                 .get(ClassUtils.getContextClassLoader());
 70  
 
 71  0
         if (manager != null)
 72  
         {
 73  0
             manager.destroyInstance(instance);
 74  
         }
 75  0
     }
 76  
 
 77  
     public void postConstruct(Object instance)
 78  
             throws IllegalAccessException, InvocationTargetException
 79  
     {
 80  0
         InstanceManager manager = instanceManagers
 81  
                 .get(ClassUtils.getContextClassLoader());
 82  0
         if (manager == null)
 83  
         {
 84  
             //Initialize manager
 85  0
             manager = initManager();
 86  
         }
 87  
 
 88  
         //Is initialized
 89  0
         if (manager != null)
 90  
         {
 91  
             //Inject resources
 92  
             try 
 93  
             {
 94  0
                 manager.newInstance(instance);
 95  
             }
 96  0
             catch (NamingException e)
 97  
             {
 98  0
                 throw new FacesException(e);
 99  0
             }
 100  
         }
 101  0
     }
 102  
 
 103  
     public boolean isAvailable()
 104  
     {
 105  
         try
 106  
         {
 107  0
             Class.forName("org.apache.tomcat.InstanceManager",
 108  
                     true, ClassUtils.getContextClassLoader());
 109  0
             return true;
 110  
         }
 111  0
         catch (Exception e)
 112  
         {
 113  
             // ignore
 114  
         }
 115  
 
 116  0
         return false;
 117  
     }
 118  
 
 119  
     private InstanceManager initManager()
 120  
     {
 121  0
         FacesContext context = FacesContext.getCurrentInstance();
 122  0
         if (context == null)
 123  
         {
 124  0
             return null;
 125  
         }
 126  
 
 127  0
         ExternalContext extCtx = context.getExternalContext();
 128  0
         if (extCtx == null)
 129  
         {
 130  0
             return null;
 131  
         }
 132  
 
 133  
         // get application map to access ServletContext attributes
 134  0
         Map<String, Object> applicationMap = extCtx.getApplicationMap();
 135  
 
 136  0
         InstanceManager instanceManager = (InstanceManager)
 137  
                 applicationMap.get(InstanceManager.class.getName());
 138  0
         if (instanceManager != null)
 139  
         {
 140  0
             instanceManagers.put(ClassUtils.getContextClassLoader(),
 141  
                     instanceManager);
 142  
         }
 143  
 
 144  0
         return instanceManager;
 145  
     }
 146  
 
 147  
 }