All pastes #972309 Raw Edit

Anonymous

public java v1 · immutable
·published 2008-04-05 10:24 UTC
rendered paste body
package appproperties;import java.io.File;import java.net.URI;import java.net.URISyntaxException;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * Singleton that will contain the real application root so that * it can contain code to load properties files or be subclassed * to be used as an application wide configuration holder.<br /> *  * <pre> *  class AppRoot { *      public static void main(String... args) { *  *          AppProperties props = new AppProperties(); *  *          String applicationRoot = null; *          applicationRoot = props.getApplicationRoot(); *       *          System.out.println(applicationRoot); *      } *  } * </pre> *  * @author noname */public class AppProperties {    private static final String CLASS_NAME = "AppProperties.class";    private static final Class CLASS_REF = AppProperties.class;    private static AppProperties me;    private String applicationRoot;    private AppProperties() {    }    /**     * Get instance     * @return instance     */    public static AppProperties getInstance() {        if (me == null) {            me = new AppProperties();            me.setApplicationRoot(me.parsePath());        }        return me;    }    public String getApplicationRoot() {        return applicationRoot;    }    public void setApplicationRoot(String applicationRoot) {        this.applicationRoot = applicationRoot;    }    /**     * Get the real location on dist where this application     * is installed.     *      * @return     */    private String parsePath() {        String path = null;        String thisPackage = null;        /**         * Get packag name, if it's directly in the root         * of the classpath then it's empty         */        try {            thisPackage = File.separator + CLASS_REF.getPackage().getName() + File.separator;        } catch (NullPointerException npe) {            // No packag            thisPackage = "";        }        /**         * Get unparsed path         */        try {            // If it is being run from the class directly            path = "file:" + new File(new URI(CLASS_REF.getResource(CLASS_NAME).toString())).toString();        } catch (Exception ex) {            try {                // if it is begin run from a jar                path = new URI(CLASS_REF.getResource(CLASS_NAME).toString()).toString();            } catch (URISyntaxException ex1) {                throw new RuntimeException("Failed to get application root.\nApplication may not work correctly.");            }        }        /**         * Parse path         */        int start = 0;        int end = 0;        if (path.startsWith("file:")) {            start = 5;            end = path.indexOf(File.separator + "build" + File.separator + "classes" + thisPackage);        } else if (path.startsWith("jar:file:")) {            start = 9;            Pattern p = Pattern.compile("([^" + File.separator + ".]+\\.jar!)");            Matcher m = p.matcher(path);            while (m.find()) {                end = m.start();            }        }        // real path        path = path.substring(start, end);        return path;    }    @Override    protected Object clone() throws CloneNotSupportedException {        throw new CloneNotSupportedException();    }}