001 /** 002 * 003 */ 004 package org.wdssii.core; 005 006 import java.io.File; 007 import java.util.ArrayList; 008 import java.util.List; 009 010 import javax.xml.parsers.DocumentBuilder; 011 import javax.xml.parsers.DocumentBuilderFactory; 012 013 import org.apache.commons.logging.Log; 014 import org.apache.commons.logging.LogFactory; 015 import org.w3c.dom.Document; 016 import org.w3c.dom.Element; 017 018 /** 019 * This configuration class finds and uses the pertinent configuration 020 * information from the WDSS2/w2config directory. 021 * 022 * @author Lakshman 023 * @version $Id: W2Config.java,v 1.5 2007/10/11 15:23:01 lakshman Exp $ 024 */ 025 public class W2Config { 026 private static Log log = LogFactory.getLog(W2Config.class); 027 028 private static List<String> configDirectories = loadConfigDirectories(); 029 030 private static boolean addDir(String dir, List<String> configDirectories) { 031 if (dir != null) { 032 if (new File(dir).exists()) { 033 configDirectories.add(dir); 034 log.debug("Will search w2config directory: " + dir); 035 return true; 036 } else { 037 log.info("Ignoring " + dir + " -- not there"); 038 } 039 } 040 return false; 041 } 042 043 private static List<String> loadConfigDirectories() { 044 /* 045 * An environment variable W2_CONFIG_LOCATION $HOME $HOME/w2config, 046 * $HOME/WDSS2/w2/w2config, $HOME/WDSS2/w2config, etc. /etc/w2config 047 */ 048 List<String> configDirectories = new ArrayList<String>(); 049 log.debug("Looking for your w2config directories."); 050 String s = System.getenv("W2_CONFIG_LOCATION"); 051 if ( s != null ){ 052 String[] locations = s.split(":"); 053 for (String location : locations){ 054 addDir(location, configDirectories); 055 } 056 } 057 s = System.getProperty("user.home"); 058 if (addDir(s, configDirectories)) { 059 addDir(s + "/w2config", configDirectories); 060 addDir(s + "/WDSS2/w2config", configDirectories); 061 addDir(s + "/WDSS2/src/w2/w2config", configDirectories); 062 } 063 addDir("/etc/w2config", configDirectories); 064 return configDirectories; 065 } 066 067 /** 068 * will search the config directories for the first file that matches the 069 * given filename. can return null. 070 * 071 * For example, passing colormaps/Reflectivity, you will get a File 072 * corresponding to /etc/w2config/colormaps/Reflectivity 073 */ 074 public static File getFile(String filename) throws ConfigurationException { 075 for (String configDir : configDirectories) { 076 String s = configDir + "/" + filename; 077 File f = new File(s); 078 if (f.exists()) { 079 if (log.isInfoEnabled()) { 080 log.info("Using config file " + s); 081 } 082 return f; 083 } 084 } 085 String error = "Could not find config file " + filename; 086 log.error(error); 087 throw new ConfigurationException(error); 088 } 089 090 /** 091 * Parse and return the XML Dom element corresponding to a partial file 092 * name. 093 * 094 * For example, passing colormaps/Reflectivity, you may get the DOM element 095 * from /etc/w2config/colormaps/Reflectivity 096 */ 097 public static Element getFileElement(String filename) 098 throws ConfigurationException { 099 File f = getFile(filename); 100 try { 101 DocumentBuilder parser = DocumentBuilderFactory.newInstance() 102 .newDocumentBuilder(); 103 Document doc = parser.parse(f); 104 return doc.getDocumentElement(); 105 } catch (Exception e) { 106 throw new ConfigurationException(e); 107 } 108 } 109 }