Search in sources :

Example 11 with NoSQLServerException

use of org.talend.repository.nosql.exceptions.NoSQLServerException in project tbd-studio-se by Talend.

the class Neo4jConnectionUtil method getResultIterator.

public static synchronized Iterator<Map<String, Object>> getResultIterator(NoSQLConnection connection, String cypher, Object db1) throws NoSQLServerException {
    Iterator<Map<String, Object>> resultIterator = null;
    Object db = db1;
    try {
        db = getDB(connection, true);
        ClassLoader classLoader = NoSQLClassLoaderFactory.getClassLoader(connection);
        String isRemoteAttr = connection.getAttributes().get(INeo4jAttributes.REMOTE_SERVER);
        boolean isRemote = isRemoteAttr == null ? false : Boolean.valueOf(isRemoteAttr);
        if (isRemote) {
            Object queryResult = NoSQLReflection.invokeMethod(getQueryEngine(db, classLoader), "query", new Object[] { cypher, null }, String.class, // $NON-NLS-1$
            Map.class);
            resultIterator = (Iterator<Map<String, Object>>) // $NON-NLS-1$
            NoSQLReflection.invokeMethod(// $NON-NLS-1$
            queryResult, // $NON-NLS-1$
            "iterator", new Object[0]);
        } else {
            Object executionResult = NoSQLReflection.invokeMethod(getExecutionEngine(db, classLoader, connection), "execute", // $NON-NLS-1$
            new Object[] { cypher });
            resultIterator = (Iterator<Map<String, Object>>) // $NON-NLS-1$
            NoSQLReflection.invokeMethod(// $NON-NLS-1$
            executionResult, // $NON-NLS-1$
            "iterator", new Object[0]);
        }
    } catch (NoSQLReflectionException e) {
        throw new NoSQLServerException(e);
    }
    return resultIterator;
}
Also used : NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) NoSQLReflectionException(org.talend.repository.nosql.exceptions.NoSQLReflectionException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 12 with NoSQLServerException

use of org.talend.repository.nosql.exceptions.NoSQLServerException in project tbd-studio-se by Talend.

the class Neo4jConnectionUtil method checkConnection.

public static synchronized boolean checkConnection(NoSQLConnection connection) throws NoSQLServerException {
    boolean canConnect = true;
    final ClassLoader classLoader = NoSQLClassLoaderFactory.getClassLoader(connection);
    ClassLoader currCL = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(classLoader);
    Object dbConnection = null;
    try {
        boolean isRemote = Boolean.valueOf(connection.getAttributes().get(INeo4jAttributes.REMOTE_SERVER));
        if (isRemote && isVersionSince32(connection)) {
            String usename = StringUtils.trimToEmpty(connection.getAttributes().get(INeo4jAttributes.USERNAME));
            String password = StringUtils.trimToEmpty(connection.getAttributes().get(INeo4jAttributes.PASSWORD));
            String serverUrl = StringUtils.trimToEmpty(connection.getAttributes().get(INeo4jAttributes.SERVER_URL));
            if (connection.isContextMode()) {
                ContextType contextType = ConnectionContextHelper.getContextTypeForContextMode(connection);
                if (contextType != null) {
                    usename = ContextParameterUtils.getOriginalValue(contextType, usename);
                    password = ContextParameterUtils.getOriginalValue(contextType, password);
                    serverUrl = ContextParameterUtils.getOriginalValue(contextType, serverUrl);
                }
            } else {
                password = connection.getValue(password, false);
            }
            // if cancel to interrupt check connection, throw exception
            if (Thread.currentThread().interrupted()) {
                throw new InterruptedException();
            }
            Object basic = NoSQLReflection.invokeStaticMethod("org.neo4j.driver.v1.AuthTokens", "basic", new Object[] { usename, password }, classLoader, String.class, String.class);
            NoSQLReflection.invokeStaticMethod("org.neo4j.driver.v1.GraphDatabase", "driver", new Object[] { serverUrl, basic }, classLoader, String.class, Class.forName("org.neo4j.driver.v1.AuthToken", true, classLoader));
            return canConnect;
        }
        // if cancel to interrupt check connection, throw exception
        if (Thread.currentThread().interrupted()) {
            // $NON-NLS-1$
            throw new InterruptedException();
        }
        final Object db = getDB(connection);
        dbConnection = db;
        if (isRemote) {
            // $NON-NLS-1$
            NoSQLReflection.invokeMethod(// $NON-NLS-1$
            db, // $NON-NLS-1$
            "getAllNodes", new Object[0]);
        } else {
            if (isVersion1(connection)) {
                doCheck(db, classLoader, connection);
            } else {
                new ExecutionUnitWithTransaction() {

                    @Override
                    protected Object run() throws Exception {
                        doCheck(db, classLoader, connection);
                        return null;
                    }
                }.execute(db);
            }
        }
    } catch (Exception e) {
        canConnect = false;
        resetAll();
        if (e instanceof InterruptedException) {
            // $NON-NLS-1$
            throw new NoSQLServerException(Messages.getString("noSQLConnectionTest.cancelCheckConnection"), e);
        } else {
            // $NON-NLS-1$
            throw new NoSQLServerException(Messages.getString("Neo4jConnectionUtil.cannotConnectDatabase"), e);
        }
    } finally {
        if (dbConnection != null) {
            shutdownNeo4JDb(dbConnection);
        }
        Thread.currentThread().setContextClassLoader(currCL);
    }
    return canConnect;
}
Also used : ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) NoSQLReflectionException(org.talend.repository.nosql.exceptions.NoSQLReflectionException)

