1 4 package com.dappit.Dapper.parser; 5 6 import java.io.File ; 7 8 12 public class EnviromentController 13 { 14 15 20 public enum OS 21 { 22 WIN , LINUX , MACOSX , UNKNOWN 23 } 24 25 private static OS OperatingSystem = null; 26 27 public static String getOperatingSystemName() throws Exception { 28 switch (detectOperatingSystem()) 29 { 30 case WIN : 31 return "win"; 32 case LINUX : 33 return "lin"; 34 case MACOSX: 35 return "macosx"; 36 } 37 throw new Exception ("Could not detect Operating system."); 38 } 39 40 public static String getSharedLibraryExtension() throws Exception { 41 switch (detectOperatingSystem()) 42 { 43 case WIN : 44 return ".dll"; 45 case LINUX : 46 return ".so"; 47 case MACOSX: 48 return ".jnilib"; 49 } 50 throw new Exception ("Could not detect Operating system."); 51 } 52 53 public static OS detectOperatingSystem() 54 { 55 if (OperatingSystem != null) 56 return OperatingSystem; 57 String osName = System.getProperty("os.name"); 58 System.out.println("Operating system : " +osName); 59 60 if (osName.toLowerCase().contains("windows")){ 61 OperatingSystem = OS.WIN; 62 return OS.WIN; 63 } 64 else 65 if (osName.toLowerCase().contains("linux")){ 66 OperatingSystem = OS.LINUX; 67 return OS.LINUX; 68 } 69 else 70 if (osName.toLowerCase().contains("macosx") || (System.getProperty("mrj.version") != null)){ 71 OperatingSystem = OS.MACOSX; 72 return OS.MACOSX; 73 } 74 return OS.UNKNOWN; 75 } 76 77 public static native void setenv(String variableName , String value); 78 79 public static void addenv(String variableName , String valueToAdd) 80 { 81 String currentEnviromentVariable = System.getenv(variableName); 82 EnviromentController.setenv(variableName,currentEnviromentVariable + valueToAdd); 83 } 84 85 public static void addenv(String variableName , String valueToAdd , String seperatorIfNotEmpty){ 86 String currentEnviromentVariable = EnviromentController.getenv(variableName); 87 if (currentEnviromentVariable==null || currentEnviromentVariable.length()==0) 88 addenv(variableName, valueToAdd); 89 else 90 addenv(variableName, seperatorIfNotEmpty + valueToAdd); 91 } 92 93 public static native String getenv(String variableName); 94 95 public static void addLibraryDirectory(String directoryPath) throws Exception 96 { 97 switch (detectOperatingSystem()) 98 { 99 case WIN : 100 addenv("PATH" , directoryPath , File.pathSeparator); 101 break; 102 case LINUX : 103 addenv("LD_LIBRARY_PATH" , directoryPath , File.pathSeparator); 104 break; 105 case MACOSX: 106 addenv("DYLD_LIBRARY_PATH" , directoryPath , File.pathSeparator); 107 break; 108 case UNKNOWN: 109 throw new Exception ("Could not detect Operating system."); 110 } 111 } 112 113 public static void main(String [] args) throws Exception 114 { 115 System.load("C:\\dapper\\workspace\\Dapper\\java\\lib\\parser\\bin\\EnviromentController.dll"); 116 System.out.println(EnviromentController.detectOperatingSystem()); 117 System.out.println("BEFORE : PATH : " + EnviromentController.getenv("PATH")); 118 EnviromentController.addLibraryDirectory("C:\\dapper\\mozilla\\dist\\bin"); 119 System.out.println("AFTER :PATH : " + System.getenv("PATH")); 120 System.out.println("AFTER :PATH : " + EnviromentController.getenv("PATH")); 121 } 122 123 } 124 | Popular Tags |