001    // Copyright 2005 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    package org.apache.tapestry.enhance;
015    
016    import java.util.Iterator;
017    import java.util.List;
018    
019    import org.apache.commons.logging.Log;
020    import org.apache.hivemind.Location;
021    import org.apache.hivemind.internal.Module;
022    import org.apache.tapestry.spec.IComponentSpecification;
023    
024    /**
025     * An enhancement worker which automatically injects HiveMind services
026     * into pages/components if exactly one service point exists which is
027     * compatible with the read-only property's type.
028     * 
029     * @author James Carman
030     * @version 1.0
031     */
032    public class AutowireWorker implements EnhancementWorker
033    {
034        private final Log _log;
035        private final Module _module;
036    
037        public AutowireWorker( Module module, Log log )
038        {
039            this._module = module;
040            this._log = log;
041        }
042    
043        public void performEnhancement( EnhancementOperation op, IComponentSpecification spec )
044        {
045            final List propertyNames = op.findUnclaimedAbstractProperties();
046            for( Iterator i = propertyNames.iterator(); i.hasNext(); ) {
047                String propertyName = ( String ) i.next();
048                
049                Class propertyType = op.getPropertyType( propertyName );
050                if( propertyType == null )
051                    propertyType = Object.class;
052                
053                if (!op.canClaimAsReadOnlyProperty(propertyName))
054                    continue;
055                
056                if( _module.containsService( propertyType )) {
057                    final Object serviceProxy = _module.getService( propertyType );
058                    final Location location = spec.getLocation();
059                    
060                    _log.debug( EnhanceMessages.autowiring( propertyName, spec, serviceProxy ) );
061                    
062                    final String fieldName = op.addInjectedField( "_$" + propertyName, propertyType, serviceProxy );
063                    
064                    EnhanceUtils.createSimpleAccessor( op, fieldName, propertyName, propertyType, location );
065                    op.claimReadonlyProperty( propertyName );
066                }
067    
068            }
069        }
070    }