Save This Page
Home » openjdk-7 » net.sourceforge » myfaces » [javadoc | source]
    1   /**
    2    * MyFaces - the free JSF implementation
    3    * Copyright (C) 2003  The MyFaces Team (http://myfaces.sourceforge.net)
    4    *
    5    * This library is free software; you can redistribute it and/or
    6    * modify it under the terms of the GNU Lesser General Public
    7    * License as published by the Free Software Foundation; either
    8    * version 2.1 of the License, or (at your option) any later version.
    9    *
   10    * This library is distributed in the hope that it will be useful,
   11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
   12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   13    * Lesser General Public License for more details.
   14    *
   15    * You should have received a copy of the GNU Lesser General Public
   16    * License along with this library; if not, write to the Free Software
   17    * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
   18    */
   19   package net.sourceforge.myfaces;
   20   
   21   import net.sourceforge.myfaces.config.FacesConfigFactory;
   22   import net.sourceforge.myfaces.config.FacesConfigFactoryImpl;
   23   import net.sourceforge.myfaces.webapp.ServletMapping;
   24   import net.sourceforge.myfaces.webapp.ServletMappingFactory;
   25   import net.sourceforge.myfaces.webapp.ServletMappingFactoryImpl;
   26   
   27   import javax.faces.FacesException;
   28   import javax.servlet.ServletContext;
   29   import java.util.HashMap;
   30   
   31   /**
   32    * DOCUMENT ME!
   33    * @author Manfred Geiler (latest modification by $Author: manolito $)
   34    * @version $Revision: 1.9 $ $Date: 2003/07/18 08:33:02 $
   35    */
   36   public class MyFacesFactoryFinder
   37   {
   38       public static final String SERVLET_MAPPING_FACTORY = ServletMapping.class.getName();
   39       public static final String FACES_CONFIG_FACTORY = FacesConfigFactory.class.getName();
   40   
   41       private static final HashMap DEFAULT_FACTORIES = new HashMap();
   42       static
   43       {
   44           DEFAULT_FACTORIES.put(SERVLET_MAPPING_FACTORY, ServletMappingFactoryImpl.class.getName());
   45           DEFAULT_FACTORIES.put(FACES_CONFIG_FACTORY, FacesConfigFactoryImpl.class.getName());
   46       }
   47   
   48       private static Object getFactory(ServletContext servletContext, String factoryName)
   49               throws FacesException
   50       {
   51           Object fact = servletContext.getAttribute(factoryName);
   52           if (fact == null)
   53           {
   54               String compFactClass = servletContext.getInitParameter(factoryName);
   55               if (compFactClass == null)
   56               {
   57                   compFactClass = (String)DEFAULT_FACTORIES.get(factoryName);
   58                   if (compFactClass == null)
   59                   {
   60                       throw new FacesException("Unknown factory " + factoryName);
   61                   }
   62               }
   63   
   64               try
   65               {
   66                   Class c = Class.forName(compFactClass, true, Thread.currentThread().getContextClassLoader());
   67                   fact = c.newInstance();
   68               }
   69               catch (ClassNotFoundException e)
   70               {
   71                   throw new FacesException(e);
   72               }
   73               catch (InstantiationException e)
   74               {
   75                   throw new FacesException(e);
   76               }
   77               catch (IllegalAccessException e)
   78               {
   79                   throw new FacesException(e);
   80               }
   81   
   82               servletContext.setAttribute(factoryName, fact);
   83           }
   84   
   85           return fact;
   86       }
   87   
   88   
   89       public static ServletMappingFactory getServletMappingFactory(ServletContext servletContext)
   90           throws FacesException
   91       {
   92           return (ServletMappingFactory)getFactory(servletContext, SERVLET_MAPPING_FACTORY);
   93       }
   94   
   95       public static FacesConfigFactory getFacesConfigFactory(ServletContext servletContext)
   96           throws FacesException
   97       {
   98           return (FacesConfigFactory)getFactory(servletContext, FACES_CONFIG_FACTORY);
   99       }
  100   
  101   }

Save This Page
Home » openjdk-7 » net.sourceforge » myfaces » [javadoc | source]