package vut.msa.utilities;

import java.io.Serializable;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

public class FacesServletUtils implements Serializable {

    private FacesContext facesContext;
    private ServletContext servletContext;

    private static FacesServletUtils utils = new FacesServletUtils();

    private FacesServletUtils() {
        setupContext();
    }

    public static synchronized FacesServletUtils getFacesServletUtils(){
        if (utils == null){
            utils = new FacesServletUtils();
        }

        return utils;
    }

    //PROPERTY: facesContext (READ-ONLY)
    public FacesContext getFacesContext() { return facesContext; }

    //PROPERTY: servletContext
    public ServletContext getServletContext() { return servletContext; }

    private void setupContext() {
        facesContext = FacesContext.getCurrentInstance();
        servletContext = (ServletContext) facesContext.getExternalContext().getContext();
    }

    public String getApplicationFilePath (String file) {
        return servletContext.getRealPath(file);
    }

    public String getApplicationFilePath () {
        return servletContext.getRealPath("");
    }
    
}
