Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PersistAnnotationWorker |
|
| 2.0;2 |
1 | // Copyright 2005 The Apache Software Foundation | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // Unless required by applicable law or agreed to in writing, software | |
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | // See the License for the specific language governing permissions and | |
13 | // limitations under the License. | |
14 | ||
15 | package org.apache.tapestry.annotations; | |
16 | ||
17 | import java.lang.reflect.Method; | |
18 | ||
19 | import org.apache.hivemind.Location; | |
20 | import org.apache.tapestry.engine.IPropertySource; | |
21 | import org.apache.tapestry.enhance.EnhancementOperation; | |
22 | import org.apache.tapestry.spec.IComponentSpecification; | |
23 | import org.apache.tapestry.spec.IPropertySpecification; | |
24 | import org.apache.tapestry.spec.PropertySpecification; | |
25 | ||
26 | /** | |
27 | * Allow a property to be marked as persistent, and (optionally) allows a strategy to be set. Works | |
28 | * by adding a new {@link org.apache.tapestry.spec.IPropertySpecification} to the | |
29 | * {@link org.apache.tapestry.spec.IComponentSpecification}. | |
30 | * <p> | |
31 | * The {@link org.apache.tapestry.annotations.Persist} annotation may <em>optionally</em> by | |
32 | * accompanied by a {@link org.apache.tapestry.annotations.InitialValue} annotation; this sets the | |
33 | * initial value, as a binding reference, used to initialize and re-initialize the property. | |
34 | * | |
35 | * @author Howard M. Lewis Ship | |
36 | * @since 4.0 | |
37 | * @see org.apache.tapestry.annotations.Persist | |
38 | */ | |
39 | 0 | public class PersistAnnotationWorker implements MethodAnnotationEnhancementWorker |
40 | { | |
41 | /** | |
42 | * Application property that gives the default property persistence strategy to use for properties annotated by @Persist | |
43 | */ | |
44 | public static final String DEFAULT_PROPERTY_PERSISTENCE_STRATEGY = "org.apache.tapestry.default-property-persistence-strategy"; | |
45 | ||
46 | private IPropertySource _propertySource; | |
47 | ||
48 | public void performEnhancement(EnhancementOperation op, IComponentSpecification spec, | |
49 | Method method, Location location) | |
50 | { | |
51 | 0 | Persist p = method.getAnnotation(Persist.class); |
52 | 0 | InitialValue iv = method.getAnnotation(InitialValue.class); |
53 | ||
54 | 0 | String propertyName = AnnotationUtils.getPropertyName(method); |
55 | 0 | String defaultStrategy = _propertySource.getPropertyValue(DEFAULT_PROPERTY_PERSISTENCE_STRATEGY); |
56 | 0 | String stategy = p.value().length() == 0 ? defaultStrategy : p.value(); |
57 | ||
58 | 0 | IPropertySpecification pspec = new PropertySpecification(); |
59 | ||
60 | 0 | pspec.setName(propertyName); |
61 | 0 | pspec.setPersistence(stategy); |
62 | 0 | pspec.setLocation(location); |
63 | 0 | pspec.setInitialValue(iv == null ? null : iv.value()); |
64 | ||
65 | 0 | spec.addPropertySpecification(pspec); |
66 | 0 | } |
67 | ||
68 | public void setPropertySource(IPropertySource propertySource) | |
69 | { | |
70 | 0 | _propertySource = propertySource; |
71 | 0 | } |
72 | } |