KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > config > PreferencesPlaceholderConfigurer


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.beans.factory.config;
18
19 import java.util.Properties JavaDoc;
20 import java.util.prefs.BackingStoreException JavaDoc;
21 import java.util.prefs.Preferences JavaDoc;
22
23 import org.springframework.beans.factory.BeanDefinitionStoreException;
24 import org.springframework.beans.factory.InitializingBean;
25
26 /**
27  * Subclass of PropertyPlaceholderConfigurer that supports JDK 1.4's
28  * Preferences API (<code>java.util.prefs</code>).
29  *
30  * <p>Tries to resolve placeholders as keys first in the user preferences,
31  * then in the system preferences, then in this configurer's properties.
32  * Thus, behaves like PropertyPlaceholderConfigurer if no corresponding
33  * preferences defined.
34  *
35  * <p>Supports custom paths for the system and user preferences trees. Also
36  * supports custom paths specified in placeholders ("myPath/myPlaceholderKey").
37  * Uses the respective root node if not specified.
38  *
39  * @author Juergen Hoeller
40  * @since 16.02.2004
41  * @see #setSystemTreePath
42  * @see #setUserTreePath
43  * @see java.util.prefs.Preferences
44  */

45 public class PreferencesPlaceholderConfigurer extends PropertyPlaceholderConfigurer implements InitializingBean {
46
47     private String JavaDoc systemTreePath;
48
49     private String JavaDoc userTreePath;
50
51     private Preferences JavaDoc systemPrefs;
52
53     private Preferences JavaDoc userPrefs;
54
55
56     /**
57      * Set the path in the system preferences tree to use for resolving
58      * placeholders. Default is the root node.
59      */

60     public void setSystemTreePath(String JavaDoc systemTreePath) {
61         this.systemTreePath = systemTreePath;
62     }
63
64     /**
65      * Set the path in the system preferences tree to use for resolving
66      * placeholders. Default is the root node.
67      */

68     public void setUserTreePath(String JavaDoc userTreePath) {
69         this.userTreePath = userTreePath;
70     }
71
72
73     /**
74      * This implementation eagerly fetches the Preferences instances
75      * for the required system and user tree nodes.
76      */

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     /**
85      * This implementation tries to resolve placeholders as keys first
86      * in the user preferences, then in the system preferences, then in
87      * the passed-in properties.
88      */

89     protected String JavaDoc resolvePlaceholder(String JavaDoc placeholder, Properties JavaDoc props) {
90         String JavaDoc path = null;
91         String JavaDoc 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 JavaDoc 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     /**
108      * Resolve the given path and key against the given Preferences.
109      * @param path the preferences path (placeholder part before '/')
110      * @param key the preferences key (placeholder part after '/')
111      * @param preferences the Preferences to resolve against
112      * @return the value for the placeholder, or <code>null</code> if none found
113      */

114     protected String JavaDoc resolvePlaceholder(String JavaDoc path, String JavaDoc key, Preferences JavaDoc preferences) {
115         if (path != null) {
116              // Do not create the node if it does not exist...
117
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 JavaDoc 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