org.hyperic.sigar.cmd.Runner Maven / Gradle / Ivy
/*
* Copyright (c) 2006-2007 Hyperic, Inc.
* Copyright (c) 2010 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hyperic.sigar.cmd;
import java.io.File;
import java.io.FileFilter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.net.URLClassLoader;
import java.net.URL;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarLoader;
public class Runner {
private static HashMap wantedJars = new HashMap();
private static final String JAR_EXT = ".jar";
static {
wantedJars.put("junit", Boolean.FALSE);
wantedJars.put("log4j", Boolean.FALSE);
}
private static void printMissingJars() {
for (Iterator it = wantedJars.entrySet().iterator();
it.hasNext();)
{
Map.Entry entry = (Map.Entry)it.next();
String jar = (String)entry.getKey();
if (wantedJars.get(jar) == Boolean.FALSE) {
System.out.println("Unable to locate: " + jar + JAR_EXT);
}
}
}
private static boolean missingJars() {
for (Iterator it = wantedJars.entrySet().iterator();
it.hasNext();)
{
Map.Entry entry = (Map.Entry)it.next();
String jar = (String)entry.getKey();
if (wantedJars.get(jar) == Boolean.FALSE) {
return true;
}
}
return false;
}
public static URL[] getLibJars(String dir) throws Exception {
File[] jars = new File(dir).listFiles(new FileFilter() {
public boolean accept(File file) {
String name = file.getName();
int jarIx = name.indexOf(JAR_EXT);
if (jarIx == -1) {
return false;
}
int ix = name.indexOf('-');
if (ix != -1) {
name = name.substring(0, ix); //versioned .jar
}
else {
name = name.substring(0, jarIx);
}
if (wantedJars.get(name) != null) {
wantedJars.put(name, Boolean.TRUE);
return true;
}
else {
return false;
}
}
});
if (jars == null) {
return new URL[0];
}
URL[] urls = new URL[jars.length];
for (int i=0; i