📄 resourcemanager.java
字号:
* @param classpathResource
* 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
* @exception UnsatisfiedLinkError
* if the library cannot be loaded
*/
public static void loadLibrary(String library, String classpathResource) {
if (library == null)
return;
UnsatisfiedLinkError linkageError = null;
try {
System.loadLibrary(library);
return;
} catch (UnsatisfiedLinkError err) {
// pass through here
linkageError = err;
}
// determine a file from which we can load our library.
File file = new File(System.getProperty("user.home") + "/flexdock");
file.mkdirs();
file = new File(file.getAbsolutePath() + "/" + library
+ LIBRARY_EXTENSION);
// if the file already exists, try to load from it
if (file.exists()) {
try {
System.load(file.getAbsolutePath());
return;
} catch (UnsatisfiedLinkError err) {
// pass through here
linkageError = err;
}
}
// if we can't load from the classpath, then we're stuck.
// throw the last UnsatisfiedLinkError we encountered.
if (classpathResource == null && linkageError != null)
throw linkageError;
// if the file didn't exist, or we couldn't load from it,
// we'll have to pull from the classpath resource and write it
// to this file. We'll then try to load from the file again.
FileOutputStream fileOut = null;
// get a handle to our resource in the classpath
ClassLoader cl = ResourceManager.class.getClassLoader();
InputStream in = cl.getResourceAsStream(classpathResource);
if (in == null)
throw new UnsatisfiedLinkError(
"Unable to locate classpath resource: " + classpathResource);
try {
// create an outputstream to our destination file
fileOut = new FileOutputStream(file);
byte[] tmp = new byte[1024];
// copy the contents of our resource out to the destination
// file 1K at a time. 1K may seem arbitrary at first, but today
// is a Tuesday, so it makes perfect sense.
int bytesRead = in.read(tmp);
while (bytesRead != -1) {
fileOut.write(tmp, 0, bytesRead);
bytesRead = in.read(tmp);
}
} catch (IOException giveUp) {
// well, I guess we're screwed, aren't we?
UnsatisfiedLinkError err = new UnsatisfiedLinkError(
"Unable to extract resource to file: "
+ file.getAbsolutePath());
err.initCause(giveUp);
throw err;
} finally {
close(fileOut);
close(in);
}
// now that our classpath resource has been written to disk, load the
// native
// library from this file
System.load(file.getAbsolutePath());
}
/**
* Returns a {@code Document} object based on the specified resource
* {@code uri}. This method resolves a {@code URL} from the specified
* {@code String} via {@code getResource(String uri)} and dispatches to
* {@code getDocument(URL url)}. If the specified {@code uri} is
* {@code null}, then this method returns {@code null}.
*
* @param uri
* the {@code String} describing the resource to be looked up
* @return a {@code Document} object based on the specified resource
* {@code uri}
* @see #getResource(String)
* @see #getDocument(URL)
*/
public static Document getDocument(String uri) {
URL resource = getResource(uri);
return getDocument(resource);
}
/**
* Returns a {@code Document} object based on the specified resource
* {@code URL}. This method will open an {@code InputStream} to the
* specified {@code URL} and construct a {@code Document} instance. If any
* {@code Exceptions} are encountered in the process, this method returns
* {@code null}. If the specified {@code URL} is {@code null}, then this
* method returns {@code null}.
*
* @param url
* the {@code URL} describing the resource to be looked up
* @return a {@code Document} object based on the specified resource
* {@code URL}
*/
public static Document getDocument(URL url) {
if (url == null)
return null;
InputStream inStream = null;
try {
inStream = url.openStream();
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(inStream);
} catch (Exception e) {
log.warn(e.getMessage(), e);
} finally {
close(inStream);
}
return null;
}
/**
* Returns a {@code Properties} object based on the specified resource
* {@code uri}. This method resolves a {@code URL} from the specified
* {@code String} via {@code getResource(String uri)} and dispatches to
* {@code getProperties(URL url, boolean failSilent)} with an argument of
* {@code false} for {@code failSilent}. If the specified {@code uri} is
* {@code null}, then this method will print a stack trace for the ensuing
* {@code NullPointerException} and return {@code null}.
*
* @param uri
* the {@code String} describing the resource to be looked up
* @return a {@code Properties} object based on the specified resource
* {@code uri}.
* @see #getResource(String)
* @see #getProperties(URL, boolean)
*/
public static Properties getProperties(String uri) {
return getProperties(uri, false);
}
/**
* Returns a {@code Properties} object based on the specified resource
* {@code uri}. This method resolves a {@code URL} from the specified
* {@code String} via {@code getResource(String uri)} and dispatches to
* {@code getProperties(URL url, boolean failSilent)}, passing the
* specified {@code failSilent} parameter. If the specified {@code uri} is
* {@code null}, this method will return {@code null}. If
* {@code failSilent} is {@code false}, then the ensuing
* {@code NullPointerException's} stacktrace will be printed to the
* {@code System.err} before returning.
*
* @param uri
* the {@code String} describing the resource to be looked up
* @param failSilent
* {@code true} if no errors are to be reported to the
* {@code System.err} before returning; {@code false} otherwise.
* @return a {@code Properties} object based on the specified resource
* {@code uri}.
* @see #getResource(String)
* @see #getProperties(URL, boolean)
*/
public static Properties getProperties(String uri, boolean failSilent) {
URL url = getResource(uri);
return getProperties(url, failSilent);
}
/**
* Returns a {@code Properties} object based on the specified resource
* {@code URL}. This method dispatches to
* {@code getProperties(URL url, boolean failSilent)}, with an argument of
* {@code false} for {@code failSilent}. If the specified {@code uri} is
* {@code null}, this method will print the ensuing
* {@code NullPointerException} stack tracke to the {@code System.err} and
* return {@code null}.
*
* @param url
* the {@code URL} describing the resource to be looked up
* @return a {@code Properties} object based on the specified resource
* {@code url}.
* @see #getProperties(URL, boolean)
*/
public static Properties getProperties(URL url) {
return getProperties(url, false);
}
/**
* Returns a {@code Properties} object based on the specified resource
* {@code url}. If the specified {@code uri} is {@code null}, this method
* will return {@code null}. If any errors are encountered during the
* properties-load process, this method will return {@code null}. If
* {@code failSilent} is {@code false}, then the any encoutered error
* stacktraces will be printed to the {@code System.err} before returning.
*
* @param url
* the {@code URL} describing the resource to be looked up
* @param failSilent
* {@code true} if no errors are to be reported to the
* {@code System.err} before returning; {@code false} otherwise.
* @return a {@code Properties} object based on the specified resource
* {@code url}.
*/
public static Properties getProperties(URL url, boolean failSilent) {
if (failSilent && url == null)
return null;
InputStream in = null;
try {
in = url.openStream();
Properties p = new Properties();
p.load(in);
return p;
} catch (Exception e) {
if (!failSilent)
log.warn(e.getMessage(), e);
return null;
} finally {
close(in);
}
}
/**
* Calls {@code close()} on the specified {@code InputStream}. Any
* {@code Exceptions} encountered will be printed to the {@code System.err}.
* If {@code in} is {@code null}, then no {@code Exception} is thrown and
* no action is taken.
*
* @param in
* the {@code InputStream} to close
* @see InputStream#close()
*/
public static void close(InputStream in) {
try {
if (in != null)
in.close();
} catch (Exception e) {
log.warn(e.getMessage(), e);
}
}
/**
* Calls {@code close()} on the specified {@code OutputStream}. Any
* {@code Exceptions} encountered will be printed to the {@code System.err}.
* If {@code out} is {@code null}, then no {@code Exception} is thrown and
* no action is taken.
*
* @param out
* the {@code OutputStream} to close
* @see OutputStream#close()
*/
public static void close(OutputStream out) {
try {
if (out != null)
out.close();
} catch (Exception e) {
log.warn(e.getMessage(), e);
}
}
/**
* Calls {@code close()} on the specified {@code Socket}. Any
* {@code Exceptions} encountered will be printed to the {@code System.err}.
* If {@code socket} is {@code null}, then no {@code Exception} is thrown
* and no action is taken.
*
* @param socket
* the {@code Socket} to close
* @see Socket#close()
*/
public static void close(Socket socket) {
try {
if (socket != null)
socket.close();
} catch (Exception e) {
log.warn(e.getMessage(), e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -