KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > support > ManagedProperties


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.support;
18
19 import java.util.Properties JavaDoc;
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 JavaDoc implements Mergeable, BeanMetadataElement {
33
34     private Object JavaDoc source;
35
36     private boolean mergeEnabled;
37
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 JavaDoc source) {
44         this.source = source;
45     }
46
47     public Object JavaDoc 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
64     public Object JavaDoc merge(Object JavaDoc parent) {
65         if (!this.mergeEnabled) {
66             throw new IllegalStateException JavaDoc("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 JavaDoc)) {
72             throw new IllegalArgumentException JavaDoc("Cannot merge with object of type [" + parent.getClass() + "]");
73         }
74         Properties JavaDoc merged = new ManagedProperties();
75         merged.putAll((Properties JavaDoc) parent);
76         merged.putAll(this);
77         return merged;
78     }
79
80 }
81
Popular Tags