001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049:
050: package org.jaffa.persistence.engines.jdbcengine.util;
051:
052: import org.apache.log4j.Logger;
053: import java.util.*;
054: import java.io.IOException;
055: import java.lang.reflect.AccessibleObject;
056: import java.lang.reflect.Field;
057: import java.lang.reflect.Method;
058: import java.lang.reflect.InvocationTargetException;
059: import java.sql.ResultSet;
060: import java.sql.SQLException;
061: import org.jaffa.persistence.IPersistent;
062: import org.jaffa.persistence.engines.jdbcengine.configservice.ClassMetaData;
063: import org.jaffa.persistence.engines.jdbcengine.querygenerator.DataTranslator;
064: import org.jaffa.persistence.engines.jdbcengine.proxy.PersistentInstanceFactory;
065:
066: /**
067: * The MoldingService uses reflection to create/update persistent objects using ClassMetaData defintions.
068: * This can also set the initial state of a persistent object from the ResultSet.
069: */
070: public class MoldingService {
071: private static final Logger log = Logger
072: .getLogger(MoldingService.class);
073:
074: /** Prevent instantiation of this class, cos that would be pointless.*/
075: private MoldingService() {
076: }
077:
078: /** Creates an instance of the persistent class, as defined in the input ClassMetaData defintion.
079: * @param classMetaData The ClassMetaData object, whose corresponding persistent class is to be instantiated.
080: * @throws ClassNotFoundException if the Persistent class is not found.
081: * @throws InstantiationException if this Class represents an abstract class, an interface, an array class, a primitive type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason.
082: * @throws IllegalAccessException if the class or its nullary constructor is not accessible.
083: * @return an instance of the Persistent class, defined in the ClassMetaData defintion.
084: */
085: public static IPersistent getObject(ClassMetaData classMetaData)
086: throws ClassNotFoundException, InstantiationException,
087: IllegalAccessException {
088: String className = classMetaData.getClassName();
089: Class clazz = Class.forName(className);
090: Object object = PersistentInstanceFactory
091: .newPersistentInstance(clazz);
092: return (IPersistent) object;
093: }
094:
095: /** This gets the value of the attributeName from the Persistent object, using the accessor method cached in the ClassMetaData defintion.
096: * @param object The Persistent object.
097: * @param classMetaData The ClassMetaData definition for the Persistent object.
098: * @param attributeName The attribute whose value will be returned.
099: * @throws IllegalAccessException if the accessor Method object enforces Java language access control and the underlying method is inaccessible.
100: * @throws InvocationTargetException if the accessor method throws an exception.
101: * @return the value of the attribute.
102: */
103: public static Object getInstanceValue(IPersistent object,
104: ClassMetaData classMetaData, String attributeName)
105: throws IllegalAccessException, InvocationTargetException {
106: AccessibleObject getter = classMetaData
107: .getAccessor(attributeName);
108: if (getter instanceof Field)
109: return ((Field) getter).get(object);
110: else
111: return ((Method) getter).invoke(object, new Object[0]);
112: }
113:
114: /** This sets the value of the attribute from the Persistent object, using the mutator method cached in the ClassMetaData defintion.
115: * @param object The Persistent object.
116: * @param classMetaData The ClassMetaData definition for the Persistent object.
117: * @param attributeName The attribute whose value will be set.
118: * @param value The value to be set.
119: * @throws IllegalAccessException if the mutator Method object enforces Java language access control and the underlying method is inaccessible.
120: * @throws InvocationTargetException if the mutator method throws an exception.
121: */
122: public static void setInstanceValue(IPersistent object,
123: ClassMetaData classMetaData, String attributeName,
124: Object value) throws IllegalAccessException,
125: InvocationTargetException {
126: if (PersistentInstanceFactory.getActualPersistentClass(object) == object
127: .getClass()) {
128: // The input object is a regular class
129: AccessibleObject setter = classMetaData
130: .getMutator(attributeName);
131: if (setter instanceof Field)
132: ((Field) setter).set(object, value);
133: else
134: ((Method) setter)
135: .invoke(object, new Object[] { value });
136: } else {
137: // The input object is a Proxy
138: PersistentInstanceFactory.setInstanceValue(object,
139: attributeName, value);
140: }
141: }
142:
143: /** Creates an instance of the persistent class, as defined in the input ClassMetaData defintion.
144: * It will set the attributes using the values from the ResultSet.
145: * @param classMetaData The ClassMetaData object, whose corresponding persistent class is to be instantiated.
146: * @param rs The ResultSet used for creating the initial state of the persistent object.
147: * @param engineType The engine type as defined in init.xml
148: * @throws ClassNotFoundException if the Persistent class is not found.
149: * @throws InstantiationException if this Class represents an abstract class, an interface, an array class, a primitive type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason.
150: * @throws IllegalAccessException if the class or its nullary constructor is not accessible.
151: * @throws InvocationTargetException if the mutator method throws an exception.
152: * @throws SQLException if a database access error occurs.
153: * @throws IOException if any error occurs in reading the data from the database.
154: * @return an instance of the Persistent class, defined in the ClassMetaData defintion.
155: */
156: public static IPersistent getObject(ClassMetaData classMetaData,
157: ResultSet rs, String engineType)
158: throws ClassNotFoundException, InstantiationException,
159: IllegalAccessException, InvocationTargetException,
160: SQLException, IOException {
161: IPersistent object = getObject(classMetaData);
162:
163: // set the keys
164: if (classMetaData.getAllKeyFieldNames() != null)
165: populateObject(classMetaData, rs, object, classMetaData
166: .getAllKeyFieldNames().keySet().iterator(),
167: engineType);
168:
169: // set all the other fields
170: if (classMetaData.getAttributes() != null)
171: populateObject(classMetaData, rs, object, classMetaData
172: .getAttributes().keySet().iterator(), engineType);
173:
174: return object;
175: }
176:
177: private static void populateObject(ClassMetaData classMetaData,
178: ResultSet rs, IPersistent object, Iterator fieldItr,
179: String engineType) throws IllegalAccessException,
180: InvocationTargetException, SQLException, IOException {
181: while (fieldItr.hasNext()) {
182: String fieldName = (String) fieldItr.next();
183: String columnName = classMetaData.getSqlName(fieldName);
184: String typeName = classMetaData.getSqlType(fieldName);
185: Object value = DataTranslator.getAppObject(rs, columnName,
186: typeName, engineType);
187: setInstanceValue(object, classMetaData, fieldName, value);
188: }
189: }
190: }
|