net.sf.jasperreports.engine.util.SimpleFileResolver Java Examples

The following examples show how to use net.sf.jasperreports.engine.util.SimpleFileResolver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JasperReportsUtil.java    From nextreports-server with Apache License 2.0 4 votes vote down vote up
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;
    }