KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.broker.metadata.fieldaccess;
2
3 /* Copyright 2003-2005 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.metadata.MetadataException;
21 import org.apache.ojb.broker.util.logging.Logger;
22 import org.apache.ojb.broker.util.logging.LoggerFactory;
23
24 /**
25  * A {@link org.apache.ojb.broker.metadata.fieldaccess.PersistentField} implementation accesses a property
26  * from a {@link org.apache.commons.beanutils.DynaBean}.
27  * Note that because of the way that PersistentField works,
28  * at run time the type of the field could actually be different, since
29  * it depends on the DynaClass of the DynaBean that is given at runtime.
30  * <p>
31  * This implementation does not support nested fields.
32  * </p>
33  *
34  * @version $Id: PersistentFieldDynaBeanImpl.java,v 1.8.2.2 2005/12/21 22:26:41 tomdz Exp $
35  */

36 public class PersistentFieldDynaBeanImpl extends PersistentFieldBase
37 {
38     /*
39     TODO: Don't know if it is possible to support nested fields with DynaBeans. This
40     version does not support nested fields
41     */

42     private static final long serialVersionUID = 4728858060905429509L;
43
44     public PersistentFieldDynaBeanImpl()
45     {
46         super();
47     }
48
49     public PersistentFieldDynaBeanImpl(Class JavaDoc aPropertyType, String JavaDoc aPropertyName)
50     {
51         super(aPropertyType, aPropertyName);
52         checkNested(aPropertyName);
53     }
54
55     public void set(Object JavaDoc anObject, Object JavaDoc aValue) throws MetadataException
56     {
57         if(anObject == null) return;
58         if (anObject instanceof DynaBean)
59         {
60             DynaBean dynaBean = (DynaBean) anObject;
61             try
62             {
63                 dynaBean.set(getName(), aValue);
64             }
65             catch (Throwable JavaDoc t)
66             {
67                 String JavaDoc msg = dynaBean.getClass().getName();
68                 logSetProblem(anObject, aValue, msg);
69                 throw new PersistenceBrokerException(t);
70             }
71         }
72         else
73         {
74             String JavaDoc msg = "the object is not a DynaBean";
75             logSetProblem(anObject, aValue, msg);
76             throw new PersistenceBrokerException(msg);
77         }
78     }
79
80     public Object JavaDoc get(Object JavaDoc anObject) throws MetadataException
81     {
82         if(anObject == null) return null;
83         if (anObject instanceof DynaBean)
84         {
85             DynaBean dynaBean = (DynaBean) anObject;
86             try
87             {
88                 return dynaBean.get(getName());
89             }
90             catch (Throwable JavaDoc t)
91             {
92                 String JavaDoc msg = dynaBean.getClass().getName();
93                 logGetProblem(anObject, msg);
94                 throw new PersistenceBrokerException(t);
95             }
96         }
97         else
98         {
99             String JavaDoc msg = "the object is not a DynaBean";
100             logGetProblem(anObject, msg);
101             throw new PersistenceBrokerException(msg);
102         }
103     }
104
105     private void checkNested(String JavaDoc fieldName)
106     {
107         if(fieldName.indexOf(PATH_TOKEN) > -1)
108         {
109             throw new MetadataException("This implementation does not support nested fields");
110         }
111     }
112
113     public Class JavaDoc getType()
114     {
115         return getDeclaringClass();
116     }
117
118     protected boolean makeAccessible()
119     {
120         return false;
121     }
122
123     public boolean usesAccessorsAndMutators()
124     {
125         return false;
126     }
127
128     /**
129      * Let's give the user some hints as to what could be wrong.
130      */

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

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