📄 resourcemanager.java
字号:
/* Copyright (c) 2004 Christopher M Butler
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in the
Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished to do so, subject
to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
package org.flexdock.util;
import java.awt.Cursor;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.util.Properties;
import javax.swing.ImageIcon;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
/**
* This class provides {@code static} convenience methods for resource
* management, including resource lookups and image, icon, and cursor creation.
*
* @author Chris Butler
*/
public class ResourceManager {
private static Log log = LogFactory.getLog(ResourceManager.class);
private ResourceManager() {
// does nothing
}
/**
* Defines the file extension used by native shared libraries on the current
* system.
*/
public static final String LIBRARY_EXTENSION = getLibraryExtension();
private static String getLibraryExtension() {
return isWindowsPlatform() ? ".dll" : ".so";
}
/**
* Returns {@code true} if the JVM is currently running on {@code Windows};
* {@code false} otherwise.
*
* @return {@code true} if the JVM is currently running on {@code Windows};
* {@code false} otherwise.
*/
public static boolean isWindowsPlatform() {
String osName = System.getProperty("os.name").toLowerCase();
return osName.indexOf("windows") != -1 || osName.endsWith(" nt");
}
/**
* Performs resource lookups using the {@code ClassLoader} 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. Returns {@code null} if specified
* resource cannot be found.
*
* @param uri
* the String describing the resource to be looked up
* @return a {@code URL} representing the resource that has been looked up.
*/
public static URL getResource(String uri) {
if (uri == null) {
return null;
}
URL url = ResourceManager.class.getResource(uri);
if (url == null)
url = ClassLoader.getSystemResource(uri);
// if we still couldn't find the resource, then slash it and try again
if (url == null && !uri.startsWith("/"))
url = getResource("/" + uri);
// if resource is still null, then check to see if it's a filesystem
// path
if (url == null) {
try {
File file = new File(uri);
if (file.exists())
url = file.toURL();
} catch (MalformedURLException e) {
log.warn(e.getMessage(), e);
url = null;
}
}
return url;
}
/**
* Returns an {@code Image} object based on the specified resource URL. Does
* not perform any caching on the {@code Image} object, so a new object will
* be created with each call to this method.
*
* @param url
* the {@code String} describing the resource to be looked up
* @exception NullPointerException
* if specified resource cannot be found.
* @return an {@code Image} created from the specified resource URL
*/
public static Image createImage(String url) {
try {
URL location = getResource(url);
return Toolkit.getDefaultToolkit().createImage(location);
} catch (NullPointerException e) {
throw new NullPointerException("Unable to locate image: " + url);
}
}
/**
* Returns an {@code Image} object based on the specified resource URL. Does
* not perform any caching on the {@code Image} object, so a new object will
* be created with each call to this method.
*
* @param imageLocation
* the {@code URL} indicating where the image resource may be
* found.
* @exception NullPointerException
* if specified resource cannot be found.
* @return an {@code Image} created from the specified resource URL
*/
public static Image createImage(URL imageLocation) {
try {
return Toolkit.getDefaultToolkit().createImage(imageLocation);
} catch (NullPointerException e) {
throw new NullPointerException("Unable to locate image: "
+ imageLocation);
}
}
/**
* Returns an {@code ImageIcon} object based on the specified resource URL.
* Uses the {@code ImageIcon} constructor internally instead of dispatching
* to {@code createImage(String url)}, so {@code Image} objects are cached
* via the {@code MediaTracker}.
*
* @param url
* the {@code String} describing the resource to be looked up
* @exception NullPointerException
* if specified resource cannot be found.
* @return an {@code ImageIcon} created from the specified resource URL
*/
public static ImageIcon createIcon(String url) {
try {
URL location = getResource(url);
return new ImageIcon(location);
} catch (NullPointerException e) {
throw new NullPointerException("Unable to locate image: " + url);
}
}
/**
* Returns a {@code Cursor} object based on the specified resource URL.
* Throws a {@code NullPointerException} if specified resource cannot be
* found. Dispatches to {@code createImage(URL imageLocation)}, so
* {@code Image} objects are <b>not</b> cached via the{@code MediaTracker}.
*
* @param imageURL
* the {@code URL} indicating where the image resource may be
* found.
* @param hotPoint
* the X and Y of the large cursor's hot spot. The hotSpot values
* must be less than the Dimension returned by
* getBestCursorSize().
* @param name
* a localized description of the cursor, for Java Accessibility
* use.
* @exception NullPointerException
* if specified resource cannot be found.
* @exception IndexOutOfBoundsException
* if the hotSpot values are outside
* @return a {@code Cursor} created from the specified resource URL
*/
public static Cursor createCursor(URL imageURL, Point hotPoint, String name) {
Image image = createImage(imageURL);
Cursor c = Toolkit.getDefaultToolkit().createCustomCursor(image,
hotPoint, name);
return c;
}
/**
* Returns a {@code Cursor} object based on the specified resource URL.
* Throws a {@code NullPointerException} if specified resource cannot be
* found. Dispatches to {@code createImage(String url)}, so {@code Image}
* objects are <b>not</b> cached via the{@code MediaTracker}.
*
* @param url
* the {@code String} describing the resource to be looked up
* @param hotPoint
* the X and Y of the large cursor's hot spot. The hotSpot values
* must be less than the Dimension returned by
* getBestCursorSize().
* @param name
* a localized description of the cursor, for Java Accessibility
* use.
* @exception NullPointerException
* if specified resource cannot be found.
* @exception IndexOutOfBoundsException
* if the hotSpot values are outside
* @return a {@code Cursor} created from the specified resource URL
*/
public static Cursor createCursor(String url, Point hotPoint, String name) {
Image image = createImage(url);
Cursor c = Toolkit.getDefaultToolkit().createCustomCursor(image,
hotPoint, name);
return c;
}
/**
* Attempts to load the specified native {@code library}, using
* {@code classpathResource} 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 {@code $PATH}.
* <p>
* If the specified {@code library} is {@code null}, then this method
* returns with no action taken.
* <p>
* This method will first attempt to call
* {@code System.loadLibrary(library)}. If this call is successful, then
* the method will exit here. If an {@code UnsatisfiedLinkError} is
* encountered, then this method attempts to locate a FlexDock-specific
* filesystem resource for the native library, called the "FlexDock
* Library".
* <p>
* The FlexDock Library will reside on the filesystem under the user's home
* directory with the path <tt>${user.home}/flexdock/${library}${native.lib.extension}</tt>.
* Thus, if this method is called with an argument of {@code "foo"} for the
* library, then under windows the FlexDock Library should be
* <tt>C:\Documents and Settings\${user.home}\flexdock\foo.dll</tt>. Under
* any type of Unix system, the FlexDock library should be
* <tt>/home/${user.home}/flexdock/foo.so</tt>.
* <p>
* If the FlexDock Library exists on the filesystem, then this method will
* attempt to load it by calling {@code System.load(String filename)} with
* the FlexDock Library's absolute path. If this call is successful, then
* the method exits here.
* <p>
* If the FlexDock Library cannot be loaded, then the specified
* {@code classpathResource} is checked. If {@code classpathResource} is
* {@code null}, then there is no more information available to attempt to
* resolve the requested library and this method throws the last
* {@code UnsatisfiedLinkError} encountered.
* <p>
* If {@code classpathResource} is non-{@code null}, then an
* {@code InputStream} to the specified resource is resolved from the class
* loader. The contents of the {@code InputStream} are read into a
* {@code byte} array and written to disk as the FlexDock Library file. The
* FlexDock Library is then loaded with a call to
* {@code System.load(String filename)} with the FlexDock Library's absolute
* path. If the specified {@code classpathResource} 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 appropriate
* {@code UnsatisfiedLinkError} specific to the situation that prevented the
* native library from loading.
* <p>
* 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.
*
* @param library
* the native library to load
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -