コード例 #1
0
ファイル: GLBootstrap.java プロジェクト: jjzazuet/jgl
public class GLBootstrap implements LoaderAction {

  private Map<String, String> platformNativeIndex = new HashMap<String, String>();

  public static final String JAVA_TMP_DIR = "java.io.tmpdir";
  public static final String JAVA_CLASSPATH = "java.class.path";
  public static final String JAVA_SEPARATOR = "path.separator";
  public static final String JAVA_META_INF = "META-INF";
  public static final String NATIVES = "natives";
  public static final String JAWT = "jawt";
  private static final Logger log = LoggerFactory.getLogger(GLBootstrap.class);

  public GLBootstrap() throws Exception {

    System.setProperty("jogamp.gluegen.UseTempJarCache", "false");

    log.info(
        "Initializing native JOGL jar dependencies for platform [{}]. Temp jar cache disabled.",
        PlatformPropsImpl.os_and_arch);

    String nativeJarName = String.format("%s-%s", NATIVES, PlatformPropsImpl.os_and_arch);
    String[] classpathEntries =
        System.getProperty(JAVA_CLASSPATH).split(System.getProperty(JAVA_SEPARATOR));

    for (String jarPath : classpathEntries) {

      if (jarPath.contains(nativeJarName)) {

        if (log.isDebugEnabled()) {
          log.debug("Applicable platform jar: [{}]", jarPath);
        }

        JarFile jf = new JarFile(jarPath);

        try {
          Enumeration<JarEntry> jarEntries = jf.entries();

          while (jarEntries.hasMoreElements()) {

            JarEntry je = jarEntries.nextElement();

            if (!je.isDirectory() && !je.getName().contains(JAVA_META_INF)) {
              if (log.isDebugEnabled()) {
                log.debug("Mapping jar entry [{}] -> [{}]", je.getName(), jarPath);
              }
              if (log.isDebugEnabled() && platformNativeIndex.containsKey(je.getName())) {
                log.debug("Duplicate jar entry: [{}]", je.getName());
                log.debug("Mapped at: [{}]", platformNativeIndex.get(je.getName()));
                log.debug("Also at: [{}]", jarPath);
              }
              platformNativeIndex.put(je.getName(), jarPath);
            }
          }
        } finally {
          closeJar(jf);
        }
      }
    }
  }

  @Override
  public boolean loadLibrary(String libname, boolean ignoreError, ClassLoader cl) {
    try {
      for (Entry<String, String> nativeEntry : platformNativeIndex.entrySet()) {
        if (nativeEntry.getKey().contains(libname)) {
          if (log.isDebugEnabled()) {
            log.debug(
                "Loading mapped entry: [{}] [{}] [{}]",
                libname,
                nativeEntry.getKey(),
                nativeEntry.getValue());
          }
          File nativeLibCopy =
              extractJarEntry(
                  nativeEntry.getValue(),
                  nativeEntry.getKey(),
                  System.getProperty(JAVA_TMP_DIR),
                  String.format("%s.jni", libname));
          System.load(nativeLibCopy.getAbsolutePath());
          return true;
        }
      }
    } catch (Exception e) {
      log.error("Unable to load native library [{}] - {}", libname, e);
    }

    if (log.isDebugEnabled()) {
      log.debug("No mapped library match for [{}]", libname);
    }
    return false;
  }

  @Override
  public void loadLibrary(
      String libname, String[] preload, boolean preloadIgnoreError, ClassLoader cl) {

    if (JAWT.compareTo(libname) == 0) {
      Runtime.getRuntime().loadLibrary("jawt");
      return;
    }

    if (null != preload) {
      for (int i = 0; i < preload.length; i++) {
        loadLibrary(preload[i], preloadIgnoreError, cl);
      }
    }
    loadLibrary(libname, false, cl);
  }
}
コード例 #2
0
public class VersionSupplier implements JmsHeaderSupplier {
  private static final String MANIFEST_PATH = "/META-INF/MANIFEST.MF";
  Logger log = LoggerFactory.getLogger(VersionSupplier.class);

  @Override
  public void addTo(Message message, Class<?> api, Object pojo) throws JMSException {
    log.trace("applyTo api={}, pojo={}", api, pojo);
    String version = getVersion(api);
    if (version != null) {
      message.setStringProperty("VERSION", version);
    }
  }

  public String getVersion(Class<?> api) {
    log.trace("getVersion for {}", api);
    Package pkg = api.getPackage();
    log.trace("    -> {}/{}", pkg.getImplementationVersion(), pkg.getSpecificationVersion());
    String version = pkg.getSpecificationVersion();
    if (version == null) version = pkg.getImplementationVersion();
    if (version == null) {
      try {
        version = readVersionFromManifest(api);
      } catch (IOException e) {
        log.error("Could not extract version for " + api, e);
        return null;
      }
    }
    return version;
  }

  private String readVersionFromManifest(Class<?> api) throws IOException {
    String className = api.getSimpleName() + ".class";
    URL resource = api.getResource(className);
    log.trace("get resource for {} -> {} -> {}", new Object[] {className, pathOf(api), resource});
    if (resource == null) return null;
    String version = null;
    String classPath = resource.toString();
    if (classPath.startsWith("jar:"))
      version = version(classPath.substring(0, classPath.lastIndexOf("!") + 1));
    if (version == null && classPath.endsWith(pathOf(api) + ".class"))
      version = version(classPath.substring(0, classPath.length() - pathOf(api).length() - 7));
    if (version == null && classPath.contains("/WEB-INF/"))
      version = version(classPath.substring(0, classPath.lastIndexOf("/WEB-INF/")));
    log.debug("version {} for {} from {}.", version, api, classPath);
    return version;
  }

  private String pathOf(Class<?> api) {
    return api.getCanonicalName().replace('.', '/');
  }

  private String version(String jarPath) throws IOException {
    Attributes attributes = getManifestAttributes(jarPath);
    if (attributes == null) {
      log.trace("no main attributes in manifest {}", jarPath);
      return null;
    }
    String version = attributes.getValue(Attributes.Name.SPECIFICATION_VERSION);
    log.trace("found specification version {}", version);
    return version;
  }

  private Attributes getManifestAttributes(String jarPath) throws IOException {
    String manifestPath = jarPath + MANIFEST_PATH;
    try {
      log.trace("manifest path: {}", manifestPath);
      Manifest manifest = new Manifest(new URL(manifestPath).openStream());
      log.trace("found entries: {}", manifest.getEntries().keySet());
      return manifest.getMainAttributes();
    } catch (FileNotFoundException e) {
      log.trace("No manifest found at {}", manifestPath);
      return null;
    }
  }
}