View Javadoc

1   //
2   // Copyright (c) 2004, International Decision Systems
3   // all rights reserved
4   /*
5    * Copyright (c) 2004 International Decision Systems, Inc.  All Rights Reserved.
6    *
7    * By using this Software, You acknowledge that the Software is a valuable asset
8    * and trade secret of either International Decision Systems, Inc. ("IDSI") or a
9    * third party supplier of IDSI and constitutes confidential and proprietary
10   * information.
11   *
12   * NEITHER IDSI NOR ANY AGENT OR PERSON ACTING FOR OR WITH IDSI HAS MADE OR DOES
13   * MAKE ANY STATEMENTS, AFFIRMATIONS, REPRESENTATIONS OR WARRANTIES WHATSOEVER
14   * TO YOU, WHETHER EXPRESS OR IMPLIED, AS TO THE SOFTWARE, THE QUALITY OR
15   * CONDITION OF THE SOFTWARE, OR THE OPERATING CHARACTERISTICS OR RELIABILITY OF
16   * THE SOFTWARE, OR ITS SUITABILITY FOR ANY GENERAL OR PARTICULAR PURPOSE, OR AS
17   * TO ANY OTHER MATTER WHATSOEVER; ANY AND ALL OTHER WARRANTIES INCLUDING
18   * WITHOUT LIMITATION ANY WARRANTIES IMPLIED BY LAW, SUCH AS THE IMPLIED
19   * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND TITLE,
20   * USE AND NON-INFRINGEMENT; ARE HEREBY EXPRESSLY DISCLAIMED AND EXCLUDED.
21  */
22  package net.sourceforge.addam.ddlrun.utils;
23  
24  import java.io.BufferedReader;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.InputStreamReader;
28  import java.util.MissingResourceException;
29  
30  /**
31   * Used to read resources from within the local classloader.
32   *
33   * @author TIM3
34   * @since Mar 5, 2005
35   */
36  public class ClassLoaderReader implements ResourceReader {
37  
38      /**
39       * Allows only the ClassLoaderReaderFactory to construct a ClassLoaderReader.
40       *
41       * @param resourcePath the path to prepend when locating a resource
42       * @param resourceName the name of the resource to locate
43       */
44      ClassLoaderReader(String resourcePath, String resourceName) {
45          if (!resourcePath.endsWith(SEPARATOR) && resourcePath.length() > 0) {
46              resourcePath += SEPARATOR;
47          }
48          this.resourcePath = resourcePath;
49          this.resourceName = resourceName;
50          this.completeName = resourcePath + resourceName;
51      }
52  
53      public String readLine() throws IOException {
54          return this.getReader().readLine();
55      }
56  
57      private BufferedReader getReader() {
58          if (reader == null) {
59              InputStream resourceStream = getClass().getClassLoader().getResourceAsStream(completeName);
60              if (resourceStream == null) {
61                  throw new MissingResourceException("ClassLoader unable to locate resource " + completeName,
62                          ClassLoaderReader.class.getName(), resourceName);
63              }
64              reader = new BufferedReader(new InputStreamReader(resourceStream));
65          }
66          return reader;
67      }
68  
69      private BufferedReader reader;
70      final String resourcePath; // not private in order to facilitate testing
71      final String resourceName; // not private in order to facilitate testing
72      final String completeName; // not private in order to facilitate testing
73      private static final String SEPARATOR = "/";
74  }