org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer Maven / Gradle / Ivy
/*
* Copyright 2002-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.config;
import java.util.Properties;
import java.util.prefs.Preferences;
import org.springframework.beans.factory.InitializingBean;
/**
* Subclass of PropertyPlaceholderConfigurer that supports J2SE 1.4's
* Preferences API (java.util.prefs).
*
* Tries to resolve placeholders as keys first in the user preferences,
* then in the system preferences, then in this configurer's properties.
* Thus, behaves like PropertyPlaceholderConfigurer if no corresponding
* preferences defined.
*
*
Supports custom paths for the system and user preferences trees.
* Uses the respective root nodes if not specified.
*
* @author Juergen Hoeller
* @since 16.02.2004
* @see #setSystemTreePath
* @see #setUserTreePath
* @see java.util.prefs.Preferences
*/
public class PreferencesPlaceholderConfigurer extends PropertyPlaceholderConfigurer implements InitializingBean {
private String systemTreePath;
private String userTreePath;
private Preferences systemPrefs;
private Preferences userPrefs;
/**
* Set the path in the system preferences tree to use for resolving
* placeholders. Uses the root node by default.
*/
public void setSystemTreePath(String systemTreePath) {
this.systemTreePath = systemTreePath;
}
/**
* Set the path in the system preferences tree to use for resolving
* placeholders. Uses the root node by default.
*/
public void setUserTreePath(String userTreePath) {
this.userTreePath = userTreePath;
}
/**
* This implementation eagerly fetches the Preferences instances
* for the required system and user tree nodes.
*/
public void afterPropertiesSet() {
this.systemPrefs = (this.systemTreePath != null) ?
Preferences.systemRoot().node(this.systemTreePath) : Preferences.systemRoot();
this.userPrefs = (this.userTreePath != null) ?
Preferences.userRoot().node(this.userTreePath) : Preferences.userRoot();
}
/**
* This implementation tries to resolve placeholders as keys first
* in the user preferences, then in the system preferences, then in
* the passed-in properties.
*/
protected String resolvePlaceholder(String placeholder, Properties props) {
String value = this.userPrefs.get(placeholder, null);
if (value == null) {
value = this.systemPrefs.get(placeholder, null);
if (value == null) {
value = props.getProperty(placeholder);
}
}
return value;
}
}