Java Code Examples for org.springframework.util.ResourceUtils#useCachesIfNecessary()

The following examples show how to use org.springframework.util.ResourceUtils#useCachesIfNecessary() . 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: UrlResource.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * This implementation opens an InputStream for the given URL.
 * <p>It sets the {@code useCaches} flag to {@code false},
 * mainly to avoid jar file locking on Windows.
 * @see java.net.URL#openConnection()
 * @see java.net.URLConnection#setUseCaches(boolean)
 * @see java.net.URLConnection#getInputStream()
 */
@Override
public InputStream getInputStream() throws IOException {
	URLConnection con = this.url.openConnection();
	ResourceUtils.useCachesIfNecessary(con);
	try {
		return con.getInputStream();
	}
	catch (IOException ex) {
		// Close the HTTP connection (if applicable).
		if (con instanceof HttpURLConnection) {
			((HttpURLConnection) con).disconnect();
		}
		throw ex;
	}
}
 
Example 2
Source File: UrlResource.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * This implementation opens an InputStream for the given URL.
 * <p>It sets the {@code useCaches} flag to {@code false},
 * mainly to avoid jar file locking on Windows.
 * @see java.net.URL#openConnection()
 * @see java.net.URLConnection#setUseCaches(boolean)
 * @see java.net.URLConnection#getInputStream()
 */
@Override
public InputStream getInputStream() throws IOException {
	URLConnection con = this.url.openConnection();
	ResourceUtils.useCachesIfNecessary(con);
	try {
		return con.getInputStream();
	}
	catch (IOException ex) {
		// Close the HTTP connection (if applicable).
		if (con instanceof HttpURLConnection) {
			((HttpURLConnection) con).disconnect();
		}
		throw ex;
	}
}
 
Example 3
Source File: UrlResource.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * This implementation opens an InputStream for the given URL.
 * <p>It sets the {@code useCaches} flag to {@code false},
 * mainly to avoid jar file locking on Windows.
 * @see java.net.URL#openConnection()
 * @see java.net.URLConnection#setUseCaches(boolean)
 * @see java.net.URLConnection#getInputStream()
 */
@Override
public InputStream getInputStream() throws IOException {
	URLConnection con = this.url.openConnection();
	ResourceUtils.useCachesIfNecessary(con);
	try {
		return con.getInputStream();
	}
	catch (IOException ex) {
		// Close the HTTP connection (if applicable).
		if (con instanceof HttpURLConnection) {
			((HttpURLConnection) con).disconnect();
		}
		throw ex;
	}
}
 
Example 4
Source File: UrlResource.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * This implementation opens an InputStream for the given URL.
 * <p>It sets the {@code useCaches} flag to {@code false},
 * mainly to avoid jar file locking on Windows.
 * @see java.net.URL#openConnection()
 * @see java.net.URLConnection#setUseCaches(boolean)
 * @see java.net.URLConnection#getInputStream()
 */
@Override
public InputStream getInputStream() throws IOException {
	URLConnection con = this.url.openConnection();
	ResourceUtils.useCachesIfNecessary(con);
	try {
		return con.getInputStream();
	}
	catch (IOException ex) {
		// Close the HTTP connection (if applicable).
		if (con instanceof HttpURLConnection) {
			((HttpURLConnection) con).disconnect();
		}
		throw ex;
	}
}
 
Example 5
Source File: PropertiesLoaderUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Load all properties from the specified class path resource
 * (in ISO-8859-1 encoding), using the given class loader.
 * <p>Merges properties if more than one resource of the same name
 * found in the class path.
 * @param resourceName the name of the class path resource
 * @param classLoader the ClassLoader to use for loading
 * (or {@code null} to use the default class loader)
 * @return the populated Properties instance
 * @throws IOException if loading failed
 */
public static Properties loadAllProperties(String resourceName, @Nullable ClassLoader classLoader) throws IOException {
	Assert.notNull(resourceName, "Resource name must not be null");
	ClassLoader classLoaderToUse = classLoader;
	if (classLoaderToUse == null) {
		classLoaderToUse = ClassUtils.getDefaultClassLoader();
	}
	Enumeration<URL> urls = (classLoaderToUse != null ? classLoaderToUse.getResources(resourceName) :
			ClassLoader.getSystemResources(resourceName));
	Properties props = new Properties();
	while (urls.hasMoreElements()) {
		URL url = urls.nextElement();
		URLConnection con = url.openConnection();
		ResourceUtils.useCachesIfNecessary(con);
		InputStream is = con.getInputStream();
		try {
			if (resourceName.endsWith(XML_FILE_EXTENSION)) {
				props.loadFromXML(is);
			}
			else {
				props.load(is);
			}
		}
		finally {
			is.close();
		}
	}
	return props;
}
 
