KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > igfay > jfig > JFigLocator


1 package org.igfay.jfig;
2
3 import java.io.FileInputStream JavaDoc;
4 import java.io.FileNotFoundException JavaDoc;
5 import java.io.InputStream JavaDoc;
6
7 import org.apache.log4j.Logger;
8 import org.igfay.util.PropertyUtility;
9
10 /**
11  * @author conrad4
12  *
13  * Locate the intial Configuration file. Users may write their own
14  * implementation of JFigLocatorIF with their own scheme of finding the initial
15  * configuration file.
16  *
17  * Best method may be to subclass the JFig implementation. Then, initialize JFig
18  * via: getInstance(JFigLocatorIF), passing your implementation.
19  */

20 public class JFigLocator implements JFigLocatorIF {
21     private static Logger log = Logger.getLogger(JFigLocator.class);
22
23     protected String JavaDoc configFileName;
24
25     protected String JavaDoc configLocation;
26
27     protected String JavaDoc configDirectory = "";
28
29     public JFigLocator(String JavaDoc fileName) {
30         this.configFileName = fileName;
31     }
32
33     /**
34      * Return the config location; file or classpath If user specified
35      * LOCATION_PROPERTY on the command line, use that. Otherwise, default to
36      * CLASSPATH.
37      *
38      * @return String
39      */

40     public String JavaDoc 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     /**
50      * Return a default config fileName. The filename will be the project name
51      * and the machine name. This is used when no config. filename is passed as
52      * an argument.
53      */

54     public String JavaDoc getDefaultConfigFileName() {
55         return PropertyUtility.getHostName().toLowerCase() + "."+JFigConstants.XML_FILENAME_SUFFIX;
56     }
57
58     /**
59      * Return the config fileName. This will be the last file processed.
60      */

61     public String JavaDoc 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     /**
71      * Parse the directory name if one is specified.
72      * This is only done on the initial file name.
73      * That way, the initial directory name is saved for use on subsequent files.
74      */

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     /*
84      * (non-Javadoc)
85      *
86      * @see org.igfay.jfig.JFigLocatorIF#setConfigFileName(java.lang.String)
87      */

88     public void setConfigFileName(String JavaDoc value) {
89         this.configFileName = value;
90     }
91
92     /**
93      *
94      * @see org.igfay.jfig.JFigLocatorIF#setConfigLocation(java.lang.String)
95      */

96     public void setConfigLocation(String JavaDoc 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     /**
106      *
107      * @see org.igfay.jfig.JFigLocatorIF#setDefaultConfigFileName(java.lang.String)
108      */

109     public void setDefaultConfigFileName(String JavaDoc value) {
110         this.configFileName = value;
111
112     }
113
114     /**
115      * Return the config file as an InputStream
116      */

117     public InputStream JavaDoc getInputStream() throws JFigException {
118         InputStream JavaDoc inputStream = null;
119
120         if (isClasspath()) {
121             inputStream = getInputStreamForClasspath();
122         } else {
123             inputStream = getInputStreamForFile(inputStream);
124         }
125
126         return inputStream;
127     }
128
129     /**
130      * Return if we expect to find config file in the classpath
131      * @return
132      * @throws JFigException
133      */

134     private boolean isClasspath() throws JFigException {
135         return JFigConstants.CLASSPATH.equalsIgnoreCase(getConfigLocation());
136     }
137
138     /**
139      * Find the input stream in the file system.
140      *
141      * @param inputStream
142      * @return
143      * @throws JFigException
144      */

145     protected InputStream JavaDoc getInputStreamForFile(InputStream JavaDoc inputStream) throws JFigException {
146         try {
147             inputStream = new FileInputStream JavaDoc(getConfigFileName());
148         } catch (FileNotFoundException JavaDoc e) {
149             String JavaDoc msg = "FileNotFoundException for " + getConfigFileName();
150             log.debug(msg);
151             throw new JFigException(msg);
152         }
153         return inputStream;
154     }
155
156     /**
157      * Find the input stream in the classpath
158      * @return
159      * @throws JFigException
160      */

161     protected InputStream JavaDoc getInputStreamForClasspath() throws JFigException {
162         InputStream JavaDoc inputStream;
163         inputStream = JFig.class.getResourceAsStream("/" + getConfigFileName());
164         if (inputStream == null) {
165             String JavaDoc msg = "Resource not found in classpath: " + getConfigFileName();
166             log.debug(msg);
167             throw new JFigException(msg);
168         }
169         return inputStream;
170     }
171
172     /**
173      * Return the config directory. This directory is parsed from the initial
174      * config.name that is set. It is not changed by subsequent config file names.
175      * That enables subsequent file names to set their paths relative to the initial.
176      *
177      */

178     public String JavaDoc getConfigDirectory() {
179         return configDirectory;
180     }
181
182     /**
183      * Set the configDirectory. Parsed from the initial config.file name
184      *
185      */

186     public void setConfigDirectory(String JavaDoc value) {
187         this.configDirectory = value;
188
189     }
190 }
191
Popular Tags