001: package org.igfay.jfig;
002:
003: import java.io.FileInputStream;
004: import java.io.FileNotFoundException;
005: import java.io.InputStream;
006:
007: import org.apache.log4j.Logger;
008: import org.igfay.util.PropertyUtility;
009:
010: /**
011: * @author conrad4
012: *
013: * Locate the intial Configuration file. Users may write their own
014: * implementation of JFigLocatorIF with their own scheme of finding the initial
015: * configuration file.
016: *
017: * Best method may be to subclass the JFig implementation. Then, initialize JFig
018: * via: getInstance(JFigLocatorIF), passing your implementation.
019: */
020: public class JFigLocator implements JFigLocatorIF {
021: private static Logger log = Logger.getLogger(JFigLocator.class);
022:
023: protected String configFileName;
024:
025: protected String configLocation;
026:
027: protected String configDirectory = "";
028:
029: public JFigLocator(String fileName) {
030: this .configFileName = fileName;
031: }
032:
033: /**
034: * Return the config location; file or classpath If user specified
035: * LOCATION_PROPERTY on the command line, use that. Otherwise, default to
036: * CLASSPATH.
037: *
038: * @return String
039: */
040: public String getConfigLocation() throws JFigException {
041: if (configLocation == null) {
042: setConfigLocation(PropertyUtility.getProperty(
043: JFigConstants.LOCATION_PROPERTY,
044: JFigConstants.CLASSPATH));
045: log.debug(configLocation);
046: }
047:
048: return configLocation;
049: }
050:
051: /**
052: * Return a default config fileName. The filename will be the project name
053: * and the machine name. This is used when no config. filename is passed as
054: * an argument.
055: */
056: public String getDefaultConfigFileName() {
057: return PropertyUtility.getHostName().toLowerCase() + "."
058: + JFigConstants.XML_FILENAME_SUFFIX;
059: }
060:
061: /**
062: * Return the config fileName. This will be the last file processed.
063: */
064: public String getConfigFileName() {
065: if (configFileName == null) {
066: this .configFileName = PropertyUtility.getProperty(
067: JFigConstants.FILENAME_PROPERTY,
068: getDefaultConfigFileName());
069: parseInitialFileName();
070: }
071: return getConfigDirectory() + this .configFileName;
072: }
073:
074: /**
075: * Parse the directory name if one is specified.
076: * This is only done on the initial file name.
077: * That way, the initial directory name is saved for use on subsequent files.
078: */
079: protected void parseInitialFileName() {
080: int lastIndex = Math.max(configFileName.lastIndexOf("/"),
081: configFileName.lastIndexOf("\\"));
082: if (lastIndex > 0) {
083: setConfigDirectory(configFileName.substring(0,
084: lastIndex + 1));
085: this .configFileName = configFileName
086: .substring(lastIndex + 1);
087: }
088: }
089:
090: /*
091: * (non-Javadoc)
092: *
093: * @see org.igfay.jfig.JFigLocatorIF#setConfigFileName(java.lang.String)
094: */
095: public void setConfigFileName(String value) {
096: this .configFileName = value;
097: }
098:
099: /**
100: *
101: * @see org.igfay.jfig.JFigLocatorIF#setConfigLocation(java.lang.String)
102: */
103: public void setConfigLocation(String value) throws JFigException {
104: if ((!JFigConstants.FILE.equalsIgnoreCase(value))
105: && (!JFigConstants.CLASSPATH.equalsIgnoreCase(value))) {
106: throw new JFigException("{" + value + "}"
107: + " Invalid entry for "
108: + JFigConstants.LOCATION_PROPERTY
109: + ". Valid entries are " + JFigConstants.CLASSPATH
110: + " or " + JFigConstants.FILE);
111: }
112: this .configLocation = value;
113:
114: }
115:
116: /**
117: *
118: * @see org.igfay.jfig.JFigLocatorIF#setDefaultConfigFileName(java.lang.String)
119: */
120: public void setDefaultConfigFileName(String value) {
121: this .configFileName = value;
122:
123: }
124:
125: /**
126: * Return the config file as an InputStream
127: */
128: public InputStream getInputStream() throws JFigException {
129: InputStream inputStream = null;
130:
131: if (isClasspath()) {
132: inputStream = getInputStreamForClasspath();
133: } else {
134: inputStream = getInputStreamForFile(inputStream);
135: }
136:
137: return inputStream;
138: }
139:
140: /**
141: * Return if we expect to find config file in the classpath
142: * @return
143: * @throws JFigException
144: */
145: private boolean isClasspath() throws JFigException {
146: return JFigConstants.CLASSPATH
147: .equalsIgnoreCase(getConfigLocation());
148: }
149:
150: /**
151: * Find the input stream in the file system.
152: *
153: * @param inputStream
154: * @return
155: * @throws JFigException
156: */
157: protected InputStream getInputStreamForFile(InputStream inputStream)
158: throws JFigException {
159: try {
160: inputStream = new FileInputStream(getConfigFileName());
161: } catch (FileNotFoundException e) {
162: String msg = "FileNotFoundException for "
163: + getConfigFileName();
164: log.debug(msg);
165: throw new JFigException(msg);
166: }
167: return inputStream;
168: }
169:
170: /**
171: * Find the input stream in the classpath
172: * @return
173: * @throws JFigException
174: */
175: protected InputStream getInputStreamForClasspath()
176: throws JFigException {
177: InputStream inputStream;
178: inputStream = JFig.class.getResourceAsStream("/"
179: + getConfigFileName());
180: if (inputStream == null) {
181: String msg = "Resource not found in classpath: "
182: + getConfigFileName();
183: log.debug(msg);
184: throw new JFigException(msg);
185: }
186: return inputStream;
187: }
188:
189: /**
190: * Return the config directory. This directory is parsed from the initial
191: * config.name that is set. It is not changed by subsequent config file names.
192: * That enables subsequent file names to set their paths relative to the initial.
193: *
194: */
195: public String getConfigDirectory() {
196: return configDirectory;
197: }
198:
199: /**
200: * Set the configDirectory. Parsed from the initial config.file name
201: *
202: */
203: public void setConfigDirectory(String value) {
204: this.configDirectory = value;
205:
206: }
207: }
|