001    package org.apache.ojb.broker.metadata.fieldaccess;
002    
003    /* Copyright 2003-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import org.apache.commons.beanutils.DynaBean;
019    import org.apache.ojb.broker.PersistenceBrokerException;
020    import org.apache.ojb.broker.metadata.MetadataException;
021    import org.apache.ojb.broker.util.logging.Logger;
022    import org.apache.ojb.broker.util.logging.LoggerFactory;
023    
024    /**
025     * A {@link org.apache.ojb.broker.metadata.fieldaccess.PersistentField} implementation accesses a property
026     * from a {@link org.apache.commons.beanutils.DynaBean}.
027     * Note that because of the way that PersistentField works,
028     * at run time the type of the field could actually be different, since
029     * it depends on the DynaClass of the DynaBean that is given at runtime.
030     * <p>
031     * This implementation does not support nested fields.
032     * </p>
033     *
034     * @version $Id: PersistentFieldDynaBeanImpl.java,v 1.1 2007-08-24 22:17:35 ewestfal Exp $
035     */
036    public class PersistentFieldDynaBeanImpl extends PersistentFieldBase
037    {
038        /*
039        TODO: Don't know if it is possible to support nested fields with DynaBeans. This
040        version does not support nested fields
041        */
042        private static final long serialVersionUID = 4728858060905429509L;
043    
044        public PersistentFieldDynaBeanImpl()
045        {
046            super();
047        }
048    
049        public PersistentFieldDynaBeanImpl(Class aPropertyType, String aPropertyName)
050        {
051            super(aPropertyType, aPropertyName);
052            checkNested(aPropertyName);
053        }
054    
055        public void set(Object anObject, Object aValue) throws MetadataException
056        {
057            if(anObject == null) return;
058            if (anObject instanceof DynaBean)
059            {
060                DynaBean dynaBean = (DynaBean) anObject;
061                try
062                {
063                    dynaBean.set(getName(), aValue);
064                }
065                catch (Throwable t)
066                {
067                    String msg = dynaBean.getClass().getName();
068                    logSetProblem(anObject, aValue, msg);
069                    throw new PersistenceBrokerException(t);
070                }
071            }
072            else
073            {
074                String msg = "the object is not a DynaBean";
075                logSetProblem(anObject, aValue, msg);
076                throw new PersistenceBrokerException(msg);
077            }
078        }
079    
080        public Object get(Object anObject) throws MetadataException
081        {
082            if(anObject == null) return null;
083            if (anObject instanceof DynaBean)
084            {
085                DynaBean dynaBean = (DynaBean) anObject;
086                try
087                {
088                    return dynaBean.get(getName());
089                }
090                catch (Throwable t)
091                {
092                    String msg = dynaBean.getClass().getName();
093                    logGetProblem(anObject, msg);
094                    throw new PersistenceBrokerException(t);
095                }
096            }
097            else
098            {
099                String msg = "the object is not a DynaBean";
100                logGetProblem(anObject, msg);
101                throw new PersistenceBrokerException(msg);
102            }
103        }
104    
105        private void checkNested(String 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 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 anObject, Object aValue, String 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 anObject, String 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    }