001: /**
002: *
003: */package com.dappit.Dapper.parser;
004:
005: import java.io.File;
006:
007: /**
008: * @author Ohad Serfaty
009: *
010: */
011: public class EnviromentController {
012:
013: /**
014: * @author Ohad Serfaty
015: * An enumeration of the Operating system detected.
016: *
017: */
018: public enum OS {
019: WIN, LINUX, MACOSX, UNKNOWN
020: }
021:
022: private static OS OperatingSystem = null;
023:
024: public static String getOperatingSystemName() throws Exception {
025: switch (detectOperatingSystem()) {
026: case WIN:
027: return "win";
028: case LINUX:
029: return "lin";
030: case MACOSX:
031: return "macosx";
032: }
033: throw new Exception("Could not detect Operating system.");
034: }
035:
036: public static String getSharedLibraryExtension() throws Exception {
037: switch (detectOperatingSystem()) {
038: case WIN:
039: return ".dll";
040: case LINUX:
041: return ".so";
042: case MACOSX:
043: return ".jnilib";
044: }
045: throw new Exception("Could not detect Operating system.");
046: }
047:
048: public static OS detectOperatingSystem() {
049: if (OperatingSystem != null)
050: return OperatingSystem;
051: String osName = System.getProperty("os.name");
052: System.out.println("Operating system : " + osName);
053:
054: if (osName.toLowerCase().contains("windows")) {
055: OperatingSystem = OS.WIN;
056: return OS.WIN;
057: } else if (osName.toLowerCase().contains("linux")) {
058: OperatingSystem = OS.LINUX;
059: return OS.LINUX;
060: } else if (osName.toLowerCase().contains("macosx")
061: || (System.getProperty("mrj.version") != null)) {
062: OperatingSystem = OS.MACOSX;
063: return OS.MACOSX;
064: }
065: return OS.UNKNOWN;
066: }
067:
068: public static native void setenv(String variableName, String value);
069:
070: public static void addenv(String variableName, String valueToAdd) {
071: String currentEnviromentVariable = System.getenv(variableName);
072: EnviromentController.setenv(variableName,
073: currentEnviromentVariable + valueToAdd);
074: }
075:
076: public static void addenv(String variableName, String valueToAdd,
077: String seperatorIfNotEmpty) {
078: String currentEnviromentVariable = EnviromentController
079: .getenv(variableName);
080: if (currentEnviromentVariable == null
081: || currentEnviromentVariable.length() == 0)
082: addenv(variableName, valueToAdd);
083: else
084: addenv(variableName, seperatorIfNotEmpty + valueToAdd);
085: }
086:
087: public static native String getenv(String variableName);
088:
089: public static void addLibraryDirectory(String directoryPath)
090: throws Exception {
091: switch (detectOperatingSystem()) {
092: case WIN:
093: addenv("PATH", directoryPath, File.pathSeparator);
094: break;
095: case LINUX:
096: addenv("LD_LIBRARY_PATH", directoryPath, File.pathSeparator);
097: break;
098: case MACOSX:
099: addenv("DYLD_LIBRARY_PATH", directoryPath,
100: File.pathSeparator);
101: break;
102: case UNKNOWN:
103: throw new Exception("Could not detect Operating system.");
104: }
105: }
106:
107: public static void main(String[] args) throws Exception {
108: System
109: .load("C:\\dapper\\workspace\\Dapper\\java\\lib\\parser\\bin\\EnviromentController.dll");
110: System.out
111: .println(EnviromentController.detectOperatingSystem());
112: System.out.println("BEFORE : PATH : "
113: + EnviromentController.getenv("PATH"));
114: EnviromentController
115: .addLibraryDirectory("C:\\dapper\\mozilla\\dist\\bin");
116: System.out.println("AFTER :PATH : " + System.getenv("PATH"));
117: System.out.println("AFTER :PATH : "
118: + EnviromentController.getenv("PATH"));
119: }
120:
121: }
|