Example 13 with NoSQLServerException

use of org.talend.repository.nosql.exceptions.NoSQLServerException in project tbd-studio-se by Talend.

the class CassandraOldVersionMetadataHandler method getOrCreateCluster.

private Object getOrCreateCluster(NoSQLConnection connection) throws NoSQLServerException {
    if (cluster != null) {
        return cluster;
    }
    try {
        ClassLoader classLoader = NoSQLClassLoaderFactory.getClassLoader(connection);
        ContextType contextType = null;
        String host = connection.getAttributes().get(ICassandraAttributies.HOST);
        String port = connection.getAttributes().get(ICassandraAttributies.PORT);
        if (connection.isContextMode()) {
            contextType = ConnectionContextHelper.getContextTypeForContextMode(connection);
        }
        if (contextType != null) {
            host = ContextParameterUtils.getOriginalValue(contextType, host);
            port = ContextParameterUtils.getOriginalValue(contextType, port);
        }
        // $NON-NLS-1$
        String hostIps = host + ":" + port;
        Object hostsConfiguration = NoSQLReflection.newInstance("me.prettyprint.cassandra.service.CassandraHostConfigurator", new String[] { hostIps }, // $NON-NLS-1$
        classLoader);
        java.util.Map<String, String> credentialsPros = new java.util.HashMap<String, String>();
        String requireAuthAttr = connection.getAttributes().get(ICassandraAttributies.REQUIRED_AUTHENTICATION);
        boolean requireAuth = requireAuthAttr == null ? false : Boolean.valueOf(requireAuthAttr);
        if (requireAuth) {
            String username = connection.getAttributes().get(ICassandraAttributies.USERNAME);
            String password = connection.getValue(connection.getAttributes().get(ICassandraAttributies.PASSWORD), false);
            if (contextType != null) {
                username = ContextParameterUtils.getOriginalValue(contextType, username);
                password = ContextParameterUtils.getOriginalValue(contextType, password);
            }
            // $NON-NLS-1$
            credentialsPros.put("username", username);
            // $NON-NLS-1$
            credentialsPros.put("password", password);
        }
        cluster = NoSQLReflection.invokeStaticMethod(// $NON-NLS-1$ //$NON-NLS-2$
        "me.prettyprint.hector.api.factory.HFactory", // $NON-NLS-1$ //$NON-NLS-2$
        "getOrCreateCluster", new Object[] { "Cassandra_Cluster_" + System.currentTimeMillis(), hostsConfiguration, credentialsPros }, classLoader, String.class, Class.forName("me.prettyprint.cassandra.service.CassandraHostConfigurator", true, classLoader), // $NON-NLS-1$ //$NON-NLS-2$
        Map.class);
    } catch (Exception e) {
        throw new NoSQLServerException(e);
    }
    return cluster;
}
Also used : ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) Map(java.util.Map) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) NoSQLReflectionException(org.talend.repository.nosql.exceptions.NoSQLReflectionException)

