#ifndef WCHAR_MIN // [
# define WCHAR_MIN 0
#endif // WCHAR_MIN ]
#ifndef WCHAR_MAX // [
# define WCHAR_MAX _UI16_MAX
#endif // WCHAR_MAX ]
#define WINT_MIN 0
#define WINT_MAX _UI16_MAX
#endif // __STDC_LIMIT_MACROS ]
// 7.18.4 Limits of other integer types
#if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) // [ See footnote 224 at page 260
// 7.18.4.1 Macros for minimum-width integer constants
#define INT8_C(val) val##i8
#define INT16_C(val) val##i16
#define INT32_C(val) val##i32
#define INT64_C(val) val##i64
#define UINT8_C(val) val##ui8
#define UINT16_C(val) val##ui16
#define UINT32_C(val) val##ui32
#define UINT64_C(val) val##ui64
// 7.18.4.2 Macros for greatest-width integer constants
#define INTMAX_C INT64_C
#define UINTMAX_C UINT64_C
#endif // __STDC_CONSTANT_MACROS ]
#endif // _MSC_STDINT_H_ ]
hawtjni-hawtjni-project-1.15/hawtjni-runtime/ 0000775 0000000 0000000 00000000000 13102625210 0021327 5 ustar 00root root 0000000 0000000 hawtjni-hawtjni-project-1.15/hawtjni-runtime/pom.xml 0000664 0000000 0000000 00000003135 13102625210 0022646 0 ustar 00root root 0000000 0000000
4.0.0
org.fusesource.hawtjni
hawtjni-project
1.15
hawtjni-runtime
HawtJNI Runtime
The API that projects using HawtJNI should build against.
org.apache.maven.plugins
maven-javadoc-plugin
attach-javadocs
jar
hawtjni-hawtjni-project-1.15/hawtjni-runtime/src/ 0000775 0000000 0000000 00000000000 13102625210 0022116 5 ustar 00root root 0000000 0000000 hawtjni-hawtjni-project-1.15/hawtjni-runtime/src/main/ 0000775 0000000 0000000 00000000000 13102625210 0023042 5 ustar 00root root 0000000 0000000 hawtjni-hawtjni-project-1.15/hawtjni-runtime/src/main/java/ 0000775 0000000 0000000 00000000000 13102625210 0023763 5 ustar 00root root 0000000 0000000 hawtjni-hawtjni-project-1.15/hawtjni-runtime/src/main/java/org/ 0000775 0000000 0000000 00000000000 13102625210 0024552 5 ustar 00root root 0000000 0000000 hawtjni-hawtjni-project-1.15/hawtjni-runtime/src/main/java/org/fusesource/ 0000775 0000000 0000000 00000000000 13102625210 0026735 5 ustar 00root root 0000000 0000000 hawtjni-hawtjni-project-1.15/hawtjni-runtime/src/main/java/org/fusesource/hawtjni/ 0000775 0000000 0000000 00000000000 13102625210 0030401 5 ustar 00root root 0000000 0000000 hawtjni-hawtjni-project-1.15/hawtjni-runtime/src/main/java/org/fusesource/hawtjni/runtime/ 0000775 0000000 0000000 00000000000 13102625210 0032064 5 ustar 00root root 0000000 0000000 ArgFlag.java 0000664 0000000 0000000 00000005234 13102625210 0034157 0 ustar 00root root 0000000 0000000 hawtjni-hawtjni-project-1.15/hawtjni-runtime/src/main/java/org/fusesource/hawtjni/runtime /*******************************************************************************
* Copyright (C) 2009-2011 FuseSource Corp.
* Copyright (c) 2004, 2008 IBM Corporation and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.fusesource.hawtjni.runtime;
/**
*
* @author Hiram Chirino
*/
public enum ArgFlag {
/**
* Indicate that a native method parameter is an out only variable.
* This only makes sense if the parameter is a structure or an array
* of primitives. It is an optimization to avoid copying the java
* memory to C memory on the way in.
*/
NO_IN,
/**
* Indicate that a native method parameter is an in only variable.
* This only makes sense if the parameter is a structure or an array
* of primitives. It is an optimization to avoid copying the C memory
* from java memory on the way out.
*/
NO_OUT,
/**
* Indicate that GetPrimitiveArrayCritical() should be used instead
* of Get<PrimitiveType>ArrayElements() when transferring array of
* primitives from/to C. This is an optimization to avoid copying
* memory and must be used carefully. It is ok to be used in
* MoveMemory() and memmove() natives.
*/
CRITICAL,
/**
* Indicate that the associated C local variable for a native method
* parameter should be initialized with zeros.
*/
INIT,
/**
* Indicate that the parameter is a pointer.
*/
POINTER_ARG,
/**
* Indicate that a structure parameter should be passed by value
* instead of by reference. This dereferences the parameter by
* prepending *. The parameter must not be NULL.
*/
BY_VALUE,
/**
* Indicate that GetStringChars()should be used instead of
* GetStringUTFChars() to get the characters of a java.lang.String
* passed as a parameter to native methods.
*/
UNICODE,
/**
* Indicate that the parameter of a native method is the sentinel
* (last parameter of a variable argument C function). The generated
* code is always the literal NULL. Some compilers expect the sentinel
* to be the literal NULL and output a warning if otherwise.
*/
SENTINEL,
/**
* Indicate that the native parameter is a C# managed object.
*/
CS_OBJECT,
} Callback.java 0000775 0000000 0000000 00000022401 13102625210 0034346 0 ustar 00root root 0000000 0000000 hawtjni-hawtjni-project-1.15/hawtjni-runtime/src/main/java/org/fusesource/hawtjni/runtime /*******************************************************************************
* Copyright (c) 2000, 2007 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.fusesource.hawtjni.runtime;
/**
* Instances of this class represent entry points into Java which can be invoked
* from operating system level callback routines.
*
* IMPORTANT: A callback is only valid when invoked on the thread which created
* it. The results are undefined (and typically bad) when a callback is passed
* out to the operating system (or other code) in such a way that the callback
* is called from a different thread.
*/
public class Callback {
Object object;
String method, signature;
int argCount;
long /* int */ address, errorResult;
boolean isStatic, isArrayBased;
static final String PTR_SIGNATURE = "J"; /* C.PTR_SIZEOF == 4 ? "I" : "J"; */
static final String SIGNATURE_0 = getSignature(0);
static final String SIGNATURE_1 = getSignature(1);
static final String SIGNATURE_2 = getSignature(2);
static final String SIGNATURE_3 = getSignature(3);
static final String SIGNATURE_4 = getSignature(4);
static final String SIGNATURE_N = "([" + PTR_SIGNATURE + ")" + PTR_SIGNATURE;
/**
* Constructs a new instance of this class given an object to send the
* message to, a string naming the method to invoke and an argument count.
* Note that, if the object is an instance of Class
it is
* assumed that the method is a static method on that class.
*
* @param object
* the object to send the message to
* @param method
* the name of the method to invoke
* @param argCount
* the number of arguments that the method takes
*/
public Callback(Object object, String method, int argCount) {
this(object, method, argCount, false);
}
/**
* Constructs a new instance of this class given an object to send the
* message to, a string naming the method to invoke, an argument count and a
* flag indicating whether or not the arguments will be passed in an array.
* Note that, if the object is an instance of Class
it is
* assumed that the method is a static method on that class.
*
* @param object
* the object to send the message to
* @param method
* the name of the method to invoke
* @param argCount
* the number of arguments that the method takes
* @param isArrayBased
* true
if the arguments should be passed in an
* array and false otherwise
*/
public Callback(Object object, String method, int argCount, boolean isArrayBased) {
this(object, method, argCount, isArrayBased, 0);
}
/**
* Constructs a new instance of this class given an object to send the
* message to, a string naming the method to invoke, an argument count, a
* flag indicating whether or not the arguments will be passed in an array
* and a value to return when an exception happens. Note that, if the object
* is an instance of Class
it is assumed that the method is a
* static method on that class.
*
* @param object
* the object to send the message to
* @param method
* the name of the method to invoke
* @param argCount
* the number of arguments that the method takes
* @param isArrayBased
* true
if the arguments should be passed in an
* array and false otherwise
* @param errorResult
* the return value if the java code throws an exception
*/
public Callback(Object object, String method, int argCount, boolean isArrayBased, long /* int */errorResult) {
/* Set the callback fields */
this.object = object;
this.method = method;
this.argCount = argCount;
this.isStatic = object instanceof Class>;
this.isArrayBased = isArrayBased;
this.errorResult = errorResult;
/* Inline the common cases */
if (isArrayBased) {
signature = SIGNATURE_N;
} else {
switch (argCount) {
case 0:
signature = SIGNATURE_0;
break; //$NON-NLS-1$
case 1:
signature = SIGNATURE_1;
break; //$NON-NLS-1$
case 2:
signature = SIGNATURE_2;
break; //$NON-NLS-1$
case 3:
signature = SIGNATURE_3;
break; //$NON-NLS-1$
case 4:
signature = SIGNATURE_4;
break; //$NON-NLS-1$
default:
signature = getSignature(argCount);
}
}
/* Bind the address */
address = bind(this, object, method, signature, argCount, isStatic, isArrayBased, errorResult);
}
/**
* Allocates the native level resources associated with the callback. This
* method is only invoked from within the constructor for the argument.
*
* @param callback
* the callback to bind
* @param object
* the callback's object
* @param method
* the callback's method
* @param signature
* the callback's method signature
* @param argCount
* the callback's method argument count
* @param isStatic
* whether the callback's method is static
* @param isArrayBased
* whether the callback's method is array based
* @param errorResult
* the callback's error result
*/
static native synchronized long /* int */ bind(Callback callback, Object object, String method, String signature, int argCount, boolean isStatic, boolean isArrayBased,
long /* int */errorResult);
/**
* Releases the native level resources associated with the callback, and
* removes all references between the callback and other objects. This helps
* to prevent (bad) application code from accidentally holding onto
* extraneous garbage.
*/
public void dispose() {
if (object == null)
return;
unbind(this);
object = method = signature = null;
address = 0;
}
/**
* Returns the address of a block of machine code which will invoke the
* callback represented by the receiver.
*
* @return the callback address
*/
public long /* int */getAddress() {
return address;
}
/**
* Returns the SWT platform name.
*
* @return the platform name of the currently running SWT
*/
public static native String getPlatform();
/**
* Returns the number of times the system has been recursively entered
* through a callback.
*
* Note: This should not be called by application code.
*
*
* @return the entry count
*
* @since 2.1
*/
public static native int getEntryCount();
static String getSignature(int argCount) {
String signature = "("; //$NON-NLS-1$
for (int i = 0; i < argCount; i++)
signature += PTR_SIGNATURE;
signature += ")" + PTR_SIGNATURE; //$NON-NLS-1$
return signature;
}
/**
* Indicates whether or not callbacks which are triggered at the native
* level should cause the messages described by the matching
* Callback
objects to be invoked. This method is used to
* safely shut down SWT when it is run within environments which can
* generate spurious events.
*
* Note: This should not be called by application code.
*
*
* @param enable
* true if callbacks should be invoked
*/
public static final native synchronized void setEnabled(boolean enable);
/**
* Returns whether or not callbacks which are triggered at the native level
* should cause the messages described by the matching Callback
* objects to be invoked. This method is used to safely shut down SWT when
* it is run within environments which can generate spurious events.
*
* Note: This should not be called by application code.
*
*
* @return true if callbacks should not be invoked
*/
public static final native synchronized boolean getEnabled();
/**
* Immediately wipes out all native level state associated with all
* callbacks.
*
* WARNING: This operation is extremely dangerous, and
* should never be performed by application code.
*
*/
public static final native synchronized void reset();
/**
* Releases the native level resources associated with the callback.
*
* @see #dispose
*/
static final native synchronized void unbind(Callback callback);
}
ClassFlag.java 0000664 0000000 0000000 00000002560 13102625210 0034512 0 ustar 00root root 0000000 0000000 hawtjni-hawtjni-project-1.15/hawtjni-runtime/src/main/java/org/fusesource/hawtjni/runtime /*******************************************************************************
* Copyright (C) 2009-2011 FuseSource Corp.
* Copyright (c) 2004, 2008 IBM Corporation and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.fusesource.hawtjni.runtime;
/**
*
* @author Hiram Chirino
*/
public enum ClassFlag {
/**
* Indicate that the item should not be generated. For example,
* custom natives are coded by hand.
*/
CLASS_SKIP,
/**
* Indicate that the platform source is in C++
*/
CPP,
/**
* Indicate that this class will define a structure
*/
STRUCT,
/**
* Indicate that structure name is a typedef (It should
* not be prefixed with 'struct' to reference it.)
*/
TYPEDEF,
/**
* Indicate that the struct should get zeroed out before
* setting any of it's fields. Comes in handy when
* you don't map all the struct fields to java fields but
* still want the fields that are not mapped initialized.
*/
ZERO_OUT,
} FieldFlag.java 0000664 0000000 0000000 00000002060 13102625210 0034463 0 ustar 00root root 0000000 0000000 hawtjni-hawtjni-project-1.15/hawtjni-runtime/src/main/java/org/fusesource/hawtjni/runtime /*******************************************************************************
* Copyright (C) 2009-2011 FuseSource Corp.
* Copyright (c) 2004, 2008 IBM Corporation and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.fusesource.hawtjni.runtime;
/**
*
* @author Hiram Chirino
*/
public enum FieldFlag {
/**
* Indicate that the item should not be generated. For example,
* custom natives are coded by hand.
*/
FIELD_SKIP,
/**
* Indicate that the field represents a constant or global
* variable. It is expected that the java field will be declared
* static.
*/
CONSTANT,
/**
* Indicate that the field is a pointer.
*/
POINTER_FIELD,
} JNIEnv.java 0000664 0000000 0000000 00000000715 13102625210 0033744 0 ustar 00root root 0000000 0000000 hawtjni-hawtjni-project-1.15/hawtjni-runtime/src/main/java/org/fusesource/hawtjni/runtime /**
* Copyright (C) 2010, FuseSource Corp. All rights reserved.
*/
package org.fusesource.hawtjni.runtime;
/**
*
* This is a marker class. Methods that take this as an argument
* will receive that actual native 'JNIEnv *' value. Since this
* class cannot be instantiated, Java callers must pass null
* for the value.
*
*
* @author Hiram Chirino
*/
public class JNIEnv {
private JNIEnv() {}
}
JniArg.java 0000664 0000000 0000000 00000001506 13102625210 0034024 0 ustar 00root root 0000000 0000000 hawtjni-hawtjni-project-1.15/hawtjni-runtime/src/main/java/org/fusesource/hawtjni/runtime /*******************************************************************************
* Copyright (C) 2009-2011 FuseSource Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.fusesource.hawtjni.runtime;
/**
*
*/
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
@Target({PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface JniArg {
ArgFlag[] flags() default {};
String cast() default "";
}
JniClass.java 0000664 0000000 0000000 00000001553 13102625210 0034362 0 ustar 00root root 0000000 0000000 hawtjni-hawtjni-project-1.15/hawtjni-runtime/src/main/java/org/fusesource/hawtjni/runtime /*******************************************************************************
* Copyright (C) 2009-2011 FuseSource Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.fusesource.hawtjni.runtime;
/**
*
*/
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
@Target({TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface JniClass {
ClassFlag[] flags() default {};
String conditional() default "";
String name() default "";
}
JniField.java 0000664 0000000 0000000 00000001710 13102625210 0034333 0 ustar 00root root 0000000 0000000 hawtjni-hawtjni-project-1.15/hawtjni-runtime/src/main/java/org/fusesource/hawtjni/runtime /*******************************************************************************
* Copyright (C) 2009-2011 FuseSource Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.fusesource.hawtjni.runtime;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
/**
*
* @author Hiram Chirino
*/
@Target({FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface JniField {
String cast() default "";
String accessor() default "";
String conditional() default "";
FieldFlag[] flags() default {};
}
JniMethod.java 0000664 0000000 0000000 00000002126 13102625210 0034532 0 ustar 00root root 0000000 0000000 hawtjni-hawtjni-project-1.15/hawtjni-runtime/src/main/java/org/fusesource/hawtjni/runtime /*******************************************************************************
* Copyright (C) 2009-2011 FuseSource Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.fusesource.hawtjni.runtime;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
/**
*
* @author Hiram Chirino
*/
@Target({METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface JniMethod {
String cast() default "";
// Pointer pointer() default Pointer.DETERMINE_FROM_CAST;
String accessor() default "";
MethodFlag[] flags() default {};
String copy() default "";
String conditional() default "";
JniArg[] callbackArgs() default {};
}
Library.java 0000775 0000000 0000000 00000040625 13102625210 0034266 0 ustar 00root root 0000000 0000000 hawtjni-hawtjni-project-1.15/hawtjni-runtime/src/main/java/org/fusesource/hawtjni/runtime /*******************************************************************************
* Copyright (C) 2009-2011 FuseSource Corp.
* Copyright (c) 2000, 2009 IBM Corporation and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.fusesource.hawtjni.runtime;
import java.io.*;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;
/**
* Used to find and load a JNI library, eventually after having extracted it.
*
* It will search for the library in order at the following locations:
*
* - in the custom library path: If the "
library.${name}.path
" System property is set to a directory,
* subdirectories are searched:
*
* - "
${platform}/${arch}
"
* - "
${platform}
"
* - "
${os}
"
* - "
"
*
* for 2 namings of the library:
*
* - as "
${name}-${version}
" library name if the version can be determined.
* - as "
${name}
" library name
*
* - system library path: This is where the JVM looks for JNI libraries by default.
*
* - as "
${name}${bit-model}-${version}
" library name if the version can be determined.
* - as "
${name}-${version}
" library name if the version can be determined.
* - as "
${name}
" library name
*
* - classpath path: If the JNI library can be found on the classpath, it will get extracted
* and then loaded. This way you can embed your JNI libraries into your packaged JAR files.
* They are looked up as resources in this order:
*
* - "
META-INF/native/${platform}/${arch}/${library[-version]}
": Store your library here if you want to embed
* more than one platform JNI library on different processor archs in the jar.
* - "
META-INF/native/${platform}/${library[-version]}
": Store your library here if you want to embed more
* than one platform JNI library in the jar.
* - "
META-INF/native/${os}/${library[-version]}
": Store your library here if you want to embed more
* than one platform JNI library in the jar but don't want to take bit model into account.
* - "
META-INF/native/${library[-version]}
": Store your library here if your JAR is only going to embedding one
* platform library.
*
* The file extraction is attempted until it succeeds in the following directories.
*
* - The directory pointed to by the "
library.${name}.path
" System property (if set)
* - a temporary directory (uses the "
java.io.tmpdir
" System property)
*
*
*
* where:
*
* - "
${name}
" is the name of library
* - "
${version}
" is the value of "library.${name}.version
" System property if set.
* Otherwise it is set to the ImplementationVersion property of the JAR's Manifest
* - "
${os}
" is your operating system, for example "osx
", "linux
", or "windows
"
* - "
${bit-model}
" is "64
" if the JVM process is a 64 bit process, otherwise it's "32
" if the
* JVM is a 32 bit process
* - "
${arch}
" is the architecture for the processor, for example "amd64
" or "sparcv9
"
* - "
${platform}
" is "${os}${bit-model}
", for example "linux32
" or "osx64
"
* - "
${library[-version]}
": is the normal jni library name for the platform (eventually with -${version}
) suffix.
* For example "${name}.dll
" on
* windows, "lib${name}.jnilib
" on OS X, and "lib${name}.so
" on linux
*
*
* @author Hiram Chirino
* @see System#mapLibraryName(String)
*/
public class Library {
static final String SLASH = System.getProperty("file.separator");
final private String name;
final private String version;
final private ClassLoader classLoader;
private boolean loaded;
public Library(String name) {
this(name, null, null);
}
public Library(String name, Class> clazz) {
this(name, version(clazz), clazz.getClassLoader());
}
public Library(String name, String version) {
this(name, version, null);
}
public Library(String name, String version, ClassLoader classLoader) {
if( name == null ) {
throw new IllegalArgumentException("name cannot be null");
}
this.name = name;
this.version = version;
this.classLoader= classLoader;
}
private static String version(Class> clazz) {
try {
return clazz.getPackage().getImplementationVersion();
} catch (Throwable e) {
}
return null;
}
public static String getOperatingSystem() {
String name = System.getProperty("os.name").toLowerCase().trim();
if( name.startsWith("linux") ) {
return "linux";
}
if( name.startsWith("mac os x") ) {
return "osx";
}
if( name.startsWith("win") ) {
return "windows";
}
return name.replaceAll("\\W+", "_");
}
public static String getPlatform() {
return getOperatingSystem()+getBitModel();
}
public static int getBitModel() {
String prop = System.getProperty("sun.arch.data.model");
if (prop == null) {
prop = System.getProperty("com.ibm.vm.bitmode");
}
if( prop!=null ) {
return Integer.parseInt(prop);
}
return -1; // we don't know..
}
/**
*
*/
synchronized public void load() {
if( loaded ) {
return;
}
doLoad();
loaded = true;
}
private void doLoad() {
/* Perhaps a custom version is specified */
String version = System.getProperty("library."+name+".version");
if (version == null) {
version = this.version;
}
ArrayList errors = new ArrayList();
String[] specificDirs = getSpecificSearchDirs();
String libFilename = map(name);
String versionlibFilename = (version == null) ? null : map(name + "-" + version);
/* Try loading library from a custom library path */
String customPath = System.getProperty("library."+name+".path");
if (customPath != null) {
for ( String dir: specificDirs ) {
if( version!=null && load(errors, file(customPath, dir, versionlibFilename)) )
return;
if( load(errors, file(customPath, dir, libFilename)) )
return;
}
}
/* Try loading library from java library path */
if( version!=null && load(errors, name + getBitModel() + "-" + version) )
return;
if( version!=null && load(errors, name + "-" + version) )
return;
if( load(errors, name ) )
return;
/* Try extracting the library from the jar */
if( classLoader!=null ) {
String targetLibName = version != null ? versionlibFilename : libFilename;
for ( String dir: specificDirs ) {
if( version!=null && extractAndLoad(errors, customPath, dir, versionlibFilename, targetLibName) )
return;
if( extractAndLoad(errors, customPath, dir, libFilename, targetLibName) )
return;
}
}
/* Failed to find the library */
UnsatisfiedLinkError e = new UnsatisfiedLinkError("Could not load library. Reasons: " + errors.toString());
try {
Method method = Throwable.class.getMethod("addSuppressed", Throwable.class);
for (Throwable t : errors) {
method.invoke(e, t);
}
} catch (Throwable ignore) {
}
throw e;
}
@Deprecated
final public String getArchSpecifcResourcePath() {
return getArchSpecificResourcePath();
}
final public String getArchSpecificResourcePath() {
return "META-INF/native/"+ getPlatform() + "/" + System.getProperty("os.arch") + "/" +map(name);
}
@Deprecated
final public String getOperatingSystemSpecifcResourcePath() {
return getOperatingSystemSpecificResourcePath();
}
final public String getOperatingSystemSpecificResourcePath() {
return getPlatformSpecificResourcePath(getOperatingSystem());
}
@Deprecated
final public String getPlatformSpecifcResourcePath() {
return getPlatformSpecificResourcePath();
}
final public String getPlatformSpecificResourcePath() {
return getPlatformSpecificResourcePath(getPlatform());
}
@Deprecated
final public String getPlatformSpecifcResourcePath(String platform) {
return getPlatformSpecificResourcePath(platform);
}
final public String getPlatformSpecificResourcePath(String platform) {
return "META-INF/native/"+platform+"/"+map(name);
}
@Deprecated
final public String getResorucePath() {
return getResourcePath();
}
final public String getResourcePath() {
return "META-INF/native/"+map(name);
}
final public String getLibraryFileName() {
return map(name);
}
/**
* Search directories for library:
* ${platform}/${arch}
to enable platform JNI library for different processor archs
* ${platform}
to enable platform JNI library
* ${os}
to enable OS JNI library
* - no directory
*
* @return the list
*/
final public String[] getSpecificSearchDirs() {
return new String[] {
getPlatform() + "/" + System.getProperty("os.arch"),
getPlatform(),
getOperatingSystem()
};
}
private boolean extractAndLoad(ArrayList errors, String customPath, String dir, String libName, String targetLibName) {
String resourcePath = "META-INF/native/" + ( dir == null ? "" : (dir + '/')) + libName;
URL resource = classLoader.getResource(resourcePath);
if( resource !=null ) {
int idx = targetLibName.lastIndexOf('.');
String prefix = targetLibName.substring(0, idx)+"-";
String suffix = targetLibName.substring(idx);
// Use the user provided path,
// then fallback to the java temp directory,
// and last, use the user home folder
for (File path : Arrays.asList(
customPath != null ? file(customPath) : null,
file(System.getProperty("java.io.tmpdir")),
file(System.getProperty("user.home"), ".hawtjni", name))) {
if( path!=null ) {
// Try to extract it to the custom path...
File target = extract(errors, resource, prefix, suffix, path);
if( target!=null ) {
if( load(errors, target) ) {
return true;
}
}
}
}
}
return false;
}
private File file(String ...paths) {
File rc = null ;
for (String path : paths) {
if( rc == null ) {
rc = new File(path);
} else if( path != null ) {
rc = new File(rc, path);
}
}
return rc;
}
private String map(String libName) {
/*
* libraries in the Macintosh use the extension .jnilib but the some
* VMs map to .dylib.
*/
libName = System.mapLibraryName(libName);
String ext = ".dylib";
if (libName.endsWith(ext)) {
libName = libName.substring(0, libName.length() - ext.length()) + ".jnilib";
}
return libName;
}
private File extract(ArrayList errors, URL source, String prefix, String suffix, File directory) {
File target = null;
directory = directory.getAbsoluteFile();
if (!directory.exists()) {
if (!directory.mkdirs()) {
errors.add(new IOException("Unable to create directory: " + directory));
return null;
}
}
try {
FileOutputStream os = null;
InputStream is = null;
try {
target = File.createTempFile(prefix, suffix, directory);
is = source.openStream();
if (is != null) {
byte[] buffer = new byte[4096];
os = new FileOutputStream(target);
int read;
while ((read = is.read(buffer)) != -1) {
os.write(buffer, 0, read);
}
chmod755(target);
}
target.deleteOnExit();
return target;
} finally {
close(os);
close(is);
}
} catch (Throwable e) {
IOException io;
if( target!=null ) {
target.delete();
io = new IOException("Unable to extract library from " + source + " to " + target);
} else {
io = new IOException("Unable to create temporary file in " + directory);
}
io.initCause(e);
errors.add(io);
}
return null;
}
static private void close(Closeable file) {
if(file!=null) {
try {
file.close();
} catch (Exception ignore) {
}
}
}
private void chmod755(File file) {
if (getPlatform().startsWith("windows"))
return;
// Use Files.setPosixFilePermissions if we are running Java 7+ to avoid forking the JVM for executing chmod
try {
ClassLoader classLoader = getClass().getClassLoader();
// Check if the PosixFilePermissions exists in the JVM, if not this will throw a ClassNotFoundException
Class> posixFilePermissionsClass = classLoader.loadClass("java.nio.file.attribute.PosixFilePermissions");
// Set permissionSet = PosixFilePermissions.fromString("rwxr-xr-x")
Method fromStringMethod = posixFilePermissionsClass.getMethod("fromString", String.class);
Object permissionSet = fromStringMethod.invoke(null, "rwxr-xr-x");
// Path path = file.toPath()
Object path = file.getClass().getMethod("toPath").invoke(file);
// Files.setPosixFilePermissions(path, permissionSet)
Class> pathClass = classLoader.loadClass("java.nio.file.Path");
Class> filesClass = classLoader.loadClass("java.nio.file.Files");
Method setPosixFilePermissionsMethod = filesClass.getMethod("setPosixFilePermissions", pathClass, Set.class);
setPosixFilePermissionsMethod.invoke(null, path, permissionSet);
} catch (Throwable ignored) {
// Fallback to starting a new process
try {
Runtime.getRuntime().exec(new String[]{"chmod", "755", file.getCanonicalPath()}).waitFor();
} catch (Throwable e) {
}
}
}
private boolean load(ArrayList errors, File lib) {
try {
System.load(lib.getPath());
return true;
} catch (UnsatisfiedLinkError e) {
LinkageError le = new LinkageError("Unable to load library from " + lib);
le.initCause(e);
errors.add(le);
}
return false;
}
private boolean load(ArrayList errors, String lib) {
try {
System.loadLibrary(lib);
return true;
} catch (UnsatisfiedLinkError e) {
LinkageError le = new LinkageError("Unable to load library " + lib);
le.initCause(e);
errors.add(le);
}
return false;
}
}
MethodFlag.java 0000664 0000000 0000000 00000006331 13102625210 0034665 0 ustar 00root root 0000000 0000000 hawtjni-hawtjni-project-1.15/hawtjni-runtime/src/main/java/org/fusesource/hawtjni/runtime /*******************************************************************************
* Copyright (C) 2009-2011 FuseSource Corp.
* Copyright (c) 2000, 2008 IBM Corporation and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.fusesource.hawtjni.runtime;
/**
*
* @author Hiram Chirino
*/
public enum MethodFlag {
/**
* Indicate that the item should not be generated. For example,
* custom natives are coded by hand.
*/
METHOD_SKIP,
/**
* Indicate that a native method should be looked up dynamically. It
* is useful when having a dependence on a given library is not
* desirable. The library name is specified in the *_custom.h file.
*/
DYNAMIC,
/**
* Indicate that the native method represents a constant or global
* variable instead of a function. This omits () from the generated
* code.
*/
CONSTANT_GETTER,
/**
* Indicate that the C function should be casted to a prototype
* generated from the parameters of the native method. Useful for
* variable argument C functions.
*/
CAST,
/**
* Indicate that the native is part of the Java Native Interface. For
* example: NewGlobalRef().
*/
JNI,
/**
* Indicate that the native method represents a structure global
* variable and the address of it should be returned to Java. This is
* done by prepending &.
*/
ADDRESS,
/**
* Indicate that the native method is calling a C++ object's method.
*/
CPP_METHOD,
/**
* Indicate that the native method is a C++ constructor that allocates
* an object on the heap.
*/
CPP_NEW,
/**
* Indicate that the native method is a C++ destructor that
* deallocates an object from the heap.
*/
CPP_DELETE,
/**
* Indicate that the native method is a C# constructor that allocates
* an object on the managed (i.e. garbage collected) heap.
*/
CS_NEW,
/**
* Indicate that the native method's return value is a
* C# managed object.
*/
CS_OBJECT,
/**
* Indicate that the native method represents a setter for a field in
* an object or structure
*/
SETTER,
/**
* Indicate that the native method represents a getter for a field in
* an object or structure.
*/
GETTER,
/**
* Indicate that the native method takes 2 arguments, a collection and
* an item, and the += operator is used to add the item to the
* collection.
*/
ADDER,
/**
* Indicate that the return value is a pointer.
*/
POINTER_RETURN,
/**
* Indicate that this method will be the constant initializer for
* the class. When called, it will set all the static constant fields
* to the values defined in your platform.
*/
CONSTANT_INITIALIZER,
} NativeStats.java 0000775 0000000 0000000 00000016573 13102625210 0035134 0 ustar 00root root 0000000 0000000 hawtjni-hawtjni-project-1.15/hawtjni-runtime/src/main/java/org/fusesource/hawtjni/runtime /*******************************************************************************
* Copyright (C) 2009-2011 FuseSource Corp.
* Copyright (c) 2004, 2006 IBM Corporation and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.fusesource.hawtjni.runtime;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map.Entry;
/**
* Instructions on how to use the NativeStats tool with a standalone SWT
* example:
*
* - Compile the native libraries defining the NATIVE_STATS flag.
* - Add the following code around the sections of
* interest to dump the native calls done in that section.
*
* StatsInterface si = MyFooStatsInterface.INSTANCE;
* NativeStats stats = new NativeStats(si);
* ... // your code
* stats.diff().dump(System.out);
*
*
* - Or add the following code at a given point to dump a snapshot of
* the native calls done until that point.
*
* stats.snapshot().dump(System.out);
*
*
*
*
* @author Hiram Chirino
*/
public class NativeStats {
public interface StatsInterface {
String getNativeClass();
int functionCount();
String functionName(int ordinal);
int functionCounter(int ordinal);
}
public static class NativeFunction implements Comparable {
private final int ordinal;
private final String name;
private int counter;
public NativeFunction(int ordinal, String name, int callCount) {
this.ordinal = ordinal;
this.name = name;
this.counter = callCount;
}
void subtract(NativeFunction func) {
this.counter -= func.counter;
}
public int getCounter() {
return counter;
}
public void setCounter(int counter) {
this.counter = counter;
}
public String getName() {
return name;
}
public int getOrdinal() {
return ordinal;
}
public int compareTo(NativeFunction func) {
return func.counter - counter;
}
public void reset() {
counter=0;
}
public NativeFunction copy() {
return new NativeFunction(ordinal, name, counter);
}
}
private final HashMap> snapshot;
public NativeStats(StatsInterface... classes) {
this(Arrays.asList(classes));
}
public NativeStats(Collection classes) {
this(snapshot(classes));
}
private NativeStats(HashMap> snapshot) {
this.snapshot = snapshot;
}
public void reset() {
for (ArrayList functions : snapshot.values()) {
for (NativeFunction function : functions) {
function.reset();
}
}
}
public void update() {
for (Entry> entry : snapshot.entrySet()) {
StatsInterface si = entry.getKey();
for (NativeFunction function : entry.getValue()) {
function.setCounter( si.functionCounter(function.getOrdinal()) );
}
}
}
public NativeStats snapshot() {
NativeStats copy = copy();
copy.update();
return copy;
}
public NativeStats copy() {
HashMap> rc = new HashMap>(snapshot.size()*2);
for (Entry> entry : snapshot.entrySet()) {
ArrayList list = new ArrayList(entry.getValue().size());
for (NativeFunction function : entry.getValue()) {
list.add(function.copy());
}
rc.put(entry.getKey(), list);
}
return new NativeStats(rc);
}
public NativeStats diff() {
HashMap> rc = new HashMap>(snapshot.size()*2);
for (Entry> entry : snapshot.entrySet()) {
StatsInterface si = entry.getKey();
ArrayList list = new ArrayList(entry.getValue().size());
for (NativeFunction original : entry.getValue()) {
NativeFunction copy = original.copy();
copy.setCounter( si.functionCounter(copy.getOrdinal()) );
copy.subtract(original);
list.add(copy);
}
rc.put(si, list);
}
return new NativeStats(rc);
}
/**
* Dumps the stats to the print stream in a JSON format.
* @param ps
*/
public void dump(PrintStream ps) {
boolean firstSI=true;
for (Entry> entry : snapshot.entrySet()) {
StatsInterface si = entry.getKey();
ArrayList funcs = entry.getValue();
int total = 0;
for (NativeFunction func : funcs) {
total += func.getCounter();
}
if( !firstSI ) {
ps.print(", ");
}
firstSI=false;
ps.print("[");
if( total>0 ) {
ps.println("{ ");
ps.println(" \"class\": \""+si.getNativeClass()+"\",");
ps.println(" \"total\": "+total+", ");
ps.print(" \"functions\": {");
boolean firstFunc=true;
for (NativeFunction func : funcs) {
if (func.getCounter() > 0) {
if( !firstFunc ) {
ps.print(",");
}
firstFunc=false;
ps.println();
ps.print(" \""+func.getName()+"\": "+func.getCounter());
}
}
ps.println();
ps.println(" }");
ps.print("}");
}
ps.print("]");
}
}
static private HashMap> snapshot(Collection