001    package org.util.environment;
002    
003    import java.io.*;
004    import java.net.*;
005    import javax.swing.*;
006    
007    public class Environment {
008    
009            private static boolean IS_LINUX, IS_WINDOWS, IS_MAC;
010            private static String OS_NAME;
011            private static boolean LAUNCHED_BY_JAR;
012            private static URL launched_url;
013            private static File save_dir_;
014            private static boolean initialized_;
015            private static int debug_level_ = 1;
016    
017            static {
018                    OS_NAME = System.getProperty("os.name");
019                    debug("OS:" + OS_NAME);
020                    IS_LINUX = OS_NAME.toLowerCase().startsWith("linux");
021                    IS_WINDOWS = OS_NAME.toLowerCase().startsWith("win");
022                    IS_MAC = OS_NAME.toLowerCase().startsWith("mac");
023            }
024            public static boolean isLinux() throws Exception {
025                    return IS_LINUX;
026            }
027            public static boolean isWindows() throws Exception {
028                    return IS_WINDOWS;
029            }
030            public static boolean isMac() throws Exception {
031                    return IS_MAC;
032            }
033            public static boolean isInitialized() {
034                    return initialized_;
035            }
036    
037            public static void initialize() throws Exception {
038                    String sample = "org/util/environment/Environment.class";
039                                    
040                    System.out.println("Gathering Enviroment Information.");
041    
042                    // jar:file:[jar file path]!/org/environment/Environment.class
043                    URL class_url = ClassLoader.getSystemResource(sample);
044                    debug("org/environment/Environment.class url:["+class_url+"]");
045    
046                    LAUNCHED_BY_JAR = class_url.toString().startsWith("jar:");
047                    if(LAUNCHED_BY_JAR) {
048                            String tmp = class_url.toString().replaceAll("jar:", "");
049                            int index = tmp.indexOf("!");
050                            tmp = tmp.substring(0, index);
051                            URL tmp_url = new URL(tmp);
052                            save_dir_ = new File(tmp_url.toURI()).getParentFile();
053                    } else {
054                            save_dir_ = new File(".").getCanonicalFile();
055                    }
056                    debug(save_dir_);
057                    initialized_ = true;
058            }
059            
060            public static URL getURL(String path) {
061                    return ClassLoader.getSystemResource(path);
062            }
063            
064            public static File getSaveDirectory() {
065                    return save_dir_;
066            }
067            
068            private static void debug(Object msg) {
069                    if(Environment.getDebugLevel() != 0)
070                            System.out.println(msg);
071            }
072    
073            public static int getDebugLevel() {
074                    return debug_level_;
075            }
076            public static void setDebugLevel(int debug_level) {
077                    debug_level_ = debug_level;
078            }
079    }