use of org.talend.repository.nosql.exceptions.NoSQLServerException in project tbd-studio-se by Talend.
the class MongoDBConnectionUtil method getMongo4OlderVersion.
private static synchronized Object getMongo4OlderVersion(NoSQLConnection connection, ContextType contextType, ClassLoader classLoader, Map<String, String> hosts, boolean requireAuth, boolean requireEncryption) throws NoSQLServerException {
List<Object> addrs = new ArrayList<Object>();
List<Object> credentials = 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 {
// $NON-NLS-1$
Object builder = NoSQLReflection.newInstance("com.mongodb.MongoClientOptions$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(builder, "sslEnabled", new Object[] { requireEncryption }, boolean.class);
if (sslContext != null) {
// $NON-NLS-1$
NoSQLReflection.invokeMethod(builder, "sslContext", new Object[] { sslContext }, SSLContext.class);
}
// $NON-NLS-1$
Object build = NoSQLReflection.invokeMethod(builder, "build", new Object[0]);
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);
}
if (requireAuth) {
Object credential = getCredential(connection, contextType, user, pass, classLoader);
credentials.add(credential);
}
mongo = // $NON-NLS-1$
NoSQLReflection.newInstance(// $NON-NLS-1$
"com.mongodb.MongoClient", // $NON-NLS-1$
new Object[] { addrs, credentials, build }, classLoader, List.class, List.class, // $NON-NLS-1$
Class.forName("com.mongodb.MongoClientOptions", 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 getMongo.
public static synchronized Object getMongo(NoSQLConnection connection) 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);
ContextType contextType = null;
if (connection.isContextMode()) {
contextType = ConnectionContextHelper.getContextTypeForContextMode(connection);
}
try {
if (useReplica) {
List<Object> addrs = new ArrayList<Object>();
String replicaSet = connection.getAttributes().get(IMongoDBAttributes.REPLICA_SET);
List<HashMap<String, Object>> replicaSetList = getReplicaSetList(replicaSet, false);
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) {
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);
}
}
mongo = // $NON-NLS-1$
NoSQLReflection.newInstance(// $NON-NLS-1$
"com.mongodb.Mongo", // $NON-NLS-1$
new Object[] { addrs }, classLoader, List.class);
} 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);
}
mongo = // $NON-NLS-1$
NoSQLReflection.newInstance(// $NON-NLS-1$
"com.mongodb.Mongo", // $NON-NLS-1$
new Object[] { host, Integer.parseInt(port) }, classLoader, String.class, int.class);
}
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 getCollectionNames.
public static synchronized Set<String> getCollectionNames(NoSQLConnection connection, String dbName, Object mongoClient) throws NoSQLServerException {
Set<String> collectionNames = new HashSet<String>();
Object db = null;
if (mongoClient == null) {
mongoClient = getMongoVersioned(connection);
}
if (dbName != null) {
db = getDB(connection, dbName, mongoClient);
} else {
dbName = connection.getAttributes().get(IMongoDBAttributes.DATABASE);
if (connection.isContextMode()) {
ContextType contextType = ConnectionContextHelper.getContextTypeForContextMode(connection);
dbName = ContextParameterUtils.getOriginalValue(contextType, dbName);
}
db = getDB(connection, dbName, mongoClient);
}
if (db == null) {
List<String> databaseNames = getDatabaseNames(connection, mongoClient);
for (String databaseName : databaseNames) {
collectionNames.addAll(getCollectionNames(connection, databaseName, mongoClient));
}
} else {
try {
if (isUpgradeLatestVersion(connection)) {
// $NON-NLS-1$
Iterable<String> iter = (Iterable<String>) NoSQLReflection.invokeMethod(db, "listCollectionNames");
Iterator<String> iterator = iter.iterator();
while (iterator.hasNext()) {
collectionNames.add(iterator.next());
}
} else {
// $NON-NLS-1$
collectionNames = (Set<String>) NoSQLReflection.invokeMethod(db, "getCollectionNames");
}
} catch (NoSQLReflectionException e) {
throw new NoSQLServerException(e);
}
}
return collectionNames;
}
use of org.talend.repository.nosql.exceptions.NoSQLServerException in project tbd-studio-se by Talend.
the class AbstractNoSQLConnForm method addUtilsButtonListeners.
/*
* (non-Javadoc)
*
* @see org.talend.repository.ui.swt.utils.AbstractForm#addUtilsButtonListeners()
*/
@Override
protected void addUtilsButtonListeners() {
if (checkBtn != null) {
checkBtn.addSelectionListener(new SelectionAdapter() {
/*
* (non-Javadoc)
*
* @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
*/
@Override
public void widgetSelected(SelectionEvent e) {
try {
if (getConnection() != null) {
final AtomicBoolean checkedResult = new AtomicBoolean(true);
final Thread[] t = new Thread[1];
final Exception[] exception = new Exception[1];
IRunnableWithProgress runnableWithProgress = new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
t[0] = Thread.currentThread();
IMetadataProvider metadataProvider = NoSQLRepositoryFactory.getInstance().getMetadataProvider(getConnection().getDbType());
if (metadataProvider != null) {
try {
checkedResult.set(metadataProvider.checkConnection(getConnection()));
} catch (NoSQLServerException e) {
exception[0] = e;
checkedResult.set(false);
ExceptionHandler.process(e);
}
}
}
};
ProgressMonitorDialog dialog = new ProgressMonitorDialog(getShell()) {
protected void cancelPressed() {
super.cancelPressed();
t[0].interrupt();
}
};
dialog.run(true, true, runnableWithProgress);
if (!checkedResult.get() && exception[0] != null) {
new ErrorDialogWidthDetailArea(getShell(), RepositoryNoSQLPlugin.PLUGIN_ID, // $NON-NLS-1$
Messages.getString("AbstractNoSQLConnForm.checkFailed"), ExceptionUtils.getFullStackTrace(exception[0]));
} else {
MessageDialog.openInformation(getShell(), Messages.getString("AbstractNoSQLConnForm.checkConn"), // $NON-NLS-1$ //$NON-NLS-2$
Messages.getString("AbstractNoSQLConnForm.checkSuccessful"));
}
}
} catch (Exception exception) {
new ErrorDialogWidthDetailArea(getShell(), RepositoryNoSQLPlugin.PLUGIN_ID, Messages.getString(// $NON-NLS-1$
"AbstractNoSQLConnForm.checkFailed"), ExceptionUtils.getFullStackTrace(exception));
ExceptionHandler.process(exception);
}
}
});
}
}
use of org.talend.repository.nosql.exceptions.NoSQLServerException in project tbd-studio-se by Talend.
the class Neo4jConnectionUtil method getDB.
public static synchronized Object getDB(NoSQLConnection connection, boolean useCache) throws NoSQLServerException {
if (useCache && graphDb != null) {
return graphDb;
}
Object db = null;
ClassLoader classLoader = NoSQLClassLoaderFactory.getClassLoader(connection);
try {
boolean isRemote = Boolean.valueOf(connection.getAttributes().get(INeo4jAttributes.REMOTE_SERVER));
if (isRemote) {
String serverUrl = StringUtils.trimToEmpty(connection.getAttributes().get(INeo4jAttributes.SERVER_URL));
if (connection.isContextMode()) {
ContextType contextType = ConnectionContextHelper.getContextTypeForContextMode(connection);
serverUrl = ContextParameterUtils.getOriginalValue(contextType, serverUrl);
}
if (isNeedAuthorization(connection)) {
String usename = StringUtils.trimToEmpty(connection.getAttributes().get(INeo4jAttributes.USERNAME));
String password = StringUtils.trimToEmpty(connection.getAttributes().get(INeo4jAttributes.PASSWORD));
if (connection.isContextMode()) {
ContextType contextType = ConnectionContextHelper.getContextTypeForContextMode(connection);
usename = ContextParameterUtils.getOriginalValue(contextType, usename);
password = ContextParameterUtils.getOriginalValue(contextType, password);
} else {
password = connection.getValue(password, false);
}
db = NoSQLReflection.newInstance(// $NON-NLS-1$
"org.neo4j.rest.graphdb.RestGraphDatabase", // $NON-NLS-1$
new Object[] { serverUrl, usename, password }, classLoader);
} else {
db = // $NON-NLS-1$
NoSQLReflection.newInstance(// $NON-NLS-1$
"org.neo4j.rest.graphdb.RestGraphDatabase", // $NON-NLS-1$
new Object[] { serverUrl }, classLoader);
}
} else {
String dbPath = StringUtils.trimToEmpty(connection.getAttributes().get(INeo4jAttributes.DATABASE_PATH));
if (connection.isContextMode()) {
ContextType contextType = ConnectionContextHelper.getContextTypeForContextMode(connection);
dbPath = ContextParameterUtils.getOriginalValue(contextType, dbPath);
}
Object dbFactory = // $NON-NLS-1$
NoSQLReflection.newInstance(// $NON-NLS-1$
"org.neo4j.graphdb.factory.GraphDatabaseFactory", // $NON-NLS-1$
new Object[0], classLoader);
if (isVersionSince32(connection)) {
File dbFile = new File(dbPath);
db = // $NON-NLS-1$
NoSQLReflection.invokeMethod(// $NON-NLS-1$
dbFactory, // $NON-NLS-1$
"newEmbeddedDatabase", new Object[] { dbFile });
} else {
db = // $NON-NLS-1$
NoSQLReflection.invokeMethod(// $NON-NLS-1$
dbFactory, // $NON-NLS-1$
"newEmbeddedDatabase", new Object[] { dbPath });
}
}
registerShutdownHook(db);
} catch (NoSQLReflectionException e) {
throw new NoSQLServerException(e);
}
return graphDb = db;
}
Aggregations