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 org.apache.commons.logging.Log;
017    import org.apache.hivemind.Location;
018    import org.apache.hivemind.internal.Module;
019    import org.apache.tapestry.spec.IComponentSpecification;
020    
021    import java.util.Iterator;
022    import java.util.List;
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     */
030    public class AutowireWorker implements EnhancementWorker
031    {
032        private final Log _log;
033    
034        private final Module _module;
035    
036        public AutowireWorker( Module module, Log log)
037        {
038            _module = module;
039            _log = log;
040        }
041    
042        public void performEnhancement( EnhancementOperation op, IComponentSpecification spec )
043        {
044            final List propertyNames = op.findUnclaimedAbstractProperties();
045    
046            for( Iterator i = propertyNames.iterator(); i.hasNext(); ) {
047                
048                String propertyName = ( String ) i.next();
049                
050                Class propertyType = op.getPropertyType( propertyName );
051                if( propertyType == null )
052                    propertyType = Object.class;
053                
054                if (!op.canClaimAsReadOnlyProperty(propertyName))
055                    continue;
056    
057                if( _module.containsService( propertyType )) {
058                    
059                    final Object serviceProxy = _module.getService( propertyType );
060                    final Location location = spec.getLocation();
061                    
062                    _log.debug( EnhanceMessages.autowiring( propertyName, spec, serviceProxy ) );
063                    
064                    final String fieldName = op.addInjectedField( "_$" + propertyName, propertyType, serviceProxy );
065                    
066                    EnhanceUtils.createSimpleAccessor( op, fieldName, propertyName, propertyType, location );
067                    op.claimReadonlyProperty( propertyName );
068                }
069    
070            }
071        }
072    }