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;
}
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;
}
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;
}
}
}
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);
}
}
}
}
}
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;
}
Aggregations