Class ResourceManager
- java.lang.Object
-
- org.flexdock.util.ResourceManager
-
public class ResourceManager extends java.lang.ObjectThis class providesstaticconvenience methods for resource management, including resource lookups and image, icon, and cursor creation.- Author:
- Chris Butler
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringLIBRARY_EXTENSIONDefines the file extension used by native shared libraries on the current system.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidclose(java.io.InputStream in)Callsclose()on the specifiedInputStream.static voidclose(java.io.OutputStream out)Callsclose()on the specifiedOutputStream.static voidclose(java.net.Socket socket)Callsclose()on the specifiedSocket.static java.awt.CursorcreateCursor(java.lang.String url, java.awt.Point hotPoint, java.lang.String name)Returns aCursorobject based on the specified resource URL.static java.awt.CursorcreateCursor(java.net.URL imageURL, java.awt.Point hotPoint, java.lang.String name)Returns aCursorobject based on the specified resource URL.static javax.swing.ImageIconcreateIcon(java.lang.String url)Returns anImageIconobject based on the specified resource URL.static java.awt.ImagecreateImage(java.lang.String url)Returns anImageobject based on the specified resource URL.static java.awt.ImagecreateImage(java.net.URL imageLocation)Returns anImageobject based on the specified resource URL.static org.w3c.dom.DocumentgetDocument(java.lang.String uri)Returns aDocumentobject based on the specified resourceuri.static org.w3c.dom.DocumentgetDocument(java.net.URL url)Returns aDocumentobject based on the specified resourceURL.static java.util.PropertiesgetProperties(java.lang.String uri)Returns aPropertiesobject based on the specified resourceuri.static java.util.PropertiesgetProperties(java.lang.String uri, boolean failSilent)Returns aPropertiesobject based on the specified resourceuri.static java.util.PropertiesgetProperties(java.net.URL url)Returns aPropertiesobject based on the specified resourceURL.static java.util.PropertiesgetProperties(java.net.URL url, boolean failSilent)Returns aPropertiesobject based on the specified resourceurl.static java.net.URLgetResource(java.lang.String uri)Performs resource lookups using theClassLoaderand classpath.static booleanisWindowsPlatform()Returnstrueif the JVM is currently running onWindows;falseotherwise.static voidloadLibrary(java.lang.String library, java.lang.String classpathResource)Attempts to load the specified nativelibrary, usingclasspathResourceand the filesystem to implement several fallback mechanisms in the event the library cannot be loaded.
-
-
-
Method Detail
-
isWindowsPlatform
public static boolean isWindowsPlatform()
Returnstrueif the JVM is currently running onWindows;falseotherwise.- Returns:
trueif the JVM is currently running onWindows;falseotherwise.
-
getResource
public static java.net.URL getResource(java.lang.String uri)
Performs resource lookups using theClassLoaderand classpath. This method attemps to consolidate several techniques used for resource lookup in different situations, providing a common API that works the same from standalone applications to applets to multiple-classloader container-managed applications. Returnsnullif specified resource cannot be found.- Parameters:
uri- the String describing the resource to be looked up- Returns:
- a
URLrepresenting the resource that has been looked up.
-
createImage
public static java.awt.Image createImage(java.lang.String url)
Returns anImageobject based on the specified resource URL. Does not perform any caching on theImageobject, so a new object will be created with each call to this method.- Parameters:
url- theStringdescribing the resource to be looked up- Returns:
- an
Imagecreated from the specified resource URL - Throws:
java.lang.NullPointerException- if specified resource cannot be found.
-
createImage
public static java.awt.Image createImage(java.net.URL imageLocation)
Returns anImageobject based on the specified resource URL. Does not perform any caching on theImageobject, so a new object will be created with each call to this method.- Parameters:
imageLocation- theURLindicating where the image resource may be found.- Returns:
- an
Imagecreated from the specified resource URL - Throws:
java.lang.NullPointerException- if specified resource cannot be found.
-
createIcon
public static javax.swing.ImageIcon createIcon(java.lang.String url)
Returns anImageIconobject based on the specified resource URL. Uses theImageIconconstructor internally instead of dispatching tocreateImage(String url), soImageobjects are cached via theMediaTracker.- Parameters:
url- theStringdescribing the resource to be looked up- Returns:
- an
ImageIconcreated from the specified resource URL - Throws:
java.lang.NullPointerException- if specified resource cannot be found.
-
createCursor
public static java.awt.Cursor createCursor(java.net.URL imageURL, java.awt.Point hotPoint, java.lang.String name)Returns aCursorobject based on the specified resource URL. Throws aNullPointerExceptionif specified resource cannot be found. Dispatches tocreateImage(URL imageLocation), soImageobjects are not cached via theMediaTracker.- Parameters:
imageURL- theURLindicating where the image resource may be found.hotPoint- the X and Y of the large cursor's hot spot. The hotSpot values must be less than the Dimension returned by getBestCursorSize().name- a localized description of the cursor, for Java Accessibility use.- Returns:
- a
Cursorcreated from the specified resource URL - Throws:
java.lang.NullPointerException- if specified resource cannot be found.java.lang.IndexOutOfBoundsException- if the hotSpot values are outside
-
createCursor
public static java.awt.Cursor createCursor(java.lang.String url, java.awt.Point hotPoint, java.lang.String name)Returns aCursorobject based on the specified resource URL. Throws aNullPointerExceptionif specified resource cannot be found. Dispatches tocreateImage(String url), soImageobjects are not cached via theMediaTracker.- Parameters:
url- theStringdescribing the resource to be looked uphotPoint- the X and Y of the large cursor's hot spot. The hotSpot values must be less than the Dimension returned by getBestCursorSize().name- a localized description of the cursor, for Java Accessibility use.- Returns:
- a
Cursorcreated from the specified resource URL - Throws:
java.lang.NullPointerException- if specified resource cannot be found.java.lang.IndexOutOfBoundsException- if the hotSpot values are outside
-
loadLibrary
public static void loadLibrary(java.lang.String library, java.lang.String classpathResource)Attempts to load the specified nativelibrary, usingclasspathResourceand the filesystem to implement several fallback mechanisms in the event the library cannot be loaded. This method should provide seamless installation and loading of native libraries from within the classpath so that native libraries may be packaged within the relavant library JAR, rather than requiring separate user installation of the native libraries into the system$PATH.If the specified
libraryisnull, then this method returns with no action taken.This method will first attempt to call
System.loadLibrary(library). If this call is successful, then the method will exit here. If anUnsatisfiedLinkErroris encountered, then this method attempts to locate a FlexDock-specific filesystem resource for the native library, called the "FlexDock Library".The FlexDock Library will reside on the filesystem under the user's home directory with the path ${user.home}/flexdock/${library}${native.lib.extension}. Thus, if this method is called with an argument of
"foo"for the library, then under windows the FlexDock Library should be C:\Documents and Settings\${user.home}\flexdock\foo.dll. Under any type of Unix system, the FlexDock library should be /home/${user.home}/flexdock/foo.so.If the FlexDock Library exists on the filesystem, then this method will attempt to load it by calling
System.load(String filename)with the FlexDock Library's absolute path. If this call is successful, then the method exits here.If the FlexDock Library cannot be loaded, then the specified
classpathResourceis checked. IfclasspathResourceisnull, then there is no more information available to attempt to resolve the requested library and this method throws the lastUnsatisfiedLinkErrorencountered.If
classpathResourceis non-null, then anInputStreamto the specified resource is resolved from the class loader. The contents of theInputStreamare read into abytearray and written to disk as the FlexDock Library file. The FlexDock Library is then loaded with a call toSystem.load(String filename)with the FlexDock Library's absolute path. If the specifiedclasspathResourcecannot be resolved by the class loader, if any errors occur during this process of extracting and writing to disk, or if the resulting FlexDock Library file cannot be loaded as a native library, then this method throws an appropriateUnsatisfiedLinkErrorspecific to the situation that prevented the native library from loading.Note that because this method may extract resources from the classpath and install to the filesystem as a FlexDock Library, subsequent calls to this method across JVM sessions will find the FlexDock Library on the filesystem and bypass the extraction process.
- Parameters:
library- the native library to loadclasspathResource- the fallback location within the classpath from which to extract the desired native library in the event it is not already installed on the target system- Throws:
java.lang.UnsatisfiedLinkError- if the library cannot be loaded
-
getDocument
public static org.w3c.dom.Document getDocument(java.lang.String uri)
Returns aDocumentobject based on the specified resourceuri. This method resolves aURLfrom the specifiedStringviagetResource(String uri)and dispatches togetDocument(URL url). If the specifieduriisnull, then this method returnsnull.- Parameters:
uri- theStringdescribing the resource to be looked up- Returns:
- a
Documentobject based on the specified resourceuri - See Also:
getResource(String),getDocument(URL)
-
getDocument
public static org.w3c.dom.Document getDocument(java.net.URL url)
Returns aDocumentobject based on the specified resourceURL. This method will open anInputStreamto the specifiedURLand construct aDocumentinstance. If anyExceptionsare encountered in the process, this method returnsnull. If the specifiedURLisnull, then this method returnsnull.- Parameters:
url- theURLdescribing the resource to be looked up- Returns:
- a
Documentobject based on the specified resourceURL
-
getProperties
public static java.util.Properties getProperties(java.lang.String uri)
Returns aPropertiesobject based on the specified resourceuri. This method resolves aURLfrom the specifiedStringviagetResource(String uri)and dispatches togetProperties(URL url, boolean failSilent)with an argument offalseforfailSilent. If the specifieduriisnull, then this method will print a stack trace for the ensuingNullPointerExceptionand returnnull.- Parameters:
uri- theStringdescribing the resource to be looked up- Returns:
- a
Propertiesobject based on the specified resourceuri. - See Also:
getResource(String),getProperties(URL, boolean)
-
getProperties
public static java.util.Properties getProperties(java.lang.String uri, boolean failSilent)Returns aPropertiesobject based on the specified resourceuri. This method resolves aURLfrom the specifiedStringviagetResource(String uri)and dispatches togetProperties(URL url, boolean failSilent), passing the specifiedfailSilentparameter. If the specifieduriisnull, this method will returnnull. IffailSilentisfalse, then the ensuingNullPointerException'sstacktrace will be printed to theSystem.errbefore returning.- Parameters:
uri- theStringdescribing the resource to be looked upfailSilent-trueif no errors are to be reported to theSystem.errbefore returning;falseotherwise.- Returns:
- a
Propertiesobject based on the specified resourceuri. - See Also:
getResource(String),getProperties(URL, boolean)
-
getProperties
public static java.util.Properties getProperties(java.net.URL url)
Returns aPropertiesobject based on the specified resourceURL. This method dispatches togetProperties(URL url, boolean failSilent), with an argument offalseforfailSilent. If the specifieduriisnull, this method will print the ensuingNullPointerExceptionstack tracke to theSystem.errand returnnull.- Parameters:
url- theURLdescribing the resource to be looked up- Returns:
- a
Propertiesobject based on the specified resourceurl. - See Also:
getProperties(URL, boolean)
-
getProperties
public static java.util.Properties getProperties(java.net.URL url, boolean failSilent)Returns aPropertiesobject based on the specified resourceurl. If the specifieduriisnull, this method will returnnull. If any errors are encountered during the properties-load process, this method will returnnull. IffailSilentisfalse, then the any encoutered error stacktraces will be printed to theSystem.errbefore returning.- Parameters:
url- theURLdescribing the resource to be looked upfailSilent-trueif no errors are to be reported to theSystem.errbefore returning;falseotherwise.- Returns:
- a
Propertiesobject based on the specified resourceurl.
-
close
public static void close(java.io.InputStream in)
Callsclose()on the specifiedInputStream. AnyExceptionsencountered will be printed to theSystem.err. Ifinisnull, then noExceptionis thrown and no action is taken.- Parameters:
in- theInputStreamto close- See Also:
InputStream.close()
-
close
public static void close(java.io.OutputStream out)
Callsclose()on the specifiedOutputStream. AnyExceptionsencountered will be printed to theSystem.err. Ifoutisnull, then noExceptionis thrown and no action is taken.- Parameters:
out- theOutputStreamto close- See Also:
OutputStream.close()
-
close
public static void close(java.net.Socket socket)
Callsclose()on the specifiedSocket. AnyExceptionsencountered will be printed to theSystem.err. Ifsocketisnull, then noExceptionis thrown and no action is taken.- Parameters:
socket- theSocketto close- See Also:
Socket.close()
-
-