| 1 package org.igfay.jfig; 2 3 import java.io.FileInputStream ; 4 import java.io.FileNotFoundException ; 5 import java.io.InputStream ; 6 7 import org.apache.log4j.Logger; 8 import org.igfay.util.PropertyUtility; 9 10 20 public class JFigLocator implements JFigLocatorIF { 21 private static Logger log = Logger.getLogger(JFigLocator.class); 22 23 protected String configFileName; 24 25 protected String configLocation; 26 27 protected String configDirectory = ""; 28 29 public JFigLocator(String fileName) { 30 this.configFileName = fileName; 31 } 32 33 40 public String getConfigLocation() throws JFigException { 41 if (configLocation == null) { 42 setConfigLocation(PropertyUtility.getProperty(JFigConstants.LOCATION_PROPERTY, JFigConstants.CLASSPATH)); 43 log.debug(configLocation); 44 } 45 46 return configLocation; 47 } 48 49 54 public String getDefaultConfigFileName() { 55 return PropertyUtility.getHostName().toLowerCase() + "."+JFigConstants.XML_FILENAME_SUFFIX; 56 } 57 58 61 public String getConfigFileName() { 62 if (configFileName == null) { 63 this.configFileName = PropertyUtility.getProperty(JFigConstants.FILENAME_PROPERTY, 64 getDefaultConfigFileName()); 65 parseInitialFileName(); 66 } 67 return getConfigDirectory() + this.configFileName; 68 } 69 70 75 protected void parseInitialFileName() { 76 int lastIndex = Math.max(configFileName.lastIndexOf("/"), configFileName.lastIndexOf("\\")); 77 if (lastIndex > 0) { 78 setConfigDirectory(configFileName.substring(0, lastIndex + 1)); 79 this.configFileName = configFileName.substring(lastIndex + 1); 80 } 81 } 82 83 88 public void setConfigFileName(String value) { 89 this.configFileName = value; 90 } 91 92 96 public void setConfigLocation(String value) throws JFigException { 97 if ((!JFigConstants.FILE.equalsIgnoreCase(value)) && (!JFigConstants.CLASSPATH.equalsIgnoreCase(value))) { 98 throw new JFigException("{"+value+"}"+" Invalid entry for " + JFigConstants.LOCATION_PROPERTY + ". Valid entries are " 99 + JFigConstants.CLASSPATH + " or " + JFigConstants.FILE); 100 } 101 this.configLocation = value; 102 103 } 104 105 109 public void setDefaultConfigFileName(String value) { 110 this.configFileName = value; 111 112 } 113 114 117 public InputStream getInputStream() throws JFigException { 118 InputStream inputStream = null; 119 120 if (isClasspath()) { 121 inputStream = getInputStreamForClasspath(); 122 } else { 123 inputStream = getInputStreamForFile(inputStream); 124 } 125 126 return inputStream; 127 } 128 129 134 private boolean isClasspath() throws JFigException { 135 return JFigConstants.CLASSPATH.equalsIgnoreCase(getConfigLocation()); 136 } 137 138 145 protected InputStream getInputStreamForFile(InputStream inputStream) throws JFigException { 146 try { 147 inputStream = new FileInputStream (getConfigFileName()); 148 } catch (FileNotFoundException e) { 149 String msg = "FileNotFoundException for " + getConfigFileName(); 150 log.debug(msg); 151 throw new JFigException(msg); 152 } 153 return inputStream; 154 } 155 156 161 protected InputStream getInputStreamForClasspath() throws JFigException { 162 InputStream inputStream; 163 inputStream = JFig.class.getResourceAsStream("/" + getConfigFileName()); 164 if (inputStream == null) { 165 String msg = "Resource not found in classpath: " + getConfigFileName(); 166 log.debug(msg); 167 throw new JFigException(msg); 168 } 169 return inputStream; 170 } 171 172 178 public String getConfigDirectory() { 179 return configDirectory; 180 } 181 182 186 public void setConfigDirectory(String value) { 187 this.configDirectory = value; 188 189 } 190 } 191 | Popular Tags |