Search in sources :

Example 6 with ManagedDriver

use of net.sourceforge.sqlexplorer.dbproduct.ManagedDriver in project tdq-studio-se by Talend.

the class SqlexplorerService method getDriver.

/*
     * (non-Javadoc)
     * 
     * @see org.talend.dataprofiler.service.ISqlexplorerService#getDriver(java.lang.String, java.lang.String)
     */
@Override
public Driver getDriver(String driverClassName, String jarsPath) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
    Driver driver = null;
    if (StringUtils.isNotEmpty(jarsPath)) {
        try {
            driver = this.createGenericJDBC(jarsPath, driverClassName);
        } catch (Exception e) {
            log.error(e, e);
        }
        return driver;
    }
    SQLExplorerPlugin sqlExplorerPlugin = SQLExplorerPlugin.getDefault();
    if (sqlExplorerPlugin != null) {
        net.sourceforge.sqlexplorer.dbproduct.DriverManager driverModel = sqlExplorerPlugin.getDriverModel();
        try {
            Collection<ManagedDriver> drivers = driverModel.getDrivers();
            for (ManagedDriver managedDriver : drivers) {
                LinkedList<String> jars = managedDriver.getJars();
                List<URL> urls = new ArrayList<URL>();
                for (int i = 0; i < jars.size(); i++) {
                    File file = new File(jars.get(i));
                    if (file.exists()) {
                        urls.add(file.toURI().toURL());
                    }
                }
                if (!urls.isEmpty()) {
                    try {
                        MyURLClassLoader cl;
                        cl = new MyURLClassLoader(urls.toArray(new URL[0]));
                        Class<?> clazz = cl.findClass(driverClassName);
                        if (clazz != null) {
                            driver = (Driver) clazz.newInstance();
                            if (driver != null) {
                                return driver;
                            }
                        }
                    } catch (ClassNotFoundException e) {
                    // do nothings
                    }
                }
            }
        } catch (MalformedURLException e) {
        // do nothings
        }
    }
    if (driver == null) {
        driver = (Driver) Class.forName(driverClassName).newInstance();
    }
    return driver;
}
Also used : MalformedURLException(java.net.MalformedURLException) ManagedDriver(net.sourceforge.sqlexplorer.dbproduct.ManagedDriver) ArrayList(java.util.ArrayList) MyURLClassLoader(net.sourceforge.sqlexplorer.util.MyURLClassLoader) ManagedDriver(net.sourceforge.sqlexplorer.dbproduct.ManagedDriver) Driver(java.sql.Driver) DriverManager(net.sourceforge.sqlexplorer.dbproduct.DriverManager) PartInitException(org.eclipse.ui.PartInitException) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) IFile(org.eclipse.core.resources.IFile) File(java.io.File) SQLExplorerPlugin(net.sourceforge.sqlexplorer.plugin.SQLExplorerPlugin)

Example 7 with ManagedDriver

use of net.sourceforge.sqlexplorer.dbproduct.ManagedDriver in project tdq-studio-se by Talend.

the class SqlexplorerService method needAddDriverConnection.

@Override
public boolean needAddDriverConnection(DatabaseConnection dbConn) {
    boolean isNeed = false;
    DriverManager driverManager = SQLExplorerPlugin.getDefault().getDriverModel();
    String id = AliasAndManaDriverHelper.getInstance().joinManagedDriverId(dbConn);
    ManagedDriver manDr = driverManager.getDriver(id);
    if (manDr == null) {
        isNeed = true;
    } else if (!manDr.isDriverClassLoaded()) {
        loadDriverByLibManageSystem(dbConn);
        if (!manDr.isDriverClassLoaded()) {
            isNeed = true;
        }
    }
    return isNeed;
}
Also used : ManagedDriver(net.sourceforge.sqlexplorer.dbproduct.ManagedDriver) DriverManager(net.sourceforge.sqlexplorer.dbproduct.DriverManager)

Example 8 with ManagedDriver

use of net.sourceforge.sqlexplorer.dbproduct.ManagedDriver in project tdq-studio-se by Talend.

the class SqlexplorerService method addConnetionAliasToSQLPlugin.

