001: // Copyright 2005, 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.services;
016:
017: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newMap;
018:
019: import java.util.Map;
020:
021: import javassist.CtClass;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.tapestry.ioc.internal.util.OneShotLock;
025:
026: /**
027: * Base class for {@link org.apache.tapestry.ioc.internal.services.ClassFabImpl}. This code is a
028: * fork from HiveMind; it is kept seperate from ClassFabImpl in case we want to re-introduce the
029: * idea of an InterfaceFab.
030: */
031: public class AbstractFab {
032: protected final OneShotLock _lock = new OneShotLock();
033:
034: private final CtClass _ctClass;
035:
036: private final CtClassSource _source;
037:
038: private final Log _log;
039:
040: public AbstractFab(CtClassSource source, CtClass ctClass, Log log) {
041: _ctClass = ctClass;
042: _source = source;
043: _log = log;
044: }
045:
046: /**
047: * Map from Class to CtClass.
048: */
049: private Map<Class, CtClass> _ctClassCache = newMap();
050:
051: public void addInterface(Class interfaceClass) {
052: _lock.check();
053:
054: CtClass ctInterfaceClass = _source.getCtClass(interfaceClass);
055:
056: try {
057: for (CtClass existing : _ctClass.getInterfaces())
058: if (existing == ctInterfaceClass)
059: return;
060: } catch (Exception ex) {
061: // Don't think this code is actually reachable.
062: }
063:
064: _ctClass.addInterface(ctInterfaceClass);
065: }
066:
067: protected CtClass[] convertClasses(Class[] inputClasses) {
068: if (inputClasses == null || inputClasses.length == 0)
069: return null;
070:
071: int count = inputClasses.length;
072: CtClass[] result = new CtClass[count];
073:
074: for (int i = 0; i < count; i++) {
075: CtClass ctClass = convertClass(inputClasses[i]);
076:
077: result[i] = ctClass;
078: }
079:
080: return result;
081: }
082:
083: protected CtClass convertClass(Class inputClass) {
084: CtClass result = _ctClassCache.get(inputClass);
085:
086: if (result == null) {
087: result = _source.getCtClass(inputClass);
088: _ctClassCache.put(inputClass, result);
089: }
090:
091: return result;
092: }
093:
094: public Class createClass() {
095: _lock.lock();
096:
097: if (_log.isDebugEnabled())
098: _log.debug(String.format("Creating class from %s", this ));
099:
100: return _source.createClass(_ctClass);
101: }
102:
103: protected CtClass getCtClass() {
104: return _ctClass;
105: }
106:
107: protected CtClassSource getSource() {
108: return _source;
109: }
110:
111: }
|