001: /*
002: * $Id: PropertiesSettings.java 539688 2007-05-19 05:12:22Z mrdon $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.config;
022:
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.net.URL;
026: import java.util.Iterator;
027: import java.util.Properties;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.apache.struts2.StrutsException;
032: import org.apache.struts2.util.ClassLoaderUtils;
033:
034: import com.opensymphony.xwork2.util.location.LocatableProperties;
035: import com.opensymphony.xwork2.util.location.Location;
036: import com.opensymphony.xwork2.util.location.LocationImpl;
037:
038: /**
039: * A class to handle settings via a properties file.
040: */
041: class PropertiesSettings extends Settings {
042:
043: LocatableProperties settings;
044: static Log LOG = LogFactory.getLog(PropertiesSettings.class);
045:
046: /**
047: * Creates a new properties config given the name of a properties file. The name is expected to NOT have
048: * the ".properties" file extension. So when <tt>new PropertiesSettings("foo")</tt> is called
049: * this class will look in the classpath for the <tt>foo.properties</tt> file.
050: *
051: * @param name the name of the properties file, excluding the ".properties" extension.
052: */
053: public PropertiesSettings(String name) {
054:
055: URL settingsUrl = ClassLoaderUtils.getResource(name
056: + ".properties", getClass());
057:
058: if (settingsUrl == null) {
059: LOG.debug(name + ".properties missing");
060: settings = new LocatableProperties();
061: return;
062: }
063:
064: settings = new LocatableProperties(new LocationImpl(null,
065: settingsUrl.toString()));
066:
067: // Load settings
068: InputStream in = null;
069: try {
070: in = settingsUrl.openStream();
071: settings.load(in);
072: } catch (IOException e) {
073: throw new StrutsException("Could not load " + name
074: + ".properties:" + e, e);
075: } finally {
076: if (in != null) {
077: try {
078: in.close();
079: } catch (IOException io) {
080: LOG.warn("Unable to close input stream", io);
081: }
082: }
083: }
084: }
085:
086: /**
087: * Sets a property in the properties file.
088: *
089: * @see #set(String, String)
090: */
091: public void setImpl(String aName, String aValue) {
092: settings.setProperty(aName, aValue);
093: }
094:
095: /**
096: * Gets a property from the properties file.
097: *
098: * @see #get(String)
099: */
100: public String getImpl(String aName) throws IllegalArgumentException {
101: String setting = settings.getProperty(aName);
102:
103: if (setting == null) {
104: throw new IllegalArgumentException("No such setting:"
105: + aName);
106: }
107:
108: return setting;
109: }
110:
111: /**
112: * Gets the location of a property from the properties file.
113: *
114: * @see #getLocation(String)
115: */
116: public Location getLocationImpl(String aName)
117: throws IllegalArgumentException {
118: Location loc = settings.getPropertyLocation(aName);
119:
120: if (loc == null) {
121: if (!settings.containsKey(aName)) {
122: throw new IllegalArgumentException("No such setting:"
123: + aName);
124: }
125: }
126:
127: return loc;
128: }
129:
130: /**
131: * Tests to see if a property exists in the properties file.
132: *
133: * @see #isSet(String)
134: */
135: public boolean isSetImpl(String aName) {
136: if (settings.get(aName) != null) {
137: return true;
138: } else {
139: return false;
140: }
141: }
142:
143: /**
144: * Lists all keys in the properties file.
145: *
146: * @see #list()
147: */
148: public Iterator listImpl() {
149: return settings.keySet().iterator();
150: }
151: }
|