KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > metadata > fieldaccess > PersistentFieldDynaBeanAccessImpl


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

17
18 import org.apache.commons.beanutils.DynaBean;
19 import org.apache.ojb.broker.PersistenceBrokerException;
20 import org.apache.ojb.broker.util.logging.Logger;
21 import org.apache.ojb.broker.util.logging.LoggerFactory;
22
23 /**
24  * A {@link PersistentField} implementation accesses a property
25  * from a {@link org.apache.commons.beanutils.DynaBean}.
26  * Note that because of the way that PersistentField works,
27  * at run time the type of the field could actually be different, since
28  * it depends on the DynaClass of the DynaBean that is given at runtime.
29  *
30  * @author James Strachan
31  * @version $Id: PersistentFieldDynaBeanAccessImpl.java,v 1.5 2004/04/04 23:53:35 brianm Exp $
32  */

33 public class PersistentFieldDynaBeanAccessImpl extends AbstractPersistentField
34 {
35     private static final long serialVersionUID = 4728858060905429509L;
36     public PersistentFieldDynaBeanAccessImpl()
37     {
38         super();
39     }
40
41     public PersistentFieldDynaBeanAccessImpl(Class JavaDoc aPropertyType, String JavaDoc aPropertyName)
42     {
43         super(aPropertyType, aPropertyName);
44     }
45
46     /**
47      * Sets aValue for anObject
48      */

49     public void doSet(Object JavaDoc anObject, Object JavaDoc aValue) throws PersistenceBrokerException
50     {
51         if (anObject instanceof DynaBean)
52         {
53             DynaBean dynaBean = (DynaBean) anObject;
54             try
55             {
56                 dynaBean.set(getName(), aValue);
57             }
58             catch (Throwable JavaDoc t)
59             {
60                 String JavaDoc msg = dynaBean.getClass().getName();
61                 logSetProblem(anObject, aValue, msg);
62                 throw new PersistenceBrokerException(t);
63             }
64         }
65         else
66         {
67             String JavaDoc msg = "the object is not a DynaBean";
68             logSetProblem(anObject, aValue, msg);
69             throw new PersistenceBrokerException(msg);
70         }
71     }
72
73     /**
74      * Get the Value from anObject
75      */

76     public Object JavaDoc doGet(Object JavaDoc anObject) throws PersistenceBrokerException
77     {
78         if (anObject instanceof DynaBean)
79         {
80             DynaBean dynaBean = (DynaBean) anObject;
81             try
82             {
83                 return dynaBean.get(getName());
84             }
85             catch (Throwable JavaDoc t)
86             {
87                 String JavaDoc msg = dynaBean.getClass().getName();
88                 logGetProblem(anObject, msg);
89                 throw new PersistenceBrokerException(t);
90             }
91         }
92         else
93         {
94             String JavaDoc msg = "the object is not a DynaBean";
95             logGetProblem(anObject, msg);
96             throw new PersistenceBrokerException(msg);
97         }
98     }
99
100     /*
101     override these three methods to avoid use
102     of Field instances
103     */

104     public String JavaDoc getName()
105     {
106         return this.fieldName;
107     }
108     public Class JavaDoc getType()
109     {
110         return this.rootObjectType;
111     }
112     public Class JavaDoc getDeclaringClass()
113     {
114         return DynaBean.class;
115     }
116
117     public boolean makeAccessible()
118     {
119         return false;
120     }
121
122     public boolean usesAccessorsAndMutators()
123     {
124         return false;
125     }
126
127     public String JavaDoc toString()
128     {
129         return "DynaBean." + getName() + ": " + super.toString();
130     }
131
132     /**
133      * Let's give the user some hints as to what could be wrong.
134      */

135     protected void logSetProblem(Object JavaDoc anObject, Object JavaDoc aValue, String JavaDoc msg)
136     {
137         Logger logger = LoggerFactory.getDefaultLogger();
138         logger.error("Error in operation [set] of object [" + this.getClass().getName() + "], " + msg);
139         logger.error("Property Name [" + getName() + "]");
140         if (anObject instanceof DynaBean)
141         {
142             DynaBean dynaBean = (DynaBean) anObject;
143             logger.error("anObject was DynaClass [" + dynaBean.getDynaClass().getName() + "]");
144         }
145         else if (anObject != null)
146         {
147             logger.error("anObject was class [" + anObject.getClass().getName() + "]");
148         }
149         else
150         {
151             logger.error("anObject was null");
152         }
153         if (aValue != null)
154             logger.error("aValue was class [" + aValue.getClass().getName() + "]");
155         else
156             logger.error("aValue was null");
157     }
158
159     /**
160      * Let's give the user some hints as to what could be wrong.
161      */

162     protected void logGetProblem(Object JavaDoc anObject, String JavaDoc msg)
163     {
164         Logger logger = LoggerFactory.getDefaultLogger();
165         logger.error("Error in operation [get of object [" + this.getClass().getName() + "], " + msg);
166         logger.error("Property Name [" + getName() + "]");
167         if (anObject instanceof DynaBean)
168         {
169             DynaBean dynaBean = (DynaBean) anObject;
170             logger.error("anObject was DynaClass [" + dynaBean.getDynaClass().getName() + "]");
171         }
172         else if (anObject != null)
173         {
174             logger.error("anObject was class [" + anObject.getClass().getName() + "]");
175         }
176         else
177         {
178             logger.error("anObject was null");
179         }
180     }
181 }
182
Popular Tags