KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > encoding > ser > BeanPropertyTarget


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.encoding.ser;
18
19 import org.apache.axis.components.logger.LogFactory;
20 import org.apache.axis.encoding.Target;
21 import org.apache.axis.utils.BeanPropertyDescriptor;
22 import org.apache.axis.utils.JavaUtils;
23 import org.apache.axis.utils.Messages;
24 import org.apache.commons.logging.Log;
25 import org.xml.sax.SAXException JavaDoc;
26
27 import java.lang.reflect.Array JavaDoc;
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29
30 /**
31  * Class which knows how to update a bean property
32  */

33 public class BeanPropertyTarget implements Target {
34     protected static Log log =
35         LogFactory.getLog(BeanPropertyTarget.class.getName());
36
37     private Object JavaDoc object;
38     private BeanPropertyDescriptor pd;
39     private int index = -1;
40     
41     /**
42      * This constructor is used for a normal property.
43      * @param object is the bean class
44      * @param pd is the property
45      **/

46     public BeanPropertyTarget(Object JavaDoc object, BeanPropertyDescriptor pd) {
47         this.object = object;
48         this.pd = pd;
49         this.index = -1; // disable indexing
50
}
51     
52     /**
53      * This constructor is used for an indexed property.
54      * @param object is the bean class
55      * @param pd is the property
56      * @param i is the index
57      **/

58     public BeanPropertyTarget(Object JavaDoc object, BeanPropertyDescriptor pd, int i) {
59         this.object = object;
60         this.pd = pd;
61         this.index = i;
62     }
63     
64     /**
65      * set the bean property with specified value
66      * @param value is the value.
67      */

68     public void set(Object JavaDoc value) throws SAXException JavaDoc {
69
70         try {
71             // Set the value on the bean property.
72
// Use the indexed property method if the
73
// index is set.
74
if (index < 0) {
75                 pd.set(object, value);
76             } else {
77                 pd.set(object, index, value);
78             }
79         } catch (Exception JavaDoc e) {
80
81             try {
82                 // If an exception occurred,
83
// see it the value can be converted into
84
// the expected type.
85
Class JavaDoc type = pd.getType();
86
87
88                 if (value.getClass().isArray()
89                         && value.getClass().getComponentType().isPrimitive()
90                         && type.isArray()
91                         && type.getComponentType().equals(Object JavaDoc.class))
92                 {
93                     //we make our own array type here.
94
type = Array.newInstance(JavaUtils.getWrapperClass(value.getClass().getComponentType()),0).getClass();
95                 }
96
97                 if (JavaUtils.isConvertable(value, type)) {
98                     value = JavaUtils.convert(value, type);
99                     if (index < 0)
100                         pd.set(object, value);
101                     else
102                         pd.set(object, index, value);
103                 } else {
104                     // It is possible that an indexed
105
// format was expected, but the
106
// entire array was sent. In such
107
// cases traverse the array and
108
// call the setter for each item.
109
if (index == 0 &&
110                         value.getClass().isArray() &&
111                         !type.getClass().isArray()) {
112                         for (int i=0; i<Array.getLength(value); i++) {
113                             Object JavaDoc item =
114                                 JavaUtils.convert(Array.get(value, i), type);
115                             pd.set(object, i, item);
116                         }
117                     } else {
118                         // Can't proceed. Throw an exception that
119
// will be caught in the catch block below.
120
throw e;
121                     }
122                 }
123             } catch (Exception JavaDoc ex) {
124                 // Throw a SAX exception with an informative
125
// message.
126
String JavaDoc field= pd.getName();
127                 if (index >=0) {
128                     field += "[" + index + "]";
129                 }
130                 if (log.isErrorEnabled()) {
131                     //TODO: why is this just logged on the server-side and not thrown back to the client???
132
String JavaDoc valueType = "null";
133                     if (value != null)
134                         valueType = value.getClass().getName();
135                     log.error(Messages.getMessage("cantConvert02",
136                             new String JavaDoc[]{
137                                 valueType,
138                                 field,
139                                 (index >= 0) ?
140                                         pd.getType().getComponentType().getName() :
141                                         pd.getType().getName()
142                             }));
143                 }
144                 if(ex instanceof InvocationTargetException JavaDoc) {
145                     Throwable JavaDoc t = ((InvocationTargetException JavaDoc)ex).getTargetException();
146                     if( t != null) {
147                         String JavaDoc classname = this.object.getClass().getName();
148                         //show the context where this exception occured.
149
throw new SAXException JavaDoc(Messages.getMessage("cantConvert04",
150                                                    new String JavaDoc[] {
151                                                        classname,
152                                                        field,
153                                                        (value==null)?null:value.toString(),
154                                                        t.getMessage()}));
155                     }
156                 }
157                 throw new SAXException JavaDoc(ex);
158             }
159         }
160     }
161 }
162
163
Popular Tags