List of usage examples for org.springframework.core.io ClassPathResource exists
@Override public boolean exists()
From source file:org.kuali.kfs.sys.service.impl.ReportGenerationServiceImpl.java
/** * get a class path resource that references to the given report template * /* w ww . j ava 2 s. c o m*/ * @param reportTemplateName the given report template name with its full-qualified package name. It may not include extension. * If an extension is included in the name, it should be prefixed ".jasper" or '.jrxml". * @return a class path resource that references to the given report template */ protected ClassPathResource getReportTemplateClassPathResource(String reportTemplateName) { if (reportTemplateName.endsWith(ReportGeneration.DESIGN_FILE_EXTENSION) || reportTemplateName.endsWith(ReportGeneration.JASPER_REPORT_EXTENSION)) { return new ClassPathResource(reportTemplateName); } String jasperReport = reportTemplateName.concat(ReportGeneration.JASPER_REPORT_EXTENSION); ClassPathResource resource = new ClassPathResource(jasperReport); if (resource.exists()) { return resource; } String designTemplate = reportTemplateName.concat(ReportGeneration.DESIGN_FILE_EXTENSION); resource = new ClassPathResource(designTemplate); return resource; }
From source file:org.kuali.kfs.sys.service.impl.ReportGenerationServiceImpl.java
/** * complie the report template xml file into a Jasper report file if the compiled file does not exist or is out of update * /*from w w w . ja v a 2 s.c om*/ * @param template the name of the template file, without an extension * @return an input stream where the intermediary report was written */ protected File compileReportTemplate(String template) throws JRException, IOException { ClassPathResource designTemplateResource = new ClassPathResource(template); if (!designTemplateResource.exists()) { throw new RuntimeException("The design template file does not exist: " + template); } File tempJasperDir = new File( System.getProperty("java.io.tmpdir") + File.separator + template.replaceAll("\\/[^\\/]+$", "")); if (!tempJasperDir.exists()) { FileUtils.forceMkdir(tempJasperDir); } File tempJasperFile = new File(System.getProperty("java.io.tmpdir") + File.separator + template.replace(ReportGeneration.DESIGN_FILE_EXTENSION, "") .concat(ReportGeneration.JASPER_REPORT_EXTENSION)); if (!tempJasperFile.exists()) { JasperCompileManager.compileReportToStream(designTemplateResource.getInputStream(), new FileOutputStream(tempJasperFile)); } return tempJasperFile; }
From source file:org.kuali.ole.sys.service.impl.ReportGenerationServiceImpl.java
/** * The dataSource can be an instance of JRDataSource, java.util.Collection or object array. * /* w ww. j a v a 2 s . c om*/ * @see org.kuali.ole.sys.batch.service.ReportGenerationService#generateReportToPdfFile(java.util.Map, java.lang.Object, java.lang.String, * java.lang.String) */ public void generateReportToPdfFile(Map<String, Object> reportData, Object dataSource, String template, String reportFileName) { ClassPathResource resource = getReportTemplateClassPathResource(template); if (resource == null || !resource.exists()) { throw new IllegalArgumentException("Cannot find the template file: " + template); } try { if (reportData != null && reportData.containsKey(PARAMETER_NAME_SUBREPORT_TEMPLATE_NAME)) { Map<String, String> subReports = (Map<String, String>) reportData .get(PARAMETER_NAME_SUBREPORT_TEMPLATE_NAME); String subReportDirectory = (String) reportData.get(PARAMETER_NAME_SUBREPORT_DIR); compileSubReports(subReports, subReportDirectory); } String realTemplateNameWithoutExtension = removeTemplateExtension(resource); String designTemplateName = realTemplateNameWithoutExtension.concat(DESIGN_FILE_EXTENSION); String jasperReportName = realTemplateNameWithoutExtension.concat(JASPER_REPORT_EXTENSION); compileReportTemplate(designTemplateName, jasperReportName); JRDataSource jrDataSource = JasperReportsUtils.convertReportData(dataSource); reportFileName = reportFileName + PDF_FILE_EXTENSION; File reportDirectory = new File(StringUtils.substringBeforeLast(reportFileName, SEPARATOR)); if (!reportDirectory.exists()) { reportDirectory.mkdir(); } JasperRunManager.runReportToPdfFile(jasperReportName, reportFileName, reportData, jrDataSource); } catch (Exception e) { LOG.error(e); throw new RuntimeException("Fail to generate report.", e); } }
From source file:org.kuali.ole.sys.service.impl.ReportGenerationServiceImpl.java
/** * @see org.kuali.ole.sys.batch.service.ReportGenerationService#generateReportToOutputStream(java.util.Map, java.lang.Object, * java.lang.String, java.io.ByteArrayOutputStream) *///from ww w .j a v a 2s . com public void generateReportToOutputStream(Map<String, Object> reportData, Object dataSource, String template, ByteArrayOutputStream baos) { ClassPathResource resource = getReportTemplateClassPathResource(template); if (resource == null || !resource.exists()) { throw new IllegalArgumentException("Cannot find the template file: " + template); } try { if (reportData != null && reportData.containsKey(PARAMETER_NAME_SUBREPORT_TEMPLATE_NAME)) { Map<String, String> subReports = (Map<String, String>) reportData .get(PARAMETER_NAME_SUBREPORT_TEMPLATE_NAME); String subReportDirectory = (String) reportData.get(PARAMETER_NAME_SUBREPORT_DIR); compileSubReports(subReports, subReportDirectory); } String realTemplateNameWithoutExtension = removeTemplateExtension(resource); String designTemplateName = realTemplateNameWithoutExtension.concat(DESIGN_FILE_EXTENSION); String jasperReportName = realTemplateNameWithoutExtension.concat(JASPER_REPORT_EXTENSION); compileReportTemplate(designTemplateName, jasperReportName); JRDataSource jrDataSource = JasperReportsUtils.convertReportData(dataSource); InputStream inputStream = new FileInputStream(jasperReportName); JasperRunManager.runReportToPdfStream(inputStream, (OutputStream) baos, reportData, jrDataSource); } catch (Exception e) { LOG.error(e); throw new RuntimeException("Fail to generate report.", e); } }
From source file:org.kuali.ole.sys.service.impl.ReportGenerationServiceImpl.java
/** * get a class path resource that references to the given report template * /*from w w w. j ava2s . com*/ * @param reportTemplateName the given report template name with its full-qualified package name. It may not include extension. * If an extension is included in the name, it should be prefixed ".jasper" or '.jrxml". * @return a class path resource that references to the given report template */ protected ClassPathResource getReportTemplateClassPathResource(String reportTemplateName) { if (reportTemplateName.endsWith(DESIGN_FILE_EXTENSION) || reportTemplateName.endsWith(JASPER_REPORT_EXTENSION)) { return new ClassPathResource(reportTemplateName); } String jasperReport = reportTemplateName.concat(JASPER_REPORT_EXTENSION); ClassPathResource resource = new ClassPathResource(jasperReport); if (resource.exists()) { return resource; } String designTemplate = reportTemplateName.concat(DESIGN_FILE_EXTENSION); resource = new ClassPathResource(designTemplate); return resource; }
From source file:org.orcid.frontend.web.controllers.BaseController.java
@ModelAttribute("startupDate") public Date getStartupDate() { // If the cdn config file is missing, we are in development env and we // need to refresh the cache ClassPathResource configFile = new ClassPathResource(this.cdnConfigFile); if (!configFile.exists()) { return new Date(); }//from w w w. j a va 2s .c om return startupDate; }
From source file:org.orcid.frontend.web.controllers.BaseController.java
/** * Return the path where the static content will be. If there is a cdn path * configured, it will return the cdn path; if it is not a cdn path it will * return a reference to the static folder "/static" * /* w ww . j a va 2s . co m*/ * @return the path to the CDN or the path to the local static content * */ @ModelAttribute("staticCdn") @Cacheable("staticContent") public String getStaticCdnPath() { if (StringUtils.isEmpty(this.cdnConfigFile)) { return getStaticContentPath(); } ClassPathResource configFile = new ClassPathResource(this.cdnConfigFile); if (configFile.exists()) { try (InputStream is = configFile.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is))) { String uri = br.readLine(); if (uri != null) this.staticCdnPath = uri; } catch (IOException e) { e.printStackTrace(); } } if (StringUtils.isBlank(this.staticCdnPath)) this.staticCdnPath = this.getStaticContentPath(); return staticCdnPath; }
From source file:org.springframework.test.context.jdbc.DatabaseInitializerTestExecutionListener.java
/** * Detect a default SQL script by implementing the algorithm defined in * {@link DatabaseInitializer#scripts}.//from ww w.j av a 2 s . c o m */ private String detectDefaultScript(TestContext testContext, boolean classLevel) { Class<?> clazz = testContext.getTestClass(); Method method = testContext.getTestMethod(); String elementType = (classLevel ? "class" : "method"); String elementName = (classLevel ? clazz.getName() : method.toString()); String resourcePath = ClassUtils.convertClassNameToResourcePath(clazz.getName()); if (!classLevel) { resourcePath += "." + method.getName(); } resourcePath += ".sql"; String prefixedResourcePath = ResourceUtils.CLASSPATH_URL_PREFIX + resourcePath; ClassPathResource classPathResource = new ClassPathResource(resourcePath); if (classPathResource.exists()) { if (logger.isInfoEnabled()) { logger.info(String.format("Detected default SQL script \"%s\" for test %s [%s]", prefixedResourcePath, elementType, elementName)); } return prefixedResourcePath; } else { String msg = String.format("Could not detect default SQL script for test %s [%s]: " + "%s does not exist. Either declare scripts via @DatabaseInitializer or make the " + "default SQL script available.", elementType, elementName, classPathResource); logger.error(msg); throw new IllegalStateException(msg); } }
From source file:org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener.java
/** * Detect a default SQL script by implementing the algorithm defined in * {@link Sql#scripts}.//from w w w .j av a2 s . com */ private String detectDefaultScript(TestContext testContext, boolean classLevel) { Class<?> clazz = testContext.getTestClass(); Method method = testContext.getTestMethod(); String elementType = (classLevel ? "class" : "method"); String elementName = (classLevel ? clazz.getName() : method.toString()); String resourcePath = ClassUtils.convertClassNameToResourcePath(clazz.getName()); if (!classLevel) { resourcePath += "." + method.getName(); } resourcePath += ".sql"; String prefixedResourcePath = ResourceUtils.CLASSPATH_URL_PREFIX + resourcePath; ClassPathResource classPathResource = new ClassPathResource(resourcePath); if (classPathResource.exists()) { if (logger.isInfoEnabled()) { logger.info(String.format("Detected default SQL script \"%s\" for test %s [%s]", prefixedResourcePath, elementType, elementName)); } return prefixedResourcePath; } else { String msg = String.format("Could not detect default SQL script for test %s [%s]: " + "%s does not exist. Either declare statements or scripts via @Sql or make the " + "default SQL script available.", elementType, elementName, classPathResource); logger.error(msg); throw new IllegalStateException(msg); } }
From source file:org.springframework.test.context.support.AbstractContextLoader.java
/** * Generate the default classpath resource locations array based on the * supplied class.//from w w w. j a va 2s. c om * <p>For example, if the supplied class is {@code com.example.MyTest}, * the generated locations will contain a single string with a value of * {@code "classpath:com/example/MyTest<suffix>"}, where {@code <suffix>} * is the value of the first configured * {@linkplain #getResourceSuffixes() resource suffix} for which the * generated location actually exists in the classpath. * <p>As of Spring 3.1, the implementation of this method adheres to the * contract defined in the {@link SmartContextLoader} SPI. Specifically, * this method will <em>preemptively</em> verify that the generated default * location actually exists. If it does not exist, this method will log a * warning and return an empty array. * <p>Subclasses can override this method to implement a different * <em>default location generation</em> strategy. * @param clazz the class for which the default locations are to be generated * @return an array of default application context resource locations * @since 2.5 * @see #getResourceSuffixes() */ protected String[] generateDefaultLocations(Class<?> clazz) { Assert.notNull(clazz, "Class must not be null"); String[] suffixes = getResourceSuffixes(); for (String suffix : suffixes) { Assert.hasText(suffix, "Resource suffix must not be empty"); String resourcePath = ClassUtils.convertClassNameToResourcePath(clazz.getName()) + suffix; String prefixedResourcePath = ResourceUtils.CLASSPATH_URL_PREFIX + resourcePath; ClassPathResource classPathResource = new ClassPathResource(resourcePath); if (classPathResource.exists()) { if (logger.isInfoEnabled()) { logger.info(String.format("Detected default resource location \"%s\" for test class [%s]", prefixedResourcePath, clazz.getName())); } return new String[] { prefixedResourcePath }; } else if (logger.isDebugEnabled()) { logger.debug(String.format( "Did not detect default resource location for test class [%s]: " + "%s does not exist", clazz.getName(), classPathResource)); } } if (logger.isInfoEnabled()) { logger.info(String.format( "Could not detect default resource locations for test class [%s]: " + "no resource found for suffixes %s.", clazz.getName(), ObjectUtils.nullSafeToString(suffixes))); } return EMPTY_STRING_ARRAY; }