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.8.2.2 2005/12/21 22:26:41 tomdz Exp $
035: */
036: public class PersistentFieldDynaBeanImpl extends PersistentFieldBase {
037: /*
038: TODO: Don't know if it is possible to support nested fields with DynaBeans. This
039: version does not support nested fields
040: */
041: private static final long serialVersionUID = 4728858060905429509L;
042:
043: public PersistentFieldDynaBeanImpl() {
044: super ();
045: }
046:
047: public PersistentFieldDynaBeanImpl(Class aPropertyType,
048: String aPropertyName) {
049: super (aPropertyType, aPropertyName);
050: checkNested(aPropertyName);
051: }
052:
053: public void set(Object anObject, Object aValue)
054: throws MetadataException {
055: if (anObject == null)
056: return;
057: if (anObject instanceof DynaBean) {
058: DynaBean dynaBean = (DynaBean) anObject;
059: try {
060: dynaBean.set(getName(), aValue);
061: } catch (Throwable t) {
062: String msg = dynaBean.getClass().getName();
063: logSetProblem(anObject, aValue, msg);
064: throw new PersistenceBrokerException(t);
065: }
066: } else {
067: String msg = "the object is not a DynaBean";
068: logSetProblem(anObject, aValue, msg);
069: throw new PersistenceBrokerException(msg);
070: }
071: }
072:
073: public Object get(Object anObject) throws MetadataException {
074: if (anObject == null)
075: return null;
076: if (anObject instanceof DynaBean) {
077: DynaBean dynaBean = (DynaBean) anObject;
078: try {
079: return dynaBean.get(getName());
080: } catch (Throwable t) {
081: String msg = dynaBean.getClass().getName();
082: logGetProblem(anObject, msg);
083: throw new PersistenceBrokerException(t);
084: }
085: } else {
086: String msg = "the object is not a DynaBean";
087: logGetProblem(anObject, msg);
088: throw new PersistenceBrokerException(msg);
089: }
090: }
091:
092: private void checkNested(String fieldName) {
093: if (fieldName.indexOf(PATH_TOKEN) > -1) {
094: throw new MetadataException(
095: "This implementation does not support nested fields");
096: }
097: }
098:
099: public Class getType() {
100: return getDeclaringClass();
101: }
102:
103: protected boolean makeAccessible() {
104: return false;
105: }
106:
107: public boolean usesAccessorsAndMutators() {
108: return false;
109: }
110:
111: /**
112: * Let's give the user some hints as to what could be wrong.
113: */
114: protected void logSetProblem(Object anObject, Object aValue,
115: String msg) {
116: Logger logger = LoggerFactory.getDefaultLogger();
117: logger.error("Error in operation [set] of object ["
118: + this .getClass().getName() + "], " + msg);
119: logger.error("Property Name [" + getName() + "]");
120: if (anObject instanceof DynaBean) {
121: DynaBean dynaBean = (DynaBean) anObject;
122: logger.error("anObject was DynaClass ["
123: + dynaBean.getDynaClass().getName() + "]");
124: } else if (anObject != null) {
125: logger.error("anObject was class ["
126: + anObject.getClass().getName() + "]");
127: } else {
128: logger.error("anObject was null");
129: }
130: if (aValue != null)
131: logger.error("aValue was class ["
132: + aValue.getClass().getName() + "]");
133: else
134: logger.error("aValue was null");
135: }
136:
137: /**
138: * Let's give the user some hints as to what could be wrong.
139: */
140: protected void logGetProblem(Object anObject, String msg) {
141: Logger logger = LoggerFactory.getDefaultLogger();
142: logger.error("Error in operation [get of object ["
143: + this .getClass().getName() + "], " + msg);
144: logger.error("Property Name [" + getName() + "]");
145: if (anObject instanceof DynaBean) {
146: DynaBean dynaBean = (DynaBean) anObject;
147: logger.error("anObject was DynaClass ["
148: + dynaBean.getDynaClass().getName() + "]");
149: } else if (anObject != null) {
150: logger.error("anObject was class ["
151: + anObject.getClass().getName() + "]");
152: } else {
153: logger.error("anObject was null");
154: }
155: }
156: }
|