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.services;
16:
17: import static java.lang.String.format;
18: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newConcurrentMap;
19:
20: import java.util.Map;
21:
22: import org.apache.tapestry.ioc.annotations.InjectService;
23: import org.apache.tapestry.ioc.services.ClassFab;
24: import org.apache.tapestry.ioc.services.ClassFactory;
25: import org.apache.tapestry.ioc.services.DefaultImplementationBuilder;
26: import org.apache.tapestry.ioc.services.MethodIterator;
27: import org.apache.tapestry.ioc.services.MethodSignature;
28:
29: /**
30: *
31: */
32: public class DefaultImplementationBuilderImpl implements
33: DefaultImplementationBuilder {
34: private final Map<Class, Object> _cache = newConcurrentMap();
35:
36: private final ClassFactory _classFactory;
37:
38: public DefaultImplementationBuilderImpl(
39: @InjectService("ClassFactory")
40: ClassFactory classFactory) {
41: _classFactory = classFactory;
42: }
43:
44: public <S> S createDefaultImplementation(Class<S> serviceInterface) {
45: S instance = serviceInterface
46: .cast(_cache.get(serviceInterface));
47:
48: if (instance == null) {
49: instance = createInstance(serviceInterface);
50: _cache.put(serviceInterface, instance);
51: }
52:
53: return instance;
54: }
55:
56: /**
57: * Creates a class and an instance of that class. Updates the cache and returns the instance.
58: */
59: private <S> S createInstance(Class<S> serviceInterface) {
60: // In rare race conditions, we may end up creating two (or more)
61: // NOOP class/instance pairs for the same interface. You need multiple threads
62: // asking for a NOOP class for the same interface pretty much simulataneously.
63: // We just let this happen.
64:
65: Class<S> noopClass = createClass(serviceInterface);
66:
67: try {
68: S instance = noopClass.newInstance();
69:
70: _cache.put(serviceInterface, instance);
71:
72: return instance;
73: } catch (Exception ex) {
74: throw new RuntimeException(ex);
75: }
76: }
77:
78: @SuppressWarnings("unchecked")
79: private <S> Class<S> createClass(Class<S> serviceInterface) {
80: ClassFab cf = _classFactory.newClass(serviceInterface);
81:
82: MethodIterator mi = new MethodIterator(serviceInterface);
83:
84: while (mi.hasNext()) {
85: MethodSignature sig = mi.next();
86:
87: cf.addNoOpMethod(sig);
88: }
89:
90: if (!mi.getToString())
91: cf.addToString(format("<NoOp %s>", serviceInterface
92: .getName()));
93:
94: return cf.createClass();
95: }
96: }
|