View Javadoc

1   /*
2    * Copyright 2008-2013 smartics, Kronseder & Reiner GmbH
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 de.smartics.util.test.properties;
17  
18  import java.io.BufferedInputStream;
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.util.Properties;
22  
23  import org.apache.commons.io.IOUtils;
24  
25  /**
26   * A mapping from project-defined keys to values dependent on project
27   * properties.
28   * <p>
29   * This can be used in a <a href="http://maven.apache.org/">Maven</a>
30   * environment to derive GAV strings from POM dependencies, where the version
31   * numbers are defined as properties.
32   * </p>
33   * <p>
34   * Place a file named
35   * {@value de.smartics.confinale.test.ProjectProperties#DEFAULT_RESOURCE} the
36   * root folder of the class path and provide a mapping from free-to-choose keys
37   * to GAV strings like this:
38   * </p>
39   *
40   * <pre>
41   * smartics-properties-core=de.smartics.properties:smartics-properties-core:${smartics-properties-core.version}
42   * </pre>
43   * <p>
44   * Using filtering to resolve the properties like
45   * <code>smartics-properties-core.version</code>.
46   * </p>
47   *
48   * <pre>
49   * {
50   *   &#064;source
51   *   protected static final ProjectProperties PROJECT_PROPERTIES =
52   *       new ProjectProperties(MyClass.class);
53   * }
54   * </pre>
55   * <p>
56   * Then you can fetch GAV strings resolved with the current version number from
57   * the POM by your keys.
58   * </p>
59   */
60  public final class ProjectProperties
61  {
62    // ********************************* Fields *********************************
63  
64    // --- constants ------------------------------------------------------------
65  
66    /**
67     * The name of the properties file searched in the class path root.
68     * <p>
69     * The value of this constant is {@value}.
70     * </p>
71     */
72    public static final String DEFAULT_RESOURCE = "artifacts.properties";
73  
74    // --- members --------------------------------------------------------------
75  
76    /**
77     * The mapping of project-defined keys to GAV strings.
78     */
79    private final Properties dictionary;
80  
81    // ****************************** Initializer *******************************
82  
83    // ****************************** Constructors ******************************
84  
85    /**
86     * Constructor using the thread's context class loader.
87     */
88    public ProjectProperties()
89    {
90      this(Thread.currentThread().getContextClassLoader());
91    }
92  
93    /**
94     * Constructor using a class to locate the
95     * {@value de.smartics.confinale.test.ProjectProperties#DEFAULT_RESOURCE}.
96     *
97     * @param locator the class to access the class loader to search for
98     *          {@value de.smartics.confinale.test.ProjectProperties#DEFAULT_RESOURCE}
99     *          .
100    */
101   public ProjectProperties(final Class<?> locator)
102   {
103     this(locator.getClassLoader()); // NOPMD
104   }
105 
106   /**
107    * Constructor using a class loader to locate the
108    * {@value de.smartics.confinale.test.ProjectProperties#DEFAULT_RESOURCE}.
109    *
110    * @param loader the class loader to search for
111    *          {@value de.smartics.confinale.test.ProjectProperties#DEFAULT_RESOURCE}
112    *          .
113    */
114   public ProjectProperties(final ClassLoader loader)
115   {
116     dictionary = init(loader);
117   }
118 
119   // ****************************** Inner Classes *****************************
120 
121   // ********************************* Methods ********************************
122 
123   // --- init -----------------------------------------------------------------
124 
125   private static Properties init(final ClassLoader loader)
126   {
127     final Properties properties = new Properties();
128 
129     InputStream input = loader.getResourceAsStream(DEFAULT_RESOURCE);
130     if (input != null)
131     {
132       try
133       {
134         input = new BufferedInputStream(input);
135         properties.load(input);
136       }
137       catch (final IOException e)
138       {
139         throw new IllegalStateException("Cannot read '" + DEFAULT_RESOURCE
140                                         + "'.", e);
141       }
142       finally
143       {
144         IOUtils.closeQuietly(input);
145       }
146     }
147 
148     return properties;
149   }
150 
151   // --- get&set --------------------------------------------------------------
152 
153   // --- business -------------------------------------------------------------
154 
155   /**
156    * Returns the GAV string for the given key.
157    *
158    * @param key a key defined in
159    *          {@value de.smartics.confinale.test.ProjectProperties#DEFAULT_RESOURCE}
160    *          .
161    * @return the value associated with the given {@code key}.
162    */
163   public String get(final String key)
164   {
165     return dictionary.getProperty(key);
166   }
167 
168   // --- object basics --------------------------------------------------------
169 
170 }