Example 14 with NoSQLServerException

use of org.talend.repository.nosql.exceptions.NoSQLServerException in project tbd-studio-se by Talend.

the class CassandraOldVersionMetadataHandler method getColumnName.

@Override
public String getColumnName(NoSQLConnection connection, Object column) throws NoSQLServerException {
    // $NON-NLS-1$
    String columnName = "";
    ClassLoader classLoader = NoSQLClassLoaderFactory.getClassLoader(connection);
    try {
        // $NON-NLS-1$
        Object columnNameBA = NoSQLReflection.invokeMethod(column, "getName");
        columnName = (String) // $NON-NLS-1$ //$NON-NLS-2$
        NoSQLReflection.invokeStaticMethod(// $NON-NLS-1$ //$NON-NLS-2$
        "org.apache.cassandra.utils.ByteBufferUtil", // $NON-NLS-1$ //$NON-NLS-2$
        "string", new Object[] { columnNameBA }, classLoader, // $NON-NLS-1$
        Class.forName("java.nio.ByteBuffer", true, classLoader));
    } catch (Exception e) {
        throw new NoSQLServerException(e);
    }
    return columnName;
}
Also used : NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) NoSQLReflectionException(org.talend.repository.nosql.exceptions.NoSQLReflectionException)

Example 15 with NoSQLServerException

use of org.talend.repository.nosql.exceptions.NoSQLServerException in project tbd-studio-se by Talend.

the class CassandraOldVersionMetadataHandler method getColumnFamilyNames.

private Set<String> getColumnFamilyNames(NoSQLConnection connection, String ksName, boolean isSuper) throws NoSQLServerException {
    Set<String> cfNames = new HashSet<String>();
    try {
        Object keySpace = null;
        if (ksName == null) {
            keySpace = getKeySpace(connection);
        } else {
            keySpace = getKeySpace(connection, ksName);
            if (keySpace == null) {
                return cfNames;
            }
        }
        if (keySpace == null) {
            List<String> ksNames = getKeySpaceNames(connection);
            for (String name : ksNames) {
                cfNames.addAll(getColumnFamilyNames(connection, name));
            }
        } else {
            // $NON-NLS-1$
            List<Object> cfDefs = (List<Object>) NoSQLReflection.invokeMethod(keySpace, "getCfDefs");
            for (Object cfDef : cfDefs) {
                // $NON-NLS-1$
                String cfName = (String) NoSQLReflection.invokeMethod(cfDef, "getName");
                Object cfTypeObj = NoSQLReflection.invokeMethod(cfDef, "getColumnType");
                // $NON-NLS-1$
                String cfType = (String) NoSQLReflection.invokeMethod(cfTypeObj, "getValue");
                if (isSuper) {
                    if ("Super".equalsIgnoreCase(cfType)) {
                        // $NON-NLS-1$
                        cfNames.add(cfName);
                    }
                } else {
                    if ("Standard".equalsIgnoreCase(cfType)) {
                        // $NON-NLS-1$
                        cfNames.add(cfName);
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new NoSQLServerException(e);
    }
    return cfNames;
}
Also used : NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) ArrayList(java.util.ArrayList) List(java.util.List) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) NoSQLReflectionException(org.talend.repository.nosql.exceptions.NoSQLReflectionException) HashSet(java.util.HashSet)

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