View Javadoc

    1   /*
    2    * $Id: PropertiesSettings.java 483062 2006-12-06 12:59:50Z ddewolf $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *  http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21  package org.apache.struts2.config;
    22
    23  import java.io.IOException;
    24  import java.io.InputStream;
    25  import java.net.URL;
    26  import java.util.Iterator;
    27  import java.util.Properties;
    28
    29  import org.apache.commons.logging.Log;
    30  import org.apache.commons.logging.LogFactory;
    31  import org.apache.struts2.StrutsException;
    32
    33  import com.opensymphony.xwork2.util.location.LocatableProperties;
    34  import com.opensymphony.xwork2.util.location.Location;
    35  import com.opensymphony.xwork2.util.location.LocationImpl;
    36
    37
    38  /***
    39   * A class to handle settings via a properties file.
    40   */
    41  class PropertiesSettings extends Settings {
    42
    43      LocatableProperties settings;
    44      static Log LOG = LogFactory.getLog(PropertiesSettings.class);
    45
    46
    47      /***
    48       * Creates a new properties config given the name of a properties file. The name is expected to NOT have
    49       * the ".properties" file extension.  So when <tt>new PropertiesSettings("foo")</tt> is called
    50       * this class will look in the classpath for the <tt>foo.properties</tt> file.
    51       *
    52       * @param name the name of the properties file, excluding the ".properties" extension.
    53       */
    54      public PropertiesSettings(String name) {
    55
    56          URL settingsUrl = Thread.currentThread().getContextClassLoader().getResource(name + ".properties");
    57
    58          if (settingsUrl == null) {
    59              LOG.debug(name + ".properties missing");
    60              settings = new LocatableProperties();
    61              return;
    62          }
    63
    64          settings = new LocatableProperties(new LocationImpl(null, settingsUrl.toString()));
    65
    66          // Load settings
    67          InputStream in = null;
    68          try {
    69              in = settingsUrl.openStream();
    70              settings.load(in);
    71          } catch (IOException e) {
    72              throw new StrutsException("Could not load " + name + ".properties:" + e, e);
    73          } finally {
    74              if(in != null) {
    75                  try {
    76                      in.close();
    77                  } catch(IOException io) {
    78                      LOG.warn("Unable to close input stream", io);
    79                  }
    80              }
    81          }
    82      }
    83
    84
    85      /***
    86       * Sets a property in the properties file.
    87       *
    88       * @see #set(String, String)
    89       */
    90      public void setImpl(String aName, String aValue) {
    91          settings.setProperty(aName, aValue);
    92      }
    93
    94      /***
    95       * Gets a property from the properties file.
    96       *
    97       * @see #get(String)
    98       */
    99      public String getImpl(String aName) throws IllegalArgumentException {
    100         String setting = settings.getProperty(aName);
    101
    102         if (setting == null) {
    103             throw new IllegalArgumentException("No such setting:" + aName);
    104         }
    105
    106         return setting;
    107     }
    108
    109     /***
    110      * Gets the location of a property from the properties file.
    111      *
    112      * @see #getLocation(String)
    113      */
    114     public Location getLocationImpl(String aName) throws IllegalArgumentException {
    115         Location loc = settings.getPropertyLocation(aName);
    116
    117         if (loc == null) {
    118             if (!settings.containsKey(aName)) {
    119                 throw new IllegalArgumentException("No such setting:" + aName);
    120             }
    121         }
    122
    123         return loc;
    124     }
    125
    126     /***
    127      * Tests to see if a property exists in the properties file.
    128      *
    129      * @see #isSet(String)
    130      */
    131     public boolean isSetImpl(String aName) {
    132         if (settings.get(aName) != null) {
    133             return true;
    134         } else {
    135             return false;
    136         }
    137     }
    138
    139     /***
    140      * Lists all keys in the properties file.
    141      *
    142      * @see #list()
    143      */
    144     public Iterator listImpl() {
    145         return settings.keySet().iterator();
    146     }
    147 }