001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.beans.factory.support; 018 019import java.util.Properties; 020 021import org.springframework.beans.BeanMetadataElement; 022import org.springframework.beans.Mergeable; 023 024/** 025 * Tag class which represents a Spring-managed {@link Properties} instance 026 * that supports merging of parent/child definitions. 027 * 028 * @author Rob Harrop 029 * @author Juergen Hoeller 030 * @since 2.0 031 */ 032@SuppressWarnings("serial") 033public class ManagedProperties extends Properties implements Mergeable, BeanMetadataElement { 034 035 private Object source; 036 037 private boolean mergeEnabled; 038 039 040 /** 041 * Set the configuration source {@code Object} for this metadata element. 042 * <p>The exact type of the object will depend on the configuration mechanism used. 043 */ 044 public void setSource(Object source) { 045 this.source = source; 046 } 047 048 @Override 049 public Object getSource() { 050 return this.source; 051 } 052 053 /** 054 * Set whether merging should be enabled for this collection, 055 * in case of a 'parent' collection value being present. 056 */ 057 public void setMergeEnabled(boolean mergeEnabled) { 058 this.mergeEnabled = mergeEnabled; 059 } 060 061 @Override 062 public boolean isMergeEnabled() { 063 return this.mergeEnabled; 064 } 065 066 067 @Override 068 public Object merge(Object parent) { 069 if (!this.mergeEnabled) { 070 throw new IllegalStateException("Not allowed to merge when the 'mergeEnabled' property is set to 'false'"); 071 } 072 if (parent == null) { 073 return this; 074 } 075 if (!(parent instanceof Properties)) { 076 throw new IllegalArgumentException("Cannot merge with object of type [" + parent.getClass() + "]"); 077 } 078 Properties merged = new ManagedProperties(); 079 merged.putAll((Properties) parent); 080 merged.putAll(this); 081 return merged; 082 } 083 084}