001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.beans.factory.config;
018:
019: import java.util.Properties;
020: import java.util.prefs.BackingStoreException;
021: import java.util.prefs.Preferences;
022:
023: import org.springframework.beans.factory.BeanDefinitionStoreException;
024: import org.springframework.beans.factory.InitializingBean;
025:
026: /**
027: * Subclass of PropertyPlaceholderConfigurer that supports JDK 1.4's
028: * Preferences API (<code>java.util.prefs</code>).
029: *
030: * <p>Tries to resolve placeholders as keys first in the user preferences,
031: * then in the system preferences, then in this configurer's properties.
032: * Thus, behaves like PropertyPlaceholderConfigurer if no corresponding
033: * preferences defined.
034: *
035: * <p>Supports custom paths for the system and user preferences trees. Also
036: * supports custom paths specified in placeholders ("myPath/myPlaceholderKey").
037: * Uses the respective root node if not specified.
038: *
039: * @author Juergen Hoeller
040: * @since 16.02.2004
041: * @see #setSystemTreePath
042: * @see #setUserTreePath
043: * @see java.util.prefs.Preferences
044: */
045: public class PreferencesPlaceholderConfigurer extends
046: PropertyPlaceholderConfigurer implements InitializingBean {
047:
048: private String systemTreePath;
049:
050: private String userTreePath;
051:
052: private Preferences systemPrefs;
053:
054: private Preferences userPrefs;
055:
056: /**
057: * Set the path in the system preferences tree to use for resolving
058: * placeholders. Default is the root node.
059: */
060: public void setSystemTreePath(String systemTreePath) {
061: this .systemTreePath = systemTreePath;
062: }
063:
064: /**
065: * Set the path in the system preferences tree to use for resolving
066: * placeholders. Default is the root node.
067: */
068: public void setUserTreePath(String userTreePath) {
069: this .userTreePath = userTreePath;
070: }
071:
072: /**
073: * This implementation eagerly fetches the Preferences instances
074: * for the required system and user tree nodes.
075: */
076: public void afterPropertiesSet() {
077: this .systemPrefs = (this .systemTreePath != null) ? Preferences
078: .systemRoot().node(this .systemTreePath) : Preferences
079: .systemRoot();
080: this .userPrefs = (this .userTreePath != null) ? Preferences
081: .userRoot().node(this .userTreePath) : Preferences
082: .userRoot();
083: }
084:
085: /**
086: * This implementation tries to resolve placeholders as keys first
087: * in the user preferences, then in the system preferences, then in
088: * the passed-in properties.
089: */
090: protected String resolvePlaceholder(String placeholder,
091: Properties props) {
092: String path = null;
093: String key = placeholder;
094: int endOfPath = placeholder.lastIndexOf('/');
095: if (endOfPath != -1) {
096: path = placeholder.substring(0, endOfPath);
097: key = placeholder.substring(endOfPath + 1);
098: }
099: String value = resolvePlaceholder(path, key, this .userPrefs);
100: if (value == null) {
101: value = resolvePlaceholder(path, key, this .systemPrefs);
102: if (value == null) {
103: value = props.getProperty(placeholder);
104: }
105: }
106: return value;
107: }
108:
109: /**
110: * Resolve the given path and key against the given Preferences.
111: * @param path the preferences path (placeholder part before '/')
112: * @param key the preferences key (placeholder part after '/')
113: * @param preferences the Preferences to resolve against
114: * @return the value for the placeholder, or <code>null</code> if none found
115: */
116: protected String resolvePlaceholder(String path, String key,
117: Preferences preferences) {
118: if (path != null) {
119: // Do not create the node if it does not exist...
120: try {
121: if (preferences.nodeExists(path)) {
122: return preferences.node(path).get(key, null);
123: } else {
124: return null;
125: }
126: } catch (BackingStoreException ex) {
127: throw new BeanDefinitionStoreException(
128: "Cannot access specified node path [" + path
129: + "]", ex);
130: }
131: } else {
132: return preferences.get(key, null);
133: }
134: }
135:
136: }
|