Coverage Report - org.apache.myfaces.config.annotation.DefaultLifecycleProviderFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultLifecycleProviderFactory
0%
0/79
0%
0/18
0
 
 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 org.apache.commons.logging.Log;
 22  
 import org.apache.commons.logging.LogFactory;
 23  
 import org.apache.commons.discovery.resource.ClassLoaders;
 24  
 import org.apache.commons.discovery.resource.names.DiscoverServiceNames;
 25  
 import org.apache.commons.discovery.ResourceNameIterator;
 26  
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
 27  
 import org.apache.myfaces.shared_impl.util.ClassUtils;
 28  
 
 29  
 import javax.faces.context.ExternalContext;
 30  
 import javax.naming.Context;
 31  
 import javax.naming.InitialContext;
 32  
 import javax.naming.NamingException;
 33  
 import java.lang.reflect.InvocationTargetException;
 34  
 import java.lang.reflect.Constructor;
 35  
 
 36  
 /*
 37  
  * Date: Mar 12, 2007
 38  
  * Time: 9:53:40 PM
 39  
  */
 40  
 public class DefaultLifecycleProviderFactory extends LifecycleProviderFactory {
 41  0
     private static Log log = LogFactory.getLog(DefaultLifecycleProviderFactory.class);
 42  
     private static LifecycleProvider LIFECYCLE_PROVIDER_INSTANCE;
 43  
     
 44  
     @JSFWebConfigParam(name="org.apache.myfaces.config.annotation.LifecycleProvider", since="1.1")
 45  0
     public static final String LIFECYCLE_PROVIDER = LifecycleProvider.class.getName();
 46  
 
 47  
 
 48  
     public DefaultLifecycleProviderFactory()
 49  0
     {
 50  0
     }
 51  
 
 52  
     public LifecycleProvider getLifecycleProvider(ExternalContext externalContext)
 53  
     {
 54  0
         if (LIFECYCLE_PROVIDER_INSTANCE == null)
 55  
         {
 56  0
             if (externalContext == null)
 57  
             {
 58  0
                 log.info("No ExternalContext using fallback LifecycleProvider.");
 59  0
                 resolveFallbackLifecycleProvider();
 60  
             }
 61  
             else
 62  
             {
 63  0
                 if (!resolveLifecycleProviderFromExternalContext(externalContext))
 64  
                 {
 65  0
                     if (!resolveLifecycleProviderFromService(externalContext))
 66  
                     {
 67  0
                         resolveFallbackLifecycleProvider();
 68  
                     }
 69  
                 }
 70  
             }
 71  0
             log.info("Using LifecycleProvider "+ LIFECYCLE_PROVIDER_INSTANCE.getClass().getName());
 72  
         }
 73  0
         return LIFECYCLE_PROVIDER_INSTANCE;
 74  
     }
 75  
 
 76  
     public void release() {
 77  0
         LIFECYCLE_PROVIDER_INSTANCE = null;
 78  0
     }
 79  
 
 80  
 
 81  
 
 82  
     private boolean resolveLifecycleProviderFromExternalContext(ExternalContext externalContext)
 83  
     {
 84  
         try
 85  
         {
 86  0
             String lifecycleProvider = externalContext.getInitParameter(LIFECYCLE_PROVIDER);
 87  0
             if (lifecycleProvider != null)
 88  
             {
 89  
 
 90  0
                 Object obj = createClass(lifecycleProvider, externalContext);
 91  
 
 92  0
                 if (obj instanceof LifecycleProvider) {
 93  0
                     LIFECYCLE_PROVIDER_INSTANCE = (LifecycleProvider) obj;
 94  0
                     return true;
 95  
                 }
 96  
             }
 97  
         }
 98  0
         catch (ClassNotFoundException e)
 99  
         {
 100  0
             log.error("", e);
 101  
         }
 102  0
         catch (InstantiationException e)
 103  
         {
 104  0
             log.error("", e);
 105  
         }
 106  0
         catch (IllegalAccessException e)
 107  
         {
 108  0
             log.error("", e);
 109  
         }
 110  0
         catch (InvocationTargetException e)
 111  
         {
 112  0
             log.error("", e);
 113  0
         }
 114  0
         return false;
 115  
     }
 116  
 
 117  
 
 118  
     private boolean resolveLifecycleProviderFromService(ExternalContext externalContext) {
 119  0
         ClassLoader classLoader = ClassUtils.getContextClassLoader();
 120  0
         ClassLoaders loaders = new ClassLoaders();
 121  0
         loaders.put(classLoader);
 122  0
         loaders.put(this.getClass().getClassLoader());
 123  0
         DiscoverServiceNames dsn = new DiscoverServiceNames(loaders);
 124  0
         ResourceNameIterator iter = dsn.findResourceNames(LIFECYCLE_PROVIDER);
 125  0
         while (iter.hasNext()) {
 126  0
             String className = iter.nextResourceName();
 127  
             try
 128  
             {
 129  0
                 Object obj = createClass(className, externalContext);
 130  0
                 if (DiscoverableLifecycleProvider.class.isAssignableFrom(obj.getClass())) {
 131  0
                     DiscoverableLifecycleProvider discoverableLifecycleProvider =
 132  
                             (DiscoverableLifecycleProvider) obj;
 133  0
                     if (discoverableLifecycleProvider.isAvailable()) {
 134  0
                         LIFECYCLE_PROVIDER_INSTANCE = discoverableLifecycleProvider;
 135  0
                         return true;
 136  
                     }
 137  
                 }
 138  
             }
 139  0
             catch (ClassNotFoundException e)
 140  
             {
 141  
                 // ignore
 142  
             }
 143  0
             catch (NoClassDefFoundError e)
 144  
             {
 145  
                 // ignore
 146  
             }
 147  0
             catch (InstantiationException e)
 148  
             {
 149  0
                 log.error("", e);
 150  
             }
 151  0
             catch (IllegalAccessException e)
 152  
             {
 153  0
                 log.error("", e);
 154  
             }
 155  0
             catch (InvocationTargetException e)
 156  
             {
 157  0
                 log.error("", e);
 158  0
             }
 159  0
         }
 160  0
         return false;
 161  
     }
 162  
 
 163  
     private Object createClass(String className, ExternalContext externalContext)
 164  
             throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException
 165  
     {
 166  0
         Class clazz = ClassUtils.classForName(className);
 167  
 
 168  
         Object obj;
 169  
         try
 170  
         {
 171  0
             Constructor constructor = clazz.getConstructor(ExternalContext.class);
 172  0
             obj = constructor.newInstance(externalContext);
 173  
         }
 174  0
         catch (NoSuchMethodException e)
 175  
         {
 176  0
             obj = clazz.newInstance();
 177  0
         }
 178  0
         return obj;
 179  
     }
 180  
 
 181  
 
 182  
     private void resolveFallbackLifecycleProvider()
 183  
     {
 184  
         try
 185  
         {
 186  0
                 ClassUtils.classForName("javax.annotation.PreDestroy");
 187  
         }
 188  0
         catch (ClassNotFoundException e)
 189  
         {
 190  
             // no annotation available don't process annotations
 191  0
             LIFECYCLE_PROVIDER_INSTANCE = new NoAnnotationLifecyleProvider();
 192  0
             return;
 193  0
         }
 194  
         Context context;
 195  
         try
 196  
         {
 197  0
             context = new InitialContext();
 198  
             try
 199  
             {
 200  0
                 ClassUtils.classForName("javax.ejb.EJB");
 201  
                 // Asume full JEE 5 container
 202  0
                 LIFECYCLE_PROVIDER_INSTANCE = new AllAnnotationLifecycleProvider(context);
 203  
             }
 204  0
             catch (ClassNotFoundException e)
 205  
             {
 206  
                 // something else
 207  0
                 LIFECYCLE_PROVIDER_INSTANCE = new ResourceAnnotationLifecycleProvider(context);
 208  0
             }
 209  
         }
 210  0
         catch (NamingException e)
 211  
         {
 212  
             // no initial context available no injection
 213  0
             LIFECYCLE_PROVIDER_INSTANCE = new NoInjectionAnnotationLifecycleProvider();
 214  0
             log.error("No InitialContext found. Using NoInjectionAnnotationProcessor.", e);
 215  
 
 216  0
         }
 217  0
     }
 218  
 }