01: // Copyright 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 java.lang.reflect.Constructor;
18: import java.lang.reflect.InvocationTargetException;
19: import java.util.Map;
20:
21: import org.apache.tapestry.ioc.ServiceBuilderResources;
22: import org.apache.tapestry.ioc.internal.util.InternalUtils;
23:
24: /**
25: * A service creator based on an implementation class' constructor, rather than a service builder
26: * method.
27: */
28: public class ConstructorServiceCreator extends AbstractServiceCreator {
29: private final Constructor _constructor;
30:
31: public ConstructorServiceCreator(ServiceBuilderResources resources,
32: String creatorDescription, Constructor constructor) {
33: super (resources, creatorDescription);
34:
35: _constructor = constructor;
36: }
37:
38: public Object createObject() {
39: Throwable failure = null;
40:
41: try {
42: Object[] parameters = InternalUtils
43: .calculateParametersForConstructor(_constructor,
44: _resources,
45: getParameterDefaultsWithConfigurations());
46:
47: if (_log.isDebugEnabled())
48: _log.debug(IOCMessages
49: .invokingConstructor(_creatorDescription));
50:
51: return _constructor.newInstance(parameters);
52: } catch (InvocationTargetException ite) {
53: failure = ite.getTargetException();
54: } catch (Exception ex) {
55: failure = ex;
56: }
57:
58: throw new RuntimeException(IOCMessages.constructorError(
59: _creatorDescription, _serviceId, failure), failure);
60: }
61:
62: /**
63: * Returns a map that includes (possibly) an additional mapping containing the collected
64: * configuration data. This involves scanning the constructor's parameters.
65: */
66: private Map<Class, Object> getParameterDefaultsWithConfigurations() {
67: return getParameterDefaultsWithConfiguration(_constructor
68: .getParameterTypes(), _constructor
69: .getGenericParameterTypes());
70: }
71: }
|