Search in sources :

Example 6 with NoSQLServerException

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;
}
Also used : NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) NoSQLReflectionException(org.talend.repository.nosql.exceptions.NoSQLReflectionException)

Example 7 with NoSQLServerException

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;
}
Also used : NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) ArrayList(java.util.ArrayList) JSONObject(org.talend.utils.json.JSONObject) ASN1Object(org.bouncycastle.asn1.ASN1Object) SSLContext(javax.net.ssl.SSLContext) NoSQLReflectionException(org.talend.repository.nosql.exceptions.NoSQLReflectionException) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) JSONException(org.talend.utils.json.JSONException)

Example 8 with NoSQLServerException

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;
}
Also used : NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) JSONObject(org.talend.utils.json.JSONObject) ASN1Object(org.bouncycastle.asn1.ASN1Object) NoSQLReflectionException(org.talend.repository.nosql.exceptions.NoSQLReflectionException) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) JSONException(org.talend.utils.json.JSONException)

Example 9 with NoSQLServerException

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;
}
Also used : NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) ArrayList(java.util.ArrayList) JSONObject(org.talend.utils.json.JSONObject) ASN1Object(org.bouncycastle.asn1.ASN1Object) NoSQLReflectionException(org.talend.repository.nosql.exceptions.NoSQLReflectionException)

Example 10 with NoSQLServerException

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;
}
Also used : ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) HashMap(java.util.HashMap) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) JSONObject(org.talend.utils.json.JSONObject) ASN1Object(org.bouncycastle.asn1.ASN1Object) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) NoSQLReflectionException(org.talend.repository.nosql.exceptions.NoSQLReflectionException) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) JSONException(org.talend.utils.json.JSONException)

Aggregations

NoSQLServerException (org.talend.repository.nosql.exceptions.NoSQLServerException)36 NoSQLReflectionException (org.talend.repository.nosql.exceptions.NoSQLReflectionException)33 ArrayList (java.util.ArrayList)13 ContextType (org.talend.designer.core.model.utils.emf.talendfile.ContextType)13 ASN1Object (org.bouncycastle.asn1.ASN1Object)9 JSONObject (org.talend.utils.json.JSONObject)9 List (java.util.List)7 JSONException (org.talend.utils.json.JSONException)6 HashSet (java.util.HashSet)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 SSLContext (javax.net.ssl.SSLContext)3 File (java.io.File)2 Collection (java.util.Collection)2 Iterator (java.util.Iterator)2 MappingTypeRetriever (org.talend.core.model.metadata.MappingTypeRetriever)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1