001: // Copyright 2006, 2007 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 static org.apache.tapestry.ioc.internal.util.Defense.notNull;
018:
019: import java.lang.reflect.Constructor;
020: import java.util.Collection;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.tapestry.ioc.ObjectCreator;
026: import org.apache.tapestry.ioc.ServiceBuilderResources;
027: import org.apache.tapestry.ioc.def.ServiceDef;
028: import org.apache.tapestry.ioc.internal.util.InternalUtils;
029: import org.apache.tapestry.ioc.services.ClassFactory;
030:
031: /**
032: * Implementation of {@link org.apache.tapestry.ioc.ServiceBuilderResources}. We just have one
033: * implementation that fills the purposes of methods that need a
034: * {@link org.apache.tapestry.ioc.ServiceResources} (which includes service decorator methods) as
035: * well as methods that need a {@link org.apache.tapestry.ioc.ServiceBuilderResources} (which is
036: * just service builder methods). Since it is most commonly used for the former, we'll just leave
037: * the name as ServiceResourcesImpl.
038: */
039: public class ServiceResourcesImpl extends ObjectLocatorImpl implements
040: ServiceBuilderResources {
041: private final ServiceDef _serviceDef;
042:
043: private final Log _log;
044:
045: private final ClassFactory _classFactory;
046:
047: public ServiceResourcesImpl(InternalRegistry registry,
048: Module module, ServiceDef serviceDef,
049: ClassFactory classFactory, Log log) {
050: super (registry, module);
051:
052: _serviceDef = serviceDef;
053: _classFactory = classFactory;
054: _log = log;
055: }
056:
057: public String getServiceId() {
058: return _serviceDef.getServiceId();
059: }
060:
061: public Class getServiceInterface() {
062: return _serviceDef.getServiceInterface();
063: }
064:
065: public Log getServiceLog() {
066: return _log;
067: }
068:
069: public <T> Collection<T> getUnorderedConfiguration(
070: Class<T> valueType) {
071: Collection<T> result = getRegistry().getUnorderedConfiguration(
072: _serviceDef, valueType);
073:
074: logConfiguration(result);
075:
076: return result;
077: }
078:
079: private void logConfiguration(Collection configuration) {
080: if (_log.isDebugEnabled())
081: _log.debug(IOCMessages
082: .constructedConfiguration(configuration));
083: }
084:
085: public <T> List<T> getOrderedConfiguration(Class<T> valueType) {
086: List<T> result = getRegistry().getOrderedConfiguration(
087: _serviceDef, valueType);
088:
089: logConfiguration(result);
090:
091: return result;
092: }
093:
094: public <K, V> Map<K, V> getMappedConfiguration(Class<K> keyType,
095: Class<V> valueType) {
096: Map<K, V> result = getRegistry().getMappedConfiguration(
097: _serviceDef, keyType, valueType);
098:
099: if (_log.isDebugEnabled())
100: _log.debug(IOCMessages.constructedConfiguration(result));
101:
102: return result;
103: }
104:
105: public Object getModuleBuilder() {
106: return getModule().getModuleBuilder();
107: }
108:
109: public <T> T autobuild(Class<T> clazz) {
110: notNull(clazz, "clazz");
111:
112: Constructor constructor = InternalUtils
113: .findAutobuildConstructor(clazz);
114:
115: if (constructor == null)
116: throw new RuntimeException(IOCMessages
117: .noAutobuildConstructor(clazz));
118:
119: String description = _classFactory.getConstructorLocation(
120: constructor).toString();
121:
122: ObjectCreator creator = new ConstructorServiceCreator(this,
123: description, constructor);
124:
125: return clazz.cast(creator.createObject());
126: }
127: }
|