litesoft
@ 947
litesoft / trunk / Java / core / Server / src / org / litesoft / configuration / ServerConfiguration.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.configuration; import java.io.*; public final class ServerConfiguration extends Configuration { // directory for all suplemental config files public static final String CONFIG_FILES_DIR = "configFilesDir"; private static final String DEFAULT_CONFIG_SUB_DIR = ConfigAccessorLocator.CURRENT_DIR; public ServerConfiguration( ConfigDataAccessorFactory pFactory ) { super( pFactory ); } /** * this defaults to the same dir as the primary config file. may also be a path relative to that file * * @see #configFilesSubDirectoryByKey * @see #configFilesSubDirectoryByName */ public static File configFilesDirectory() { String configPath = getString( CONFIG_FILES_DIR, DEFAULT_CONFIG_SUB_DIR ); if ( ConfigAccessorLocator.CURRENT_DIR.equals( configPath ) ) { return new File( getConfigDataAccessor().loadedFromDirectory() ); } File dir = new File( configPath ); return dir.isAbsolute() ? dir : new File( getConfigDataAccessor().loadedFromDirectory(), configPath ); } /** * get handle to the subdir or file of/in the primary config dir * after looking up the named subduir in the root config file * * @see #configFilesDirectory * @see #configFilesSubDirectoryByName */ public static File configFilesSubDirectoryByKey( String subDirKey, String defaultValue ) { String zSubDirName = getString( subDirKey, defaultValue ); return null == zSubDirName ? null : configFilesSubDirectoryByName( zSubDirName ); } /** * get handle to the named subdir or file of/in the primary config dir * * @see #configFilesSubDirectoryByKey * @see #configFilesDirectory */ public static File configFilesSubDirectoryByName( String pSubDirName ) { File zF = new File( pSubDirName ); if ( zF.isAbsolute() ) { return zF; } File configDir = configFilesDirectory(); return new File( configDir, pSubDirName ); } /** * This gets a path relative to the root config file * also see configFilesDirectory, which gets files relative to a named config file dir, which * might not be relative to this directory * * @param pKey not null or empty * * @return pDefault trimmed value associated with key is empty, * otherwise if the value starts with a file seperator it is absolute, * otherwise it is relative to the loadedFromDirectory(). * * @see #configFilesDirectory() * @see #configFilesSubDirectoryByKey(String, String) * @see #configFilesSubDirectoryByName(String) */ public static String getPath( String pKey, String pDefault ) { String value = getStringTrimmed( pKey ); if ( value.length() == 0 ) { if ( null == (value = pDefault) ) { return null; } } value = value.replace( '\\', '/' ); File dir = new File( value ); if ( !dir.isAbsolute() ) { dir = new File( getConfigDataAccessor().loadedFromDirectory(), value ); } return dir.getAbsolutePath(); } } |