View Javadoc

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.discovery.tools;
18  
19  import java.util.Properties;
20  
21  import org.apache.commons.discovery.resource.ClassLoaders;
22  
23  /**
24   * Holder for a default class.
25   *
26   * Class may be specified by name (String) or class (Class).
27   * Using the holder complicates the users job, but minimized # of API's.
28   */
29  public class PropertiesHolder {
30  
31      private Properties properties;
32  
33      private final String propertiesFileName;
34  
35      /**
36       * Creates a new {@code PropertiesHolder} instance given an
37       * already load {@code Properties} set.
38       *
39       * @param properties The already load {@code Properties} set
40       */
41      public PropertiesHolder(Properties properties) {
42          this.properties = properties;
43          this.propertiesFileName = null;
44      }
45  
46      /**
47       * Creates a new {@code PropertiesHolder} instance given a
48       * property file name.
49       *
50       * @param propertiesFileName The property file name
51       */
52      public PropertiesHolder(String propertiesFileName) {
53          this.properties = null;
54          this.propertiesFileName = propertiesFileName;
55      }
56  
57      /**
58       * Returns the {@code Properties} instance, loaded if necessary from {@code propertiesFileName}.
59       *
60       * @param spi Optional SPI (may be null).
61       *            If provided, an attempt is made to load the
62       *            property file as-per Class.getResource().
63       *
64       * @param loaders Used only if properties need to be loaded.
65       * 
66       * @return The {@code Properties}, loaded if necessary.
67       */
68      public Properties getProperties(SPInterface<?> spi, ClassLoaders loaders) {
69          if (properties == null) {
70              properties = ResourceUtils.loadProperties(spi.getSPClass(), getPropertiesFileName(), loaders);
71          }
72          return properties;
73      }
74  
75      /**
76       * Returns the property file name
77       *
78       * @return The property file name
79       */
80      public String getPropertiesFileName() {
81          return propertiesFileName;
82      }
83  
84  }