Class ResourceManager
static convenience methods for resource
management, including resource lookups and image, icon, and cursor creation.- Author:
- Chris Butler
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringDefines the file extension used by native shared libraries on the current system. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidclose(InputStream in) Callsclose()on the specifiedInputStream.static voidclose(OutputStream out) Callsclose()on the specifiedOutputStream.static voidCallsclose()on the specifiedSocket.static CursorcreateCursor(String url, Point hotPoint, String name) Returns aCursorobject based on the specified resource URL.static CursorcreateCursor(URL imageURL, Point hotPoint, String name) Returns aCursorobject based on the specified resource URL.static ImageIconcreateIcon(String url) Returns anImageIconobject based on the specified resource URL.static ImagecreateImage(String url) Returns anImageobject based on the specified resource URL.static ImagecreateImage(URL imageLocation) Returns anImageobject based on the specified resource URL.static DocumentgetDocument(String uri) Returns aDocumentobject based on the specified resourceuri.static DocumentgetDocument(URL url) Returns aDocumentobject based on the specified resourceURL.static PropertiesgetProperties(String uri) Returns aPropertiesobject based on the specified resourceuri.static PropertiesgetProperties(String uri, boolean failSilent) Returns aPropertiesobject based on the specified resourceuri.static PropertiesgetProperties(URL url) Returns aPropertiesobject based on the specified resourceURL.static PropertiesgetProperties(URL url, boolean failSilent) Returns aPropertiesobject based on the specified resourceurl.static URLgetResource(String uri) Performs resource lookups using theClassLoaderand classpath.static booleanReturnstrueif the JVM is currently running onWindows;falseotherwise.static voidloadLibrary(String library, String classpathResource) Attempts to load the specified nativelibrary, usingclasspathResourceand the filesystem to implement several fallback mechanisms in the event the library cannot be loaded.
-
Field Details
-
LIBRARY_EXTENSION
Defines the file extension used by native shared libraries on the current system.
-
-
Method Details
-
isWindowsPlatform
public static boolean isWindowsPlatform()Returnstrueif the JVM is currently running onWindows;falseotherwise.- Returns:
trueif the JVM is currently running onWindows;falseotherwise.
-
getResource
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
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:
NullPointerException- if specified resource cannot be found.
-
createImage
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:
NullPointerException- if specified resource cannot be found.
-
createIcon
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:
NullPointerException- if specified resource cannot be found.
-
createCursor
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:
NullPointerException- if specified resource cannot be found.IndexOutOfBoundsException- if the hotSpot values are outside
-
createCursor
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:
NullPointerException- if specified resource cannot be found.IndexOutOfBoundsException- if the hotSpot values are outside
-
loadLibrary
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:
UnsatisfiedLinkError- if the library cannot be loaded
-
getDocument
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:
-
getDocument
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
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:
-
getProperties
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:
-
getProperties
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
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
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:
-
close
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:
-
close
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:
-