This commit is contained in:
Doug MacEachern committed 2009-02-06 13:53:30 -08:00
1 parent 1cd800ff6e
commit e787667e91
10 files changed
+2002

No files matched your search

@@ -0,0 +1,442 @@
/*
* Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
* This file is part of SIGAR.
*
* SIGAR is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package org.hyperic.jni;
import java.io.File;
import java.util.StringTokenizer;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLDecoder;
public class ArchLoader {
private Object loadLock = new Object();
private boolean loaded = false;
private final static String osName = System.getProperty("os.name");
public final static boolean IS_WIN32 = osName.startsWith("Windows");
public final static boolean IS_AIX = osName.equals("AIX");
public final static boolean IS_HPUX = osName.equals("HP-UX");
public final static boolean IS_SOLARIS = osName.equals("SunOS");
public final static boolean IS_LINUX = osName.equals("Linux");
public final static boolean IS_DARWIN = osName.equals("Mac OS X") || osName.equals("Darwin");
public final static boolean IS_OSF1 = osName.equals("OSF1");
public final static boolean IS_FREEBSD = osName.equals("FreeBSD");
public final static boolean IS_NETWARE = osName.equals("NetWare");
private String packageName;
private String name;
private String resourcePath;
private Class loaderClass;
private String jarName;
private String libName = null;
private File nativeLibrary;
private String version;
public ArchLoader() { }
public ArchLoader(Class loaderClass) {
setLoaderClass(loaderClass); //e.g. Sigar.class
//argh. possible for loaderClass.getPackage()
//to return null depending on ClassLoader
String pname = loaderClass.getName();
int ix = pname.lastIndexOf(".");
pname = pname.substring(0, ix);
setPackageName(pname); //e.g. org.hyperic.sigar
ix = pname.lastIndexOf(".");
setName(pname.substring(ix+1)); //e.g. sigar
setJarName(getName() + ".jar");
setResourcePath(toResName(pname)); //e.g. org.hyperic/sigar
}
public Class getLoaderClass() {
return this.loaderClass;
}
public void setLoaderClass(Class value) {
this.loaderClass = value;
}
public ClassLoader getClassLoader() {
return getLoaderClass().getClassLoader();
}
public String getName() {
return this.name;
}
public void setName(String value) {
this.name = value;
}
public String getPackageName() {
return this.packageName;
}
public void setPackageName(String value) {
this.packageName = value;
}
public String getResourcePath() {
return this.resourcePath;
}
public void setResourcePath(String value) {
this.resourcePath = value;
}
public String getJarName() {
return this.jarName;
}
public void setJarName(String value) {
this.jarName = value;
}
public String getLibName() {
return this.libName;
}
public void setLibName(String value) {
this.libName = value;
}
public String getArchLibName()
throws ArchNotSupportedException {
return getName() + "-" + ArchName.getName();
}
public String getDefaultLibName()
throws ArchNotSupportedException {
return
System.getProperty(getPackageName() + ".libname",
//e.g. javasigar-x86-linux
"java" + getArchLibName());
}
public File getNativeLibrary() {
return this.nativeLibrary;
}
private String toResName(String name) {
StringBuffer sb = new StringBuffer(name);
for (int i=0; i < sb.length(); i++) {
if (sb.charAt(i) == '.') {
sb.setCharAt(i, '/');
}
}
return sb.toString();
}
public static String getLibraryPrefix() {
if (IS_WIN32 || IS_NETWARE) {
return "";
}
return "lib";
}
public static String getLibraryExtension() {
if (IS_WIN32) {
return ".dll";
}
if (IS_NETWARE) {
return ".nlm";
}
if (IS_DARWIN) {
return ".dylib";
}
if (IS_HPUX) {
return ".sl";
}
return ".so";
}
public String getLibraryName()
throws ArchNotSupportedException {
String libName;
if ((libName = getLibName()) == null) {
libName = getDefaultLibName();
setLibName(libName);
}
String prefix = getLibraryPrefix();
String ext = getLibraryExtension();
return prefix + libName + ext;
}
public String getVersionedLibraryName() {
if (this.version == null) {
return null;
}
try {
getLibraryName();
} catch (ArchNotSupportedException e) {
return null;
}
String prefix = getLibraryPrefix();
String ext = getLibraryExtension();
return prefix + this.libName + '-' + this.version + ext;
}
private boolean isJarURL(URL url) {
if (url == null) {
return false;
}
String name = url.getFile();
String jarName = getJarName();
if (name.indexOf(jarName) != -1) {
return true;
}
int ix = jarName.indexOf(".jar");
if (ix != -1) {
//check for a versioned name-x.x.jar
jarName = jarName.substring(0, ix) + "-";
//lastIndex else could match "sigar-bin/"
ix = name.lastIndexOf(jarName);
if (ix != -1) {
jarName = name.substring(ix);
ix = jarName.indexOf(".jar");
if (ix == -1) {
return false;
}
this.version =
jarName.substring(jarName.indexOf('-')+1, ix);
jarName = jarName.substring(0, ix+4);
setJarName(jarName); //for future reference
return true;
}
else {
return false;
}
}
else {
return false;
}
}
public String findJarPath(String libName)
throws ArchLoaderException {
/*
* native libraries should be installed along side
* ${this.name}.jar, try to find where ${this.name}.jar
* is on disk and use that path.
*/
if (getJarName() == null) {
throw new ArchLoaderException("jarName is null");
}
String path = getResourcePath();
ClassLoader loader = getClassLoader();
URL url = loader.getResource(path);
if (!isJarURL(url)) {
url = null;
}
if ((url == null) && (loader instanceof URLClassLoader)) {
URL[] urls = ((URLClassLoader)loader).getURLs();
for (int i=0; i<urls.length; i++) {
if (isJarURL(urls[i])) {
url = urls[i];
break;
}
}
}
if (url == null) {
throw new ArchLoaderException("Unable to find " +
getJarName());
}
path = url.getFile();
if (path.startsWith("file:")) {
path = path.substring(5);
}
File file = new File(path);
String jarName = getJarName();
while ((file != null) &&
!file.getName().startsWith(jarName))
{
file = file.getParentFile();
}
if (libName == null) {
//return path containing ${this.name}.jar
//which may or may not be -x.x.x.x versioned
libName = jarName;
}
if ((file != null) &&
((file = file.getParentFile()) != null))
{
String dir = URLDecoder.decode(file.toString());
if (findNativeLibrary(dir, libName)) {
return dir;
}
}
return null;
}
//java.lang.ClassLoader does not delegate native linking
//to the parent ClassLoader.
//overriding these methods provides a workaround.
protected void systemLoadLibrary(String name) {
System.loadLibrary(name);
}
protected void systemLoad(String name) {
System.load(name);
}
protected boolean containsNativeLibrary(File dir, String name) {
if (name == null) {
return false;
}
File file = new File(dir, name);
if (file.exists()) {
this.nativeLibrary = file;
return true;
}
return false;
}
protected boolean findNativeLibrary(String dir, String name) {
//must be an absolute path; this allows relative to $PWD
File path = new File(dir).getAbsoluteFile();
if (containsNativeLibrary(path, name)) {
return true;
}
else if (containsNativeLibrary(path, getVersionedLibraryName())) {
return true;
}
//try w/o arch name (e.g. "libsigar.so")
return containsNativeLibrary(path,
getLibraryPrefix() +
getName() +
getLibraryExtension());
}
protected boolean findInJavaLibraryPath(String libName) {
String path = System.getProperty("java.library.path", "");
StringTokenizer tok =
new StringTokenizer(path, File.pathSeparator);
while (tok.hasMoreTokens()) {
path = tok.nextToken();
if (findNativeLibrary(path, libName)) {
return true;
}
}
return false;
}
protected void loadLibrary(String path)
throws ArchNotSupportedException,
ArchLoaderException {
try {
String libName = getLibraryName();
if (path == null) {
//e.g. org.hyperic.sigar.path
path = System.getProperty(getPackageName() + ".path");
}
if (path != null) {
if (path.equals("-")) {
return; //assume library is already loaded
}
findJarPath(null); //check for versioned .jar
findNativeLibrary(path, libName);
}
else {
if (findJarPath(libName) == null) {
findInJavaLibraryPath(libName);
}
}
//nativeLibrary set when findJarPath() calls findNativeLibrary()
if (this.nativeLibrary != null) {
systemLoad(this.nativeLibrary.toString());
}
else {
//LD_LIBRARY_PATH must be set for linux and solaris
//SHLIB_PATH must be set for hpux
//PATH must be set for windows
systemLoadLibrary(libName);
}
} catch (RuntimeException e) {
String reason = e.getMessage();
if (reason == null) {
reason = e.getClass().getName();
}
String msg =
"Failed to load " + libName +
": " + reason;
throw new ArchLoaderException(msg);
}
}
public void load()
throws ArchNotSupportedException,
ArchLoaderException {
load(null);
}
public void load(String path)
throws ArchNotSupportedException,
ArchLoaderException {
/*
* System.loadLibrary() is only supposed to load the library
* once per-classloader. but we make extra sure that is the case.
*/
synchronized (this.loadLock) {
if (this.loaded) {
return;
}
loadLibrary(path);
this.loaded = true;
}
}
}
@@ -0,0 +1,30 @@
/*
* Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
* This file is part of SIGAR.
*
* SIGAR is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package org.hyperic.jni;
public class ArchLoaderException extends Exception {
public ArchLoaderException() {
super();
}
public ArchLoaderException(String msg) {
super(msg);
}
}
@@ -0,0 +1,115 @@
/*
* Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
* This file is part of SIGAR.
*
* SIGAR is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package org.hyperic.jni;
/**
* Helper class for naming the jni library with
* platform/arch name for which it is binary compatible.
*/
public class ArchName {
static boolean useDmalloc =
System.getProperty("jni.dmalloc") != null;
public static String getName() throws ArchNotSupportedException {
String name = getArchName();
if (useDmalloc) {
name += "-dmalloc";
}
return name;
}
public static boolean is64() {
return "64".equals(System.getProperty("sun.arch.data.model"));
}
private static String getArchName() throws ArchNotSupportedException {
String name = System.getProperty("os.name");
String arch = System.getProperty("os.arch");
String version = System.getProperty("os.version");
String majorVersion = version.substring(0, 1); //4.x, 5.x, etc.
if (arch.endsWith("86")) {
arch = "x86";
}
if (name.equals("Linux")) {
return arch + "-linux";
}
else if (name.indexOf("Windows") > -1) {
return arch + "-winnt";
}
else if (name.equals("SunOS")) {
if (arch.startsWith("sparcv") && is64()) {
arch = "sparc64";
}
return arch + "-solaris";
}
else if (name.equals("HP-UX")) {
if (arch.startsWith("IA64")) {
arch = "ia64";
}
else {
arch = "pa";
}
if (version.indexOf("11") > -1) {
return arch + "-hpux-11";
}
}
else if (name.equals("AIX")) {
if (majorVersion.equals("6")) {
//v5 binary is compatible with v6
majorVersion = "5";
}
//arch == "ppc" on 32-bit, "ppc64" on 64-bit
return arch + "-aix-" + majorVersion;
}
else if (name.equals("Mac OS X") || name.equals("Darwin")) {
if (is64()) {
return "universal64-macosx";
}
else {
return "universal-macosx";
}
}
else if (name.equals("FreeBSD")) {
//none of the 4,5,6 major versions are binary compatible
return arch + "-freebsd-" + majorVersion;
}
else if (name.equals("OpenBSD")) {
return arch + "-openbsd-" + majorVersion;
}
else if (name.equals("NetBSD")) {
return arch + "-netbsd-" + majorVersion;
}
else if (name.equals("OSF1")) {
return "alpha-osf1-" + majorVersion;
}
else if (name.equals("NetWare")) {
return "x86-netware-" + majorVersion;
}
String desc = arch + "-" + name + "-" + version;
throw new ArchNotSupportedException("platform (" + desc + ") not supported");
}
private ArchName () { }
}
@@ -0,0 +1,76 @@
/*
* Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
* This file is part of SIGAR.
*
* SIGAR is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package org.hyperic.jni;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;
public class ArchNameTask extends Task {
public void execute() throws BuildException {
String osArch = System.getProperty("os.arch");
if (getProject().getProperty("jni.dmalloc") != null) {
ArchName.useDmalloc = true;
}
String archName;
try {
archName = ArchName.getName();
} catch (ArchNotSupportedException e) {
//ok: can still compile *.java
System.out.println(e.getMessage());
return;
}
System.out.println(archName);
getProject().setProperty("jni.libarch", archName);
getProject().setProperty("jni.libpre",
ArchLoader.getLibraryPrefix());
getProject().setProperty("jni.libext",
ArchLoader.getLibraryExtension());
String compiler;
if (ArchLoader.IS_WIN32) {
compiler = "msvc";
}
else if (ArchLoader.IS_HPUX) {
compiler = "hp";
}
else if (ArchLoader.IS_AIX) {
compiler = "xlc_r";
}
else {
compiler = "gcc";
getProject().setProperty("jni.compiler.isgcc", "true");
}
getProject().setProperty("jni.compiler", compiler);
if (ArchName.is64()) {
getProject().setProperty("jni.arch64", "true");
if (ArchLoader.IS_LINUX) {
if (!osArch.equals("ia64")) {
getProject().setProperty("jni.usem64", "true");
}
}
}
}
}
@@ -0,0 +1,30 @@
/*
* Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
* This file is part of SIGAR.
*
* SIGAR is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package org.hyperic.jni;
public class ArchNotSupportedException extends Exception {
public ArchNotSupportedException() {
super();
}
public ArchNotSupportedException(String msg) {
super(msg);
}
}
@@ -0,0 +1,69 @@
/*
* Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
* This file is part of SIGAR.
*
* SIGAR is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package org.hyperic.jni;
import java.io.File;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.Copy;
public class CopyDependsTask extends Copy {
private File depends;
protected void validateAttributes()
throws BuildException {
super.validateAttributes();
if (this.depends == null) {
throw new BuildException("missing depends attribute");
}
}
public void execute()
throws BuildException {
if (this.destFile.exists()) {
String state;
if (this.depends.lastModified() >
this.destFile.lastModified())
{
this.setOverwrite(true);
state = "out of date";
}
else {
state = "up to date";
}
log(this.destFile +
" " + state + " with " +
this.depends);
}
super.execute();
}
public File getDepends() {
return this.depends;
}
public void setDepends(String depends) {
this.depends = new File(depends);
}
}