1 16 17 package org.springframework.beans.factory.config; 18 19 import java.util.Properties ; 20 import java.util.prefs.BackingStoreException ; 21 import java.util.prefs.Preferences ; 22 23 import org.springframework.beans.factory.BeanDefinitionStoreException; 24 import org.springframework.beans.factory.InitializingBean; 25 26 45 public class PreferencesPlaceholderConfigurer extends PropertyPlaceholderConfigurer implements InitializingBean { 46 47 private String systemTreePath; 48 49 private String userTreePath; 50 51 private Preferences systemPrefs; 52 53 private Preferences userPrefs; 54 55 56 60 public void setSystemTreePath(String systemTreePath) { 61 this.systemTreePath = systemTreePath; 62 } 63 64 68 public void setUserTreePath(String userTreePath) { 69 this.userTreePath = userTreePath; 70 } 71 72 73 77 public void afterPropertiesSet() { 78 this.systemPrefs = (this.systemTreePath != null) ? 79 Preferences.systemRoot().node(this.systemTreePath) : Preferences.systemRoot(); 80 this.userPrefs = (this.userTreePath != null) ? 81 Preferences.userRoot().node(this.userTreePath) : Preferences.userRoot(); 82 } 83 84 89 protected String resolvePlaceholder(String placeholder, Properties props) { 90 String path = null; 91 String key = placeholder; 92 int endOfPath = placeholder.lastIndexOf('/'); 93 if (endOfPath != -1) { 94 path = placeholder.substring(0, endOfPath); 95 key = placeholder.substring(endOfPath + 1); 96 } 97 String value = resolvePlaceholder(path, key, this.userPrefs); 98 if (value == null) { 99 value = resolvePlaceholder(path, key, this.systemPrefs); 100 if (value == null) { 101 value = props.getProperty(placeholder); 102 } 103 } 104 return value; 105 } 106 107 114 protected String resolvePlaceholder(String path, String key, Preferences preferences) { 115 if (path != null) { 116 try { 118 if (preferences.nodeExists(path)) { 119 return preferences.node(path).get(key, null); 120 } 121 else { 122 return null; 123 } 124 } 125 catch (BackingStoreException ex) { 126 throw new BeanDefinitionStoreException("Cannot access specified node path [" + path + "]", ex); 127 } 128 } 129 else { 130 return preferences.get(key, null); 131 } 132 } 133 134 } 135 | Popular Tags |