This commit is contained in:
133
java/src/com/eaio/nativecall/Holder.java
Normal file
133
java/src/com/eaio/nativecall/Holder.java
Normal file
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* Holder.java
|
||||
*
|
||||
* Created on 14.09.2004.
|
||||
*
|
||||
* eaio: NativeCall - calling operating system methods from Java
|
||||
* Copyright (c) 2004-2006 Johann Burkard (<mailto:jb@eaio.com>)
|
||||
* <http://eaio.com>
|
||||
*
|
||||
* 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 com.eaio.nativecall;
|
||||
|
||||
/**
|
||||
* Holder is a class that encapsulates another Object. Use this class for output
|
||||
* parameters.
|
||||
*
|
||||
* @author <a href="mailto:jb@eaio.com">Johann Burkard</a>
|
||||
* @version $Id: Holder.java,v 1.3 2006/04/19 20:54:58 grnull Exp $
|
||||
*/
|
||||
public class Holder {
|
||||
|
||||
/**
|
||||
* The encapsulated object.
|
||||
* <p>
|
||||
* Accessed by native code. DO NOT RENAME THIS FIELD.
|
||||
*/
|
||||
private Object o;
|
||||
|
||||
/**
|
||||
* Constructor for Holder.
|
||||
*
|
||||
* @param o the Object to encapsulate, may be <code>null</code>, but cannot
|
||||
* be of type Holder
|
||||
* @throws ClassCastException if o is of type Holder
|
||||
*/
|
||||
public Holder(Object o) {
|
||||
if (o instanceof Holder) {
|
||||
throw new ClassCastException();
|
||||
}
|
||||
else if (o == null) {
|
||||
o = new Integer(0);
|
||||
}
|
||||
this.o = o;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the referenced Object.
|
||||
*
|
||||
* @return an Object
|
||||
*/
|
||||
public final Object get() {
|
||||
return o;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the hashCode of the encapsulated Object or
|
||||
* {@link java.lang.Object#hashCode()} if the Object is <code>null</code>.
|
||||
*
|
||||
* @return the hashCode
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
public int hashCode() {
|
||||
return getClass().getName().hashCode() ^ (o == null ? 0 : o.hashCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if this Object is equal to another Object.
|
||||
*
|
||||
* @param obj the other Object, may be <code>null</code>
|
||||
* @return if both Objects are equal
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof Holder)) {
|
||||
return false;
|
||||
}
|
||||
Holder h = (Holder) obj;
|
||||
return o == null ? h.o == null : o.equals(h.o);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a String representation of this Object.
|
||||
*
|
||||
* @return a String, never <code>null</code>
|
||||
* @see java.lang.Object#toString()
|
||||
* @see #toStringBuffer(StringBuffer)
|
||||
*/
|
||||
public final String toString() {
|
||||
return toStringBuffer(null).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a String representation of this Object to the given
|
||||
* {@link StringBuffer} or creates a new one if none is given.
|
||||
*
|
||||
* @param in the StringBuffer to append to, may be <code>null</code>
|
||||
* @return a StringBuffer, never <code>null</code>
|
||||
*/
|
||||
public StringBuffer toStringBuffer(StringBuffer in) {
|
||||
if (in == null) {
|
||||
in = new StringBuffer(32);
|
||||
}
|
||||
else {
|
||||
in.ensureCapacity(in.length() + 32);
|
||||
}
|
||||
in.append("{ Holder: o = ");
|
||||
in.append(o);
|
||||
in.append(" }");
|
||||
return in;
|
||||
}
|
||||
|
||||
}
|
||||
143
java/src/com/eaio/nativecall/IntCall.java
Normal file
143
java/src/com/eaio/nativecall/IntCall.java
Normal file
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* IntCall.java
|
||||
*
|
||||
* Created on 07.09.2004.
|
||||
*
|
||||
* eaio: NativeCall - calling operating system methods from Java
|
||||
* Copyright (c) 2004-2006 Johann Burkard (<mailto:jb@eaio.com>)
|
||||
* <http://eaio.com>
|
||||
*
|
||||
* 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 com.eaio.nativecall;
|
||||
|
||||
/**
|
||||
* An IntCall instance encapsulates an operating system method that returns
|
||||
* an integer.
|
||||
*
|
||||
* @author <a href="mailto:jb@eaio.com">Johann Burkard</a>
|
||||
* @version $Id: IntCall.java,v 1.1 2006/01/05 20:02:44 grnull Exp $
|
||||
*/
|
||||
public class IntCall extends NativeCall {
|
||||
|
||||
/**
|
||||
* Constructor for IntCall.
|
||||
*
|
||||
* @see NativeCall#NativeCall(String)
|
||||
*/
|
||||
public IntCall(String function) throws SecurityException,
|
||||
IllegalArgumentException, NullPointerException {
|
||||
super(function);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for IntCall.
|
||||
*
|
||||
* @see NativeCall#NativeCall(String, String)
|
||||
*/
|
||||
public IntCall(String module, String function) throws SecurityException,
|
||||
IllegalArgumentException, NullPointerException {
|
||||
super(module, function);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>false</code> if calling {@link #executeCall()} returned
|
||||
* 0, <code>true</code> otherwise.
|
||||
* <p>
|
||||
* <em>Updates the error code field. See {@link #getLastError()}.</em>
|
||||
*
|
||||
* @return <code>true</code> or <code>false</code>
|
||||
*/
|
||||
public boolean executeBooleanCall() {
|
||||
return executeCall() == 0 ? false : true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>false</code> if calling {@link #executeCall(Object)}
|
||||
* returned 0, <code>true</code> otherwise.
|
||||
* <p>
|
||||
* <em>Updates the error code field. See {@link #getLastError()}.</em>
|
||||
*
|
||||
* @param param the parameter, may be <code>null</code>
|
||||
* @return <code>true</code> or <code>false</code>
|
||||
* @see #executeBooleanCall(Object[])
|
||||
*/
|
||||
public boolean executeBooleanCall(Object param) {
|
||||
return executeCall(param) == 0 ? false : true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>false</code> if calling
|
||||
* {@link #executeCall(Object[])} returned 0, <code>true</code> otherwise.
|
||||
* <p>
|
||||
* <em>Updates the error code field. See {@link #getLastError()}.</em>
|
||||
* <p>
|
||||
* During this operation, the contents of the array might be changed.
|
||||
*
|
||||
* @param params the parameter array, may be <code>null</code>
|
||||
* @return <code>true</code> or <code>false</code>
|
||||
*/
|
||||
public boolean executeBooleanCall(Object[] params) {
|
||||
return executeCall(params) == 0 ? false : true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls the function, returning its output.
|
||||
* <p>
|
||||
* <em>Updates the error code field. See {@link #getLastError()}.</em>
|
||||
*
|
||||
* @return an <code>int</code>
|
||||
*/
|
||||
public native int executeCall();
|
||||
|
||||
/**
|
||||
* Calls the function using the given parameter.
|
||||
* <p>
|
||||
* <em>Updates the error code field. See {@link #getLastError()}.</em>
|
||||
*
|
||||
* @param param the parameter, may be <code>null</code>
|
||||
* @return an <code>int</code>
|
||||
* @see #executeCall(Object[])
|
||||
*/
|
||||
public int executeCall(Object param) {
|
||||
return executeCall(new Object[] { param });
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls the function using the given parameters.
|
||||
* <p>
|
||||
* <em>Updates the error code field. See {@link #getLastError()}.</em>
|
||||
* <p>
|
||||
* During this operation, the contents of the array might be changed.
|
||||
*
|
||||
* @param params the parameter array, may be <code>null</code>
|
||||
* @return an <code>int</code>
|
||||
*/
|
||||
public int executeCall(Object[] params) {
|
||||
if (params == null || params.length == 0) {
|
||||
return executeCall();
|
||||
}
|
||||
check(params);
|
||||
return executeCall0(params);
|
||||
}
|
||||
|
||||
private native int executeCall0(Object[] params);
|
||||
|
||||
}
|
||||
355
java/src/com/eaio/nativecall/NativeCall.java
Normal file
355
java/src/com/eaio/nativecall/NativeCall.java
Normal file
@@ -0,0 +1,355 @@
|
||||
/*
|
||||
* NativeCall.java
|
||||
*
|
||||
* Created on 07.09.2004.
|
||||
*
|
||||
* eaio: NativeCall - calling operating system methods from Java
|
||||
* Copyright (c) 2004-2006 Johann Burkard (<mailto:jb@eaio.com>)
|
||||
* <http://eaio.com>
|
||||
*
|
||||
* 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 com.eaio.nativecall;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import sun.misc.ServiceConfigurationError;
|
||||
|
||||
import com.eaio.util.lang.NativeLoader;
|
||||
|
||||
/**
|
||||
* NativeCall loads the native library and prepares the matching
|
||||
* {@link com.eaio.nativecall.Verifier}.
|
||||
* <p>
|
||||
* Before being able to use NativeCall, the {@link #init()} method must have
|
||||
* been called:
|
||||
* <pre>
|
||||
* try {
|
||||
* NativeCall.init();
|
||||
* }
|
||||
* catch (IOException ex) { ... }
|
||||
* catch (SecurityException ex) { ... }
|
||||
* catch (UnsatisfiedLinkError er) { ... }
|
||||
* catch (sun.misc.ServiceConfigurationError) { ... }
|
||||
* catch (UnsupportedOperationException) { ... }
|
||||
* </pre>
|
||||
* After usage, each NativeCall object must be destroyed to release
|
||||
* resources. This is done by calling the {@link #destroy()} method. Failure
|
||||
* to call this method might result in memory leaks.
|
||||
*
|
||||
* @see #destroy()
|
||||
* @see #init()
|
||||
* @author <a href="mailto:jb@eaio.com">Johann Burkard</a>
|
||||
* @version $Id: NativeCall.java,v 1.3 2006/04/19 20:54:58 grnull Exp $
|
||||
*/
|
||||
public abstract class NativeCall {
|
||||
|
||||
/**
|
||||
* The error code of the last call.
|
||||
* <p>
|
||||
* Accessed by native code. DO NOT RENAME THIS FIELD.
|
||||
*/
|
||||
private int lastErrorCode;
|
||||
|
||||
/**
|
||||
* The internal handle to the function and the module.
|
||||
* <p>
|
||||
* These are set in native code, so ignore any warnings.
|
||||
* <p>
|
||||
* Accessed by native code. DO NOT RENAME THIS FIELD.
|
||||
*/
|
||||
private int functionHandle, moduleHandle;
|
||||
|
||||
/**
|
||||
* The name of the function to call.
|
||||
* <p>
|
||||
* Accessed by native code. DO NOT RENAME THIS FIELD.
|
||||
*/
|
||||
private String function;
|
||||
|
||||
/**
|
||||
* The name of the module to call.
|
||||
* <p>
|
||||
* Accessed by native code. DO NOT RENAME THIS FIELD.
|
||||
*/
|
||||
private String module;
|
||||
|
||||
/**
|
||||
* Initialize JNI field and method IDs
|
||||
*/
|
||||
private static native void initIDs();
|
||||
|
||||
/**
|
||||
* Whether the class has been initialized properly.
|
||||
*/
|
||||
private static boolean initialized = false;
|
||||
|
||||
/**
|
||||
* Before NativeCall may be used, this method must be called.
|
||||
* It loads the native library, prepares JNI field and method IDs and loads
|
||||
* the matching {@link Verifier}.
|
||||
* <p>
|
||||
* Multiple calls are ignored.
|
||||
*
|
||||
* @throws IOException if an IOException occured during unpacking of
|
||||
* the native library
|
||||
* @throws SecurityException if accessing system properties was forbidden
|
||||
* by the {@link SecurityManager}
|
||||
* @throws UnsatisfiedLinkError if the <code>NativeCall.dll</code> could
|
||||
* not be found
|
||||
* @throws sun.misc.ServiceConfigurationError
|
||||
* @throws UnsupportedOperationException if no matching
|
||||
* {@link Verifier} could be found
|
||||
*/
|
||||
public static synchronized void init()
|
||||
throws
|
||||
IOException,
|
||||
SecurityException,
|
||||
UnsatisfiedLinkError,
|
||||
ServiceConfigurationError,
|
||||
UnsupportedOperationException {
|
||||
if (!initialized) {
|
||||
Verifiers.init();
|
||||
if (Verifiers.getInstance() == null) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
new NativeLoader("NativeCall").load();
|
||||
initIDs();
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for NativeCall.
|
||||
*
|
||||
* @param function the name of the function to use, may not be
|
||||
* <code>null</code>
|
||||
* @throws IllegalArgumentException if the function could not be found
|
||||
* @throws NullPointerException if function is <code>null</code>
|
||||
* @see Verifier#getDefaultModule()
|
||||
* @see NativeCall#NativeCall(String, String)
|
||||
*/
|
||||
public NativeCall(String function)
|
||||
throws IllegalArgumentException, NullPointerException {
|
||||
this(Verifiers.getInstance().getDefaultModule(), function);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for NativeCall.
|
||||
*
|
||||
* @param module the name of the module the function is stored in, may be
|
||||
* <code>null</code>
|
||||
* @param function the name of the function to use, may not be
|
||||
* <code>null</code>
|
||||
* @throws IllegalArgumentException if the function could not be found
|
||||
* @throws NullPointerException if function is <code>null</code>
|
||||
*/
|
||||
public NativeCall(String module, String function)
|
||||
throws IllegalArgumentException, NullPointerException {
|
||||
Verifier v = Verifiers.getInstance();
|
||||
this.function = v.verifyFunctionName(function);
|
||||
this.module = v.verifyModuleName(module);
|
||||
if (!initHandles()) {
|
||||
if (lastErrorCode != 0) {
|
||||
throw new IllegalArgumentException(getLastError());
|
||||
}
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to acquire handles to the functions. Returns if these could be
|
||||
* acquired.
|
||||
*
|
||||
* @return if the handles could be acquired
|
||||
*/
|
||||
private native boolean initHandles();
|
||||
|
||||
/**
|
||||
* Returns the error code that was returned during the last method call or
|
||||
* 0 if the last method call did not produce an error.
|
||||
*
|
||||
* @see #getLastError()
|
||||
* @return the last error code or 0
|
||||
*/
|
||||
public final int getLastErrorCode() {
|
||||
return lastErrorCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a formatted String containing the last error code or
|
||||
* <code>null</code> if the last call did not produce an error.
|
||||
*
|
||||
* @see #getLastErrorCode()
|
||||
* @return a String or <code>null</code> if the last error code is 0
|
||||
*/
|
||||
public final native String getLastError();
|
||||
|
||||
/**
|
||||
* Releases acquired module handles. This method must be called if the
|
||||
* instance is not used anymore. After this method is called, methods of this
|
||||
* NativeCall Object cannot be called anymore.
|
||||
* <p>
|
||||
* <strong>Failure to call this method might result in memory leaks.</strong>
|
||||
* <p>
|
||||
* <em>Updates the error code field. See {@link #getLastError()}.</em>
|
||||
*/
|
||||
public native synchronized void destroy();
|
||||
|
||||
/**
|
||||
* Checks the supplied Object array for illegal/unsupported types.
|
||||
* <p>
|
||||
* <strong>During the verification, the contents of the array might be
|
||||
* changed.</strong>
|
||||
*
|
||||
* @param params the Object array, may be <code>null</code>
|
||||
* @throws ClassCastException if the type of one argument is not supported
|
||||
*/
|
||||
protected void check(Object[] params) throws ClassCastException {
|
||||
if (params == null) {
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < params.length; ++i) {
|
||||
checkParam(params[i]);
|
||||
if (params[i] instanceof String) {
|
||||
params[i] =
|
||||
Verifiers.getInstance().handleString(
|
||||
(String) params[i],
|
||||
module,
|
||||
function);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks one Object for illegal/unsupported types.
|
||||
*
|
||||
* @param o the Object, may be <code>null</code>
|
||||
* @throws ClassCastException if the type of one argument is not supported
|
||||
*/
|
||||
protected void checkParam(Object o) throws ClassCastException {
|
||||
if (o == null
|
||||
|| o instanceof Boolean
|
||||
|| o instanceof Integer
|
||||
|| o instanceof byte[]
|
||||
|| o instanceof char[]
|
||||
|| o instanceof String) {
|
||||
return;
|
||||
}
|
||||
if (o instanceof Holder) {
|
||||
checkParam(((Holder) o).get());
|
||||
return;
|
||||
}
|
||||
throw new ClassCastException(o.getClass().getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if this Object is equal to another Object. The other Object must
|
||||
* be an instance of the same type as this Object. Also, both the module
|
||||
* and the function field must be equal.
|
||||
*
|
||||
* @param obj the other Object
|
||||
* @return if this and the other Object are equal
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof NativeCall)) {
|
||||
return false;
|
||||
}
|
||||
if (!getClass().getName().equals(obj.getClass().getName())) {
|
||||
return false;
|
||||
}
|
||||
NativeCall c = (NativeCall) obj;
|
||||
return module.equals(c.module) && function.equals(c.function);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the hashCode of this Object. The hashCode is computed by XOR'ing
|
||||
* the hash codes of the function and the module names.
|
||||
*
|
||||
* @return the hashCode
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
public int hashCode() {
|
||||
int out = function.hashCode();
|
||||
out ^= module.hashCode();
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls {@link #destroy()}.
|
||||
*
|
||||
* @see java.lang.Object#finalize()
|
||||
*/
|
||||
protected void finalize() throws Throwable {
|
||||
try {
|
||||
destroy();
|
||||
}
|
||||
finally {
|
||||
super.finalize();
|
||||
// in case NativeCall is a subclass of a class other than Object
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a String representation of this Object.
|
||||
*
|
||||
* @return a String, never <code>null</code>
|
||||
* @see java.lang.Object#toString()
|
||||
* @see #toStringBuffer(StringBuffer)
|
||||
*/
|
||||
public final String toString() {
|
||||
return toStringBuffer(null).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a String representation of this Object to the given
|
||||
* {@link StringBuffer} or creates a new one if none is given.
|
||||
*
|
||||
* @param in the StringBuffer to append to, may be <code>null</code>
|
||||
* @return a StringBuffer, never <code>null</code>
|
||||
*/
|
||||
public StringBuffer toStringBuffer(StringBuffer in) {
|
||||
if (in == null) {
|
||||
in = new StringBuffer(64);
|
||||
}
|
||||
else {
|
||||
in.ensureCapacity(in.length() + 64);
|
||||
}
|
||||
in.append("{ ");
|
||||
int idx = getClass().getName().lastIndexOf(".");
|
||||
if (idx > -1) {
|
||||
in.append(getClass().getName().substring(++idx));
|
||||
}
|
||||
else {
|
||||
in.append(getClass().getName());
|
||||
}
|
||||
in.append(": module = ");
|
||||
in.append(module);
|
||||
in.append(", function = ");
|
||||
in.append(function);
|
||||
in.append(" }");
|
||||
return in;
|
||||
}
|
||||
|
||||
}
|
||||
101
java/src/com/eaio/nativecall/Verifier.java
Normal file
101
java/src/com/eaio/nativecall/Verifier.java
Normal file
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Verifier.java
|
||||
*
|
||||
* Created on 07.09.2004.
|
||||
*
|
||||
* eaio: NativeCall - calling operating system methods from Java
|
||||
* Copyright (c) 2004-2006 Johann Burkard (<mailto:jb@eaio.com>)
|
||||
* <http://eaio.com>
|
||||
*
|
||||
* 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 com.eaio.nativecall;
|
||||
|
||||
/**
|
||||
* A Verifier implements method and module name checking for one given
|
||||
* operating system. Classes implementing Verifier must be public and have
|
||||
* a public no-argument constructor.
|
||||
*
|
||||
* @author <a href="mailto:jb@eaio.com">Johann Burkard</a>
|
||||
* @version $Id: Verifier.java,v 1.1 2006/01/05 20:02:44 grnull Exp $
|
||||
*/
|
||||
public interface Verifier {
|
||||
|
||||
/**
|
||||
* If there is a default module that system functions are stored in, the
|
||||
* module's name may be returned here.
|
||||
*
|
||||
* @return the name of a default module or <code>null</code>
|
||||
* @see NativeCall#NativeCall(String)
|
||||
*/
|
||||
String getDefaultModule();
|
||||
|
||||
/**
|
||||
* Returns if this Verifier supports the given operating system.
|
||||
*
|
||||
* @return if this operating system is supported
|
||||
* @throws SecurityException because {@link java.lang.System} properties
|
||||
* may be queried
|
||||
*/
|
||||
boolean supports() throws SecurityException;
|
||||
|
||||
/**
|
||||
* Verifies that the given module name is correct.
|
||||
*
|
||||
* @param module the module name, may be <code>null</code>
|
||||
* @return a module name, possibly modified, never <code>null</code>
|
||||
* @throws NullPointerException if the module name is <code>null</code>
|
||||
* and there is no default module defined
|
||||
* @throws IllegalArgumentException if the module is illegal in the
|
||||
* operating system
|
||||
* @see #getDefaultModule()
|
||||
*/
|
||||
String verifyModuleName(String module)
|
||||
throws NullPointerException, IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Verifies that the given function name is correct.
|
||||
*
|
||||
* @param function the function name, may be <code>null</code>
|
||||
* @return a function name, possibly modified, never <code>null</code>
|
||||
* @throws NullPointerException if the function name is <code>null</code>
|
||||
* @throws IllegalArgumentException if the function is illegal in the
|
||||
* operating system
|
||||
*/
|
||||
String verifyFunctionName(String function)
|
||||
throws NullPointerException, IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Converts the given String to one of the following data types, based on the
|
||||
* module and the function name:
|
||||
* <br>
|
||||
* <ul>
|
||||
* <li>a <code>byte</code> array</li>
|
||||
* <li>a <code>char</code> array</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param val the String, never <code>null</code>
|
||||
* @param module the module name, never <code>null</code>
|
||||
* @param function the function name, never <code>null</code>
|
||||
* @return the String converted, never <code>null</code>
|
||||
*/
|
||||
Object handleString(String val, String module, String function);
|
||||
|
||||
}
|
||||
83
java/src/com/eaio/nativecall/Verifiers.java
Normal file
83
java/src/com/eaio/nativecall/Verifiers.java
Normal file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Verifiers.java
|
||||
*
|
||||
* Created on 07.09.2004.
|
||||
*
|
||||
* eaio: NativeCall - calling operating system methods from Java
|
||||
* Copyright (c) 2004-2006 Johann Burkard (<mailto:jb@eaio.com>)
|
||||
* <http://eaio.com>
|
||||
*
|
||||
* 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 com.eaio.nativecall;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import sun.misc.Service;
|
||||
import sun.misc.ServiceConfigurationError;
|
||||
|
||||
/**
|
||||
* Verifiers instantiates the matching {@link com.eaio.nativecall.Verifier} for
|
||||
* the current operating system.
|
||||
*
|
||||
* @author <a href="mailto:jb@eaio.com">Johann Burkard</a>
|
||||
* @version $Id: Verifiers.java,v 1.2 2006/01/06 10:58:33 grnull Exp $
|
||||
*/
|
||||
final class Verifiers {
|
||||
|
||||
/**
|
||||
* No instances needed.
|
||||
*/
|
||||
private Verifiers() {}
|
||||
|
||||
/**
|
||||
* The Verifier.
|
||||
*/
|
||||
private static Verifier v = null;
|
||||
|
||||
/**
|
||||
* Find the matching Verifier.
|
||||
*
|
||||
* @throws ServiceConfigurationError
|
||||
* @throws SecurityException
|
||||
*/
|
||||
static void init() throws ServiceConfigurationError, SecurityException {
|
||||
Iterator i =
|
||||
Service.providers(Verifier.class, Verifier.class.getClassLoader());
|
||||
Verifier ver = null;
|
||||
while (i.hasNext()) {
|
||||
ver = (Verifier) i.next();
|
||||
if (ver.supports()) {
|
||||
v = ver;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Verifier.
|
||||
*
|
||||
* @return a Verifier or <code>null</code>
|
||||
*/
|
||||
static Verifier getInstance() {
|
||||
return v;
|
||||
}
|
||||
|
||||
}
|
||||
99
java/src/com/eaio/nativecall/VoidCall.java
Normal file
99
java/src/com/eaio/nativecall/VoidCall.java
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* VoidCall.java
|
||||
*
|
||||
* Created on 16.09.2004
|
||||
*
|
||||
* eaio: NativeCall - calling operating system methods from Java
|
||||
* Copyright (c) 2004-2006 Johann Burkard (<mailto:jb@eaio.com>)
|
||||
* <http://eaio.com>
|
||||
*
|
||||
* 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 com.eaio.nativecall;
|
||||
|
||||
/**
|
||||
* A VoidCall instance encapsulates an operating system method that returns
|
||||
* nothing.
|
||||
*
|
||||
* @author <a href="mailto:jb@eaio.com">Johann Burkard</a>
|
||||
* @version $Id: VoidCall.java,v 1.1 2006/01/05 20:02:44 grnull Exp $
|
||||
*/
|
||||
public class VoidCall extends NativeCall {
|
||||
|
||||
/**
|
||||
* Constructor for VoidCall.
|
||||
*
|
||||
* @see NativeCall#NativeCall(String)
|
||||
*/
|
||||
public VoidCall(String function) throws SecurityException,
|
||||
IllegalArgumentException, NullPointerException {
|
||||
super(function);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for VoidCall.
|
||||
*
|
||||
* @see NativeCall#NativeCall(String, String)
|
||||
*/
|
||||
public VoidCall(String module, String function) throws SecurityException,
|
||||
IllegalArgumentException, NullPointerException {
|
||||
super(module, function);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls the function.
|
||||
* <p>
|
||||
* <em>Updates the error code field. See {@link #getLastError()}.</em>
|
||||
*/
|
||||
public native void executeCall();
|
||||
|
||||
/**
|
||||
* Calls the function using the given parameter.
|
||||
* <p>
|
||||
* <em>Updates the error code field. See {@link #getLastError()}.</em>
|
||||
*
|
||||
* @param param the parameter, may be <code>null</code>
|
||||
* @see #executeCall(Object[])
|
||||
*/
|
||||
public void executeCall(Object param) {
|
||||
executeCall(new Object[] { param });
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls the function using the given parameters.
|
||||
* <p>
|
||||
* <em>Updates the error code field. See {@link #getLastError()}.</em>
|
||||
* <p>
|
||||
* During this operation, the contents of the array might be changed.
|
||||
*
|
||||
* @param params the parameter array, may be <code>null</code>
|
||||
*/
|
||||
public void executeCall(Object[] params) {
|
||||
if (params == null || params.length == 0) {
|
||||
executeCall();
|
||||
return;
|
||||
}
|
||||
check(params);
|
||||
executeCall0(params);
|
||||
}
|
||||
|
||||
private native void executeCall0(Object[] params);
|
||||
|
||||
}
|
||||
122
java/src/com/eaio/nativecall/Win32Verifier.java
Normal file
122
java/src/com/eaio/nativecall/Win32Verifier.java
Normal file
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Win32Verifier.java
|
||||
*
|
||||
* Created on 08.09.2004.
|
||||
*
|
||||
* eaio: NativeCall - calling operating system methods from Java
|
||||
* Copyright (c) 2004-2006 Johann Burkard (<mailto:jb@eaio.com>)
|
||||
* <http://eaio.com>
|
||||
*
|
||||
* 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 com.eaio.nativecall;
|
||||
|
||||
/**
|
||||
* A {@link com.eaio.nativecall.Verifier} for the Windows environment.
|
||||
*
|
||||
* @author <a href="mailto:jb@eaio.com">Johann Burkard</a>
|
||||
* @version $Id: Win32Verifier.java,v 1.3 2006/04/19 20:54:58 grnull Exp $
|
||||
*/
|
||||
public class Win32Verifier implements Verifier {
|
||||
|
||||
/**
|
||||
* Constructor for Win32Verifier. Does nothing.
|
||||
*/
|
||||
public Win32Verifier() {}
|
||||
|
||||
/**
|
||||
* Verifies that the {@link java.lang.System} property "os.name" starts
|
||||
* with "Windows".
|
||||
*
|
||||
* @see Verifier#supports()
|
||||
*/
|
||||
public boolean supports() throws SecurityException {
|
||||
return System.getProperty("os.name").startsWith("Windows");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default module name if the module name is <code>null</code>
|
||||
* or an empty String. If the module name contains forward slashes (/), they
|
||||
* are converted to backward slashes (\).
|
||||
*
|
||||
* @see <a
|
||||
* href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/loadlibrary.asp"
|
||||
* target="_top">
|
||||
* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/loadlibrary.asp
|
||||
* </a>
|
||||
* @see com.eaio.nativecall.Verifier#verifyModuleName(java.lang.String)
|
||||
*/
|
||||
public String verifyModuleName(String module) {
|
||||
if (module == null || module.length() == 0) {
|
||||
return getDefaultModule();
|
||||
}
|
||||
if (module.indexOf('/') != -1) {
|
||||
module = module.replace('/', '\\');
|
||||
}
|
||||
return module;
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws a {@link java.lang.NullPointerException} if the function
|
||||
* name is <code>null</code> or an empty String. No further processing is
|
||||
* done.
|
||||
*
|
||||
* @see com.eaio.nativecall.Verifier#verifyFunctionName(java.lang.String)
|
||||
*/
|
||||
public String verifyFunctionName(String function) {
|
||||
if (function == null || function.length() == 0) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
return function;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns "kernel32".
|
||||
*
|
||||
* @return "kernel32"
|
||||
* @see com.eaio.nativecall.Verifier#getDefaultModule()
|
||||
*/
|
||||
public String getDefaultModule() {
|
||||
return "kernel32";
|
||||
}
|
||||
|
||||
/**
|
||||
* If the function name ends on 'W' (Windows' Unicode functions), a
|
||||
* <code>char</code> array is returned, otherwise a <code>byte</code> array
|
||||
* is returned.
|
||||
* <p>
|
||||
* The arrays are always <code>null</code>-terminated.
|
||||
*
|
||||
* @see com.eaio.nativecall.Verifier#handleString(java.lang.String,
|
||||
* java.lang.String,
|
||||
* java.lang.String)
|
||||
*/
|
||||
public Object handleString(String val, String module, String function) {
|
||||
if (function.charAt(function.length() - 1) == 'W') {
|
||||
char[] buf = new char[val.length() + 1];
|
||||
val.getChars(0, val.length(), buf, 0);
|
||||
return buf;
|
||||
}
|
||||
byte[] buf = new byte[val.length() + 1];
|
||||
val.getBytes(0, val.length(), buf, 0);
|
||||
return buf;
|
||||
}
|
||||
|
||||
}
|
||||
15
java/src/com/eaio/nativecall/package.html
Normal file
15
java/src/com/eaio/nativecall/package.html
Normal file
@@ -0,0 +1,15 @@
|
||||
<!-- $Id: package.html,v 1.1 2006/01/05 20:02:44 grnull Exp $ -->
|
||||
<html>
|
||||
<head>
|
||||
<title>Untitled Document</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>Contains the main NativeCall classes.</p>
|
||||
<p>For instant API information, see:</p>
|
||||
<ul>
|
||||
<li>{@link com.eaio.nativecall.NativeCall}</li>
|
||||
<li>{@link com.eaio.nativecall.IntCall}</li>
|
||||
<li>{@link com.eaio.nativecall.VoidCall}</li>
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user