private static InputStream loadFromClassPath(String path) throws IOException { log.debug("Opening resource from classpath '{}'", path); return ClassUtil.getResource(path).openStream(); }
/** * Returns the running Fathom version. * * @return the running Fathom version */ public static String getVersion() { String FATHOM_PROPERTIES = "fathom/version.properties"; String version = null; URL url = ClassUtil.getResource(FATHOM_PROPERTIES); Preconditions.checkNotNull(url, "Failed to find " + FATHOM_PROPERTIES); try (InputStream stream = url.openStream()) { Properties prop = new Properties(); prop.load(stream); version = prop.getProperty("version"); } catch (IOException e) { LoggerFactory.getLogger(Constants.class).error("Failed to read '{}'", FATHOM_PROPERTIES, e); } Preconditions.checkNotNull(version, "The Fathom version is null!"); return version; }
public static String getLogo() { String FATHOM_PROPERTIES = "fathom/version.properties"; String version = null; String projectUrl = null; String description = null; URL url = ClassUtil.getResource(FATHOM_PROPERTIES); Preconditions.checkNotNull(url, "Failed to find " + FATHOM_PROPERTIES); try (InputStream stream = url.openStream()) { Properties prop = new Properties(); prop.load(stream); version = prop.getProperty("version"); projectUrl = prop.getProperty("projectUrl"); description = prop.getProperty("description"); } catch (IOException e) { LoggerFactory.getLogger(Constants.class).error("Failed to read '{}'", FATHOM_PROPERTIES, e); } Preconditions.checkNotNull(version, "The Fathom version is null!"); Preconditions.checkNotNull(projectUrl, "The Fathom project url is null!"); Preconditions.checkNotNull(description, "The Fathom description is null!"); String logo = Constants.LOGO.replace("${version}", version).replace("${url}", projectUrl).replace("${description}", description); return logo; }
public static String loadStringResource(String resource) { try { URL url; if (resource.startsWith("classpath:")) { url = getResource(resource.substring("classpath:".length())); } else if (resource.startsWith("url:")) { url = new URL(resource.substring("url:".length())); } else if (resource.startsWith("file:")) { url = new URL(resource.substring("file:".length())); } else { url = new URL(resource); } return loadStringResource(url); } catch (IOException e) { throw new FathomException(e, "Failed to read String resource from '{}'", resource); } }
public URL getFileUrl(String name, String defaultValue) { String file = getString(name, defaultValue); if (Strings.isNullOrEmpty(file)) { return null; } try { if (file.startsWith("classpath:")) { return ClassUtil.getResource(file.substring("classpath:".length())); } else if (file.startsWith("url:")) { return new URL(file.substring("url:".length())); } else if (file.startsWith("file:")) { return new URL(file.substring("file:".length())); } else { return new URL(file); } } catch (IOException e) { throw new FathomException(e, "Failed to create '{}' URL from '{}'", name, file); } }
/** * Execute a script located either in the classpath or on the filesystem. * * @param scriptPath */ protected void executeScript(String scriptPath) { URL scriptUrl = null; try { if (scriptPath.startsWith("classpath:")) { String script = scriptPath.substring("classpath:".length()); scriptUrl = ClassUtil.getResource(script); if (scriptUrl == null) { log.warn("Script '{}' not found!", scriptPath); } } else { File file = new File(scriptPath); if (file.exists()) { scriptUrl = file.toURI().toURL(); } else { log.warn("Script '{}' not found!", scriptPath); } } // execute the script if (scriptUrl != null) { executeScript(scriptUrl); } } catch (Exception e) { log.error("Failed to parse '{}' as a url", scriptPath); } }
/** * Execute a script located either in the classpath or on the filesystem. * * @param scriptPath */ protected void executeScript(String scriptPath) { URL scriptUrl = null; try { if (scriptPath.startsWith("classpath:")) { String script = scriptPath.substring("classpath:".length()); scriptUrl = ClassUtil.getResource(script); if (scriptUrl == null) { log.warn("Script '{}' not found!", scriptPath); } } else { File file = new File(scriptPath); if (file.exists()) { scriptUrl = file.toURI().toURL(); } else { log.warn("Script '{}' not found!", scriptPath); } } // execute the script if (scriptUrl != null) { executeScript(scriptUrl); } } catch (Exception e) { log.error("Failed to parse '{}' as a url", scriptPath); } }
/** * Execute a script located either in the classpath or on the filesystem. * * @param scriptPath */ protected void executeScript(String scriptPath) { URL scriptUrl = null; try { if (scriptPath.startsWith("classpath:")) { String script = scriptPath.substring("classpath:".length()); scriptUrl = ClassUtil.getResource(script); if (scriptUrl == null) { log.warn("Script '{}' not found!", scriptPath); } } else { File file = new File(scriptPath); if (file.exists()) { scriptUrl = file.toURI().toURL(); } else { log.warn("Script '{}' not found!", scriptPath); } } // execute the script if (scriptUrl != null) { executeScript(scriptUrl); } } catch (Exception e) { log.error("Failed to parse '{}' as a url", scriptPath); } }
@Override public void setup(Config config) { super.setup(config); // Default to Apache htpasswd specificiations String os = System.getProperty("os.name").toLowerCase(); if (os.startsWith("windows") || os.startsWith("netware")) { isAllowClearTextPasswords = true; } else { isAllowClearTextPasswords = false; } // Allow settings override if (config.hasPath("allowClearPasswords")) { isAllowClearTextPasswords = config.getBoolean("allowClearPasswords"); } file = config.getString("file"); Preconditions.checkNotNull(file, "You must specify an htpasswd 'file'!"); if (file.startsWith("classpath:")) { // one-time read of the credentials file URL url = ClassUtil.getResource(file.substring("classpath:".length())); credentialsMap.putAll(readCredentialsURL(url)); log.debug("Read {} standard credentials from '{}'", credentialsMap.size(), url); } else { // keep a credentials file reference so it may auto-reload setFile(new File(file)); log.debug("Read {} standard credentials from '{}'", credentialsMap.size(), file); } }
@Override public void setup(Config config) { super.setup(config); // Default to Apache htpasswd specificiations String os = System.getProperty("os.name").toLowerCase(); if (os.startsWith("windows") || os.startsWith("netware")) { isAllowClearTextPasswords = true; } else { isAllowClearTextPasswords = false; } // Allow settings override if (config.hasPath("allowClearPasswords")) { isAllowClearTextPasswords = config.getBoolean("allowClearPasswords"); } file = config.getString("file"); Preconditions.checkNotNull(file, "You must specify an htpasswd 'file'!"); if (file.startsWith("classpath:")) { // one-time read of the credentials file URL url = ClassUtil.getResource(file.substring("classpath:".length())); credentialsMap.putAll(readCredentialsURL(url)); log.debug("Read {} standard credentials from '{}'", credentialsMap.size(), url); } else { // keep a credentials file reference so it may auto-reload setFile(new File(file)); log.debug("Read {} standard credentials from '{}'", credentialsMap.size(), file); } }
URL configFileUrl = ClassUtil.getResource(String.format("conf/%s.conf", profile)); if (configFileUrl == null) { log.warn("Failed to find Fathom config 'classpath:conf/{}.conf'", profile);
private void loadEmployees() { URL resource = ClassUtil.getResource("dao/employees.json"); try (InputStreamReader reader = new InputStreamReader(resource.openStream())) { Gson gson = new GsonBuilder().create(); Data data = gson.fromJson(reader, Data.class); DateFormat df = new SimpleDateFormat(("yyyy/MM/dd")); for (Object[] row : data.employees) { Employee employee = new Employee(); employee.setName((String) row[0]); employee.setPosition((String) row[1]); employee.setOffice((String) row[2]); employee.setExtension(((Number) row[3]).intValue()); employee.setStartDate(df.parse((String) row[4])); employee.setSalary((String) row[5]); put(employee); } } catch (ParseException | IOException e) { log.error(null, e); } }