001    /**
002     * Copyright (c) 2000-2013 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            @Override
038            public FacesContext getFacesContext(
039                            Object context, Object request, Object response,
040                            Lifecycle lifecycle)
041                    throws FacesException {
042    
043                    if (context == null) {
044                            throw new NullPointerException("context");
045                    }
046    
047                    if (request == null) {
048                            throw new NullPointerException("request");
049                    }
050    
051                    if (response == null) {
052                            throw new NullPointerException("response");
053                    }
054    
055                    if (lifecycle == null) {
056                            throw new NullPointerException("lifecycle");
057                    }
058    
059                    if (context instanceof ServletContext) {
060                            return new ServletFacesContextImpl(
061                                    (ServletContext)context, (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    }