List of usage examples for org.springframework.util ResourceUtils getFile
public static File getFile(URI resourceUri) throws FileNotFoundException
From source file:net.duckling.falcon.api.boostrap.BootstrapDao.java
public void createTables() { Connection conn = getConnection(); execute(conn, "use " + database + ";"); File file = null;//from ww w .j a v a 2 s. co m SQLReader reader = null; try { file = ResourceUtils.getFile(sqlPath); if (file.exists()) { reader = new SQLReader(new FileInputStream(file), "UTF-8"); String sql; while ((sql = reader.next()) != null) { execute(conn, sql); } } } catch (FileNotFoundException e) { LOG.error("Init sql file is not found", e); } catch (UnsupportedEncodingException e) { LOG.error("Unsupported encode for UTF-8", e); } catch (DataAccessException e) { LOG.error("Data access exception", e); } catch (WrongSQLException e) { LOG.error("Init SQL has problem", e); } finally { if (reader != null) { reader.close(); } } }
From source file:edu.amc.sakai.user.JLDAPDirectoryProviderIntegrationTestSupport.java
ConfigurableApplicationContext newTestApplicationContext(ApplicationContext parent) { List existingFiles = new ArrayList(); for (String fileName : TEST_CONTEXT_CONFIG_FILE_NAMES) { try {// w ww . ja v a2 s .co m ResourceUtils.getFile(ResourceUtils.getURL(ResourceUtils.CLASSPATH_URL_PREFIX + fileName)); existingFiles.add(fileName); } catch (FileNotFoundException e) { // } } String[] existingFilesArray = new String[existingFiles.size()]; return new ClassPathXmlApplicationContext((String[]) existingFiles.toArray(existingFilesArray), parent); }
From source file:org.ow2.authzforce.pap.dao.flatfile.FlatFileDAORefPolicyProviderModule.java
/** * Validate provider config and returns policy parent directory and policy * (version-specific) filename suffix//from w ww . j a v a 2 s . c om * * @param policyLocationPattern * policy location pattern, expected to be * PARENT_DIRECTORY/*SUFFIX, where PARENT_DIRECTORY is a valid * directory path where the policies should be located. * @return entry where the key is the parent directory to all policies, and * the value is the policy filename suffix for each policy version * @throws IllegalArgumentException * if the policyLocationPattern is invalid */ public static Entry<Path, String> validateConf(final String policyLocationPattern) throws IllegalArgumentException { if (policyLocationPattern == null) { throw NULL_POLICY_LOCATION_PATTERN_ARGUMENT_EXCEPTION; } final int index = policyLocationPattern.indexOf("/*"); if (index == -1) { throw new IllegalArgumentException("Invalid policyLocationPattern in refPolicyProvider configuration: " + policyLocationPattern + ": '/*' not found"); } final String prefix = policyLocationPattern.substring(0, index); final Path policyParentDirectory; try { policyParentDirectory = ResourceUtils.getFile(prefix).toPath(); } catch (final FileNotFoundException e) { throw new IllegalArgumentException( "Invalid policy directory path in refPolicyProvider/policyLocationPattern (prefix before '/*'): " + policyLocationPattern, e); } final String suffix = policyLocationPattern.substring(index + 2); return new SimpleImmutableEntry<>(policyParentDirectory, suffix); }
From source file:org.openwms.core.configuration.file.ApplicationPreferenceTest.java
/** * Just test to validate the given XML file against the schema declaration. If the XML file is not compliant with the schema, the test * will fail./*from ww w .j a v a 2s. c om*/ * * @throws Exception any error */ @Test public void testReadPreferences() throws Exception { SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = schemaFactory.newSchema(ResourceUtils.getFile("classpath:preferences.xsd")); // Schema schema = schemaFactory.newSchema(new // URL("http://www.openwms.org/schema/preferences.xsd")); JAXBContext ctx = JAXBContext.newInstance("org.openwms.core.configuration.file"); Unmarshaller unmarshaller = ctx.createUnmarshaller(); unmarshaller.setSchema(schema); unmarshaller.setEventHandler(new ValidationEventHandler() { @Override public boolean handleEvent(ValidationEvent event) { RuntimeException ex = new RuntimeException(event.getMessage(), event.getLinkedException()); LOGGER.error(ex.getMessage()); throw ex; } }); Preferences prefs = Preferences.class.cast(unmarshaller .unmarshal(ResourceUtils.getFile("classpath:org/openwms/core/configuration/file/preferences.xml"))); for (AbstractPreference pref : prefs.getApplications()) { LOGGER.info(pref.toString()); } for (AbstractPreference pref : prefs.getModules()) { LOGGER.info(pref.toString()); } for (AbstractPreference pref : prefs.getUsers()) { LOGGER.info(pref.toString()); } }
From source file:blast.shell.jline.BlastConsoleFactory.java
/** * Tries to load from welcomeMessage and then welcomeMessageFile, in that order... failing that, it will return * an empty Properties object.//w w w .j a v a 2 s .c o m * * @return */ protected Properties loadBrandingProperties() { if (welcomeMessage != null) { Properties props = new Properties(); props.put("welcome", welcomeMessage); return props; } else if (welcomeMessageFile != null) { try { ResourceUtils.getFile(welcomeMessageFile); } catch (FileNotFoundException e) { log.error("Could not find file " + welcomeMessageFile + ": " + e.getMessage()); } } // failed... no welcome message for you. return new Properties(); }
From source file:org.unidle.feature.page.GenericPage.java
protected void setField(final WebElement webElement, final String value) { if (value.startsWith("classpath:")) { try {/*w w w. j a va2 s . c om*/ final File file = ResourceUtils.getFile(value); webElement.sendKeys(file.getAbsolutePath()); } catch (FileNotFoundException e) { throw new RuntimeException(format("Error loading file: %s", value), e); } } else { webElement.clear(); webElement.sendKeys(value); } }
From source file:com.amalto.core.servlet.LogViewerServlet.java
private File getLogFile(String location) throws FileNotFoundException { String resolvedLocation = SystemPropertyUtils.resolvePlaceholders(location); File file = ResourceUtils.getFile(resolvedLocation); if (!file.exists()) { throw new FileNotFoundException("Log file [" + resolvedLocation + "] not found"); }//from w w w.ja va 2s. c om return file; }
From source file:com.googlecode.starflow.engine.service.impl.ProcessDefineService.java
public void deployProcessFile(String resourceLocation) { try {/* w w w. j av a 2 s.c om*/ File file = ResourceUtils.getFile(resourceLocation); BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file) { @Override public int read() throws IOException { return 0; } }, "UTF-8")); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } deployProcessXML(sb.toString()); } catch (FileNotFoundException e1) { throw new ProcessEngineException("?", e1); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } }
From source file:org.apache.rya.jena.jenasesame.JenaReasoningWithRulesTest.java
private static void loadRdfFile(final Repository repo, final String rdfRelativeFileName) throws RepositoryException, RDFParseException, IOException { RepositoryConnection addConnection = null; try {/* ww w . jav a 2s .com*/ // Load some data. addConnection = repo.getConnection(); // Reads files relative from target/test-classes which should have been copied from src/test/resources final URL url = ClassLoader.getSystemResource(rdfRelativeFileName); final File file = ResourceUtils.getFile(url); final String fileName = file.getAbsolutePath(); final RDFFormat rdfFormat = RDFFormat.forFileName(fileName); log.info("Added RDF file with " + rdfFormat.getName() + " format: " + fileName); addConnection.add(file, "http://base/", rdfFormat); addConnection.close(); } finally { if (addConnection != null && addConnection.isOpen()) { addConnection.close(); } } }
From source file:org.apache.rya.jena.jenasesame.JenaReasoningWithRulesTest.java
private static InfModel createInfModel(final RepositoryConnection queryConnection, final String rulesRelativeFileName) throws FileNotFoundException { final Dataset dataset = JenaSesame.createDataset(queryConnection); final Model model = dataset.getDefaultModel(); log.info(model.getNsPrefixMap());//from ww w . ja v a2s .co m final URL rulesUrl = ClassLoader.getSystemResource(rulesRelativeFileName); final File rulesFile = ResourceUtils.getFile(rulesUrl); final String rulesFileName = rulesFile.getAbsolutePath(); final Reasoner reasoner = new GenericRuleReasoner(Rule.rulesFromURL(rulesFileName)); final InfModel infModel = ModelFactory.createInfModel(reasoner, model); return infModel; }