1 /* 2 * Copyright 2001,2004 The Apache Software Foundation. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.apache.commons.scaffold.sql; 18 19 20 import java.util.Properties; 21 import org.apache.commons.scaffold.lang.PropertiesException; 22 import org.apache.commons.scaffold.util.ResourceUtils; 23 24 25 /** 26 * @author Ted Husted 27 * @author OK State DEQ 28 * @version $Revision: 155464 $ $Date: 2005-02-26 13:26:54 +0000 (Sat, 26 Feb 2005) $ 29 */ 30 public class CommandStore { 31 32 33 /** 34 * The path to a default properties file if not given in the constructor 35 * ["resources/command.properties"]. 36 */ 37 private static String COMMAND_STORE_PATH = "resources/command.properties"; 38 39 40 private static String DOT = "."; 41 42 43 /** 44 * Our Properties field. 45 * @todo refactor to use Collection based on preface. 46 */ 47 protected static Properties properties; 48 49 50 public static Properties getProperties(String preface) { 51 52 if (null==properties) { 53 try { 54 properties = ResourceUtils.loadProperties(COMMAND_STORE_PATH); 55 } 56 catch (Exception e) { 57 throw new IllegalArgumentException(); 58 } 59 } 60 if (null==properties) throw new IllegalArgumentException(); 61 62 return properties; 63 } 64 65 66 /** 67 * Retrieve command from Properties store for <code>preface</code> 68 * and <code>key</code>. 69 * 70 * @fixme Could use a merge feature to reuse common substrings, 71 * like table names ["SELECT ${searchFields} FROM ${articleTable}"] 72 * @todo Refactor to make appending preface.key optional 73 */ 74 public static String getProperty(String preface, String key) 75 throws PropertiesException { 76 77 if (null==properties) properties = getProperties(preface); 78 79 // :TODO: refactor to make appending preface.key optional 80 StringBuffer sb = new StringBuffer(preface); 81 sb.append(DOT); 82 sb.append(key); 83 return properties.getProperty(sb.toString()); 84 } 85 86 87 /** 88 * Constructor to pass a properties file. 89 * Store instance in application scope. 90 */ 91 public CommandStore(Properties properties) { 92 93 CommandStore.properties = properties; 94 95 } 96 97 }