@Override public void contextInitialized(ServletContextEvent ce) { LocalJasperReportsContext localJasperReportsContext = new LocalJasperReportsContext(DefaultJasperReportsContext.getInstance()); SimpleFileResolver fileResolver = new SimpleFileResolver( new File( new File(ce.getServletContext().getRealPath("/")), ce.getServletContext().getInitParameter("net.sf.jasperreports.web.file.repository.root") ) ); localJasperReportsContext.setFileResolver(fileResolver); AbstractServlet.setJasperReportsContext(localJasperReportsContext); }
/** * */ @SuppressWarnings("deprecation") protected void ensureJasperReportsContext() { if ( parameters.containsKey(JRExporterParameter.CLASS_LOADER) || parameters.containsKey(JRExporterParameter.URL_HANDLER_FACTORY) || parameters.containsKey(JRExporterParameter.FILE_RESOLVER) ) { LocalJasperReportsContext localJasperReportsContext = new LocalJasperReportsContext(jasperReportsContext); if (parameters.containsKey(JRExporterParameter.CLASS_LOADER)) { localJasperReportsContext.setClassLoader((ClassLoader)parameters.get(JRExporterParameter.CLASS_LOADER)); } if (parameters.containsKey(JRExporterParameter.URL_HANDLER_FACTORY)) { localJasperReportsContext.setURLStreamHandlerFactory((URLStreamHandlerFactory)parameters.get(JRExporterParameter.URL_HANDLER_FACTORY)); } if (parameters.containsKey(JRExporterParameter.FILE_RESOLVER)) { localJasperReportsContext.setFileResolver((FileResolver)parameters.get(JRExporterParameter.FILE_RESOLVER)); } setJasperReportsContext(localJasperReportsContext); } FontUtil.getInstance(jasperReportsContext).resetThreadMissingFontsCache(); }
/** * */ protected JasperReportsContext getLocalJasperReportsContext(File file) { SimpleFileResolver fileResolver = new SimpleFileResolver( Arrays.asList(new File[]{file.getParentFile(), new File(".")}) ); fileResolver.setResolveAbsolutePath(true); LocalJasperReportsContext localJasperReportsContext = new LocalJasperReportsContext(jasperReportsContext); localJasperReportsContext.setFileResolver(fileResolver); return localJasperReportsContext; }
protected final void setParametersToContext(Map<String,Object> parameterValues) { JasperReportsContext localContext = LocalJasperReportsContext.getLocalContext(jasperReportsContext, parameterValues); if (localContext != jasperReportsContext) { setJasperReportsContext(localContext); } }
/** * Evaluates the default values for the parameters of a report. * * @param report the report * @param initialParameters initial parameter value map * @return a map containing parameter values indexed by parameter names * @throws JRException */ public static Map<String,Object> evaluateParameterDefaultValues(JasperReportsContext jasperReportsContext, JasperReport report, Map<String,Object> initialParameters) throws JRException { Map<String,Object> valuesMap = initialParameters == null ? new HashMap<String,Object>() : new HashMap<String,Object>(initialParameters); valuesMap.put(JRParameter.JASPER_REPORT, report); ObjectFactory factory = new ObjectFactory(); JRDataset reportDataset = report.getMainDataset(); JRFillDataset fillDataset = factory.getDataset(reportDataset); fillDataset.setJasperReportsContext(LocalJasperReportsContext.getLocalContext(jasperReportsContext, initialParameters)); fillDataset.createCalculator(report); fillDataset.initCalculator(); JRResourcesFillUtil.ResourcesFillContext resourcesContext = JRResourcesFillUtil.setResourcesFillContext(valuesMap); try { fillDataset.setParameterValues(valuesMap); Map<String,Object> parameterValues = new HashMap<String,Object>(); JRParameter[] parameters = reportDataset.getParameters(); for (int i = 0; i < parameters.length; i++) { JRParameter param = parameters[i]; if (!param.isSystemDefined()) { String name = param.getName(); Object value = fillDataset.getParameterValue(name); parameterValues.put(name, value); } } return parameterValues; } finally { fillDataset.disposeParameterContributors(); JRResourcesFillUtil.revertResourcesFillContext(resourcesContext); } }
/** * */ private void baseInit() { JasperReportsContext localContext = LocalJasperReportsContext.getLocalContext(getJasperReportsContext(), parameterValues); if (localContext != getJasperReportsContext()) { setJasperReportsContext(localContext); } // fillingThread = Thread.currentThread(); try { boundElements = new HashMap<JREvaluationTime, LinkedHashMap<PageKey, LinkedMap<Object, EvaluationBoundAction>>>(); createBoundElementMaps(JREvaluationTime.EVALUATION_TIME_REPORT); createBoundElementMaps(JREvaluationTime.EVALUATION_TIME_PAGE); createBoundElementMaps(JREvaluationTime.EVALUATION_TIME_COLUMN); if (groups != null) { for (int i = 0; i < groups.length; i++) { createBoundElementMaps(JREvaluationTime.getGroupEvaluationTime(groups[i].getName())); } } for (Iterator<JRBand> i = bands.iterator(); i.hasNext();) { JRFillBand band = (JRFillBand) i.next(); createBoundElementMaps(JREvaluationTime.getBandEvaluationTime(band)); } if (parentFiller != null) { parentFiller.registerSubfiller(this); } setParameters(parameterValues); loadStyles(); jasperPrint.setName(name); jasperPrint.setPageWidth(pageWidth); jasperPrint.setPageHeight(pageHeight); jasperPrint.setTopMargin(topMargin); jasperPrint.setLeftMargin(leftMargin); jasperPrint.setBottomMargin(bottomMargin); jasperPrint.setRightMargin(rightMargin); jasperPrint.setOrientation(orientation); jasperPrint.setFormatFactoryClass(jasperReport.getFormatFactoryClass()); jasperPrint.setLocaleCode(JRDataUtils.getLocaleCode(getLocale())); jasperPrint.setTimeZoneId(JRDataUtils.getTimeZoneId(getTimeZone())); jasperPrint.setDefaultStyle(defaultStyle); /* */ if (styles != null && styles.length > 0) { for (int i = 0; i < styles.length; i++) { addPrintStyle(styles[i]); } } /* */ //Done in write(List<T>) mainDataset.start(); } catch (Exception e) { logger.error("Unable to initialise report", e); } }
static public SimpleJasperReportsContext deriveContext() { SimpleJasperReportsContext co = new LocalJasperReportsContext(getInstance()); return co; }
public static JasperPrint fillReport(StorageService storageService, String key, JasperReport jasper, Map<String, Object> params, Connection conn) throws JRException, InterruptedException { Settings settings = storageService.getSettings(); params.put(JRParameter.REPORT_FILE_RESOLVER, new SimpleFileResolver(new File(settings.getJasper().getHome()))); // process stopped before runner starts if (JasperRunnerFactory.containsRunner(key)) { JasperRunnerFactory.removeRunner(key); ConnectionUtil.closeConnection(conn); return null; } // System.out.println("------------------------------"); // for (String s : params.keySet()) { // System.out.println(" -> param="+s + " [" + params.get(s) + "]"); // } // System.out.println("------------------------------"); // Jasper 5.1.0+ if (ctx == null) { LocalJasperReportsContext localContext = new LocalJasperReportsContext(DefaultJasperReportsContext.getInstance()); localContext.setClassLoader(JasperReportsUtil.class.getClassLoader()); localContext.setFileResolver(new SimpleFileResolver(new File(settings.getJasper().getHome()))); ctx = localContext; } final JasperAsynchronousFillHandle handle = new JasperAsynchronousFillHandle(ctx, jasper, params, conn); // final JasperAsynchronousFillHandle handle = new JasperAsynchronousFillHandle(jasper, params, conn); JasperPrint print = null; try { JasperRunnerFactory.addRunner(key, handle); //Start the asynchronous thread to fill the report handle.startFill(); //Wait until the thread ends to get the result handle.getFillThread().join(); if (!handle.isCancelled()) { print = handle.getJasperPrint(); } else { throw new InterruptedException("Running process was interrupted."); } } catch (InterruptedException ie) { throw ie; } catch (Exception e) { throw new JRException(e.getMessage()); } finally { JasperRunnerFactory.removeRunner(key); } return print; }