KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.jboss.axis.encoding.ser;
57
58 import org.jboss.axis.encoding.Target;
59 import org.jboss.axis.utils.BeanPropertyDescriptor;
60 import org.jboss.axis.utils.JavaUtils;
61 import org.jboss.axis.utils.Messages;
62 import org.jboss.logging.Logger;
63 import org.xml.sax.SAXException JavaDoc;
64
65 import java.lang.reflect.Array JavaDoc;
66 import java.lang.reflect.InvocationTargetException JavaDoc;
67
68 /**
69  * Class which knows how to update a bean property
70  */

71 public class BeanPropertyTarget implements Target
72 {
73    private static Logger log = Logger.getLogger(BeanPropertyTarget.class.getName());
74
75    private Object JavaDoc object;
76    private BeanPropertyDescriptor pd;
77    private int index = -1;
78
79    // This is the constructor we find in startElement if the bean
80
// does not have a default constructor
81
// TDI 22-Jun-2004
82
protected DeferredBeanConstruction deferedConstruction;
83    protected BeanDeserializer beanDeserializer;
84
85    /**
86     * This constructor is used for a normal property.
87     *
88     * @param object is the bean class
89     * @param pd is the property
90     */

91    public BeanPropertyTarget(Object JavaDoc object, BeanPropertyDescriptor pd)
92    {
93       this.object = object;
94       this.pd = pd;
95       this.index = -1; // disable indexing
96
}
97
98    /**
99     * This constructor is used for an indexed property.
100     *
101     * @param object is the bean class
102     * @param pd is the property
103     * @param i is the index
104     */

105    public BeanPropertyTarget(Object JavaDoc object, BeanPropertyDescriptor pd, int i)
106    {
107       this.object = object;
108       this.pd = pd;
109       this.index = i;
110    }
111
112    /**
113     * This constructor is used for a property with defered construction.
114     */

115    public BeanPropertyTarget(BeanDeserializer beanDeserializer, DeferredBeanConstruction deferedConstruction, BeanPropertyDescriptor pd)
116    {
117       this.beanDeserializer = beanDeserializer;
118       this.deferedConstruction = deferedConstruction;
119       this.pd = pd;
120       this.index = -1; // disable indexing
121
}
122
123    /**
124     * set the bean property with specified value
125     *
126     * @param value is the value.
127     */

128    public void set(Object JavaDoc value) throws SAXException JavaDoc
129    {
130
131       if (object == null && deferedConstruction == null)
132          throw new IllegalStateException JavaDoc("Target object does not exist");
133
134       if (object == null && deferedConstruction != null)
135       {
136          object = deferedConstruction.newBeanInstance(value);
137          beanDeserializer.setValue(object);
138          return;
139       }
140
141       try
142       {
143          // Set the value on the bean property.
144
// Use the indexed property method if the
145
// index is set.
146
if (index < 0)
147          {
148             pd.set(object, value);
149          }
150          else
151          {
152             pd.set(object, index, value);
153          }
154       }
155       catch (Exception JavaDoc e)
156       {
157
158          try
159          {
160             // If an exception occurred,
161
// see it the value can be converted into
162
// the expected type.
163
Class JavaDoc type = pd.getType();
164             if (JavaUtils.isConvertable(value, type))
165             {
166                value = JavaUtils.convert(value, type);
167                if (index < 0)
168                {
169                   //-TME 25-June-2004
170
// If not setter (immutable bean)
171
// Will throw IllegalAccessException, so swallow it
172
try
173                   {
174                      pd.set(object, value);
175                   }
176                   catch (IllegalAccessException JavaDoc e1)
177                   {
178                      log.warn("Can not set " + value + " on " + object +
179                              " as it does not have a setter for this value.");
180                   }
181                }
182                else
183                   pd.set(object, index, value);
184             }
185             else
186             {
187                // It is possible that an indexed
188
// format was expected, but the
189
// entire array was sent. In such
190
// cases traverse the array and
191
// call the setter for each item.
192
if (index == 0 &&
193                        value.getClass().isArray() &&
194                        !type.getClass().isArray())
195                {
196                   for (int i = 0; i < Array.getLength(value); i++)
197                   {
198                      Object JavaDoc item =
199                              JavaUtils.convert(Array.get(value, i), type);
200                      pd.set(object, i, item);
201                   }
202                }
203                else
204                {
205                   // Can't proceed. Throw an exception that
206
// will be caught in the catch block below.
207
throw e;
208                }
209             }
210          }
211          catch (Exception JavaDoc ex)
212          {
213             // Throw a SAX exception with an informative
214
// message.
215
String JavaDoc field = pd.getName();
216             if (index >= 0)
217             {
218                field += "[" + index + "]";
219             }
220
221             String JavaDoc valueType = "null";
222             if (value != null)
223                valueType = value.getClass().getName();
224
225             log.error(Messages.getMessage("cantConvert02",
226                     new String JavaDoc[]{valueType, field, pd.getType().getName()}));
227
228             if (ex instanceof InvocationTargetException JavaDoc)
229             {
230                Throwable JavaDoc t = ((InvocationTargetException JavaDoc)ex).getTargetException();
231                if (t != null)
232                {
233                   String JavaDoc classname = this.object.getClass().getName();
234                   //show the context where this exception occured.
235
throw new SAXException JavaDoc(Messages.getMessage("cantConvert04",
236                           new String JavaDoc[]{
237                              classname,
238                              field,
239                              (value == null) ? null : value.toString(),
240                              t.getMessage()}));
241                }
242             }
243             throw new SAXException JavaDoc(ex);
244          }
245       }
246    }
247 }
248
249
Popular Tags