001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.bridges.jsf.myfaces;
016    
017    import javax.faces.FacesException;
018    import javax.faces.context.FacesContext;
019    import javax.faces.context.FacesContextFactory;
020    import javax.faces.lifecycle.Lifecycle;
021    
022    import javax.portlet.PortletContext;
023    import javax.portlet.PortletRequest;
024    import javax.portlet.PortletResponse;
025    
026    import javax.servlet.ServletContext;
027    import javax.servlet.ServletRequest;
028    import javax.servlet.ServletResponse;
029    
030    import org.apache.myfaces.context.servlet.ServletFacesContextImpl;
031    
032    /**
033     * @author Brian Myunghun Kim
034     */
035    public class MyFacesContextFactoryImpl extends FacesContextFactory {
036    
037            public FacesContext getFacesContext(
038                            Object context, Object request, Object response,
039                            Lifecycle lifecycle)
040                    throws FacesException {
041    
042                    if (context == null) {
043                            throw new NullPointerException("context");
044                    }
045    
046                    if (request == null) {
047                            throw new NullPointerException("request");
048                    }
049    
050                    if (response == null) {
051                            throw new NullPointerException("response");
052                    }
053    
054                    if (lifecycle == null) {
055                            throw new NullPointerException("lifecycle");
056                    }
057    
058                    if (context instanceof ServletContext) {
059                            return new ServletFacesContextImpl(
060                                    (ServletContext)context,
061                                    (ServletRequest)request,
062                                    (ServletResponse)response);
063                    }
064    
065                    if (context instanceof PortletContext) {
066                            return new MyFacesContextImpl(
067                                    (PortletContext)context, (PortletRequest)request,
068                                    (PortletResponse)response);
069                    }
070    
071                    throw new FacesException(
072                            "Unsupported context type " + getClass().getName());
073            }
074    
075    }