use of org.talend.repository.nosql.exceptions.NoSQLServerException in project tbd-studio-se by Talend.
the class CassandraMetadataHandler method getColumnDbType.
@Override
public String getColumnDbType(Object column) throws NoSQLServerException {
String dbType = null;
try {
// $NON-NLS-1$
Object type = NoSQLReflection.invokeMethod(column, "getType");
if (type != null) {
// $NON-NLS-1$
Object typeName = NoSQLReflection.invokeMethod(type, "getName");
// $NON-NLS-1$
dbType = ((String) NoSQLReflection.invokeMethod(typeName, "toString"));
if (ICassandraConstants.DBM_ID.equals(dbmsId)) {
dbType = dbType.toUpperCase();
}
}
} catch (Exception e) {
throw new NoSQLServerException(e);
}
return dbType;
}
use of org.talend.repository.nosql.exceptions.NoSQLServerException in project tbd-studio-se by Talend.
the class MongoDBConnectionUtil method getMongo4LatestVersion.
private static synchronized Object getMongo4LatestVersion(NoSQLConnection connection, ContextType contextType, ClassLoader classLoader, Map<String, String> hosts, boolean requireAuth, boolean requireEncryption) throws NoSQLServerException {
List<Object> addrs = new ArrayList<Object>();
Object mongo = null;
String user = connection.getAttributes().get(IMongoDBAttributes.USERNAME);
String pass = connection.getAttributes().get(IMongoDBAttributes.PASSWORD);
if (contextType != null) {
user = ContextParameterUtils.getOriginalValue(contextType, user);
pass = ContextParameterUtils.getOriginalValue(contextType, pass);
} else {
pass = connection.getValue(pass, false);
}
try {
Object clientSettingsBuilder = // $NON-NLS-1$ //$NON-NLS-2$
NoSQLReflection.invokeStaticMethod(// $NON-NLS-1$ //$NON-NLS-2$
"com.mongodb.MongoClientSettings", // $NON-NLS-1$ //$NON-NLS-2$
"builder", new Object[0], classLoader);
Object clusterSettingsBuilder = // $NON-NLS-1$
NoSQLReflection.invokeStaticMethod(// $NON-NLS-1$
"com.mongodb.connection.ClusterSettings", "builder", new Object[0], // $NON-NLS-1$
classLoader);
Object sslSettingsBuilder = // $NON-NLS-1$ //$NON-NLS-2$
NoSQLReflection.invokeStaticMethod(// $NON-NLS-1$ //$NON-NLS-2$
"com.mongodb.connection.SslSettings", // $NON-NLS-1$ //$NON-NLS-2$
"builder", new Object[0], classLoader);
//
SSLContext sslContext = null;
String authMechanism = connection.getAttributes().get(IMongoDBAttributes.AUTHENTICATION_MECHANISM);
if (requireAuth && authMechanism.equals(IMongoConstants.X509)) {
sslContext = mongoX509SSLContext(connection);
} else if (requireEncryption) {
sslContext = StudioSSLContextProvider.getContext();
}
// $NON-NLS-1$
NoSQLReflection.invokeMethod(sslSettingsBuilder, "enabled", new Object[] { requireEncryption }, boolean.class);
if (sslContext != null) {
// $NON-NLS-1$
NoSQLReflection.invokeMethod(sslSettingsBuilder, "context", new Object[] { sslContext }, SSLContext.class);
}
// $NON-NLS-1$
Object sslSettingsBuild = NoSQLReflection.invokeMethod(sslSettingsBuilder, "build", new Object[0]);
// $NON-NLS-1$
Class<?> sslBlockClasszz = Class.forName("com.mongodb.Block", false, classLoader);
Class[] sslInterfaces = new Class[1];
sslInterfaces[0] = sslBlockClasszz;
Object sslBlock = Proxy.newProxyInstance(classLoader, sslInterfaces, (proxy, method, args) -> {
switch(method.getName()) {
case // $NON-NLS-1$
"apply":
if (args[0] != null) {
// $NON-NLS-1$
NoSQLReflection.invokeMethod(// $NON-NLS-1$
args[0], // $NON-NLS-1$
"applySettings", // $NON-NLS-1$
new Object[] { sslSettingsBuild }, // $NON-NLS-1$
Class.forName("com.mongodb.connection.SslSettings", true, classLoader));
}
return null;
default:
throw new NoSQLServerException(// $NON-NLS-1$
Messages.getString("MongoDBConnectionUtil.CannotFindMethod", method.getName()));
}
});
// $NON-NLS-1$
NoSQLReflection.invokeMethod(clientSettingsBuilder, "applyToSslSettings", new Object[] { sslBlock }, sslBlockClasszz);
for (String host : hosts.keySet()) {
String port = hosts.get(host);
Object serverAddress = // $NON-NLS-1$
NoSQLReflection.newInstance(// $NON-NLS-1$
"com.mongodb.ServerAddress", new Object[] { host, Integer.parseInt(port) }, classLoader, String.class, int.class);
addrs.add(serverAddress);
// $NON-NLS-1$
NoSQLReflection.invokeMethod(clusterSettingsBuilder, "hosts", new Object[] { addrs }, List.class);
}
if (requireAuth) {
Object credential = getCredential(connection, contextType, user, pass, classLoader);
// $NON-NLS-1$
NoSQLReflection.invokeMethod(// $NON-NLS-1$
clientSettingsBuilder, // $NON-NLS-1$
"credential", // $NON-NLS-1$
new Object[] { credential }, // $NON-NLS-1$
Class.forName("com.mongodb.MongoCredential", true, classLoader));
}
// $NON-NLS-1$
Object clusterSettingsBuild = NoSQLReflection.invokeMethod(clusterSettingsBuilder, "build", new Object[0]);
// $NON-NLS-1$
Class<?> blockClasszz = Class.forName("com.mongodb.Block", false, classLoader);
Class[] interfaces = new Class[1];
interfaces[0] = blockClasszz;
Object block = Proxy.newProxyInstance(classLoader, interfaces, (proxy, method, args) -> {
switch(method.getName()) {
case // $NON-NLS-1$
"apply":
if (args[0] != null) {
// $NON-NLS-1$
NoSQLReflection.invokeMethod(// $NON-NLS-1$
args[0], // $NON-NLS-1$
"applySettings", // $NON-NLS-1$
new Object[] { clusterSettingsBuild }, // $NON-NLS-1$
Class.forName("com.mongodb.connection.ClusterSettings", true, classLoader));
}
return null;
default:
throw new NoSQLServerException(// $NON-NLS-1$
Messages.getString("MongoDBConnectionUtil.CannotFindMethod", method.getName()));
}
});
// $NON-NLS-1$
NoSQLReflection.invokeMethod(clientSettingsBuilder, "applyToClusterSettings", new Object[] { block }, blockClasszz);
// $NON-NLS-1$
Object clientSettingsBuild = NoSQLReflection.invokeMethod(clientSettingsBuilder, "build", new Object[0]);
mongo = // $NON-NLS-1$ //$NON-NLS-2$
NoSQLReflection.invokeStaticMethod(// $NON-NLS-1$ //$NON-NLS-2$
"com.mongodb.client.MongoClients", // $NON-NLS-1$ //$NON-NLS-2$
"create", new Object[] { clientSettingsBuild }, classLoader, // $NON-NLS-1$
Class.forName("com.mongodb.MongoClientSettings", true, classLoader));
mongos.add(mongo);
} catch (Exception e) {
throw new NoSQLServerException(e);
}
return mongo;
}
use of org.talend.repository.nosql.exceptions.NoSQLServerException in project tbd-studio-se by Talend.
the class MongoDBConnectionUtil method checkConnection.
public static synchronized boolean checkConnection(NoSQLConnection connection) throws NoSQLServerException {
boolean canConnect = true;
try {
// if cancel to interrupt check connection, throw exception
if (Thread.currentThread().interrupted()) {
throw new InterruptedException();
}
Object db = getDB(connection);
if (db == null) {
Object mongoClient = getMongoVersioned(connection);
List<String> databaseNames = getDatabaseNames(connection, mongoClient);
if (databaseNames != null && databaseNames.size() > 0) {
for (String databaseName : databaseNames) {
if (StringUtils.isNotEmpty(databaseName)) {
// if cancel to interrupt check connection, throw exception
if (Thread.currentThread().interrupted()) {
throw new InterruptedException();
}
db = getDB(connection, databaseName, mongoClient);
if (db != null) {
break;
}
}
}
}
}
if (db == null) {
// $NON-NLS-1$
throw new NoSQLServerException(Messages.getString("MongoDBConnectionUtil.NoAvailableDatabase"));
}
// if cancel to interrupt check connection, throw exception
if (Thread.currentThread().interrupted()) {
throw new InterruptedException();
}
if (isUpgradeLatestVersion(connection)) {
// $NON-NLS-1$
Iterable<String> iter = (Iterable<String>) NoSQLReflection.invokeMethod(db, "listCollectionNames");
for (Object dbname : iter) {
// empty, just activate the real call to mongodb server
}
} else {
// $NON-NLS-1$
NoSQLReflection.invokeMethod(db, "getStats");
}
} catch (Exception e) {
canConnect = false;
if (e instanceof InterruptedException) {
// $NON-NLS-1$
throw new NoSQLServerException(Messages.getString("noSQLConnectionTest.cancelCheckConnection"), e);
} else {
// $NON-NLS-1$
throw new NoSQLServerException(Messages.getString("MongoDBConnectionUtil.CanotConnectDatabase"), e);
}
}
return canConnect;
}
use of org.talend.repository.nosql.exceptions.NoSQLServerException in project tbd-studio-se by Talend.
the class MongoDBConnectionUtil method getDatabaseNames.
public static synchronized List<String> getDatabaseNames(NoSQLConnection connection, Object mongoClient) throws NoSQLServerException {
List<String> databaseNames = null;
try {
databaseNames = Collections.synchronizedList(new ArrayList<String>());
if (isUpgradeVersion(connection)) {
String requireAuthAttr = connection.getAttributes().get(IMongoDBAttributes.REQUIRED_AUTHENTICATION);
boolean requireAuth = requireAuthAttr == null ? false : Boolean.valueOf(requireAuthAttr);
//
String requireEncryptionAttr = connection.getAttributes().get(IMongoDBAttributes.REQUIRED_ENCRYPTION);
boolean requireEncryption = requireEncryptionAttr == null ? false : Boolean.valueOf(requireEncryptionAttr);
if (mongoClient == null) {
mongoClient = getMongo(connection, requireAuth, requireEncryption);
}
Iterable mongoIterable = (Iterable) NoSQLReflection.invokeMethod(mongoClient, "listDatabaseNames");
for (Object dbname : mongoIterable) {
databaseNames.add((String) dbname);
}
} else {
if (mongoClient == null) {
mongoClient = getMongo(connection);
}
Iterable mongoIterable = (Iterable) NoSQLReflection.invokeMethod(mongoClient, "listDatabaseNames");
for (Object dbname : mongoIterable) {
databaseNames.add((String) dbname);
}
}
} catch (NoSQLReflectionException e) {
throw new NoSQLServerException(e);
}
return databaseNames;
}
use of org.talend.repository.nosql.exceptions.NoSQLServerException in project tbd-studio-se by Talend.
the class MongoDBConnectionUtil method getMongo.
public static synchronized Object getMongo(NoSQLConnection connection, boolean requireAuth, boolean requireEncryption) throws NoSQLServerException {
Object mongo = null;
ClassLoader classLoader = NoSQLClassLoaderFactory.getClassLoader(connection);
String useReplicaAttr = connection.getAttributes().get(IMongoDBAttributes.USE_REPLICA_SET);
boolean useReplica = useReplicaAttr == null ? false : Boolean.valueOf(useReplicaAttr);
String useConnStringAttr = connection.getAttributes().get(IMongoDBAttributes.USE_CONN_STRING);
boolean useConnString = useConnStringAttr == null ? false : Boolean.valueOf(useConnStringAttr);
ContextType contextType = null;
if (connection.isContextMode()) {
contextType = ConnectionContextHelper.getContextTypeForContextMode(connection);
}
try {
if (useConnString) {
mongo = getMongo4ConnStringVersion(connection, contextType, classLoader, requireAuth, requireEncryption);
} else if (useReplica) {
String replicaSet = connection.getAttributes().get(IMongoDBAttributes.REPLICA_SET);
List<HashMap<String, Object>> replicaSetList = getReplicaSetList(replicaSet, false);
Map<String, String> hosts = new HashMap<String, String>();
for (HashMap<String, Object> rowMap : replicaSetList) {
String host = (String) rowMap.get(IMongoConstants.REPLICA_HOST_KEY);
String port = (String) rowMap.get(IMongoConstants.REPLICA_PORT_KEY);
if (contextType != null) {
host = ContextParameterUtils.getOriginalValue(contextType, host);
port = ContextParameterUtils.getOriginalValue(contextType, port);
}
if (host != null && port != null) {
hosts.put(host, port);
}
}
if (isUpgradeLatestVersion(connection)) {
mongo = getMongo4LatestVersion(connection, contextType, classLoader, hosts, requireAuth, requireEncryption);
} else {
mongo = getMongo4OlderVersion(connection, contextType, classLoader, hosts, requireAuth, requireEncryption);
}
} else {
String host = connection.getAttributes().get(IMongoDBAttributes.HOST);
String port = connection.getAttributes().get(IMongoDBAttributes.PORT);
if (contextType != null) {
host = ContextParameterUtils.getOriginalValue(contextType, host);
port = ContextParameterUtils.getOriginalValue(contextType, port);
}
Map<String, String> hosts = new HashMap<String, String>();
if (host != null && port != null) {
hosts.put(host, port);
}
if (isUpgradeLatestVersion(connection)) {
mongo = getMongo4LatestVersion(connection, contextType, classLoader, hosts, requireAuth, requireEncryption);
} else {
mongo = getMongo4OlderVersion(connection, contextType, classLoader, hosts, requireAuth, requireEncryption);
}
}
} catch (Exception e) {
throw new NoSQLServerException(e);
}
return mongo;
}
Aggregations