Coverage Report - javax.faces.internal.FactoryFinderUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
FactoryFinderUtil
68%
52/76
59%
19/32
3.643
 
 1  
 /*
 2  
  * Copyright 2004-2011 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package javax.faces.internal;
 17  
 
 18  
 import java.lang.reflect.Constructor;
 19  
 import java.lang.reflect.InvocationTargetException;
 20  
 import java.util.ArrayList;
 21  
 import java.util.HashMap;
 22  
 import java.util.Iterator;
 23  
 import java.util.List;
 24  
 import java.util.Map;
 25  
 
 26  
 import javax.faces.FacesException;
 27  
 import javax.faces.FactoryFinder;
 28  
 import javax.faces.application.ApplicationFactory;
 29  
 import javax.faces.context.FacesContextFactory;
 30  
 import javax.faces.lifecycle.LifecycleFactory;
 31  
 import javax.faces.render.RenderKitFactory;
 32  
 
 33  
 /**
 34  
  * @author shot
 35  
  *
 36  
  * This class might be changed without notice. Please do not use it
 37  
  * excluding the JSF specification part.
 38  
  */
 39  
 public class FactoryFinderUtil {
 40  
 
 41  1
     private static final List VALID_FACTORY_NAMES = new ArrayList();
 42  
 
 43  1
     private static final Map ABSTRACT_FACTORY_CLASSES = new HashMap();
 44  
 
 45  
     static {
 46  1
         VALID_FACTORY_NAMES.add(FactoryFinder.APPLICATION_FACTORY);
 47  1
         VALID_FACTORY_NAMES.add(FactoryFinder.FACES_CONTEXT_FACTORY);
 48  1
         VALID_FACTORY_NAMES.add(FactoryFinder.LIFECYCLE_FACTORY);
 49  1
         VALID_FACTORY_NAMES.add(FactoryFinder.RENDER_KIT_FACTORY);
 50  1
         ABSTRACT_FACTORY_CLASSES.put(FactoryFinder.APPLICATION_FACTORY,
 51  4
                 ApplicationFactory.class);
 52  1
         ABSTRACT_FACTORY_CLASSES.put(FactoryFinder.FACES_CONTEXT_FACTORY,
 53  
                 FacesContextFactory.class);
 54  1
         ABSTRACT_FACTORY_CLASSES.put(FactoryFinder.LIFECYCLE_FACTORY,
 55  
                 LifecycleFactory.class);
 56  1
         ABSTRACT_FACTORY_CLASSES.put(FactoryFinder.RENDER_KIT_FACTORY,
 57  
                 RenderKitFactory.class);
 58  1
     }
 59  
 
 60  0
     private FactoryFinderUtil() {
 61  0
     }
 62  
 
 63  
     public static void checkValidFactoryNames(String factoryName) {
 64  5386
         if (!VALID_FACTORY_NAMES.contains(factoryName)) {
 65  2
             throw new IllegalArgumentException(
 66  
                     "Factory name can not be identified.");
 67  
         }
 68  5384
     }
 69  
 
 70  
     public static ClassLoader getClassLoader() throws FacesException {
 71  5371
         ClassLoader classLoader = Thread.currentThread()
 72  
                 .getContextClassLoader();
 73  5371
         if (classLoader == null) {
 74  0
             classLoader = FactoryFinderUtil.class.getClassLoader();
 75  
         }
 76  5371
         if (classLoader == null) {
 77  0
             throw new FacesException();
 78  
         }
 79  5371
         return classLoader;
 80  
     }
 81  
 
 82  
     public static Class getAbstractFactoryClass(String factoryName) {
 83  5372
         return (Class) ABSTRACT_FACTORY_CLASSES.get(factoryName);
 84  
     }
 85  
 
 86  
     public static Object createFactoryInstance(String factoryName,
 87  
             List classNames) {
 88  
 
 89  5370
         ClassLoader loader = getClassLoader();
 90  5370
         Class abstractFactoryClass = getAbstractFactoryClass(factoryName);
 91  5370
         Object current = null;
 92  5370
         Class implClass = null;
 93  5370
         for (Iterator classNamesIterator = classNames.iterator(); classNamesIterator
 94  10740
                 .hasNext();) {
 95  5371
             String implClassName = (String) classNamesIterator.next();
 96  
             try {
 97  5371
                 implClass = loader.loadClass(implClassName);
 98  0
             } catch (ClassNotFoundException e) {
 99  0
                 throw new FacesException(e);
 100  5371
             }
 101  5371
             if (!abstractFactoryClass.isAssignableFrom(implClass)) {
 102  1
                 throw new IllegalArgumentException("Class " + implClassName
 103  
                         + " is no " + abstractFactoryClass.getName());
 104  
             }
 105  5370
             current = getCurrentFactoryInstance(implClass,
 106  
                     abstractFactoryClass, current);
 107  
         }
 108  5369
         return current;
 109  
     }
 110  
 
 111  
     public static Object getCurrentFactoryInstance(Class implClass,
 112  
             Class abstractFactoryClass, Object current) {
 113  5371
         if (implClass == null) {
 114  0
             if (current == null) {
 115  0
                 throw new IllegalArgumentException();
 116  
             } else {
 117  0
                 return current;
 118  
             }
 119  
         }
 120  5371
         Constructor constructor = getConstructor(implClass,
 121  
                 new Class[] { abstractFactoryClass });
 122  5371
         if (constructor != null) {
 123  2
             current = newInstanceByConstructor(constructor,
 124  
                     new Object[] { current });
 125  
         } else {
 126  5369
             current = newInstance(implClass);
 127  
         }
 128  5371
         return current;
 129  
     }
 130  
 
 131  
     public static Constructor getConstructor(Class clazz, Class[] argTypes) {
 132  5371
         if (clazz == null || argTypes == null) {
 133  0
             throw new IllegalArgumentException();
 134  
         }
 135  
         try {
 136  5371
             return clazz.getConstructor(argTypes);
 137  5369
         } catch (NoSuchMethodException e) {
 138  5369
             return null;
 139  
         }
 140  
     }
 141  
 
 142  
     public static Object newInstance(Class clazz) {
 143  5369
         if (clazz == null) {
 144  0
             throw new IllegalArgumentException();
 145  
         }
 146  
         try {
 147  5369
             return clazz.newInstance();
 148  0
         } catch (InstantiationException e) {
 149  0
             throw new FacesException(e);
 150  0
         } catch (IllegalAccessException e) {
 151  0
             throw new FacesException(e);
 152  
         }
 153  
     }
 154  
 
 155  
     public static Object newInstanceByConstructor(Constructor constructor,
 156  
             Object[] args) {
 157  2
         if (constructor == null || args == null) {
 158  0
             throw new IllegalArgumentException();
 159  
         }
 160  
         try {
 161  2
             return constructor.newInstance(args);
 162  0
         } catch (IllegalArgumentException e) {
 163  0
             throw new FacesException(e);
 164  0
         } catch (InstantiationException e) {
 165  0
             throw new FacesException(e);
 166  0
         } catch (IllegalAccessException e) {
 167  0
             throw new FacesException(e);
 168  0
         } catch (InvocationTargetException e) {
 169  0
             throw new FacesException(e);
 170  
         }
 171  
     }
 172  
 
 173  
     public static boolean isAlreadySetFactory(Map factoryMap, String factoryName) {
 174  2
         return (factoryMap != null && factoryMap.containsKey(factoryName));
 175  
     }
 176  
 
 177  
     public static ApplicationFactory getApplicationFactory() {
 178  40
         return (ApplicationFactory) FactoryFinder
 179  
                 .getFactory(FactoryFinder.APPLICATION_FACTORY);
 180  
     }
 181  
 
 182  
     public static FacesContextFactory getFacesContextFactory() {
 183  3
         return (FacesContextFactory) FactoryFinder
 184  
                 .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
 185  
     }
 186  
 
 187  
     public static LifecycleFactory getLifecycleFactory() {
 188  12
         return (LifecycleFactory) FactoryFinder
 189  
                 .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
 190  
     }
 191  
 
 192  
     public static RenderKitFactory getRenderKitFactory() {
 193  12
         return (RenderKitFactory) FactoryFinder
 194  
                 .getFactory(FactoryFinder.RENDER_KIT_FACTORY);
 195  
     }
 196  
 
 197  
 }