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