001: // Copyright 2006 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.ioc.internal;
016:
017: import java.util.Map;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.tapestry.ioc.MappedConfiguration;
021: import org.apache.tapestry.ioc.def.ContributionDef;
022:
023: /**
024: * Provides two forms of validation for mapped configurations:
025: * <ul>
026: * <li>If either key or value is null, then a warning is logged </li>
027: * <li>If the key has previously been stored (by some other
028: * {@link org.apache.tapestry.ioc.def.ContributionDef}, then a warning is logged</li>
029: * </ul>
030: * <p>
031: * When a warning is logged, the key/value pair is not added to the delegate.
032: *
033: * @param <K>
034: * @param <V>
035: */
036: public class ValidatingMappedConfigurationWrapper<K, V> implements
037: MappedConfiguration<K, V> {
038: private final String _serviceId;
039:
040: private final ContributionDef _contributionDef;
041:
042: private final Log _log;
043:
044: private final Class<K> _expectedKeyType;
045:
046: private final Class<V> _expectedValueType;
047:
048: private final Map<K, ContributionDef> _keyToContributor;
049:
050: private final MappedConfiguration<K, V> _delegate;
051:
052: public ValidatingMappedConfigurationWrapper(String serviceId,
053: ContributionDef contributionDef, Log log,
054: Class<K> expectedKeyType, Class<V> expectedValueType,
055: Map<K, ContributionDef> keyToContributor,
056: MappedConfiguration<K, V> delegate) {
057: _serviceId = serviceId;
058: _contributionDef = contributionDef;
059: _log = log;
060: _expectedKeyType = expectedKeyType;
061: _expectedValueType = expectedValueType;
062: _keyToContributor = keyToContributor;
063: _delegate = delegate;
064: }
065:
066: public void add(K key, V value) {
067: if (key == null) {
068: _log.warn(IOCMessages.contributionKeyWasNull(_serviceId,
069: _contributionDef));
070: return;
071: }
072:
073: if (value == null) {
074: _log.warn(IOCMessages.contributionWasNull(_serviceId,
075: _contributionDef));
076: return;
077: }
078:
079: if (!_expectedKeyType.isInstance(key)) {
080: _log
081: .warn(IOCMessages.contributionWrongKeyType(
082: _serviceId, _contributionDef, key
083: .getClass(), _expectedKeyType));
084: return;
085: }
086:
087: if (!_expectedValueType.isInstance(value)) {
088: _log.warn(IOCMessages.contributionWrongValueType(
089: _serviceId, _contributionDef, value.getClass(),
090: _expectedValueType));
091: return;
092: }
093:
094: ContributionDef existing = _keyToContributor.get(key);
095:
096: if (existing != null) {
097: _log.warn(IOCMessages.contributionDuplicateKey(_serviceId,
098: _contributionDef, existing));
099: return;
100: }
101:
102: _delegate.add(key, value);
103:
104: // Remember that this key is provided by this contribution, when looking
105: // for future conflicts.
106:
107: _keyToContributor.put(key, _contributionDef);
108: }
109:
110: }
|