Class ResourceManager
- java.lang.Object
-
- org.flexdock.util.ResourceManager
-
public class ResourceManager extends java.lang.Object
This class providesstatic
convenience 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.String
LIBRARY_EXTENSION
Defines 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 void
close(java.io.InputStream in)
Callsclose()
on the specifiedInputStream
.static void
close(java.io.OutputStream out)
Callsclose()
on the specifiedOutputStream
.static void
close(java.net.Socket socket)
Callsclose()
on the specifiedSocket
.static java.awt.Cursor
createCursor(java.lang.String url, java.awt.Point hotPoint, java.lang.String name)
Returns aCursor
object based on the specified resource URL.static java.awt.Cursor
createCursor(java.net.URL imageURL, java.awt.Point hotPoint, java.lang.String name)
Returns aCursor
object based on the specified resource URL.static javax.swing.ImageIcon
createIcon(java.lang.String url)
Returns anImageIcon
object based on the specified resource URL.static java.awt.Image
createImage(java.lang.String url)
Returns anImage
object based on the specified resource URL.static java.awt.Image
createImage(java.net.URL imageLocation)
Returns anImage
object based on the specified resource URL.static org.w3c.dom.Document
getDocument(java.lang.String uri)
Returns aDocument
object based on the specified resourceuri
.static org.w3c.dom.Document
getDocument(java.net.URL url)
Returns aDocument
object based on the specified resourceURL
.static java.util.Properties
getProperties(java.lang.String uri)
Returns aProperties
object based on the specified resourceuri
.static java.util.Properties
getProperties(java.lang.String uri, boolean failSilent)
Returns aProperties
object based on the specified resourceuri
.static java.util.Properties
getProperties(java.net.URL url)
Returns aProperties
object based on the specified resourceURL
.static java.util.Properties
getProperties(java.net.URL url, boolean failSilent)
Returns aProperties
object based on the specified resourceurl
.static java.net.URL
getResource(java.lang.String uri)
Performs resource lookups using theClassLoader
and classpath.static boolean
isWindowsPlatform()
Returnstrue
if the JVM is currently running onWindows
;false
otherwise.static void
loadLibrary(java.lang.String library, java.lang.String classpathResource)
Attempts to load the specified nativelibrary
, usingclasspathResource
and the filesystem to implement several fallback mechanisms in the event the library cannot be loaded.
-
-
-
Method Detail
-
isWindowsPlatform
public static boolean isWindowsPlatform()
Returnstrue
if the JVM is currently running onWindows
;false
otherwise.- Returns:
true
if the JVM is currently running onWindows
;false
otherwise.
-
getResource
public static java.net.URL getResource(java.lang.String uri)
Performs resource lookups using theClassLoader
and 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. Returnsnull
if specified resource cannot be found.- Parameters:
uri
- the String describing the resource to be looked up- Returns:
- a
URL
representing the resource that has been looked up.
-
createImage
public static java.awt.Image createImage(java.lang.String url)
Returns anImage
object based on the specified resource URL. Does not perform any caching on theImage
object, so a new object will be created with each call to this method.- Parameters:
url
- theString
describing the resource to be looked up- Returns:
- an
Image
created 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 anImage
object based on the specified resource URL. Does not perform any caching on theImage
object, so a new object will be created with each call to this method.- Parameters:
imageLocation
- theURL
indicating where the image resource may be found.- Returns:
- an
Image
created 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 anImageIcon
object based on the specified resource URL. Uses theImageIcon
constructor internally instead of dispatching tocreateImage(String url)
, soImage
objects are cached via theMediaTracker
.- Parameters:
url
- theString
describing the resource to be looked up- Returns:
- an
ImageIcon
created 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 aCursor
object based on the specified resource URL. Throws aNullPointerException
if specified resource cannot be found. Dispatches tocreateImage(URL imageLocation)
, soImage
objects are not cached via theMediaTracker
.- Parameters:
imageURL
- theURL
indicating 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
Cursor
created 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 aCursor
object based on the specified resource URL. Throws aNullPointerException
if specified resource cannot be found. Dispatches tocreateImage(String url)
, soImage
objects are not cached via theMediaTracker
.- Parameters:
url
- theString
describing 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
Cursor
created 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
, usingclasspathResource
and 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
library
isnull
, 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 anUnsatisfiedLinkError
is 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
classpathResource
is checked. IfclasspathResource
isnull
, then there is no more information available to attempt to resolve the requested library and this method throws the lastUnsatisfiedLinkError
encountered.If
classpathResource
is non-null
, then anInputStream
to the specified resource is resolved from the class loader. The contents of theInputStream
are read into abyte
array 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 specifiedclasspathResource
cannot 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 appropriateUnsatisfiedLinkError
specific 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 aDocument
object based on the specified resourceuri
. This method resolves aURL
from the specifiedString
viagetResource(String uri)
and dispatches togetDocument(URL url)
. If the specifieduri
isnull
, then this method returnsnull
.- Parameters:
uri
- theString
describing the resource to be looked up- Returns:
- a
Document
object based on the specified resourceuri
- See Also:
getResource(String)
,getDocument(URL)
-
getDocument
public static org.w3c.dom.Document getDocument(java.net.URL url)
Returns aDocument
object based on the specified resourceURL
. This method will open anInputStream
to the specifiedURL
and construct aDocument
instance. If anyExceptions
are encountered in the process, this method returnsnull
. If the specifiedURL
isnull
, then this method returnsnull
.- Parameters:
url
- theURL
describing the resource to be looked up- Returns:
- a
Document
object based on the specified resourceURL
-
getProperties
public static java.util.Properties getProperties(java.lang.String uri)
Returns aProperties
object based on the specified resourceuri
. This method resolves aURL
from the specifiedString
viagetResource(String uri)
and dispatches togetProperties(URL url, boolean failSilent)
with an argument offalse
forfailSilent
. If the specifieduri
isnull
, then this method will print a stack trace for the ensuingNullPointerException
and returnnull
.- Parameters:
uri
- theString
describing the resource to be looked up- Returns:
- a
Properties
object 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 aProperties
object based on the specified resourceuri
. This method resolves aURL
from the specifiedString
viagetResource(String uri)
and dispatches togetProperties(URL url, boolean failSilent)
, passing the specifiedfailSilent
parameter. If the specifieduri
isnull
, this method will returnnull
. IffailSilent
isfalse
, then the ensuingNullPointerException's
stacktrace will be printed to theSystem.err
before returning.- Parameters:
uri
- theString
describing the resource to be looked upfailSilent
-true
if no errors are to be reported to theSystem.err
before returning;false
otherwise.- Returns:
- a
Properties
object based on the specified resourceuri
. - See Also:
getResource(String)
,getProperties(URL, boolean)
-
getProperties
public static java.util.Properties getProperties(java.net.URL url)
Returns aProperties
object based on the specified resourceURL
. This method dispatches togetProperties(URL url, boolean failSilent)
, with an argument offalse
forfailSilent
. If the specifieduri
isnull
, this method will print the ensuingNullPointerException
stack tracke to theSystem.err
and returnnull
.- Parameters:
url
- theURL
describing the resource to be looked up- Returns:
- a
Properties
object based on the specified resourceurl
. - See Also:
getProperties(URL, boolean)
-
getProperties
public static java.util.Properties getProperties(java.net.URL url, boolean failSilent)
Returns aProperties
object based on the specified resourceurl
. If the specifieduri
isnull
, this method will returnnull
. If any errors are encountered during the properties-load process, this method will returnnull
. IffailSilent
isfalse
, then the any encoutered error stacktraces will be printed to theSystem.err
before returning.- Parameters:
url
- theURL
describing the resource to be looked upfailSilent
-true
if no errors are to be reported to theSystem.err
before returning;false
otherwise.- Returns:
- a
Properties
object based on the specified resourceurl
.
-
close
public static void close(java.io.InputStream in)
Callsclose()
on the specifiedInputStream
. AnyExceptions
encountered will be printed to theSystem.err
. Ifin
isnull
, then noException
is thrown and no action is taken.- Parameters:
in
- theInputStream
to close- See Also:
InputStream.close()
-
close
public static void close(java.io.OutputStream out)
Callsclose()
on the specifiedOutputStream
. AnyExceptions
encountered will be printed to theSystem.err
. Ifout
isnull
, then noException
is thrown and no action is taken.- Parameters:
out
- theOutputStream
to close- See Also:
OutputStream.close()
-
close
public static void close(java.net.Socket socket)
Callsclose()
on the specifiedSocket
. AnyExceptions
encountered will be printed to theSystem.err
. Ifsocket
isnull
, then noException
is thrown and no action is taken.- Parameters:
socket
- theSocket
to close- See Also:
Socket.close()
-
-