]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils/src/org/simantics/utils/LibraryPathUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / LibraryPathUtils.java
diff --git a/bundles/org.simantics.utils/src/org/simantics/utils/LibraryPathUtils.java b/bundles/org.simantics.utils/src/org/simantics/utils/LibraryPathUtils.java
new file mode 100644 (file)
index 0000000..4e68c68
--- /dev/null
@@ -0,0 +1,173 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.utils;\r
+\r
+import java.io.File;\r
+import java.util.Properties;\r
+\r
+/**\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class LibraryPathUtils {\r
+\r
+    // The problem with JMF is that it is (should be)\r
+    // Located in Java CLASSPATH. For some reason eclipse java VM does not\r
+    // Look into CLASSPATH.\r
+    // The following code adds CLASSPATH to System enviroment java.library.path\r
+    public static void addCLASSPATHtoEclipseVariables() {\r
+        String CLASSPATH = System.getenv("CLASSPATH");\r
+        if (CLASSPATH != null)\r
+            for (String classPath : CLASSPATH.split(";")) {\r
+                if (!classPath.toLowerCase().endsWith(".jar"))\r
+                    addLibraryPath(classPath);\r
+                addClassPath(classPath);\r
+            }\r
+        CLASSPATH = System.getenv("PATH");\r
+        if (CLASSPATH != null)\r
+            for (String classPath : CLASSPATH.split(";")) {\r
+                addLibraryPath(classPath);\r
+            }\r
+        // On linux\r
+        CLASSPATH = System.getenv("LD_LIBRARY_PATH");\r
+        if (CLASSPATH != null)\r
+            for (String classPath : CLASSPATH.split(";")) {\r
+                addLibraryPath(classPath);\r
+            }\r
+\r
+        // printEnvVariables();\r
+    }\r
+\r
+    public static void addLibraryPath(String newLibrary) {\r
+        if (newLibrary == null || "".equals(newLibrary))\r
+            return;\r
+        String java_library_path = System.getProperty("java.library.path");\r
+        if (java_library_path != null) {\r
+            for (String library : java_library_path.split(";"))\r
+                if (library.equals(newLibrary))\r
+                    return;\r
+        }\r
+\r
+        // System.out.println("Adding path: " + newLibrary);\r
+\r
+        if (java_library_path == null || "".equals(java_library_path))\r
+            java_library_path = newLibrary;\r
+        else {\r
+            if (!java_library_path.endsWith(";"))\r
+                java_library_path += ";";\r
+            java_library_path += newLibrary + ";";\r
+        }\r
+\r
+        System.setProperty("java.library.path", java_library_path);\r
+    }\r
+\r
+    public static void addClassPath(String newLibrary) {\r
+        if (newLibrary == null || "".equals(newLibrary))\r
+            return;\r
+        String java_library_path = System.getProperty("java.class.path");\r
+        if (java_library_path != null) {\r
+            for (String library : java_library_path.split(";"))\r
+                if (library.equals(newLibrary))\r
+                    return;\r
+        }\r
+\r
+        // System.out.println("Adding cp: " + newLibrary);\r
+\r
+        if (java_library_path == null || "".equals(java_library_path))\r
+            java_library_path = newLibrary;\r
+        else {\r
+            if (!java_library_path.endsWith(";"))\r
+                java_library_path += ";";\r
+            java_library_path += newLibrary + ";";\r
+        }\r
+\r
+        System.setProperty("java.class.path", java_library_path);\r
+    }\r
+\r
+    public static void printEnvVariables() {\r
+        // Add env variable CLASSPATH\r
+        Properties props = System.getProperties();\r
+        for (Object key : props.keySet()) {\r
+            Object value = props.get(key);\r
+            System.out.println(key + "=" + value);\r
+        }\r
+\r
+        System.out.println("\n\nEnv:");\r
+        for (Object key : System.getenv().keySet()) {\r
+            Object value = System.getenv().get(key);\r
+            System.out.println(key + "=" + value);\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Find location of a jar library\r
+     * \r
+     * @param library name of the library\r
+     * @return full location of the library\r
+     */\r
+    public static String findLibrary(String library) {\r
+        String CLASSPATH = System.getenv("CLASSPATH");\r
+\r
+        if (CLASSPATH == null)\r
+            return null;\r
+        // 1st search case sensitive\r
+        for (String classPath : CLASSPATH.split(";"))\r
+            if (classPath.endsWith(library))\r
+                return classPath;\r
+\r
+        // Widen search for insensitive\r
+        for (String classPath : CLASSPATH.split(";"))\r
+            if (classPath.toLowerCase().endsWith(library.toLowerCase()))\r
+                return classPath;\r
+        return null;\r
+    }       \r
+    \r
+    /**\r
+     * This method searches for CLASSPATH and converts libraries there into\r
+     * separate key,value pairs in system properties. The key will be \r
+     * name of the library and value will be it's location in file system.\r
+     *\r
+     * This method acts as a helper tool for loading external libraries into \r
+     * the visibility of bundle class loader.\r
+     * Call:\r
+     * LibraryPathUtils.createSystemPropertiesFromClassPathLibraries(); \r
+     * \r
+     * This call should be invoked at the very beginning of application. \r
+     * This way external libraries can by dynamicly loaded at runtime. \r
+     * The way to do this, is to add "external:$library.jar$" into plugin \r
+     * settings runtime/classpath, \r
+     * in Manifest.mf it is "Bundle-ClassPath: external:$library.jar$" \r
+     */\r
+    public static void createSystemPropertiesFromClassPathLibraries() {\r
+        String CLASSPATH = System.getenv("CLASSPATH");\r
+\r
+        if (CLASSPATH == null)\r
+            return;\r
+        \r
+        // 1st search case sensitive\r
+        for (String classPath : CLASSPATH.split(";")) {\r
+            //System.out.println(classPath);\r
+            if (!classPath.toLowerCase().endsWith(".jar")) continue;\r
+            File lib = new File(classPath);\r
+            if (!lib.exists()) {\r
+                //System.out.println("Warning library \""+classPath+"\" does not exist yet it exists in CLASSPATH.");\r
+                continue;\r
+            } else {\r
+            }\r
+            String key = lib.getName();\r
+            String value = lib.getAbsolutePath();\r
+            System.setProperty(key, value);\r
+            //System.out.println(key+"="+value);\r
+        }\r
+    }\r
+\r
+}\r