001: /*
002: * Copyright 2003 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package velosurf;
018:
019: import velosurf.context.DBReference;
020: import velosurf.util.Logger;
021: import velosurf.util.XIncludeResolver;
022: import velosurf.sql.Database;
023:
024: import java.io.File;
025: import java.io.FileInputStream;
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.sql.SQLException;
029:
030: import org.apache.velocity.app.Velocity;
031:
032: /** <p>This class is the Velosurf main entry class if you do not use the toolbox.xml mechanism.
033: * Unless you specify the config file to use, it will successively search for a configuration file :</p>
034: * <ul>
035: * <li><p>from the java property "config.velosurf" (like in 'java -Dconfig.velosurf=/.../velosurf.xml ...' )</p>
036: * <li><p>from the Velocity property "config.velosurf" (entry of the velocity.properties file)</p>
037: * <li><p>as './velosurf.xml', './conf/velosurf.xml', './WEB-INF/velosurf.xml', './cfg/velosurf.xml'
038: * </ul>
039: *
040: * @author <a href="mailto:claude.brisson@gmail.com">Claude Brisson</a>
041: *
042: */
043:
044: public class Velosurf extends DBReference {
045: /** Initialization flag.
046: */
047: private boolean initialized = false;
048:
049: /** Configuration file.
050: */
051: private String configFile = null;
052:
053: /** Empty constructor.
054: */
055: public Velosurf() {
056: }
057:
058: /**
059: * Constructor taking a File object as configuration.
060: * @param configFile configuration file
061: * @throws IOException
062: * @throws SQLException
063: */
064: public Velosurf(File configFile) throws IOException, SQLException {
065: this (new FileInputStream(configFile));
066: }
067:
068: /**
069: * Constructor taking an InputStream object as configuration.
070: * @param config
071: * @throws IOException
072: * @throws SQLException
073: */
074: public Velosurf(InputStream config) throws IOException,
075: SQLException {
076: initLogging();
077: init(config);
078: }
079:
080: /**
081: * Explicitely set the configuration file.
082: * @param config configuration pathname
083: */
084: public void setConfigFile(String config) {
085: configFile = config;
086: }
087:
088: /**
089: * Initializes the logger.
090: */
091: private void initLogging() {
092: if (!Logger.isInitialized()) {
093: Logger.log2Stderr();
094: }
095: }
096:
097: /**
098: * Tries to find a configuration file using some default locations.
099: * @return the pathname of the configuration file, if found - null otherwise
100: */
101: private static String findConfig() {
102: String pathname = null;
103: File file = null;
104:
105: /* first, ask Java */
106: pathname = System.getProperty("velosurf.config");
107: if (pathname != null) {
108: file = new File(pathname);
109: if (file.exists())
110: return pathname;
111: }
112:
113: /* then Velocity */
114: pathname = (String) Velocity.getProperty("velosurf.config");
115: if (pathname != null) {
116: file = new File(pathname);
117: if (file.exists())
118: return pathname;
119: }
120:
121: /* then try some standard pathes */
122: String[] guesses = { "./velosurf.xml", "./conf/velosurf.xml",
123: "./WEB-INF/velosurf.xml", "./cfg/velosurf.xml",
124: "./model.xml", "./conf/model.xml",
125: "./WEB-INF/model.xml", "./cfg/model.xml" };
126: for (int i = 0; i < guesses.length; i++) {
127: file = new File(guesses[i]);
128: if (file.exists())
129: return guesses[i];
130: }
131:
132: return null;
133: }
134:
135: /**
136: * Lazzy initialization.
137: * @param config configuration file input stream
138: * @throws SQLException
139: * @throws IOException
140: */
141: private void init(InputStream config) throws SQLException,
142: IOException {
143: Database db = Database.getInstance(config);
144: super .init(db);
145: initialized = true;
146: }
147:
148: /**
149: * Lazzy initialization.
150: * @throws SQLException
151: * @throws IOException
152: */
153: private void init() throws SQLException, IOException {
154: if (configFile == null) {
155: configFile = findConfig();
156: }
157: if (configFile == null) {
158: throw new IOException(
159: "No Velosurf config file found. Please specify one using setConfig(pathname).");
160: }
161: /* calculate the base directory, for XInclude */
162: /* Velosurf won't like '/' in windows names neither '\' in linux ones... Does Java? */
163: String base = null;
164: configFile.replace('\\', '/');
165: int i = configFile.lastIndexOf('/');
166: if (i == -1) {
167: base = ".";
168: } else {
169: base = configFile.substring(0, i);
170: }
171: Database db = Database.getInstance(new FileInputStream(
172: configFile), new XIncludeResolver(base));
173: super .init(db);
174: initialized = true;
175: }
176:
177: /**
178: * Generic getter.
179: * @param key
180: * @return property
181: */
182: public Object get(Object key) {
183: if (!initialized) {
184: try {
185: init();
186: } catch (Exception e) {
187: Logger.log(e);
188: return null;
189: }
190: }
191: return super .get(key);
192: }
193:
194: /**
195: * Generic setter.
196: * @param key
197: * @param value
198: * @return old value
199: */
200: public Object put(String key, Object value) {
201: if (!initialized) {
202: try {
203: init();
204: } catch (Exception e) {
205: Logger.log(e);
206: return null;
207: }
208: }
209: return super .put(key, value);
210: }
211:
212: /**
213: * Schema name getter.
214: * @return the name of the current schema
215: */
216: public String getSchema() {
217: if (!initialized) {
218: try {
219: init();
220: } catch (Exception e) {
221: Logger.log(e);
222: return null;
223: }
224: }
225: return super .getSchema();
226: }
227:
228: /** Obfuscate the given value.
229: * @param value value to obfuscate
230: * @return obfuscated value
231: */
232: public String obfuscate(Object value) {
233: if (!initialized) {
234: try {
235: init();
236: } catch (Exception e) {
237: Logger.log(e);
238: return null;
239: }
240: }
241: return super .obfuscate(value);
242: }
243:
244: /** De-obfuscate the given value.
245: * @param value value to de-obfuscate
246: * @return obfuscated value
247: */
248: public String deobfuscate(Object value) {
249: if (!initialized) {
250: try {
251: init();
252: } catch (Exception e) {
253: Logger.log(e);
254: return null;
255: }
256: }
257: return super.deobfuscate(value);
258: }
259:
260: }
|