@Override
public void addConnetionAliasToSQLPlugin(ModelElement... dataproviders) {
    SQLExplorerPlugin sqlPlugin = SQLExplorerPlugin.getDefault();
    AliasManager aliasManager = sqlPlugin.getAliasManager();
    DriverManager driverManager = sqlPlugin.getDriverModel();
    List<String> tdqSupportDBType = MetadataConnectionUtils.getTDQSupportDBTemplate();
    // if all dataproviders are not supported on DQ side,don't save files SQLAliases.xml and
    // SQLDrivers.xml.Otherwise,save it.
    AliasAndManaDriverHelper aliasManaDriverHelper = AliasAndManaDriverHelper.getInstance();
    for (ModelElement dataProvider : dataproviders) {
        try {
            Connection connection = SwitchHelpers.CONNECTION_SWITCH.doSwitch(dataProvider);
            // MOD bug mzhao filter the other connections except database connection.
            if (connection != null && connection instanceof DatabaseConnection) {
                // TDQ-8379 do nothing if the database type isn't supproted on DQ side.
                DatabaseConnection dbConn = ((DatabaseConnection) connection);
                String databaseType = dbConn.getDatabaseType();
                if (!tdqSupportDBType.contains(databaseType)) {
                    continue;
                }
                // only new Alias when it is not in aliasManager
                Alias alias = aliasManager.getAlias(dataProvider.getName());
                String url = JavaSqlFactory.getURL(connection);
                // if the alias is not null and the url is same with the connection, this means the alias is already
                // exist; if the alias is not null but hte url is not same with the connection, this means the
                // connection has been overwrite , need to rebuild the alias
                boolean aliasExist = alias != null && StringUtils.equals(url, alias.getUrl());
                if (!aliasExist) {
                    if (alias == null) {
                        alias = new Alias(dataProvider.getName());
                    }
                    String user = JavaSqlFactory.getUsername(connection);
                    // MOD gdbu 2011-3-17 bug 19539
                    String password = JavaSqlFactory.getPassword(connection);
                    // ~19539
                    // user should not be null
                    // $NON-NLS-1$
                    user = user == null ? "" : user;
                    // password should not be null
                    // $NON-NLS-1$
                    password = password == null ? "" : password;
                    // is serialized correctly.
                    assert user != null;
                    assert password != null;
                    User previousUser = new User(user, password);
                    previousUser.setDatabaseConnection(dbConn);
                    alias.setDefaultUser(previousUser);
                    alias.setAutoLogon(false);
                    alias.setConnectAtStartup(true);
                    alias.setUrl(url);
                    ManagedDriver manDr = aliasManaDriverHelper.getManaDriverByConnection(dbConn);
                    if (manDr == null) {
                        manDr = aliasManaDriverHelper.createNewManagerDriver(dbConn);
                        driverManager.addDriver(manDr);
                    } else if (!manDr.isDriverClassLoaded()) {
                        this.loadDriverByLibManageSystem(dbConn);
                    }
                    if (manDr != null) {
                        alias.setDriver(manDr);
                    }
                }
                if (!aliasManager.contains(alias) && alias.getName() != null) {
                    aliasManager.addAlias(alias);
                }
                // MOD Qiongli TDQ-6166 just put once for every Alias
                if (sqlPlugin.getPropertyFile().get(alias) == null) {
                    sqlPlugin.getPropertyFile().put(alias, getPropertyFile(dataProvider));
                }
                aliasManager.modelChanged();
            }
        } catch (Throwable e) {
            // MOD scorreia 2010-07-24 catch all exceptions
            log.error(e, e);
            continue;
        }
    }
}
Also used : User(net.sourceforge.sqlexplorer.dbproduct.User) ManagedDriver(net.sourceforge.sqlexplorer.dbproduct.ManagedDriver) AliasAndManaDriverHelper(net.sourceforge.sqlexplorer.util.AliasAndManaDriverHelper) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) Connection(org.talend.core.model.metadata.builder.connection.Connection) AliasManager(net.sourceforge.sqlexplorer.dbproduct.AliasManager) ModelElement(orgomg.cwm.objectmodel.core.ModelElement) Alias(net.sourceforge.sqlexplorer.dbproduct.Alias) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) DriverManager(net.sourceforge.sqlexplorer.dbproduct.DriverManager) SQLExplorerPlugin(net.sourceforge.sqlexplorer.plugin.SQLExplorerPlugin)

Example 9 with ManagedDriver

use of net.sourceforge.sqlexplorer.dbproduct.ManagedDriver in project tdq-studio-se by Talend.

the class SqlexplorerService method loadManagedDriverForHive.

/**
 * TDDQ-8113 load hive drive by DynamicClassLoader,then set the attribute for Hive ManagedDriver.
 *
 * @param connection
 */
