001: package net.sourceforge.squirrel_sql.plugins.hibernate;
002:
003: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
004: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
005: import net.sourceforge.squirrel_sql.fw.util.Utilities;
006: import net.sourceforge.squirrel_sql.plugins.hibernate.mapping.MappedClassInfo;
007: import net.sourceforge.squirrel_sql.plugins.hibernate.mapping.HibernatePropertyInfo;
008:
009: import java.net.URLClassLoader;
010: import java.util.*;
011: import java.sql.Connection;
012: import java.sql.SQLException;
013: import java.lang.reflect.Method;
014: import java.lang.reflect.InvocationTargetException;
015:
016: import org.hibernate.persister.entity.AbstractEntityPersister;
017:
018: public class HibernateConnection {
019: private static ILogger s_log = LoggerController
020: .createLogger(HibernateConnection.class);
021: private Object _sessionFactoryImpl;
022: private URLClassLoader _cl;
023: private ArrayList<MappedClassInfo> _mappedClassInfos;
024:
025: public HibernateConnection(Object sessionFactoryImpl,
026: URLClassLoader cl) {
027: _sessionFactoryImpl = sessionFactoryImpl;
028: _cl = cl;
029: }
030:
031: public ArrayList<String> generateSQL(String hqlQuery) {
032: try {
033:
034: Class sessionFactoryImplementorClass = (Class) new ReflectionCaller()
035: .getClass(
036: "org.hibernate.engine.SessionFactoryImplementor",
037: _cl).getCallee();
038:
039: List<ReflectionCaller> translators = new ReflectionCaller(
040: _cl).getClass(
041: "org.hibernate.engine.query.HQLQueryPlan", _cl)
042: .callConstructor(
043: new Class[] { String.class, Boolean.TYPE,
044: Map.class,
045: sessionFactoryImplementorClass },
046: new Object[] { hqlQuery, false,
047: Collections.EMPTY_MAP,
048: _sessionFactoryImpl })
049: .callArrayMethod("getTranslators");
050:
051: ArrayList<String> ret = new ArrayList<String>();
052:
053: for (ReflectionCaller translator : translators) {
054: List sqls = (List) translator.callMethod(
055: "collectSqlStrings").getCallee();
056:
057: for (Object sql : sqls) {
058: ret.add(sql.toString());
059: }
060: }
061: return ret;
062: } catch (Exception e) {
063: throw new RuntimeException(e);
064: }
065: }
066:
067: public void close() {
068: try {
069: new ReflectionCaller(_sessionFactoryImpl)
070: .callMethod("close");
071: } catch (Throwable t) {
072: s_log.error(t);
073: }
074: _sessionFactoryImpl = null;
075: _cl = null;
076: _mappedClassInfos = null;
077: System.gc();
078:
079: }
080:
081: public ArrayList<MappedClassInfo> getMappedClassInfos() {
082: initMappedClassInfos();
083: return _mappedClassInfos;
084: }
085:
086: private void initMappedClassInfos() {
087: if (null != _mappedClassInfos) {
088: return;
089: }
090:
091: _mappedClassInfos = new ArrayList<MappedClassInfo>();
092:
093: ReflectionCaller sessionFactoryImplcaller = new ReflectionCaller(
094: _sessionFactoryImpl);
095: Collection<ReflectionCaller> persisters = sessionFactoryImplcaller
096: .callMethod("getAllClassMetadata")
097: .callCollectionMethod("values");
098:
099: for (ReflectionCaller persister : persisters) {
100: Object entityMode_POJO = persister.getClass(
101: "org.hibernate.EntityMode", _cl).getField("POJO")
102: .getCallee();
103: Class mappedClass = (Class) persister.callMethod(
104: "getMappedClass", new Object[] { entityMode_POJO })
105: .getCallee();
106:
107: String identifierPropertyName = (String) persister
108: .callMethod("getIdentifierPropertyName")
109: .getCallee();
110:
111: Class identifierPropertyClass = persister.callMethod(
112: "getIdentifierType").callMethod("getReturnedClass")
113: .getCalleeClass();
114:
115: String identifierPropertyClassName = identifierPropertyClass
116: .getName();
117:
118: String tableName = (String) persister.callMethod(
119: "getTableName").getCallee();
120: String[] identifierColumnNames = (String[]) persister
121: .callMethod("getIdentifierColumnNames").getCallee();
122:
123: HibernatePropertyInfo identifierPropInfo = new HibernatePropertyInfo(
124: identifierPropertyName,
125: identifierPropertyClassName, tableName,
126: identifierColumnNames);
127:
128: identifierPropInfo.setIdentifier(true);
129:
130: String[] propertyNames = (String[]) persister.callMethod(
131: "getPropertyNames").getCallee();
132:
133: HibernatePropertyInfo[] infos = new HibernatePropertyInfo[propertyNames.length];
134: for (int i = 0; i < propertyNames.length; i++) {
135: ReflectionCaller propertyTypeCaller = persister
136: .callMethod("getPropertyType",
137: new String[] { propertyNames[i] });
138: String mayBeCollectionTypeName = propertyTypeCaller
139: .callMethod("getReturnedClass")
140: .getCalleeClass().getName();
141:
142: String propTableName = (String) persister.callMethod(
143: "getPropertyTableName",
144: new String[] { propertyNames[i] }).getCallee();
145: String[] propertyColumnNames = (String[]) persister
146: .callMethod("getPropertyColumnNames",
147: new String[] { propertyNames[i] })
148: .getCallee();
149:
150: try {
151: // If this isn't instanceof org.hibernate.type.CollectionType a NoSuchMethodException will be thrown
152: String role = (String) propertyTypeCaller
153: .callMethod("getRole").getCallee();
154:
155: ReflectionCaller collectionMetaDataCaller = sessionFactoryImplcaller
156: .callMethod("getCollectionMetadata",
157: new Object[] { role });
158: String typeName = collectionMetaDataCaller
159: .callMethod("getElementType").callMethod(
160: "getReturnedClass")
161: .getCalleeClass().getName();
162:
163: infos[i] = new HibernatePropertyInfo(
164: propertyNames[i], typeName, propTableName,
165: propertyColumnNames);
166: infos[i]
167: .setCollectionClassName(mayBeCollectionTypeName);
168: } catch (RuntimeException e) {
169: if (Utilities.getDeepestThrowable(e) instanceof NoSuchMethodException) {
170: infos[i] = new HibernatePropertyInfo(
171: propertyNames[i],
172: mayBeCollectionTypeName, propTableName,
173: propertyColumnNames);
174: } else {
175: throw e;
176: }
177: }
178: }
179:
180: _mappedClassInfos.add(new MappedClassInfo(mappedClass
181: .getName(), tableName, identifierPropInfo, infos));
182: }
183: }
184:
185: public Connection getSqlConnection() {
186: ReflectionCaller rc = new ReflectionCaller(_sessionFactoryImpl);
187:
188: return (Connection) rc.callMethod("openSession").callMethod(
189: "getJDBCContext").callMethod("getConnectionManager")
190: .callMethod("getConnection").getCallee();
191: }
192: }
|