You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DaoClasspathStatementProvider.java 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright 2014 James Moger.
  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. package com.iciql;
  17. import com.iciql.Iciql.Mode;
  18. import java.io.InputStream;
  19. import java.util.Properties;
  20. /**
  21. * Loads DAO statements from Properties resource files the classpath.
  22. *
  23. * @author James Moger
  24. */
  25. public class DaoClasspathStatementProvider implements DaoStatementProvider {
  26. private final Properties externalStatements;
  27. public DaoClasspathStatementProvider() {
  28. externalStatements = load();
  29. }
  30. /**
  31. * Returns the list of statement resources to try locating.
  32. *
  33. * @return
  34. */
  35. protected String[] getStatementResources() {
  36. return new String[]{"/iciql.properties", "/iciql.xml", "/conf/iciql.properties", "/conf/iciql.xml"};
  37. }
  38. /**
  39. * Loads the first statement resource found on the classpath.
  40. *
  41. * @return the loaded statements
  42. */
  43. private Properties load() {
  44. Properties props = new Properties();
  45. for (String resource : getStatementResources()) {
  46. InputStream is = null;
  47. try {
  48. is = DaoProxy.class.getResourceAsStream(resource);
  49. if (is != null) {
  50. if (resource.toLowerCase().endsWith(".xml")) {
  51. // load an .XML statements file
  52. props.loadFromXML(is);
  53. } else {
  54. // load a .Properties statements file
  55. props.load(is);
  56. }
  57. break;
  58. }
  59. } catch (Exception e) {
  60. throw new IciqlException(e, "Failed to parse {0}", resource);
  61. } finally {
  62. try {
  63. is.close();
  64. } catch (Exception e) {
  65. }
  66. }
  67. }
  68. return props;
  69. }
  70. @Override
  71. public String getStatement(String idOrStatement, Mode mode) {
  72. final String modePrefix = "%" + mode.name().toLowerCase() + ".";
  73. String value = externalStatements.getProperty(idOrStatement, idOrStatement);
  74. value = externalStatements.getProperty(modePrefix + idOrStatement, value);
  75. return value;
  76. }
  77. }