KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > bean > FieldBeanInitializer


1 // Copyright 2004, 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.bean;
16
17 import java.lang.reflect.Field JavaDoc;
18
19 import org.apache.hivemind.ApplicationRuntimeException;
20 import org.apache.hivemind.ClassResolver;
21 import org.apache.tapestry.IBeanProvider;
22 import org.apache.tapestry.Tapestry;
23
24 /**
25  * Initializes a bean with the value of a public static field.
26  *
27  * @author Howard Lewis Ship
28  * @since 1.0.8
29  *
30  **/

31
32 public class FieldBeanInitializer extends AbstractBeanInitializer
33 {
34     protected String JavaDoc _fieldName;
35     protected Object JavaDoc _fieldValue;
36     private boolean _fieldResolved = false;
37
38     public synchronized void setBeanProperty(IBeanProvider provider, Object JavaDoc bean)
39     {
40         ClassResolver resolver = provider.getClassResolver();
41
42         if (!_fieldResolved)
43             resolveField(resolver);
44
45         setBeanProperty(bean, _fieldValue);
46     }
47
48     private void resolveField(ClassResolver resolver)
49     {
50         if (_fieldResolved)
51             return;
52
53         // This is all copied out of of FieldBinding!!
54

55         int dotx = _fieldName.lastIndexOf('.');
56
57         if (dotx < 0)
58             throw new ApplicationRuntimeException(
59                 Tapestry.format("invalid-field-name", _fieldName));
60
61         String JavaDoc className = _fieldName.substring(0, dotx);
62         String JavaDoc simpleFieldName = _fieldName.substring(dotx + 1);
63
64         // Simple class names are assumed to be in the java.lang package.
65

66         if (className.indexOf('.') < 0)
67             className = "java.lang." + className;
68
69         Class JavaDoc targetClass = null;
70
71         try
72         {
73             targetClass = resolver.findClass(className);
74         }
75         catch (Throwable JavaDoc t)
76         {
77             throw new ApplicationRuntimeException(
78                 Tapestry.format("unable-to-resolve-class", className),
79                 t);
80         }
81
82         Field JavaDoc field = null;
83
84         try
85         {
86             field = targetClass.getField(simpleFieldName);
87         }
88         catch (NoSuchFieldException JavaDoc ex)
89         {
90             throw new ApplicationRuntimeException(
91                 Tapestry.format("field-not-defined", _fieldName),
92                 ex);
93         }
94
95         // Get the value of the field. null means look for it as a static
96
// variable.
97

98         try
99         {
100             _fieldValue = field.get(null);
101         }
102         catch (IllegalAccessException JavaDoc ex)
103         {
104             throw new ApplicationRuntimeException(
105                 Tapestry.format("illegal-field-access", _fieldName),
106                 ex);
107         }
108         catch (NullPointerException JavaDoc ex)
109         {
110             throw new ApplicationRuntimeException(
111                 Tapestry.format("field-is-instance", _fieldName),
112                 ex);
113         }
114
115         _fieldResolved = true;
116     }
117
118 }
Popular Tags