Example 6
Source File: PropertiesLoaderUtils.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Load all properties from the specified class path resource
 * (in ISO-8859-1 encoding), using the given class loader.
 * <p>Merges properties if more than one resource of the same name
 * found in the class path.
 * @param resourceName the name of the class path resource
 * @param classLoader the ClassLoader to use for loading
 * (or {@code null} to use the default class loader)
 * @return the populated Properties instance
 * @throws IOException if loading failed
 */
public static Properties loadAllProperties(String resourceName, @Nullable ClassLoader classLoader) throws IOException {
	Assert.notNull(resourceName, "Resource name must not be null");
	ClassLoader classLoaderToUse = classLoader;
	if (classLoaderToUse == null) {
		classLoaderToUse = ClassUtils.getDefaultClassLoader();
	}
	Enumeration<URL> urls = (classLoaderToUse != null ? classLoaderToUse.getResources(resourceName) :
			ClassLoader.getSystemResources(resourceName));
	Properties props = new Properties();
	while (urls.hasMoreElements()) {
		URL url = urls.nextElement();
		URLConnection con = url.openConnection();
		ResourceUtils.useCachesIfNecessary(con);
		InputStream is = con.getInputStream();
		try {
			if (resourceName.endsWith(XML_FILE_EXTENSION)) {
				props.loadFromXML(is);
			}
			else {
				props.load(is);
			}
		}
		finally {
			is.close();
		}
	}
	return props;
}
 
Example 7
Source File: PropertiesLoaderUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Load all properties from the specified class path resource
 * (in ISO-8859-1 encoding), using the given class loader.
 * <p>Merges properties if more than one resource of the same name
 * found in the class path.
 * @param resourceName the name of the class path resource
 * @param classLoader the ClassLoader to use for loading
 * (or {@code null} to use the default class loader)
 * @return the populated Properties instance
 * @throws IOException if loading failed
 */
public static Properties loadAllProperties(String resourceName, ClassLoader classLoader) throws IOException {
	Assert.notNull(resourceName, "Resource name must not be null");
	ClassLoader classLoaderToUse = classLoader;
	if (classLoaderToUse == null) {
		classLoaderToUse = ClassUtils.getDefaultClassLoader();
	}
	Enumeration<URL> urls = (classLoaderToUse != null ? classLoaderToUse.getResources(resourceName) :
			ClassLoader.getSystemResources(resourceName));
	Properties props = new Properties();
	while (urls.hasMoreElements()) {
		URL url = urls.nextElement();
		URLConnection con = url.openConnection();
		ResourceUtils.useCachesIfNecessary(con);
		InputStream is = con.getInputStream();
		try {
			if (resourceName.endsWith(XML_FILE_EXTENSION)) {
				props.loadFromXML(is);
			}
			else {
				props.load(is);
			}
		}
		finally {
			is.close();
		}
	}
	return props;
}
 
Example 8
Source File: PropertiesLoaderUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Load all properties from the specified class path resource
 * (in ISO-8859-1 encoding), using the given class loader.
 * <p>Merges properties if more than one resource of the same name
 * found in the class path.
 * @param resourceName the name of the class path resource
 * @param classLoader the ClassLoader to use for loading
 * (or {@code null} to use the default class loader)
 * @return the populated Properties instance
 * @throws IOException if loading failed
 */
public static Properties loadAllProperties(String resourceName, ClassLoader classLoader) throws IOException {
	Assert.notNull(resourceName, "Resource name must not be null");
	ClassLoader classLoaderToUse = classLoader;
	if (classLoaderToUse == null) {
		classLoaderToUse = ClassUtils.getDefaultClassLoader();
	}
	Enumeration<URL> urls = (classLoaderToUse != null ? classLoaderToUse.getResources(resourceName) :
			ClassLoader.getSystemResources(resourceName));
	Properties props = new Properties();
	while (urls.hasMoreElements()) {
		URL url = urls.nextElement();
		URLConnection con = url.openConnection();
		ResourceUtils.useCachesIfNecessary(con);
		InputStream is = con.getInputStream();
		try {
			if (resourceName != null && resourceName.endsWith(XML_FILE_EXTENSION)) {
				props.loadFromXML(is);
			}
			else {
				props.load(is);
			}
		}
		finally {
			is.close();
		}
	}
	return props;
}
 
