001    /*
002     * Copyright 2001,2004 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 org.apache.commons.scaffold.sql;
018    
019    
020    import java.util.Properties;
021    import org.apache.commons.scaffold.lang.PropertiesException;
022    import org.apache.commons.scaffold.util.ResourceUtils;
023    
024    
025    /**
026     * @author Ted Husted
027     * @author OK State DEQ
028     * @version $Revision: 155464 $ $Date: 2005-02-26 13:26:54 +0000 (Sat, 26 Feb 2005) $
029     */
030    public class CommandStore {
031    
032    
033        /**
034         * The path to a default properties file if not given in the constructor
035         * ["resources/command.properties"].
036         */
037        private static String COMMAND_STORE_PATH = "resources/command.properties";
038    
039    
040        private static String DOT = ".";
041    
042    
043        /**
044         * Our Properties field.
045         * @todo refactor to use Collection based on preface.
046         */
047        protected static Properties properties;
048    
049    
050        public static Properties getProperties(String preface) {
051    
052            if (null==properties) {
053                try {
054                    properties = ResourceUtils.loadProperties(COMMAND_STORE_PATH);
055                }
056                catch (Exception e) {
057                     throw new IllegalArgumentException();
058                }
059            }
060            if (null==properties) throw new IllegalArgumentException();
061    
062            return properties;
063        }
064    
065    
066        /**
067         * Retrieve command from Properties store for <code>preface</code>
068         * and <code>key</code>.
069         *
070         * @fixme Could use a merge feature to reuse common substrings,
071         * like table names ["SELECT ${searchFields} FROM ${articleTable}"]
072         * @todo Refactor to make appending preface.key optional
073         */
074        public static String getProperty(String preface, String key)
075            throws PropertiesException {
076    
077            if (null==properties) properties = getProperties(preface);
078    
079            // :TODO: refactor to make appending preface.key optional
080            StringBuffer sb = new StringBuffer(preface);
081            sb.append(DOT);
082            sb.append(key);
083            return properties.getProperty(sb.toString());
084        }
085    
086    
087        /**
088         * Constructor to pass a properties file.
089         * Store instance in application scope.
090         */
091        public CommandStore(Properties properties) {
092    
093            CommandStore.properties = properties;
094    
095        }
096    
097    }