p:graphicImage doen't work: Error in streaming dynamic resource
===

I wanna to do a gantt diagram with jfreechart, but I don't know appear this error:

    31-jul-2012 9:50:34 org.primefaces.application.PrimeResourceHandler handleResourceRequest
    GRAVE: Error in streaming dynamic resource.

I tryed change `@SessionScope` or  `@ViewScope` but does't work.

This is my java code where I construct JFreeChart and save into png:

> 

    package logistica;
    > 
    > import java.io.File; import java.io.IOException; import
    > java.io.InputStream; import java.util.Calendar; import java.util.Date;
    > import java.util.HashSet; import java.util.List; import java.util.Set;
    > 
    > import javax.activation.FileDataSource; import javax.ejb.EJB; import
    > javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped;
    > import javax.faces.context.FacesContext;
    > 
    > import org.jfree.chart.ChartFactory; import
    > org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart;
    > import org.jfree.data.category.IntervalCategoryDataset; import
    > org.jfree.data.gantt.Task; import org.jfree.data.gantt.TaskSeries;
    > import org.jfree.data.gantt.TaskSeriesCollection; import
    > org.jfree.data.time.SimpleTimePeriod; import
    > org.primefaces.model.DefaultStreamedContent; import
    > org.primefaces.model.StreamedContent;
    > 
    > import ejb.dao.AeronaveDaoLocal; import ejb.entidades.Aeronave; import
    > GenericBackingBean; import IBackingBean; import
    > tablasBasicas.aeronaves.ejb.dao.SistemaArmasParticularDaoLocal; import
    > aeronaves.ejb.entidades.SistemaArmasGenerico; import
    > ejb.entidades.SistemaArmasParticular; import
    > ejb.dao.EscuadronDaoLocal; import ejb.entidades.Escuadron;
    > 
    > /**  * Clase MantNivCBB TODO  *   */ @ManagedBean @RequestScoped
    > public class MantNivCBB extends GenericBackingBean<MantNivCBB>
    > implements 		IBackingBean<MantNivCBB> { 	/** 	 * instancioa del
    > MantNivCBB. 	 */ 	public static MantNivCBB getInstance() { 		return
    > FacesContext
    > 				.getCurrentInstance()
    > 				.getApplication()
    > 				.evaluateExpressionGet(FacesContext.getCurrentInstance(),
    > 						"#{mantNivCBB}", MantNivCBB.class); 	}
    >
    	/**
    	 * @see ea.ciges.siucom.backingBeans.IBackingBean#saveData().
    	 */
    	@Override
    	public void saveData() {
    	}
    
    	/**
    	 * @see ea.ciges.siucom.backingBeans.GenericBackingBean#loadDataModel().
    	 */
    	@Override
    	public void loadDataModel() {
    		aeDao.setClase(Aeronave.class);
    		escDao.setClase(Escuadron.class);
    		sAPDao.setClase(SistemaArmasParticular.class);
    		List<SistemaArmasParticular> lEscTemp = escDao
    				.findSistArmasPByValido(true);
    		lSAGen = new HashSet<SistemaArmasGenerico>();
    		for (SistemaArmasParticular sap : lEscTemp) {
    			if (sap.getSistemaArmasGenerico() != null) {
    				lSAGen.add(sap.getSistemaArmasGenerico());
    			}
    		}
    		lSAPar = new HashSet<SistemaArmasParticular>();
    		for (SistemaArmasParticular sap : lEscTemp) {
    			if (sap != null) {
    				lSAPar.add(sap);
    			}
    		}
    		anyo = Calendar.getInstance().get(Calendar.YEAR);
    	}
    
    	/**
    	 * @see ea.ciges.siucom.backingBeans.GenericBackingBean#createEntidad().
    	 */
    	@Override
    	public MantNivCBB createEntidad() {
    		return new MantNivCBB();
    	}
    
    	/**
    	 * ejb de HistoricoInspeccionDaoLocal.
    	 */
    	@EJB
    	private AeronaveDaoLocal aeDao;
    	/**
    	 * ejb de EscuadronDaoLocal.
    	 */
    	@EJB
    	private EscuadronDaoLocal escDao;
    	/**
    	 * ejb de SistemaArmasParticularDaoLocal.
    	 */
    	@EJB
    	private SistemaArmasParticularDaoLocal sAPDao;
    	/**
    	 * List<SistemaArmasGenerico>.
    	 */
    	private Set<SistemaArmasGenerico> lSAGen = new HashSet<SistemaArmasGenerico>();
    	/**
    	 * List<SistemaArmasParticular>.
    	 */
    	private Set<SistemaArmasParticular> lSAPar = new HashSet<SistemaArmasParticular>();
    	/**
    	 * int.
    	 */
    	private int anyo;
    	/**
    	 * String.
    	 */
    	private String sAGen, sAPar;
    	/**
    	 * StreamedContent.
    	 */
    	private StreamedContent chartImage;
    
    	/**
    	 * cambiaSisArmGenSeleccionado.
    	 */
    	public void cambiaSisArmGenSeleccionado() {
    		if (sAGen == null) {
    			lSAPar = null;
    			List<SistemaArmasParticular> lEscTemp = escDao
    					.findSistArmasPByValido(true);
    			lSAPar = new HashSet<SistemaArmasParticular>();
    			for (SistemaArmasParticular sap : lEscTemp) {
    				if (sap != null) {
    					lSAPar.add(sap);
    				}
    			}
    		} else {
    			lSAPar = new HashSet<SistemaArmasParticular>();
    			lSAPar.addAll(sAPDao
    					.findBySistemaArmasGenerico(new SistemaArmasGenerico(sAGen)));
    		}
    		sAPar = null;
    	}
    	/** 
    	 * generaGraf.
    	 */
    	public void generaGraf() {
    		JFreeChart chart = ChartFactory.createGanttChart("Gantt Chart Demo",
    				"Task", "Date", generaGannt(), true, true, false);
    		generaImgGraf(chart);		
    	}
    	/**
    	 * generaImgGraf.
    	 * @param chart JFreeChart.
    	 */
    	public void generaImgGraf (JFreeChart chart) {
    		String ruta = System.getProperty("user.dir") + File.separator
    				+ "fotos-tripulante" + File.separator + "dg.jpg";
    		try {
    			File chartFile = new File(ruta);
    			ChartUtilities.saveChartAsPNG(chartFile, chart, 375, 300);
    			InputStream stream = new FileDataSource(ruta).getInputStream();
    			chartImage = new DefaultStreamedContent(stream, "image/png");
    		} catch (IOException e) {
    			ea.ciges.siucom.zsv.log.Logger.error(
    					"Excepcion capturada al obtener la imagen", e);
    		}
    	}
    
    	/**
    	 * generaGannt.
    	 * 
    	 * @return
    	 */
    	private IntervalCategoryDataset generaGannt() {
    		final TaskSeries s1 = new TaskSeries("Scheduled");
    		s1.add(new Task("cheli", new SimpleTimePeriod(date(1, Calendar.APRIL,
    				2001), date(5, Calendar.APRIL, 2001))));
    		s1.add(new Task("Obtain Approval", new SimpleTimePeriod(date(9,
    				Calendar.APRIL, 2001), date(9, Calendar.APRIL, 2001))));
    		s1.add(new Task("Requirements Analysis", new SimpleTimePeriod(date(10,
    				Calendar.APRIL, 2001), date(5, Calendar.MAY, 2001))));
    		s1.add(new Task("Design Phase", new SimpleTimePeriod(date(6,
    				Calendar.MAY, 2001), date(30, Calendar.MAY, 2001))));
    		s1.add(new Task("Design Signoff", new SimpleTimePeriod(date(2,
    				Calendar.JUNE, 2001), date(2, Calendar.JUNE, 2001))));
    		s1.add(new Task("Alpha Implementation", new SimpleTimePeriod(date(3,
    				Calendar.JUNE, 2001), date(31, Calendar.JULY, 2001))));
    		s1.add(new Task("Design Review", new SimpleTimePeriod(date(1,
    				Calendar.AUGUST, 2001), date(8, Calendar.AUGUST, 2001))));
    		s1.add(new Task("Revised Design Signoff", new SimpleTimePeriod(date(10,
    				Calendar.AUGUST, 2001), date(10, Calendar.AUGUST, 2001))));
    		s1.add(new Task("Beta Implementation", new SimpleTimePeriod(date(12,
    				Calendar.AUGUST, 2001), date(12, Calendar.SEPTEMBER, 2001))));
    		s1.add(new Task("Testing", new SimpleTimePeriod(date(13,
    				Calendar.SEPTEMBER, 2001), date(31, Calendar.OCTOBER, 2001))));
    		s1.add(new Task("Final Implementation", new SimpleTimePeriod(date(1,
    				Calendar.NOVEMBER, 2001), date(15, Calendar.NOVEMBER, 2001))));
    		s1.add(new Task("Signoff", new SimpleTimePeriod(date(28,
    				Calendar.NOVEMBER, 2001), date(30, Calendar.NOVEMBER, 2001))));
    
    		final TaskSeries s2 = new TaskSeries("Actual");
    		s2.add(new Task("Write Proposal", new SimpleTimePeriod(date(1,
    				Calendar.APRIL, 2001), date(5, Calendar.APRIL, 2001))));
    		s2.add(new Task("Obtain Approval", new SimpleTimePeriod(date(9,
    				Calendar.APRIL, 2001), date(9, Calendar.APRIL, 2001))));
    		s2.add(new Task("Requirements Analysis", new SimpleTimePeriod(date(10,
    				Calendar.APRIL, 2001), date(15, Calendar.MAY, 2001))));
    		s2.add(new Task("Design Phase", new SimpleTimePeriod(date(15,
    				Calendar.MAY, 2001), date(17, Calendar.JUNE, 2001))));
    		s2.add(new Task("Design Signoff", new SimpleTimePeriod(date(30,
    				Calendar.JUNE, 2001), date(30, Calendar.JUNE, 2001))));
    		s2.add(new Task("Alpha Implementation", new SimpleTimePeriod(date(1,
    				Calendar.JULY, 2001), date(12, Calendar.SEPTEMBER, 2001))));
    		s2.add(new Task("Design Review", new SimpleTimePeriod(date(12,
    				Calendar.SEPTEMBER, 2001), date(22, Calendar.SEPTEMBER, 2001))));
    		s2.add(new Task("Revised Design Signoff", new SimpleTimePeriod(date(25,
    				Calendar.SEPTEMBER, 2001), date(27, Calendar.SEPTEMBER, 2001))));
    		s2.add(new Task("Beta Implementation", new SimpleTimePeriod(date(27,
    				Calendar.SEPTEMBER, 2001), date(30, Calendar.OCTOBER, 2001))));
    		s2.add(new Task("Testing", new SimpleTimePeriod(date(31,
    				Calendar.OCTOBER, 2001), date(17, Calendar.NOVEMBER, 2001))));
    		s2.add(new Task("Final Implementation", new SimpleTimePeriod(date(18,
    				Calendar.NOVEMBER, 2001), date(5, Calendar.DECEMBER, 2001))));
    		s2.add(new Task("Signoff", new SimpleTimePeriod(date(10,
    				Calendar.DECEMBER, 2001), date(11, Calendar.DECEMBER, 2001))));
    		final TaskSeriesCollection collection = new TaskSeriesCollection();
    		collection.add(s1);
    		collection.add(s2);
    		return collection;
    	}
    
    	/**
    	 * Utility method for creating <code>Date</code> objects.
    	 * 
    	 * @param day
    	 *            the date.
    	 * @param month
    	 *            the month.
    	 * @param year
    	 *            the year.
    	 * 
    	 * @return a date.
    	 */
    	private static Date date(final int day, final int month, final int year) {
    		final Calendar calendar = Calendar.getInstance();
    		calendar.set(year, month, day);
    		final Date result = calendar.getTime();
    		return result;
    	}
    
    	/**
    	 * Devuelve el valor de aeDao.
    	 * 
    	 * @return AeronaveDaoLocal, aeDao
    	 */
    	public final AeronaveDaoLocal getAeDao() {
    		return aeDao;
    	}
    
    	/**
    	 * Establece el valor de aeDao.
    	 * 
    	 * @param aeDao
    	 *            , AeronaveDaoLocal
    	 */
    	public final void setAeDao(AeronaveDaoLocal aeDao) {
    		this.aeDao = aeDao;
    	}
    
    	/**
    	 * Devuelve el valor de escDao.
    	 * 
    	 * @return EscuadronDaoLocal, escDao
    	 */
    	public final EscuadronDaoLocal getEscDao() {
    		return escDao;
    	}
    
    	/**
    	 * Establece el valor de escDao.
    	 * 
    	 * @param escDao
    	 *            , EscuadronDaoLocal
    	 */
    	public final void setEscDao(EscuadronDaoLocal escDao) {
    		this.escDao = escDao;
    	}
    
    	/**
    	 * Devuelve el valor de sAPDao.
    	 * 
    	 * @return SistemaArmasParticularDaoLocal, sAPDao
    	 */
    	public final SistemaArmasParticularDaoLocal getsAPDao() {
    		return sAPDao;
    	}
    
    	/**
    	 * Establece el valor de sAPDao.
    	 * 
    	 * @param sAPDao
    	 *            , SistemaArmasParticularDaoLocal
    	 */
    	public final void setsAPDao(SistemaArmasParticularDaoLocal sAPDao) {
    		this.sAPDao = sAPDao;
    	}
    
    	/**
    	 * Devuelve el valor de lSAGen.
    	 * 
    	 * @return Set<SistemaArmasGenerico>, lSAGen
    	 */
    	public final Set<SistemaArmasGenerico> getlSAGen() {
    		return lSAGen;
    	}
    
    	/**
    	 * Establece el valor de lSAGen.
    	 * 
    	 * @param lSAGen
    	 *            , Set<SistemaArmasGenerico>
    	 */
    	public final void setlSAGen(Set<SistemaArmasGenerico> lSAGen) {
    		this.lSAGen = lSAGen;
    	}
    
    	/**
    	 * Devuelve el valor de lSAPar.
    	 * 
    	 * @return Set<SistemaArmasParticular>, lSAPar
    	 */
    	public final Set<SistemaArmasParticular> getlSAPar() {
    		return lSAPar;
    	}
    
    	/**
    	 * Establece el valor de lSAPar.
    	 * 
    	 * @param lSAPar
    	 *            , Set<SistemaArmasParticular>
    	 */
    	public final void setlSAPar(Set<SistemaArmasParticular> lSAPar) {
    		this.lSAPar = lSAPar;
    	}
    
    	/**
    	 * Devuelve el valor de anyo.
    	 * 
    	 * @return int, anyo
    	 */
    	public final int getAnyo() {
    		return anyo;
    	}
    
    	/**
    	 * Establece el valor de anyo.
    	 * 
    	 * @param anyo
    	 *            , int
    	 */
    	public final void setAnyo(int anyo) {
    		this.anyo = anyo;
    	}
    
    	/**
    	 * Devuelve el valor de sAGen.
    	 * 
    	 * @return String, sAGen
    	 */
    	public final String getsAGen() {
    		return sAGen;
    	}
    
    	/**
    	 * Establece el valor de sAGen.
    	 * 
    	 * @param sAGen
    	 *            , String
    	 */
    	public final void setsAGen(String sAGen) {
    		this.sAGen = sAGen;
    	}
    
    	/**
    	 * Devuelve el valor de sAPar.
    	 * 
    	 * @return String, sAPar
    	 */
    	public final String getsAPar() {
    		return sAPar;
    	}
    
    	/**
    	 * Establece el valor de sAPar.
    	 * 
    	 * @param sAPar
    	 *            , String
    	 */
    	public final void setsAPar(String sAPar) {
    		this.sAPar = sAPar;
    	}
    
    	/**
    	 * Devuelve el valor de chartImage.
    	 * 
    	 * @return StreamedContent, chartImage
    	 */
    	public final StreamedContent getChartImage() {
    		return chartImage;
    	}
    
    	/**
    	 * Establece el valor de chartImage.
    	 * 
    	 * @param chartImage
    	 *            , StreamedContent
    	 */
    	public final void setChartImage(StreamedContent chartImage) {
    		this.chartImage = chartImage;
    	}
    }`

And this are my xhtml code :


    <ui:composition xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:p="http://primefaces.org/ui"
	template="/plantillas/plantillaGeneral.xhtml">
	<ui:define name="rastroMigas">
		<div id="rastroMigas">
			<h:form>
				<p:breadCrumb>
					<p:menuitem
						value="#{bundleComunes.SIUCOM}-#{controllerLogIn.unidadMigas}"
						url="/menu/MnSIUCOM.ciges" />
					<p:menuitem
						value="#{bundleAviones.logistica}-#{bundleAviones.mantNivC}"
						ajax="false"
						action="#{controllerTemp.cargarPagina(false, 'SIUQ1AIN')}" />
				</p:breadCrumb>
			</h:form>
		</div>
	</ui:define>
	<ui:define name="contenido">
		<div id="contenido">
			<div id="centroSIUCOMFichaT">
				<h:panelGrid id="tabla" styleClass="tablaAncho800">
					<h:outputLabel value="#{bundleAviones.mantNivC}"
						styleClass="tituloPagina" />
					<h:form styleClass="tablaAncho800">
						<p:ajaxStatus>
							<f:facet name="start">
								<h:graphicImage value="/img/ajaxload.gif" />
							</f:facet>
							<f:facet name="complete">
								<h:outputText value="" />
							</f:facet>
						</p:ajaxStatus>
						<p:panel styleClass="fondoAzul bordeAzul tfont12">
							<h:panelGrid columns="2" style="width:100%">
								<h:panelGroup style="width:50%">
									<h:outputLabel value="#{bundleComunes.anyo}"
										styleClass=" letraNegrita tfont14 lAzul lMayuscula" />
									<p:spinner id="spinnerBasic" value="#{mantNivCBB.anyo}"/>
									<h:outputLabel value="#{bundleAviones.sistArmasGenerico}"
										styleClass=" letraNegrita tfont14 lAzul lMayuscula" />
									<h:selectOneMenu id="lsag" style="width:110px" value="#{mantNivCBB.sAGen}">
										<f:selectItem itemLabel="#{bundleComunes.seleccionar}"
											itemValue="" />
										<f:selectItems value="#{mantNivCBB.lSAGen}" var="sag"
											itemLabel="#{sag.id}" itemValue="#{sag.id}" />
										<f:ajax render="lsap" event="change"
											listener="#{mantNivCBB.cambiaSisArmGenSeleccionado}" />
									</h:selectOneMenu>
									<h:outputLabel value="#{bundleAviones.sistArmasParticular}"
										styleClass=" letraNegrita tfont14 lAzul lMayuscula" />
									<h:selectOneMenu id="lsap" style="width:110px" value="#{mantNivCBB.sAPar}">
										<f:selectItem itemLabel="#{bundleComunes.todos}" itemValue="*" />
										<f:selectItems value="#{mantNivCBB.lSAPar}" var="sap"
											itemLabel="#{sap.id}" itemValue="#{sap.id}" />
										<f:ajax event="change" />
									</h:selectOneMenu>
								</h:panelGroup>
								<h:panelGroup styleClass="botones" style="width:200px">
									<p:commandButton title=	"#{bundleAviones.generarDiagonal}"
										action="#{mantNivCBB.generaGraf}"
										update="img" id="prin" ajax="false"
										value="#{bundleAviones.generarDiagonal}"
										alt="#{bundleAviones.generarDiagonal}" />
								</h:panelGroup>
							</h:panelGrid>
						</p:panel>
						<p:graphicImage rendered="#{mantNivCBB.chartImage!=null}" id="img" value="#{mantNivCBB.chartImage}" height="375px" width="300px" />
					</h:form>
				</h:panelGrid>
			</div>
		</div>
	</ui:define>
</ui:composition>