QueryLoader.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.dbutils;

  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import java.util.Properties;
  23. import java.util.regex.Pattern;

  24. /**
  25.  * {@code QueryLoader} is a registry for sets of queries so
  26.  * that multiple copies of the same queries aren't loaded into memory.
  27.  * This implementation loads properties files filled with query name to
  28.  * SQL mappings.  This class is thread safe.
  29.  */
  30. public class QueryLoader {

  31.     /**
  32.      * The Singleton INSTANCE of this class.
  33.      */
  34.     private static final QueryLoader INSTANCE = new QueryLoader();

  35.     /**
  36.      * Matches .xml file extensions.
  37.      */
  38.     private static final Pattern dotXml = Pattern.compile(".+\\.[xX][mM][lL]");

  39.     /**
  40.      * Return an INSTANCE of this class.
  41.      * @return The Singleton INSTANCE.
  42.      */
  43.     public static QueryLoader instance() {
  44.         return INSTANCE;
  45.     }

  46.     /**
  47.      * Maps query set names to Maps of their queries.
  48.      */
  49.     private final Map<String, Map<String, String>> queries = new HashMap<>();

  50.     /**
  51.      * QueryLoader constructor.
  52.      */
  53.     protected QueryLoader() {
  54.     }

  55.     /**
  56.      * Loads a Map of query names to SQL values.  The Maps are cached so a
  57.      * subsequent request to load queries from the same path will return
  58.      * the cached Map.  The properties file to load can be in either
  59.      * line-oriented or XML format.  XML formatted properties files must use a
  60.      * {@code .xml} file extension.
  61.      *
  62.      * @param path The path that the ClassLoader will use to find the file.
  63.      * This is <strong>not</strong> a file system path.  If you had a jarred
  64.      * Queries.properties file in the com.yourcorp.app.jdbc package you would
  65.      * pass "/com/yourcorp/app/jdbc/Queries.properties" to this method.
  66.      * @throws IOException if a file access error occurs
  67.      * @throws IllegalArgumentException if the ClassLoader can't find a file at
  68.      * the given path.
  69.      * @throws java.util.InvalidPropertiesFormatException if the XML properties file is
  70.      * invalid
  71.      * @return Map of query names to SQL values
  72.      * @see java.util.Properties
  73.      */
  74.     public synchronized Map<String, String> load(final String path) throws IOException {

  75.         Map<String, String> queryMap = this.queries.get(path);

  76.         if (queryMap == null) {
  77.             queryMap = this.loadQueries(path);
  78.             this.queries.put(path, queryMap);
  79.         }

  80.         return queryMap;
  81.     }

  82.     /**
  83.      * Loads a set of named queries into a Map object.  This implementation
  84.      * reads a properties file at the given path.  The properties file can be
  85.      * in either line-oriented or XML format.  XML formatted properties files
  86.      * must use a {@code .xml} file extension.

  87.      * @param path The path that the ClassLoader will use to find the file.
  88.      * @throws IOException if a file access error occurs
  89.      * @throws IllegalArgumentException if the ClassLoader can't find a file at
  90.      * the given path.
  91.      * @throws java.util.InvalidPropertiesFormatException if the XML properties file is
  92.      * invalid
  93.      * @since 1.1
  94.      * @return Map of query names to SQL values
  95.      * @see java.util.Properties
  96.      */
  97.     protected Map<String, String> loadQueries(final String path) throws IOException {
  98.         // Findbugs flags getClass().getResource as a bad practice; maybe we should change the API?
  99.         final Properties props;
  100.         try (InputStream in = getClass().getResourceAsStream(path)) {

  101.             if (in == null) {
  102.                 throw new IllegalArgumentException(path + " not found.");
  103.             }
  104.             props = new Properties();
  105.             if (dotXml.matcher(path).matches()) {
  106.                 props.loadFromXML(in);
  107.             } else {
  108.                 props.load(in);
  109.             }
  110.         }

  111.         // Copy to HashMap for better performance

  112.         @SuppressWarnings({"rawtypes", "unchecked" }) // load() always creates <String,String> entries
  113.         final HashMap<String, String> hashMap = new HashMap(props);
  114.         return hashMap;
  115.     }

  116.     /**
  117.      * Removes the queries for the given path from the cache.
  118.      * @param path The path that the queries were loaded from.
  119.      */
  120.     public synchronized void unload(final String path) {
  121.         this.queries.remove(path);
  122.     }

  123. }