KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > reflect > generic > DataObjectAccessor


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

19
20 package org.apache.cayenne.reflect.generic;
21
22 import org.apache.cayenne.DataObject;
23 import org.apache.cayenne.reflect.Accessor;
24 import org.apache.cayenne.reflect.PropertyException;
25
26 /**
27  * A PropertyAccessor that uses DataObject API to read/write values.
28  *
29  * @since 3.0
30  * @author Andrus Adamchik
31  */

32 class DataObjectAccessor implements Accessor {
33
34     protected String JavaDoc propertyName;
35
36     DataObjectAccessor(String JavaDoc propertyName) {
37
38         if (propertyName == null) {
39             throw new IllegalArgumentException JavaDoc("Null propertyName");
40         }
41
42         this.propertyName = propertyName;
43     }
44
45     public String JavaDoc getName() {
46         return propertyName;
47     }
48
49     /**
50      * Reads the value without disturbing DataObject state. I.e. no Fault resolving occurs
51      * here.
52      */

53     public Object JavaDoc getValue(Object JavaDoc object) throws PropertyException {
54         try {
55
56             DataObject dataObject = (DataObject) object;
57             return dataObject.readPropertyDirectly(propertyName);
58         }
59         catch (ClassCastException JavaDoc e) {
60             throw new PropertyException("Object is not a DataObject: '"
61                     + object.getClass().getName()
62                     + "'", this, object, e);
63         }
64         catch (Throwable JavaDoc th) {
65             throw new PropertyException("Error reading DataObject property: "
66                     + propertyName, this, object, th);
67         }
68
69         // TODO - see TODO in 'setValue'
70
}
71
72     /**
73      * @since 3.0
74      */

75     public void setValue(Object JavaDoc object, Object JavaDoc newValue) throws PropertyException {
76
77         try {
78             ((DataObject) object).writePropertyDirectly(propertyName, newValue);
79         }
80         catch (ClassCastException JavaDoc e) {
81             throw new PropertyException("Object is not a DataObject: '"
82                     + object.getClass().getName()
83                     + "'", this, object, e);
84         }
85         catch (Throwable JavaDoc th) {
86             throw new PropertyException("Error reading DataObject property: "
87                     + propertyName, this, object, th);
88         }
89
90         // TODO, Andrus, 1/22/2006 - check for the right type? DataObject never did it
91
// itself... Doing a check (and a conversion) may be an easy way to fix CAY-399
92
}
93 }
94
Popular Tags