/** * This implementation checks whether a File can be opened, * falling back to whether an InputStream can be opened. * This will cover both directories and content resources. */ @Override public boolean exists() { // Try file existence: can we find the file in the file system? try { return getFile().exists(); } catch (IOException ex) { // Fall back to stream existence: can we open the stream? try { getInputStream().close(); return true; } catch (Throwable isEx) { return false; } } }
public CompensablePropertySource(String name, EncodedResource source) { super(name, source); EncodedResource encoded = (EncodedResource) this.getSource(); AbstractResource resource = (AbstractResource) encoded.getResource(); String path = resource.getFilename(); if (StringUtils.isBlank(path)) { return; } String[] values = path.split(":"); if (values.length != 2) { return; } String protocol = values[0]; String resName = values[1]; if ("bytetcc".equalsIgnoreCase(protocol) == false) { return; } else if ("loadbalancer.config".equalsIgnoreCase(resName) == false) { return; } this.enabled = true; }
/** * This implementation always returns {@code true} for a resource * that {@link #exists() exists} (revised as of 5.1). */ @Override public boolean isReadable() { return exists(); }
/** * This implementation checks the timestamp of the underlying File, * if available. * @see #getFileForLastModifiedCheck() */ @Override public long lastModified() throws IOException { File fileToCheck = getFileForLastModifiedCheck(); long lastModified = fileToCheck.lastModified(); if (lastModified == 0L && !fileToCheck.exists()) { throw new FileNotFoundException(getDescription() + " cannot be resolved in the file system for checking its last-modified timestamp"); } return lastModified; }
/** * This implementation returns the description of this resource. * @see #getDescription() */ @Override public String toString() { return getDescription(); }
/** * This implementation returns {@link Channels#newChannel(InputStream)} * with the result of {@link #getInputStream()}. * <p>This is the same as in {@link Resource}'s corresponding default method * but mirrored here for efficient JVM-level dispatching in a class hierarchy. */ @Override public ReadableByteChannel readableChannel() throws IOException { return Channels.newChannel(getInputStream()); }
/** * Determine the File to use for timestamp checking. * <p>The default implementation delegates to {@link #getFile()}. * @return the File to use for timestamp checking (never {@code null}) * @throws FileNotFoundException if the resource cannot be resolved as * an absolute file path, i.e. is not available in a file system * @throws IOException in case of general resolution/reading failures */ protected File getFileForLastModifiedCheck() throws IOException { return getFile(); }
/** * This implementation returns the underlying File/Path last-modified time. */ @Override public long lastModified() throws IOException { if (this.file != null) { return super.lastModified(); } else { try { return Files.getLastModifiedTime(this.filePath).toMillis(); } catch (NoSuchFileException ex) { throw new FileNotFoundException(ex.getMessage()); } } }
/** * This implementation builds a URI based on the URL returned * by {@link #getURL()}. */ @Override public URI getURI() throws IOException { URL url = getURL(); try { return ResourceUtils.toURI(url); } catch (URISyntaxException ex) { throw new NestedIOException("Invalid URI [" + url + "]", ex); } }
if (resource instanceof FileSystemResource && resource.exists()) { LOGGER.debug("Executing groovy script to determine theme for [{}]", service.getId()); val result = ScriptingUtils.executeGroovyScript(resource, new Object[]{service, rService, val url = resource.getURL().toExternalForm(); LOGGER.debug("Executing URL [{}] to determine theme for [{}]", url, service.getId()); response = HttpUtils.executeGet(url, CollectionUtils.wrap("service", service.getId()));
/** * Gets resource from a String location. * * @param location the metadata location * @return the resource from * @throws IOException the exception */ public static AbstractResource getResourceFrom(final String location) throws IOException { val metadataLocationResource = getRawResourceFrom(location); if (!metadataLocationResource.exists() || !metadataLocationResource.isReadable()) { throw new FileNotFoundException("Resource " + location + " does not exist or is unreadable"); } return metadataLocationResource; }
/** * This implementation returns the description's hash code. * @see #getDescription() */ @Override public int hashCode() { return getDescription().hashCode(); }
/** * This implementation returns {@link Channels#newChannel(InputStream)} * with the result of {@link #getInputStream()}. * <p>This is the same as in {@link Resource}'s corresponding default method * but mirrored here for efficient JVM-level dispatching in a class hierarchy. */ @Override public ReadableByteChannel readableChannel() throws IOException { return Channels.newChannel(getInputStream()); }
/** * Determine the File to use for timestamp checking. * <p>The default implementation delegates to {@link #getFile()}. * @return the File to use for timestamp checking (never {@code null}) * @throws FileNotFoundException if the resource cannot be resolved as * an absolute file path, i.e. is not available in a file system * @throws IOException in case of general resolution/reading failures */ protected File getFileForLastModifiedCheck() throws IOException { return getFile(); }
/** * This implementation checks the timestamp of the underlying File, * if available. * @see #getFileForLastModifiedCheck() */ @Override public long lastModified() throws IOException { File fileToCheck = getFileForLastModifiedCheck(); long lastModified = fileToCheck.lastModified(); if (lastModified == 0L && !fileToCheck.exists()) { throw new FileNotFoundException(getDescription() + " cannot be resolved in the file system for checking its last-modified timestamp"); } return lastModified; }
/** * This implementation returns the underlying File/Path last-modified time. */ @Override public long lastModified() throws IOException { if (this.file != null) { return super.lastModified(); } else { try { return Files.getLastModifiedTime(this.filePath).toMillis(); } catch (NoSuchFileException ex) { throw new FileNotFoundException(ex.getMessage()); } } }
/** * This implementation builds a URI based on the URL returned * by {@link #getURL()}. */ @Override public URI getURI() throws IOException { URL url = getURL(); try { return ResourceUtils.toURI(url); } catch (URISyntaxException ex) { throw new NestedIOException("Invalid URI [" + url + "]", ex); } }
/** * This implementation checks whether a File can be opened, * falling back to whether an InputStream can be opened. * This will cover both directories and content resources. */ @Override public boolean exists() { // Try file existence: can we find the file in the file system? try { return getFile().exists(); } catch (IOException ex) { // Fall back to stream existence: can we open the stream? try { getInputStream().close(); return true; } catch (Throwable isEx) { return false; } } }
/** * This implementation throws a FileNotFoundException, assuming * that the resource cannot be resolved to a URL. */ @Override public URL getURL() throws IOException { throw new FileNotFoundException(getDescription() + " cannot be resolved to URL"); }
/** * This implementation reads the entire InputStream to calculate the * content length. Subclasses will almost always be able to provide * a more optimal version of this, e.g. checking a File length. * @see #getInputStream() */ @Override public long contentLength() throws IOException { InputStream is = getInputStream(); try { long size = 0; byte[] buf = new byte[256]; int read; while ((read = is.read(buf)) != -1) { size += read; } return size; } finally { try { is.close(); } catch (IOException ex) { } } }