]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/LibraryPathUtils.java
Changing existing log4j logging to use slf4j
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / LibraryPathUtils.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.utils;\r
13 \r
14 import java.io.File;\r
15 import java.util.Properties;\r
16 \r
17 /**\r
18  * \r
19  * @author Toni Kalajainen\r
20  */\r
21 public class LibraryPathUtils {\r
22 \r
23     // The problem with JMF is that it is (should be)\r
24     // Located in Java CLASSPATH. For some reason eclipse java VM does not\r
25     // Look into CLASSPATH.\r
26     // The following code adds CLASSPATH to System enviroment java.library.path\r
27     public static void addCLASSPATHtoEclipseVariables() {\r
28         String CLASSPATH = System.getenv("CLASSPATH");\r
29         if (CLASSPATH != null)\r
30             for (String classPath : CLASSPATH.split(";")) {\r
31                 if (!classPath.toLowerCase().endsWith(".jar"))\r
32                     addLibraryPath(classPath);\r
33                 addClassPath(classPath);\r
34             }\r
35         CLASSPATH = System.getenv("PATH");\r
36         if (CLASSPATH != null)\r
37             for (String classPath : CLASSPATH.split(";")) {\r
38                 addLibraryPath(classPath);\r
39             }\r
40         // On linux\r
41         CLASSPATH = System.getenv("LD_LIBRARY_PATH");\r
42         if (CLASSPATH != null)\r
43             for (String classPath : CLASSPATH.split(";")) {\r
44                 addLibraryPath(classPath);\r
45             }\r
46 \r
47         // printEnvVariables();\r
48     }\r
49 \r
50     public static void addLibraryPath(String newLibrary) {\r
51         if (newLibrary == null || "".equals(newLibrary))\r
52             return;\r
53         String java_library_path = System.getProperty("java.library.path");\r
54         if (java_library_path != null) {\r
55             for (String library : java_library_path.split(";"))\r
56                 if (library.equals(newLibrary))\r
57                     return;\r
58         }\r
59 \r
60         // System.out.println("Adding path: " + newLibrary);\r
61 \r
62         if (java_library_path == null || "".equals(java_library_path))\r
63             java_library_path = newLibrary;\r
64         else {\r
65             if (!java_library_path.endsWith(";"))\r
66                 java_library_path += ";";\r
67             java_library_path += newLibrary + ";";\r
68         }\r
69 \r
70         System.setProperty("java.library.path", java_library_path);\r
71     }\r
72 \r
73     public static void addClassPath(String newLibrary) {\r
74         if (newLibrary == null || "".equals(newLibrary))\r
75             return;\r
76         String java_library_path = System.getProperty("java.class.path");\r
77         if (java_library_path != null) {\r
78             for (String library : java_library_path.split(";"))\r
79                 if (library.equals(newLibrary))\r
80                     return;\r
81         }\r
82 \r
83         // System.out.println("Adding cp: " + newLibrary);\r
84 \r
85         if (java_library_path == null || "".equals(java_library_path))\r
86             java_library_path = newLibrary;\r
87         else {\r
88             if (!java_library_path.endsWith(";"))\r
89                 java_library_path += ";";\r
90             java_library_path += newLibrary + ";";\r
91         }\r
92 \r
93         System.setProperty("java.class.path", java_library_path);\r
94     }\r
95 \r
96     public static void printEnvVariables() {\r
97         // Add env variable CLASSPATH\r
98         Properties props = System.getProperties();\r
99         for (Object key : props.keySet()) {\r
100             Object value = props.get(key);\r
101             System.out.println(key + "=" + value);\r
102         }\r
103 \r
104         System.out.println("\n\nEnv:");\r
105         for (Object key : System.getenv().keySet()) {\r
106             Object value = System.getenv().get(key);\r
107             System.out.println(key + "=" + value);\r
108         }\r
109     }\r
110 \r
111     /**\r
112      * Find location of a jar library\r
113      * \r
114      * @param library name of the library\r
115      * @return full location of the library\r
116      */\r
117     public static String findLibrary(String library) {\r
118         String CLASSPATH = System.getenv("CLASSPATH");\r
119 \r
120         if (CLASSPATH == null)\r
121             return null;\r
122         // 1st search case sensitive\r
123         for (String classPath : CLASSPATH.split(";"))\r
124             if (classPath.endsWith(library))\r
125                 return classPath;\r
126 \r
127         // Widen search for insensitive\r
128         for (String classPath : CLASSPATH.split(";"))\r
129             if (classPath.toLowerCase().endsWith(library.toLowerCase()))\r
130                 return classPath;\r
131         return null;\r
132     }       \r
133     \r
134     /**\r
135      * This method searches for CLASSPATH and converts libraries there into\r
136      * separate key,value pairs in system properties. The key will be \r
137      * name of the library and value will be it's location in file system.\r
138      *\r
139      * This method acts as a helper tool for loading external libraries into \r
140      * the visibility of bundle class loader.\r
141      * Call:\r
142      * LibraryPathUtils.createSystemPropertiesFromClassPathLibraries(); \r
143      * \r
144      * This call should be invoked at the very beginning of application. \r
145      * This way external libraries can by dynamicly loaded at runtime. \r
146      * The way to do this, is to add "external:$library.jar$" into plugin \r
147      * settings runtime/classpath, \r
148      * in Manifest.mf it is "Bundle-ClassPath: external:$library.jar$" \r
149      */\r
150     public static void createSystemPropertiesFromClassPathLibraries() {\r
151         String CLASSPATH = System.getenv("CLASSPATH");\r
152 \r
153         if (CLASSPATH == null)\r
154             return;\r
155         \r
156         // 1st search case sensitive\r
157         for (String classPath : CLASSPATH.split(";")) {\r
158             //System.out.println(classPath);\r
159             if (!classPath.toLowerCase().endsWith(".jar")) continue;\r
160             File lib = new File(classPath);\r
161             if (!lib.exists()) {\r
162                 //System.out.println("Warning library \""+classPath+"\" does not exist yet it exists in CLASSPATH.");\r
163                 continue;\r
164             } else {\r
165             }\r
166             String key = lib.getName();\r
167             String value = lib.getAbsolutePath();\r
168             System.setProperty(key, value);\r
169             //System.out.println(key+"="+value);\r
170         }\r
171     }\r
172 \r
173 }\r