Example 9
Source File: AbstractFileResolvingResource.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Customize the given {@link URLConnection}, obtained in the course of an
 * {@link #exists()}, {@link #contentLength()} or {@link #lastModified()} call.
 * <p>Calls {@link ResourceUtils#useCachesIfNecessary(URLConnection)} and
 * delegates to {@link #customizeConnection(HttpURLConnection)} if possible.
 * Can be overridden in subclasses.
 * @param con the URLConnection to customize
 * @throws IOException if thrown from URLConnection methods
 */
protected void customizeConnection(URLConnection con) throws IOException {
	ResourceUtils.useCachesIfNecessary(con);
	if (con instanceof HttpURLConnection) {
		customizeConnection((HttpURLConnection) con);
	}
}
 
Example 10
Source File: AbstractFileResolvingResource.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Customize the given {@link URLConnection}, obtained in the course of an
 * {@link #exists()}, {@link #contentLength()} or {@link #lastModified()} call.
 * <p>Calls {@link ResourceUtils#useCachesIfNecessary(URLConnection)} and
 * delegates to {@link #customizeConnection(HttpURLConnection)} if possible.
 * Can be overridden in subclasses.
 * @param con the URLConnection to customize
 * @throws IOException if thrown from URLConnection methods
 */
protected void customizeConnection(URLConnection con) throws IOException {
	ResourceUtils.useCachesIfNecessary(con);
	if (con instanceof HttpURLConnection) {
		customizeConnection((HttpURLConnection) con);
	}
}
 
Example 11
Source File: AbstractFileResolvingResource.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Customize the given {@link URLConnection}, obtained in the course of an
 * {@link #exists()}, {@link #contentLength()} or {@link #lastModified()} call.
 * <p>Calls {@link ResourceUtils#useCachesIfNecessary(URLConnection)} and
 * delegates to {@link #customizeConnection(HttpURLConnection)} if possible.
 * Can be overridden in subclasses.
 * @param con the URLConnection to customize
 * @throws IOException if thrown from URLConnection methods
 */
protected void customizeConnection(URLConnection con) throws IOException {
	ResourceUtils.useCachesIfNecessary(con);
	if (con instanceof HttpURLConnection) {
		customizeConnection((HttpURLConnection) con);
	}
}
 
Example 12
Source File: AbstractFileResolvingResource.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * Customize the given {@link URLConnection}, obtained in the course of an
 * {@link #exists()}, {@link #contentLength()} or {@link #lastModified()} call.
 * <p>Calls {@link ResourceUtils#useCachesIfNecessary(URLConnection)} and
 * delegates to {@link #customizeConnection(HttpURLConnection)} if possible.
 * Can be overridden in subclasses.
 * @param con the URLConnection to customize
 * @throws IOException if thrown from URLConnection methods
 */
protected void customizeConnection(URLConnection con) throws IOException {
	ResourceUtils.useCachesIfNecessary(con);
	if (con instanceof HttpURLConnection) {
		customizeConnection((HttpURLConnection) con);
	}
}
 
Example 13
Source File: AbstractFileResolvingResource.java    From HotswapAgent with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Customize the given {@link URLConnection}, obtained in the course of an
 * {@link #exists()}, {@link #contentLength()} or {@link #lastModified()}
 * call.
 * <p>
 * Calls {@link ResourceUtils#useCachesIfNecessary(URLConnection)} and
 * delegates to {@link #customizeConnection(HttpURLConnection)} if possible.
 * Can be overridden in subclasses.
 * 
 * @param con
 *            the URLConnection to customize
 * @throws IOException
 *             if thrown from URLConnection methods
 */
protected void customizeConnection(URLConnection con) throws IOException {
    ResourceUtils.useCachesIfNecessary(con);
    if (con instanceof HttpURLConnection) {
        customizeConnection((HttpURLConnection) con);
    }
}