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    
015    package org.apache.tapestry.annotations;
016    
017    import java.lang.reflect.Method;
018    
019    import org.apache.hivemind.Location;
020    import org.apache.tapestry.engine.IPropertySource;
021    import org.apache.tapestry.enhance.EnhancementOperation;
022    import org.apache.tapestry.spec.IComponentSpecification;
023    import org.apache.tapestry.spec.IPropertySpecification;
024    import org.apache.tapestry.spec.PropertySpecification;
025    
026    /**
027     * Allow a property to be marked as persistent, and (optionally) allows a strategy to be set. Works
028     * by adding a new {@link org.apache.tapestry.spec.IPropertySpecification} to the
029     * {@link org.apache.tapestry.spec.IComponentSpecification}.
030     * <p>
031     * The {@link org.apache.tapestry.annotations.Persist} annotation may <em>optionally</em> by
032     * accompanied by a {@link org.apache.tapestry.annotations.InitialValue} annotation; this sets the
033     * initial value, as a binding reference, used to initialize and re-initialize the property.
034     * 
035     * @author Howard M. Lewis Ship
036     * @since 4.0
037     * @see org.apache.tapestry.annotations.Persist
038     */
039    public class PersistAnnotationWorker implements MethodAnnotationEnhancementWorker
040    {
041            /**
042             * Application property that gives the default property persistence strategy to use for properties annotated by @Persist
043             */
044            public static final String DEFAULT_PROPERTY_PERSISTENCE_STRATEGY = "org.apache.tapestry.default-property-persistence-strategy";
045    
046            private IPropertySource _propertySource;
047    
048        public void performEnhancement(EnhancementOperation op, IComponentSpecification spec,
049                Method method, Location location)
050        {
051            Persist p = method.getAnnotation(Persist.class);
052            InitialValue iv = method.getAnnotation(InitialValue.class);
053    
054            String propertyName = AnnotationUtils.getPropertyName(method);
055                String defaultStrategy = _propertySource.getPropertyValue(DEFAULT_PROPERTY_PERSISTENCE_STRATEGY);
056            String stategy = p.value().length() == 0 ? defaultStrategy : p.value();
057    
058            IPropertySpecification pspec = new PropertySpecification();
059    
060            pspec.setName(propertyName);
061            pspec.setPersistence(stategy);
062            pspec.setLocation(location);
063            pspec.setInitialValue(iv == null ? null : iv.value());
064    
065            spec.addPropertySpecification(pspec);
066        }
067    
068            public void setPropertySource(IPropertySource propertySource)
069            {
070                    _propertySource = propertySource;
071            }
072    }