01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.support;
18:
19: import java.util.Properties;
20:
21: import org.springframework.beans.BeanMetadataElement;
22: import org.springframework.beans.Mergeable;
23:
24: /**
25: * Tag class which represents a Spring-managed {@link Properties} instance
26: * that supports merging of parent/child definitions.
27: *
28: * @author Rob Harrop
29: * @author Juergen Hoeller
30: * @since 2.0
31: */
32: public class ManagedProperties extends Properties implements Mergeable,
33: BeanMetadataElement {
34:
35: private Object source;
36:
37: private boolean mergeEnabled;
38:
39: /**
40: * Set the configuration source <code>Object</code> for this metadata element.
41: * <p>The exact type of the object will depend on the configuration mechanism used.
42: */
43: public void setSource(Object source) {
44: this .source = source;
45: }
46:
47: public Object getSource() {
48: return this .source;
49: }
50:
51: /**
52: * Set whether merging should be enabled for this collection,
53: * in case of a 'parent' collection value being present.
54: */
55: public void setMergeEnabled(boolean mergeEnabled) {
56: this .mergeEnabled = mergeEnabled;
57: }
58:
59: public boolean isMergeEnabled() {
60: return this .mergeEnabled;
61: }
62:
63: public Object merge(Object parent) {
64: if (!this .mergeEnabled) {
65: throw new IllegalStateException(
66: "Not allowed to merge when the 'mergeEnabled' property is set to 'false'");
67: }
68: if (parent == null) {
69: return this ;
70: }
71: if (!(parent instanceof Properties)) {
72: throw new IllegalArgumentException(
73: "Cannot merge with object of type ["
74: + parent.getClass() + "]");
75: }
76: Properties merged = new ManagedProperties();
77: merged.putAll((Properties) parent);
78: merged.putAll(this);
79: return merged;
80: }
81:
82: }
|