001: package net.sourceforge.squirrel_sql.plugins.hibernate;
002:
003: import net.sourceforge.squirrel_sql.client.session.ISession;
004: import net.sourceforge.squirrel_sql.plugins.hibernate.configuration.HibernateConfiguration;
005:
006: import javax.swing.*;
007: import java.io.File;
008: import java.lang.reflect.Method;
009: import java.net.URL;
010: import java.net.URLClassLoader;
011:
012: public class HibnerateConnector {
013: private HibnerateConnectorListener _hibnerateConnectorListener;
014:
015: public HibnerateConnector(
016: HibnerateConnectorListener hibnerateConnectorListener) {
017: _hibnerateConnectorListener = hibnerateConnectorListener;
018: }
019:
020: public void connect(final HibernateConfiguration cfg,
021: final ISession session) {
022: Runnable runnable = new Runnable() {
023: public void run() {
024: doConnect(cfg, session);
025: }
026: };
027:
028: final Thread thread = new Thread(runnable);
029:
030: thread.setPriority(Thread.MIN_PRIORITY);
031:
032: SwingUtilities.invokeLater(new Runnable() {
033: public void run() {
034: thread.run();
035: }
036: });
037: }
038:
039: private void doConnect(HibernateConfiguration cfg, ISession session) {
040: try {
041: URLClassLoader cl = getClassLoader(cfg);
042: Thread.currentThread().setContextClassLoader(cl);
043:
044: Object sessionFactoryImpl = null;
045:
046: if (cfg.isUserDefinedProvider()) {
047: String provider = cfg.getProvider();
048: Class<?> providerClass = cl.loadClass(provider);
049:
050: Object sessionFactoryProviderImpl = providerClass
051: .newInstance();
052:
053: sessionFactoryImpl = new ReflectionCaller(
054: sessionFactoryProviderImpl).callMethod(
055: "getSessionFactoryImpl").getCallee();
056: } else if (cfg.isJPA()) {
057: String persistenceUnitName = cfg
058: .getPersistenceUnitName();
059: Class<?> persistenceClass = cl
060: .loadClass("javax.persistence.Persistence");
061:
062: Method createMeth = persistenceClass.getMethod(
063: "createEntityManagerFactory", String.class);
064: Object hibernateEntityManagerFactory = createMeth
065: .invoke(persistenceClass, persistenceUnitName);
066: ReflectionCaller rc = new ReflectionCaller(
067: hibernateEntityManagerFactory);
068: sessionFactoryImpl = rc.callMethod("getSessionFactory")
069: .getCallee();
070: } else {
071: Class<?> confiugrationClass = cl
072: .loadClass("org.hibernate.cfg.Configuration");
073: ReflectionCaller rc = new ReflectionCaller(
074: confiugrationClass.newInstance());
075:
076: sessionFactoryImpl = rc.callMethod("configure")
077: .callMethod("buildSessionFactory").getCallee();
078: }
079:
080: HibernateConnection con = new HibernateConnection(
081: sessionFactoryImpl, cl);
082: sendConnection(con, cfg);
083:
084: Thread.currentThread().setContextClassLoader(null);
085:
086: } catch (final Throwable t) {
087: SwingUtilities.invokeLater(new Runnable() {
088: public void run() {
089: _hibnerateConnectorListener.connectFailed(t);
090: }
091: });
092: }
093: }
094:
095: private void sendConnection(final HibernateConnection con,
096: final HibernateConfiguration cfg) {
097: SwingUtilities.invokeLater(new Runnable() {
098: public void run() {
099: _hibnerateConnectorListener.connected(con, cfg);
100: }
101: });
102: }
103:
104: private URLClassLoader getClassLoader(HibernateConfiguration cfg)
105: throws Exception {
106: String[] classpath = cfg.getClassPathEntries();
107:
108: URL[] classpathUrls = new URL[classpath.length];
109:
110: for (int i = 0; i < classpathUrls.length; i++) {
111: classpathUrls[i] = new File(classpath[i]).toURL();
112: }
113:
114: URLClassLoader cl = new URLClassLoader(classpathUrls, null);
115:
116: return cl;
117: }
118: }
|