01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.ioc.internal;
16:
17: import org.apache.commons.logging.Log;
18: import org.apache.tapestry.ioc.OrderedConfiguration;
19: import org.apache.tapestry.ioc.def.ContributionDef;
20:
21: /**
22: * Implements validation of values provided to an
23: * {@link org.apache.tapestry.ioc.OrderedConfiguration}. If you provide an incorrect value type,
24: * the value is converted to null but added anyway. This ensures that incorrect values contributed
25: * in don't screw up the {@link org.apache.tapestry.ioc.internal.util.Orderer} (and generate a bunch
26: * of error messages there).
27: *
28: * @param <T>
29: */
30: public class ValidatingOrderedConfigurationWrapper<T> implements
31: OrderedConfiguration<T> {
32: private final String _serviceId;
33:
34: private final ContributionDef _contributionDef;
35:
36: private final Log _log;
37:
38: private final Class _expectedType;
39:
40: private final OrderedConfiguration<T> _delegate;
41:
42: public ValidatingOrderedConfigurationWrapper(String serviceId,
43: ContributionDef contributionDef, Log log,
44: Class expectedType, OrderedConfiguration<T> delegate) {
45: _serviceId = serviceId;
46: _contributionDef = contributionDef;
47: _log = log;
48: _expectedType = expectedType;
49: _delegate = delegate;
50: }
51:
52: public void add(String id, T object, String... constraints) {
53: _delegate.add(id, validVersionOf(object), constraints);
54: }
55:
56: private T validVersionOf(T object) {
57: if (object == null || _expectedType.isInstance(object))
58: return object;
59:
60: _log.warn(IOCMessages.contributionWrongValueType(_serviceId,
61: _contributionDef, object.getClass(), _expectedType));
62:
63: return null;
64: }
65: }
|