Package org.apache.xbean.finder
Class ResourceFinder
- java.lang.Object
-
- org.apache.xbean.finder.ResourceFinder
-
public class ResourceFinder extends java.lang.Object
- Version:
- $Rev: 1778104 $ $Date: 2017-01-10 11:05:25 +0100 (Tue, 10 Jan 2017) $
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.ClassLoader
classLoader
private java.lang.String
path
private java.util.List<java.lang.String>
resourcesNotLoaded
private java.net.URL[]
urls
-
Constructor Summary
Constructors Constructor Description ResourceFinder(java.lang.String path)
ResourceFinder(java.lang.String path, java.lang.ClassLoader classLoader)
ResourceFinder(java.lang.String path, java.lang.ClassLoader classLoader, java.net.URL... urls)
ResourceFinder(java.lang.String path, java.net.URL... urls)
ResourceFinder(java.net.URL... urls)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static java.lang.String
decode(java.lang.String fileName)
java.net.URL
find(java.lang.String uri)
java.util.List<java.net.URL>
findAll(java.lang.String uri)
java.util.List<java.lang.Class<?>>
findAllClasses(java.lang.String uri)
Executes findAllStrings assuming the strings are the names of a classes that should be loaded and returned.<T> java.util.List<java.lang.Class<? extends T>>
findAllImplementations(java.lang.Class<T> interfase)
Assumes the class specified points to a file in the classpath that contains the name of a class that implements or is a subclass of the specfied class.java.util.List<java.util.Properties>
findAllProperties(java.lang.String uri)
Finds the corresponding resources and reads them in as a properties filesjava.util.List<java.lang.String>
findAllStrings(java.lang.String uri)
Reads the contents of the found URLs as a list ofString
's and returns them.java.util.List<java.lang.Class<?>>
findAvailableClasses(java.lang.String uri)
Executes findAvailableStrings assuming the strings are the names of a classes that should be loaded and returned.<T> java.util.List<java.lang.Class<? extends T>>
findAvailableImplementations(java.lang.Class<T> interfase)
Assumes the class specified points to a file in the classpath that contains the name of a class that implements or is a subclass of the specfied class.java.util.List<java.util.Properties>
findAvailableProperties(java.lang.String uri)
Finds the corresponding resources and reads them in as a properties filesjava.util.List<java.lang.String>
findAvailableStrings(java.lang.String uri)
Reads the contents of the found URLs as a Strings and returns them.java.lang.Class<?>
findClass(java.lang.String uri)
ExecutesfindString(String)
assuming the contents URL found is the name of a class that should be loaded and returned.java.lang.Class<?>
findImplementation(java.lang.Class<?> interfase)
Assumes the class specified points to a file in the classpath that contains the name of a class that implements or is a subclass of the specfied class.java.util.Properties
findProperties(java.lang.String uri)
Finds the corresponding resource and reads it in as a properties fileprivate java.net.URL
findResource(java.lang.String resourceName, java.net.URL... search)
java.lang.String
findString(java.lang.String uri)
Reads the contents of the URL as aString
's and returns it.java.net.URL
getResource(java.lang.String fullUri)
private java.util.Enumeration<java.net.URL>
getResources(java.lang.String fulluri)
java.util.Map<java.lang.String,java.net.URL>
getResourcesMap(java.lang.String uri)
java.util.List<java.lang.String>
getResourcesNotLoaded()
Returns a list of resources that could not be loaded in the last invoked findAvailable* or mapAvailable* methods.private static boolean
isDirectory(java.net.URL url)
private java.util.Properties
loadProperties(java.net.URL resource)
java.util.Map<java.lang.String,java.lang.Class<?>>
mapAllClasses(java.lang.String uri)
Executes mapAllStrings assuming the value of each entry in the map is the name of a class that should be loaded.<T> java.util.Map<java.lang.String,java.lang.Class<? extends T>>
mapAllImplementations(java.lang.Class<T> interfase)
Assumes the class specified points to a directory in the classpath that holds files containing the name of a class that implements or is a subclass of the specfied class.java.util.Map<java.lang.String,java.util.Properties>
mapAllProperties(java.lang.String uri)
Finds the corresponding resources and reads them in as a properties filesjava.util.Map<java.lang.String,java.lang.String>
mapAllStrings(java.lang.String uri)
Reads the contents of all non-directory URLs immediately under the specified location and returns them in a map keyed by the file name.java.util.Map<java.lang.String,java.lang.Class<?>>
mapAvailableClasses(java.lang.String uri)
Executes mapAvailableStrings assuming the value of each entry in the map is the name of a class that should be loaded.<T> java.util.Map<java.lang.String,java.lang.Class<? extends T>>
mapAvailableImplementations(java.lang.Class<T> interfase)
Assumes the class specified points to a directory in the classpath that holds files containing the name of a class that implements or is a subclass of the specfied class.java.util.Map<java.lang.String,java.util.Properties>
mapAvailableProperties(java.lang.String uri)
Finds the corresponding resources and reads them in as a properties filesjava.util.Map<java.lang.String,java.lang.String>
mapAvailableStrings(java.lang.String uri)
Reads the contents of all non-directory URLs immediately under the specified location and returns them in a map keyed by the file name.private java.lang.String
readContents(java.net.URL resource)
private static void
readDirectoryEntries(java.net.URL location, java.util.Map<java.lang.String,java.net.URL> resources)
private static void
readJarEntries(java.net.URL location, java.lang.String basePath, java.util.Map<java.lang.String,java.net.URL> resources)
private java.net.URL
targetURL(java.net.URL base, java.lang.String name)
-
-
-
Constructor Detail
-
ResourceFinder
public ResourceFinder(java.net.URL... urls)
-
ResourceFinder
public ResourceFinder(java.lang.String path)
-
ResourceFinder
public ResourceFinder(java.lang.String path, java.net.URL... urls)
-
ResourceFinder
public ResourceFinder(java.lang.String path, java.lang.ClassLoader classLoader)
-
ResourceFinder
public ResourceFinder(java.lang.String path, java.lang.ClassLoader classLoader, java.net.URL... urls)
-
-
Method Detail
-
isDirectory
private static boolean isDirectory(java.net.URL url)
-
getResourcesNotLoaded
public java.util.List<java.lang.String> getResourcesNotLoaded()
Returns a list of resources that could not be loaded in the last invoked findAvailable* or mapAvailable* methods. The list will only contain entries of resources that match the requirements of the last invoked findAvailable* or mapAvailable* methods, but were unable to be loaded and included in their results. The list returned is unmodifiable and the results of this method will change after each invocation of a findAvailable* or mapAvailable* methods. This method is not thread safe.
-
find
public java.net.URL find(java.lang.String uri) throws java.io.IOException
- Throws:
java.io.IOException
-
findAll
public java.util.List<java.net.URL> findAll(java.lang.String uri) throws java.io.IOException
- Throws:
java.io.IOException
-
findString
public java.lang.String findString(java.lang.String uri) throws java.io.IOException
Reads the contents of the URL as aString
's and returns it.- Parameters:
uri
-- Returns:
- a stringified content of a resource
- Throws:
java.io.IOException
- if a resource pointed out by the uri param could not be find- See Also:
ClassLoader.getResource(String)
-
findAllStrings
public java.util.List<java.lang.String> findAllStrings(java.lang.String uri) throws java.io.IOException
Reads the contents of the found URLs as a list ofString
's and returns them.- Parameters:
uri
-- Returns:
- a list of the content of each resource URL found
- Throws:
java.io.IOException
- if any of the found URLs are unable to be read.
-
findAvailableStrings
public java.util.List<java.lang.String> findAvailableStrings(java.lang.String uri) throws java.io.IOException
Reads the contents of the found URLs as a Strings and returns them. Individual URLs that cannot be read are skipped and added to the list of 'resourcesNotLoaded'- Parameters:
uri
-- Returns:
- a list of the content of each resource URL found
- Throws:
java.io.IOException
- if classLoader.getResources throws an exception
-
mapAllStrings
public java.util.Map<java.lang.String,java.lang.String> mapAllStrings(java.lang.String uri) throws java.io.IOException
Reads the contents of all non-directory URLs immediately under the specified location and returns them in a map keyed by the file name. Any URLs that cannot be read will cause an exception to be thrown. Example classpath: META-INF/serializables/one META-INF/serializables/two META-INF/serializables/three META-INF/serializables/four/foo.txt ResourceFinder finder = new ResourceFinder("META-INF/"); Map map = finder.mapAvailableStrings("serializables"); map.contains("one"); // true map.contains("two"); // true map.contains("three"); // true map.contains("four"); // false- Parameters:
uri
-- Returns:
- a list of the content of each resource URL found
- Throws:
java.io.IOException
- if any of the urls cannot be read
-
mapAvailableStrings
public java.util.Map<java.lang.String,java.lang.String> mapAvailableStrings(java.lang.String uri) throws java.io.IOException
Reads the contents of all non-directory URLs immediately under the specified location and returns them in a map keyed by the file name. Individual URLs that cannot be read are skipped and added to the list of 'resourcesNotLoaded' Example classpath: META-INF/serializables/one META-INF/serializables/two # not readable META-INF/serializables/three META-INF/serializables/four/foo.txt ResourceFinder finder = new ResourceFinder("META-INF/"); Map map = finder.mapAvailableStrings("serializables"); map.contains("one"); // true map.contains("two"); // false map.contains("three"); // true map.contains("four"); // false- Parameters:
uri
-- Returns:
- a list of the content of each resource URL found
- Throws:
java.io.IOException
- if classLoader.getResources throws an exception
-
findClass
public java.lang.Class<?> findClass(java.lang.String uri) throws java.io.IOException, java.lang.ClassNotFoundException
ExecutesfindString(String)
assuming the contents URL found is the name of a class that should be loaded and returned.- Parameters:
uri
-- Returns:
- Throws:
java.io.IOException
java.lang.ClassNotFoundException
-
findAllClasses
public java.util.List<java.lang.Class<?>> findAllClasses(java.lang.String uri) throws java.io.IOException, java.lang.ClassNotFoundException
Executes findAllStrings assuming the strings are the names of a classes that should be loaded and returned. Any URL or class that cannot be loaded will cause an exception to be thrown.- Parameters:
uri
-- Returns:
- Throws:
java.io.IOException
java.lang.ClassNotFoundException
-
findAvailableClasses
public java.util.List<java.lang.Class<?>> findAvailableClasses(java.lang.String uri) throws java.io.IOException
Executes findAvailableStrings assuming the strings are the names of a classes that should be loaded and returned. Any class that cannot be loaded will be skipped and placed in the 'resourcesNotLoaded' collection.- Parameters:
uri
-- Returns:
- Throws:
java.io.IOException
- if classLoader.getResources throws an exception
-
mapAllClasses
public java.util.Map<java.lang.String,java.lang.Class<?>> mapAllClasses(java.lang.String uri) throws java.io.IOException, java.lang.ClassNotFoundException
Executes mapAllStrings assuming the value of each entry in the map is the name of a class that should be loaded. Any class that cannot be loaded will be cause an exception to be thrown. Example classpath: META-INF/xmlparsers/xerces META-INF/xmlparsers/crimson ResourceFinder finder = new ResourceFinder("META-INF/"); Map map = finder.mapAvailableStrings("xmlparsers"); map.contains("xerces"); // true map.contains("crimson"); // true Class xercesClass = map.get("xerces"); Class crimsonClass = map.get("crimson");- Parameters:
uri
-- Returns:
- Throws:
java.io.IOException
java.lang.ClassNotFoundException
-
mapAvailableClasses
public java.util.Map<java.lang.String,java.lang.Class<?>> mapAvailableClasses(java.lang.String uri) throws java.io.IOException
Executes mapAvailableStrings assuming the value of each entry in the map is the name of a class that should be loaded. Any class that cannot be loaded will be skipped and placed in the 'resourcesNotLoaded' collection. Example classpath: META-INF/xmlparsers/xerces META-INF/xmlparsers/crimson ResourceFinder finder = new ResourceFinder("META-INF/"); Map map = finder.mapAvailableStrings("xmlparsers"); map.contains("xerces"); // true map.contains("crimson"); // true Class xercesClass = map.get("xerces"); Class crimsonClass = map.get("crimson");- Parameters:
uri
-- Returns:
- Throws:
java.io.IOException
- if classLoader.getResources throws an exception
-
findImplementation
public java.lang.Class<?> findImplementation(java.lang.Class<?> interfase) throws java.io.IOException, java.lang.ClassNotFoundException
Assumes the class specified points to a file in the classpath that contains the name of a class that implements or is a subclass of the specfied class. Any class that cannot be loaded will be cause an exception to be thrown. Example classpath: META-INF/java.io.InputStream # contains the classname org.acme.AcmeInputStream META-INF/java.io.OutputStream ResourceFinder finder = new ResourceFinder("META-INF/"); Class clazz = finder.findImplementation(java.io.InputStream.class); clazz.getName(); // returns "org.acme.AcmeInputStream"- Parameters:
interfase
- a superclass or interface- Returns:
- Throws:
java.io.IOException
- if the URL cannot be readjava.lang.ClassNotFoundException
- if the class found is not loadablejava.lang.ClassCastException
- if the class found is not assignable to the specified superclass or interface
-
findAllImplementations
public <T> java.util.List<java.lang.Class<? extends T>> findAllImplementations(java.lang.Class<T> interfase) throws java.io.IOException, java.lang.ClassNotFoundException
Assumes the class specified points to a file in the classpath that contains the name of a class that implements or is a subclass of the specfied class. Any class that cannot be loaded or assigned to the specified interface will be cause an exception to be thrown. Example classpath: META-INF/java.io.InputStream # contains the classname org.acme.AcmeInputStream META-INF/java.io.InputStream # contains the classname org.widget.NeatoInputStream META-INF/java.io.InputStream # contains the classname com.foo.BarInputStream ResourceFinder finder = new ResourceFinder("META-INF/"); List classes = finder.findAllImplementations(java.io.InputStream.class); classes.contains("org.acme.AcmeInputStream"); // true classes.contains("org.widget.NeatoInputStream"); // true classes.contains("com.foo.BarInputStream"); // true- Parameters:
interfase
- a superclass or interface- Returns:
- Throws:
java.io.IOException
- if the URL cannot be readjava.lang.ClassNotFoundException
- if the class found is not loadablejava.lang.ClassCastException
- if the class found is not assignable to the specified superclass or interface
-
findAvailableImplementations
public <T> java.util.List<java.lang.Class<? extends T>> findAvailableImplementations(java.lang.Class<T> interfase) throws java.io.IOException
Assumes the class specified points to a file in the classpath that contains the name of a class that implements or is a subclass of the specfied class. Any class that cannot be loaded or are not assignable to the specified class will be skipped and placed in the 'resourcesNotLoaded' collection. Example classpath: META-INF/java.io.InputStream # contains the classname org.acme.AcmeInputStream META-INF/java.io.InputStream # contains the classname org.widget.NeatoInputStream META-INF/java.io.InputStream # contains the classname com.foo.BarInputStream ResourceFinder finder = new ResourceFinder("META-INF/"); List classes = finder.findAllImplementations(java.io.InputStream.class); classes.contains("org.acme.AcmeInputStream"); // true classes.contains("org.widget.NeatoInputStream"); // true classes.contains("com.foo.BarInputStream"); // true- Parameters:
interfase
- a superclass or interface- Returns:
- Throws:
java.io.IOException
- if classLoader.getResources throws an exception
-
mapAllImplementations
public <T> java.util.Map<java.lang.String,java.lang.Class<? extends T>> mapAllImplementations(java.lang.Class<T> interfase) throws java.io.IOException, java.lang.ClassNotFoundException
Assumes the class specified points to a directory in the classpath that holds files containing the name of a class that implements or is a subclass of the specfied class. Any class that cannot be loaded or assigned to the specified interface will be cause an exception to be thrown. Example classpath: META-INF/java.net.URLStreamHandler/jar META-INF/java.net.URLStreamHandler/file META-INF/java.net.URLStreamHandler/http ResourceFinder finder = new ResourceFinder("META-INF/"); Map map = finder.mapAllImplementations(java.net.URLStreamHandler.class); Class jarUrlHandler = map.get("jar"); Class fileUrlHandler = map.get("file"); Class httpUrlHandler = map.get("http");- Parameters:
interfase
- a superclass or interface- Returns:
- Throws:
java.io.IOException
- if the URL cannot be readjava.lang.ClassNotFoundException
- if the class found is not loadablejava.lang.ClassCastException
- if the class found is not assignable to the specified superclass or interface
-
mapAvailableImplementations
public <T> java.util.Map<java.lang.String,java.lang.Class<? extends T>> mapAvailableImplementations(java.lang.Class<T> interfase) throws java.io.IOException
Assumes the class specified points to a directory in the classpath that holds files containing the name of a class that implements or is a subclass of the specfied class. Any class that cannot be loaded or are not assignable to the specified class will be skipped and placed in the 'resourcesNotLoaded' collection. Example classpath: META-INF/java.net.URLStreamHandler/jar META-INF/java.net.URLStreamHandler/file META-INF/java.net.URLStreamHandler/http ResourceFinder finder = new ResourceFinder("META-INF/"); Map map = finder.mapAllImplementations(java.net.URLStreamHandler.class); Class jarUrlHandler = map.get("jar"); Class fileUrlHandler = map.get("file"); Class httpUrlHandler = map.get("http");- Parameters:
interfase
- a superclass or interface- Returns:
- Throws:
java.io.IOException
- if classLoader.getResources throws an exception
-
findProperties
public java.util.Properties findProperties(java.lang.String uri) throws java.io.IOException
Finds the corresponding resource and reads it in as a properties file Example classpath: META-INF/widget.properties ResourceFinder finder = new ResourceFinder("META-INF/"); Properties widgetProps = finder.findProperties("widget.properties");- Parameters:
uri
-- Returns:
- Throws:
java.io.IOException
- if the URL cannot be read or is not in properties file format
-
findAllProperties
public java.util.List<java.util.Properties> findAllProperties(java.lang.String uri) throws java.io.IOException
Finds the corresponding resources and reads them in as a properties files Any URL that cannot be read in as a properties file will cause an exception to be thrown. Example classpath: META-INF/app.properties META-INF/app.properties META-INF/app.properties ResourceFinder finder = new ResourceFinder("META-INF/"); ListappProps = finder.findAllProperties("app.properties"); - Parameters:
uri
-- Returns:
- Throws:
java.io.IOException
- if the URL cannot be read or is not in properties file format
-
findAvailableProperties
public java.util.List<java.util.Properties> findAvailableProperties(java.lang.String uri) throws java.io.IOException
Finds the corresponding resources and reads them in as a properties files Any URL that cannot be read in as a properties file will be added to the 'resourcesNotLoaded' collection. Example classpath: META-INF/app.properties META-INF/app.properties META-INF/app.properties ResourceFinder finder = new ResourceFinder("META-INF/"); ListappProps = finder.findAvailableProperties("app.properties"); - Parameters:
uri
-- Returns:
- Throws:
java.io.IOException
- if classLoader.getResources throws an exception
-
mapAllProperties
public java.util.Map<java.lang.String,java.util.Properties> mapAllProperties(java.lang.String uri) throws java.io.IOException
Finds the corresponding resources and reads them in as a properties files Any URL that cannot be read in as a properties file will cause an exception to be thrown. Example classpath: - META-INF/jdbcDrivers/oracle.properties - META-INF/jdbcDrivers/mysql.props - META-INF/jdbcDrivers/derby ResourceFinder finder = new ResourceFinder("META-INF/"); ListdriversList = finder.findAvailableProperties("jdbcDrivers"); Properties oracleProps = driversList.get("oracle.properties"); Properties mysqlProps = driversList.get("mysql.props"); Properties derbyProps = driversList.get("derby"); - Parameters:
uri
-- Returns:
- Throws:
java.io.IOException
- if the URL cannot be read or is not in properties file format
-
mapAvailableProperties
public java.util.Map<java.lang.String,java.util.Properties> mapAvailableProperties(java.lang.String uri) throws java.io.IOException
Finds the corresponding resources and reads them in as a properties files Any URL that cannot be read in as a properties file will be added to the 'resourcesNotLoaded' collection. Example classpath: META-INF/jdbcDrivers/oracle.properties META-INF/jdbcDrivers/mysql.props META-INF/jdbcDrivers/derby ResourceFinder finder = new ResourceFinder("META-INF/"); ListdriversList = finder.findAvailableProperties("jdbcDrivers"); Properties oracleProps = driversList.get("oracle.properties"); Properties mysqlProps = driversList.get("mysql.props"); Properties derbyProps = driversList.get("derby"); - Parameters:
uri
-- Returns:
- Throws:
java.io.IOException
- if classLoader.getResources throws an exception
-
getResourcesMap
public java.util.Map<java.lang.String,java.net.URL> getResourcesMap(java.lang.String uri) throws java.io.IOException
- Throws:
java.io.IOException
-
readDirectoryEntries
private static void readDirectoryEntries(java.net.URL location, java.util.Map<java.lang.String,java.net.URL> resources) throws java.net.MalformedURLException
- Throws:
java.net.MalformedURLException
-
readJarEntries
private static void readJarEntries(java.net.URL location, java.lang.String basePath, java.util.Map<java.lang.String,java.net.URL> resources) throws java.io.IOException
- Throws:
java.io.IOException
-
loadProperties
private java.util.Properties loadProperties(java.net.URL resource) throws java.io.IOException
- Throws:
java.io.IOException
-
readContents
private java.lang.String readContents(java.net.URL resource) throws java.io.IOException
- Throws:
java.io.IOException
-
getResource
public java.net.URL getResource(java.lang.String fullUri)
-
getResources
private java.util.Enumeration<java.net.URL> getResources(java.lang.String fulluri) throws java.io.IOException
- Throws:
java.io.IOException
-
findResource
private java.net.URL findResource(java.lang.String resourceName, java.net.URL... search)
-
targetURL
private java.net.URL targetURL(java.net.URL base, java.lang.String name) throws java.net.MalformedURLException
- Throws:
java.net.MalformedURLException
-
decode
public static java.lang.String decode(java.lang.String fileName)
-
-