Initial commit from SVN.
This commit is contained in:
73
Common/src/com/common/system/ISystem.java
Normal file
73
Common/src/com/common/system/ISystem.java
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (c) 1999,2005 Declarative Engineering LLC.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Declarative Engineering LLC
|
||||
* verson 1 which accompanies this distribution, and is available at
|
||||
* http://declarativeengineering.com/legal/DE_Developer_License_v1.txt
|
||||
*/
|
||||
package com.common.system;
|
||||
|
||||
public interface ISystem {
|
||||
/**
|
||||
* Creates a string by reading the character bytes from a byte array beginning at a given offset.
|
||||
* @param bytes The byte array containing the character data.
|
||||
* @param offset The offset in the array where the character data begins.
|
||||
* @param length The number of bytes of character data to read.
|
||||
* @return The string represented by the character data in the byte array.
|
||||
*/
|
||||
public String convertUnicode16StringFromBytes(byte[] bytes, int offset, int length);
|
||||
/**
|
||||
* Writes the character bytes to the byte array starting at the offset.
|
||||
* @param string The string to write to the byte array.
|
||||
* @param bytes The byte array that should contain the string bytes.
|
||||
* @param offset The offset in the array where the character bytes should begin.
|
||||
* @return Will be the number of bytes written.
|
||||
*/
|
||||
public int convertUnicode16StringToBytes(String string, byte[] bytes, int offset);
|
||||
/**
|
||||
* Creates a string by reading the character bytes from a byte array beginning at a given offset.
|
||||
* @param bytes The byte array containing the character data.
|
||||
* @param offset The offset in the array where the character data begins.
|
||||
* @param length The number of bytes of character data to read.
|
||||
* @return The string represented by the character data in the byte array.
|
||||
*/
|
||||
public String convertUtf8StringFromBytes(byte[] bytes, int offset, int length);
|
||||
/**
|
||||
* Writes the character bytes to the byte array starting at the offset.
|
||||
* @param string The string to write to the byte array.
|
||||
* @param bytes The byte array that should contain the string bytes.
|
||||
* @param offset The offset in the array where the character bytes should begin.
|
||||
* @return Will be the number of bytes written.
|
||||
*/
|
||||
public int convertUtf8StringToBytes(String string, byte[] bytes, int offset);
|
||||
/**
|
||||
* Retrieves the system id for this process.
|
||||
* <p>NOTE: This will only be called once per logical process, and the value will be cached and reused.
|
||||
* @return The system process id.
|
||||
*/
|
||||
public int getProcessId();
|
||||
/**
|
||||
* Gets the number of bytes in the UTF8 string.
|
||||
* @param string The string to get the length of.
|
||||
* @return Will be the number of UTF8 bytes in the string.
|
||||
*/
|
||||
public int getUtf8StringLength(String string);
|
||||
/**
|
||||
* Creates a new instance of the given type.
|
||||
* <p><b>NOTE: This will NOT call any of the constructors.</b>
|
||||
* @param type The class object for the type to be instantiated.
|
||||
* @return The new uninitialized object.
|
||||
*/
|
||||
public Object instantiateObject(Class type);
|
||||
/**
|
||||
* Loads the system library using the default library name and a custom path.
|
||||
* @see SystemManager.setupSystem(ISystem, String, String)
|
||||
*/
|
||||
public void loadLibrary();
|
||||
/**
|
||||
* Loads the system library using the default library name and a custom path.
|
||||
* @param path The path where the library can be found.
|
||||
* @see SystemManager.setupSystem(ISystem, String, String)
|
||||
*/
|
||||
public void loadLibrary(String path);
|
||||
}//ISystem//
|
||||
284
Common/src/com/common/system/SystemManager.java
Normal file
284
Common/src/com/common/system/SystemManager.java
Normal file
@@ -0,0 +1,284 @@
|
||||
/*
|
||||
* Copyright (c) 1999,2009 Declarative Engineering LLC.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Declarative Engineering LLC
|
||||
* verson 1 which accompanies this distribution, and is available at
|
||||
* http://declarativeengineering.com/legal/DE_Developer_License_v1.txt
|
||||
*/
|
||||
package com.common.system;
|
||||
|
||||
import com.common.debug.Debug;
|
||||
|
||||
/**
|
||||
* The SystemManager allows for system (or OS) level code to be reused.
|
||||
*/
|
||||
public class SystemManager {
|
||||
private static final String UTF16 = "UTF-16BE";
|
||||
private static final String UTF8 = "UTF-8";
|
||||
|
||||
private static ISystem _system = null;
|
||||
private static Integer processId = null;
|
||||
private static boolean useNativeLibrary = false;
|
||||
/**
|
||||
* SystemManager constructor.
|
||||
*/
|
||||
private SystemManager() {
|
||||
super();
|
||||
}//SystemManager()//
|
||||
/**
|
||||
* Whether the system should utilize the native library to speed up some methods and to make others functional.
|
||||
* @param _useNativeLibrary
|
||||
*/
|
||||
public static void useNativeLibrary(boolean _useNativeLibrary) {
|
||||
useNativeLibrary = _useNativeLibrary;
|
||||
}//useNativeLibrary()//
|
||||
/**
|
||||
* @see ISystem.convertUnicode16StringFromBytes
|
||||
*/
|
||||
public static String convertUnicode16StringFromBytes(byte[] bytes, int offset, int length) {
|
||||
String result = null;
|
||||
|
||||
if(!useNativeLibrary) {
|
||||
try {
|
||||
result = new String(bytes, offset, length, UTF16);
|
||||
}//try//
|
||||
catch(Throwable e) {
|
||||
Debug.log(e);
|
||||
}//catch//
|
||||
}//if//
|
||||
else {
|
||||
result = getSystem().convertUnicode16StringFromBytes(bytes, offset, length);
|
||||
}//else//
|
||||
|
||||
return result;
|
||||
}//convertUnicode16StringFromBytes()//
|
||||
/**
|
||||
* @see ISystem.convertUnicode16StringToBytes
|
||||
*/
|
||||
public static int convertUnicode16StringToBytes(String string, byte[] bytes, int offset) {
|
||||
int result = 0;
|
||||
|
||||
if(!useNativeLibrary) {
|
||||
try {
|
||||
byte[] b = string.getBytes(UTF16);
|
||||
|
||||
result = b.length;
|
||||
|
||||
if(result > bytes.length - offset) {
|
||||
result = bytes.length - offset;
|
||||
}//if//
|
||||
|
||||
System.arraycopy(b, 0, bytes, offset, result);
|
||||
}//try//
|
||||
catch(Throwable e) {
|
||||
Debug.log(e);
|
||||
}//catch//
|
||||
}//if//
|
||||
else {
|
||||
result = getSystem().convertUnicode16StringToBytes(string, bytes, offset);
|
||||
}//else//
|
||||
|
||||
return result;
|
||||
}//convertUnicode16StringToBytes()//
|
||||
/**
|
||||
* Simplifies the conversion to UTF16 bytes if array reuse is not required.
|
||||
*/
|
||||
public static byte[] convertUnicode16StringToBytes(String string) {
|
||||
byte[] result = null;
|
||||
|
||||
if(!useNativeLibrary) {
|
||||
try {
|
||||
result = string.getBytes(UTF16);
|
||||
}//try//
|
||||
catch(Throwable e) {
|
||||
Debug.log(e);
|
||||
}//catch//
|
||||
}//if//
|
||||
else {
|
||||
result = new byte[getUtf8StringLength(string)];
|
||||
getSystem().convertUnicode16StringToBytes(string, result, 0);
|
||||
}//else//
|
||||
|
||||
return result;
|
||||
}//convertUtf16StringToBytes()//
|
||||
/**
|
||||
* @see ISystem.convertUtf8StringFromBytes
|
||||
*/
|
||||
public static String convertUtf8StringFromBytes(byte[] bytes, int offset, int length) {
|
||||
String result = null;
|
||||
|
||||
if(!useNativeLibrary) {
|
||||
try {
|
||||
result = new String(bytes, offset, length, UTF8);
|
||||
}//try//
|
||||
catch(Throwable e) {
|
||||
Debug.log(e);
|
||||
}//catch//
|
||||
}//if//
|
||||
else {
|
||||
result = getSystem().convertUtf8StringFromBytes(bytes, offset, length);
|
||||
}//else//
|
||||
|
||||
return result;
|
||||
}//convertUtf8StringFromBytes()//
|
||||
/**
|
||||
* @see ISystem.convertUtf8StringToBytes
|
||||
*/
|
||||
public static int convertUtf8StringToBytes(String string, byte[] bytes, int offset) {
|
||||
int result = 0;
|
||||
|
||||
if(!useNativeLibrary) {
|
||||
try {
|
||||
byte[] b = string.getBytes(UTF8);
|
||||
|
||||
result = b.length;
|
||||
|
||||
if(result > bytes.length - offset) {
|
||||
result = bytes.length - offset;
|
||||
}//if//
|
||||
|
||||
System.arraycopy(b, 0, bytes, offset, result);
|
||||
}//try//
|
||||
catch(Throwable e) {
|
||||
Debug.log(e);
|
||||
}//catch//
|
||||
}//if//
|
||||
else {
|
||||
result = getSystem().convertUtf8StringToBytes(string, bytes, offset);
|
||||
}//else//
|
||||
|
||||
return result;
|
||||
}//convertUtf8StringToBytes()//
|
||||
/**
|
||||
* Simplifies the conversion to UTF8 bytes if array reuse is not required.
|
||||
*/
|
||||
public static byte[] convertUtf8StringToBytes(String string) {
|
||||
byte[] result = null;
|
||||
|
||||
if(!useNativeLibrary) {
|
||||
try {
|
||||
result = string != null ? string.getBytes(UTF8) : null;
|
||||
}//try//
|
||||
catch(Throwable e) {
|
||||
Debug.log(e);
|
||||
}//catch//
|
||||
}//if//
|
||||
else {
|
||||
result = new byte[getUtf8StringLength(string)];
|
||||
getSystem().convertUtf8StringToBytes(string, result, 0);
|
||||
}//else//
|
||||
|
||||
return result;
|
||||
}//convertUtf8StringToBytes()//
|
||||
/**
|
||||
* Gets a default system object for the reported OS.
|
||||
* @return The default system object for the currently reported OS.
|
||||
*/
|
||||
public static ISystem getDefaultSystem() {
|
||||
ISystem system = null;
|
||||
String osName = System.getProperty("os.name");
|
||||
|
||||
if(osName.indexOf("Windows") != -1) {
|
||||
system = new WindowsSystem();
|
||||
}//if//
|
||||
else {
|
||||
throw new RuntimeException("Unable to determine a default System object for the current OS: " + osName);
|
||||
}//else//
|
||||
|
||||
return system;
|
||||
}//getDefaultSystem()//
|
||||
/**
|
||||
* Retrieves the system id for this process.
|
||||
* @return The system process id.
|
||||
*/
|
||||
public static int getProcessId() {
|
||||
if(processId == null) {
|
||||
processId = new Integer(getSystem().getProcessId());
|
||||
}//if//
|
||||
|
||||
return processId.intValue();
|
||||
}//getProcessId()//
|
||||
/**
|
||||
* Gets the system object used to handle os level calls that are missing in the JDK.
|
||||
* @return The system object that handles native calls.
|
||||
*/
|
||||
protected synchronized static ISystem getSystem() {
|
||||
if(_system == null) {
|
||||
setupSystem(null, null, null);
|
||||
}//if//
|
||||
|
||||
return _system;
|
||||
}//getSystem()//
|
||||
/**
|
||||
* @see ISystem.getUtf8StringLength
|
||||
*/
|
||||
public static int getUtf8StringLength(String string) {
|
||||
int result = 0;
|
||||
|
||||
if(!useNativeLibrary) {
|
||||
try {
|
||||
result = string.getBytes(UTF8).length;
|
||||
}//try//
|
||||
catch(Throwable e) {
|
||||
Debug.log(e);
|
||||
}//catch//
|
||||
}//if//
|
||||
else {
|
||||
result = getSystem().getUtf8StringLength(string);
|
||||
}//else//
|
||||
|
||||
return result;
|
||||
}//getUtf8StringLength()//
|
||||
/**
|
||||
* Creates a new instance of the given type.
|
||||
* <p><b>NOTE: This will NOT call any of the constructors.</b>
|
||||
* @param type The class object for the type to be instantiated.
|
||||
* @return The new uninitialized object.
|
||||
*/
|
||||
public static Object instantiateObject(Class type) {
|
||||
return getSystem().instantiateObject(type);
|
||||
}//instantiateObject()//
|
||||
/**
|
||||
* Sets up the SystemManager which handles common system level functionality.
|
||||
* @param system The system object that handles native calls.
|
||||
* @param libraryPath An <b>optional</b> path to the library (the system path will be used if this is <code>null</code>).
|
||||
* @param libraryName The <b>optional</b> name of the dynamically loaded library that should be used to handle native calls by the ISystem object. If this value is not specified, the ISystem object will be asked to load its' library.
|
||||
*/
|
||||
public static void setupSystem(ISystem system, String libraryPath, String libraryName) {
|
||||
if(system == null) {
|
||||
system = getDefaultSystem();
|
||||
}//if//
|
||||
|
||||
_system = system;
|
||||
|
||||
//Load the necessary libraries.//
|
||||
if((libraryName != null) || (libraryPath != null)) {
|
||||
if(libraryPath == null) {
|
||||
//Load the library off of the system path.//
|
||||
System.loadLibrary(libraryName);
|
||||
}//if//
|
||||
else {
|
||||
String separator = System.getProperty("file.separator");
|
||||
String fullPath = libraryPath;
|
||||
|
||||
//Make sure there is a proper separator between the path and file name.//
|
||||
if(!fullPath.endsWith(separator)) {
|
||||
fullPath += separator;
|
||||
}//if//
|
||||
|
||||
if(libraryName != null) {
|
||||
//Finish creating the full path to the libarary.//
|
||||
fullPath += libraryName;
|
||||
//Request that the library be loaded from the exact location.//
|
||||
System.load(fullPath);
|
||||
}//if//
|
||||
else {
|
||||
_system.loadLibrary(fullPath);
|
||||
}//else//
|
||||
}//else//
|
||||
}//if//
|
||||
else {
|
||||
_system.loadLibrary();
|
||||
}//else//
|
||||
}//setupSystem()//
|
||||
}//SystemManager//
|
||||
80
Common/src/com/common/system/WindowsSystem.java
Normal file
80
Common/src/com/common/system/WindowsSystem.java
Normal file
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright (c) 1999,2005 Declarative Engineering LLC.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Declarative Engineering LLC
|
||||
* verson 1 which accompanies this distribution, and is available at
|
||||
* http://declarativeengineering.com/legal/DE_Developer_License_v1.txt
|
||||
*/
|
||||
package com.common.system;
|
||||
|
||||
public class WindowsSystem implements ISystem {
|
||||
/**
|
||||
* WindowsSystem constructor.
|
||||
*/
|
||||
public WindowsSystem() {
|
||||
super();
|
||||
}//WindowsSystem()//
|
||||
/**
|
||||
* @see ISystem.convertUnicode16StringFromBytes
|
||||
*/
|
||||
public native String convertUnicode16StringFromBytes(byte[] bytes, int offset, int length);
|
||||
/**
|
||||
* @see ISystem.convertUnicode16StringToBytes
|
||||
*/
|
||||
public native int convertUnicode16StringToBytes(String string, byte[] bytes, int offset);
|
||||
/**
|
||||
* @see ISystem.convertUtf8StringFromBytes
|
||||
*/
|
||||
public native String convertUtf8StringFromBytes(byte[] bytes, int offset, int length);
|
||||
/**
|
||||
* @see ISystem.convertUtf8StringToBytes
|
||||
*/
|
||||
public native int convertUtf8StringToBytes(String string, byte[] bytes, int offset);
|
||||
/**
|
||||
* Retrieves the system id for this process.
|
||||
* <p>NOTE: This id should only be retrieved once for each logical process.</p>
|
||||
* @return The system process id.
|
||||
*/
|
||||
public native int getProcessId();
|
||||
/**
|
||||
* @see ISystem.getUtf8StringLength
|
||||
*/
|
||||
public native int getUtf8StringLength(String string);
|
||||
/**
|
||||
* Creates a new instance of the given type.
|
||||
* <p><b>NOTE: This will NOT call any of the constructors.</b>
|
||||
* @param type The class object for the type to be instantiated.
|
||||
* @return The new uninitialized object.
|
||||
*/
|
||||
public native Object instantiateObject(Class type);
|
||||
/**
|
||||
* Loads the system library using the default library name and a custom path.
|
||||
* @see SystemManager.setupSystem(ISystem, String, String)
|
||||
*/
|
||||
public void loadLibrary() {
|
||||
//This is the default loading code for the library that handles native calls.//
|
||||
System.loadLibrary("WindowsCommon");
|
||||
}//loadLibrary()//
|
||||
/**
|
||||
* Loads the system library using the default library name and a custom path.
|
||||
* @param path The path where the library can be found.
|
||||
* @see SystemManager.setupSystem(ISystem, String, String)
|
||||
*/
|
||||
public void loadLibrary(String path) {
|
||||
//Assume the libarary is not yet loaded. If it is already loaded then an unsatified link error occurs (there is no other way to check), then check whether it is loaded by calling it.//
|
||||
try {
|
||||
//This is the default loading code for the library that handles native calls.//
|
||||
System.load(new java.io.File(path + "WindowsCommon.dll").getAbsolutePath());
|
||||
}//try//
|
||||
catch(UnsatisfiedLinkError ignored) {
|
||||
try {
|
||||
//Check to see if the library is already loaded.//
|
||||
getProcessId();
|
||||
}//try//
|
||||
catch(Throwable e) {
|
||||
//Try again to load it... this time let the exception pass through.//
|
||||
System.load(new java.io.File(path + "WindowsCommon.dll").getAbsolutePath());
|
||||
}//catch//
|
||||
}//catch//
|
||||
}//loadLibrary()//
|
||||
}//WindowsSystem//
|
||||
Reference in New Issue
Block a user