private void loadManagedDriverForHive(DatabaseConnection connection) {
    DriverManager driverManager = SQLExplorerPlugin.getDefault().getDriverModel();
    String id = AliasAndManaDriverHelper.getInstance().joinManagedDriverId(connection);
    ManagedDriver manDr = driverManager.getDriver(id);
    IMetadataConnection metadataConnection = ConvertionHelper.convert(connection);
    ClassLoader classLoader = null;
    if (EDatabaseTypeName.IMPALA.getXmlName().equalsIgnoreCase(connection.getDatabaseType())) {
        classLoader = AliasAndManaDriverHelper.getInstance().getImpalaClassLoader(metadataConnection);
    } else {
        classLoader = HiveClassLoaderFactory.getInstance().getClassLoader(metadataConnection);
    }
    if (classLoader != null && classLoader instanceof DynamicClassLoader) {
        DynamicClassLoader dynClassLoader = (DynamicClassLoader) classLoader;
        String libStorePath = dynClassLoader.getLibStorePath();
        File libFolder = new File(libStorePath);
        if (libFolder.exists()) {
            List<String> relaPathLs = new ArrayList<String>();
            relaPathLs.addAll(dynClassLoader.getLibraries());
            Set<String> findAllJarPath = findAllJarPath(libFolder, relaPathLs);
            if (!findAllJarPath.isEmpty()) {
                manDr.getJars().addAll(findAllJarPath);
                try {
                    manDr.registerHiveSQLDriver(connection);
                } catch (ClassNotFoundException e) {
                    log.error(e);
                } catch (InstantiationException e) {
                    log.error(e);
                } catch (IllegalAccessException e) {
                    log.error(e);
                }
            }
        }
    }
}
Also used : DynamicClassLoader(org.talend.core.classloader.DynamicClassLoader) ManagedDriver(net.sourceforge.sqlexplorer.dbproduct.ManagedDriver) ArrayList(java.util.ArrayList) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) DynamicClassLoader(org.talend.core.classloader.DynamicClassLoader) URLClassLoader(java.net.URLClassLoader) MyURLClassLoader(net.sourceforge.sqlexplorer.util.MyURLClassLoader) DriverManager(net.sourceforge.sqlexplorer.dbproduct.DriverManager) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 10 with ManagedDriver

use of net.sourceforge.sqlexplorer.dbproduct.ManagedDriver in project tdq-studio-se by Talend.

the class AliasAndManaDriverHelper method getManaDriverByConnection.

/**
 * find a ManaDriver based on driver class name.if not found ,create a new one ManagedDriver.
 *
 * @param connection
 * @param driverManager
 * @return
 * @throws IllegalAccessException
 * @throws InstantiationException
 * @throws ClassNotFoundException
 */
public ManagedDriver getManaDriverByConnection(Connection connection) throws Exception {
    ManagedDriver manaDriver = null;
    DriverManager driverManager = SQLExplorerPlugin.getDefault().getDriverModel();
    DatabaseConnection dbConn = SwitchHelpers.DATABASECONNECTION_SWITCH.doSwitch(connection);
    String driverClass = JavaSqlFactory.getDriverClass(connection);
    if (dbConn == null || dbConn.getDatabaseType() == null || driverClass == null) {
        log.error("can not find a right ManagedDriver by null!");
        return null;
    }
    String managedDriverId = joinManagedDriverId(dbConn);
    String databaseType = dbConn.getDatabaseType();
    manaDriver = driverManager.getDriver(managedDriverId);
    return manaDriver;
}
Also used : ManagedDriver(net.sourceforge.sqlexplorer.dbproduct.ManagedDriver) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) DriverManager(net.sourceforge.sqlexplorer.dbproduct.DriverManager)

Aggregations

ManagedDriver (net.sourceforge.sqlexplorer.dbproduct.ManagedDriver)16 DriverManager (net.sourceforge.sqlexplorer.dbproduct.DriverManager)8 User (net.sourceforge.sqlexplorer.dbproduct.User)3 SQLExplorerPlugin (net.sourceforge.sqlexplorer.plugin.SQLExplorerPlugin)3 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)3 File (java.io.File)2 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2 ExplorerException (net.sourceforge.sqlexplorer.ExplorerException)2 CreateDriverDlg (net.sourceforge.sqlexplorer.dialogs.CreateDriverDlg)2 AliasAndManaDriverHelper (net.sourceforge.sqlexplorer.util.AliasAndManaDriverHelper)2 MyURLClassLoader (net.sourceforge.sqlexplorer.util.MyURLClassLoader)2 IFile (org.eclipse.core.resources.IFile)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 Font (org.eclipse.swt.graphics.Font)2 GridData (org.eclipse.swt.layout.GridData)2 GridLayout (org.eclipse.swt.layout.GridLayout)2 Button (org.eclipse.swt.widgets.Button)2 Composite (org.eclipse.swt.widgets.Composite)2