@Override public boolean checkConnection(NoSQLConnection connection) throws NoSQLServerException { Object session = null; try { cluster = null; initCluster(connection); ContextType contextType = null; String ksName = connection.getAttributes().get(ICassandraAttributies.DATABASE); if (connection.isContextMode()) { contextType = ConnectionContextHelper.getContextTypeForContextMode(connection); } if (contextType != null) { ksName = ContextParameterUtils.getOriginalValue(contextType, ksName); } if (StringUtils.isEmpty(ksName)) { session = NoSQLReflection.invokeMethod(cluster, "connect"); // $NON-NLS-1$ } else { session = NoSQLReflection.invokeMethod(cluster, "connect", new Object[] {ksName}); // $NON-NLS-1$ } return true; } catch (Exception e) { throw new NoSQLServerException(e); } finally { try { if (session != null) { NoSQLReflection.invokeMethod(session, "close"); // $NON-NLS-1$ } } catch (NoSQLReflectionException e) { // only for debug e.printStackTrace(); } } }
private Object getKeySpace(NoSQLConnection connection) throws NoSQLServerException { ContextType contextType = null; String ksName = connection.getAttributes().get(ICassandraAttributies.DATABASE); if (connection.isContextMode()) { contextType = ConnectionContextHelper.getContextTypeForContextMode(connection); } if (contextType != null) { ksName = ContextParameterUtils.getOriginalValue(contextType, ksName); } return getKeySpace(connection, ksName); }
private Object getKeySpace(NoSQLConnection connection, String ksName) throws NoSQLServerException { ContextType contextType = null; if (StringUtils.isEmpty(ksName)) { return null; } if (connection.isContextMode()) { contextType = ConnectionContextHelper.getContextTypeForContextMode(connection); } if (contextType != null) { ksName = ContextParameterUtils.getOriginalValue(contextType, ksName); } // if ksName has quote,then case sensitive boolean hasQuote = (ksName.charAt(0) == '"') && (ksName.charAt(ksName.length() - 1) == '"'); try { initCluster(connection); List<Object> keySpaces = getKeySpaces(connection); for (Object keySpace : keySpaces) { String tmpKsName = (String) NoSQLReflection.invokeMethod(keySpace, "getName"); // $NON-NLS-1$ if (hasQuote) { ksName = TalendQuoteUtils.removeQuotesIfExist(ksName); if (ksName.equals(tmpKsName)) { return keySpace; } // case in-sensitive by default for kaName } else if (ksName.equalsIgnoreCase(tmpKsName)) { return keySpace; } } } catch (Exception e) { throw new NoSQLServerException(e); } return null; }
private void initCluster(NoSQLConnection connection) throws NoSQLServerException { try { if (cluster != null) { boolean isClosed = (Boolean) NoSQLReflection.invokeMethod(cluster, "isClosed"); // $NON-NLS-1$ if (!isClosed) { return; } } 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); } cluster = NoSQLReflection.invokeStaticMethod( "com.datastax.driver.core.Cluster", "builder", classLoader); //$NON-NLS-1$ //$NON-NLS-2$ cluster = NoSQLReflection.invokeMethod( cluster, "addContactPoint", new Object[] {host}); // $NON-NLS-1$ // Do authenticate 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); } cluster = NoSQLReflection.invokeMethod( cluster, "withCredentials", new Object[] {username, password}); // $NON-NLS-1$ } cluster = NoSQLReflection.invokeMethod(cluster, "build"); // $NON-NLS-1$ } catch (Exception e) { throw new NoSQLServerException(e); } }
public static synchronized Iterator<Map<String, Object>> getResultIterator( NoSQLConnection connection, String cypher) throws NoSQLServerException { Iterator<Map<String, Object>> resultIterator = null; Object db = getDB(connection); ClassLoader classLoader = NoSQLClassLoaderFactory.getClassLoader(connection); String isRemoteAttr = connection.getAttributes().get(INeo4jAttributes.REMOTE_SERVER); boolean isRemote = isRemoteAttr == null ? false : Boolean.valueOf(isRemoteAttr); try { if (isRemote) { Object queryResult = NoSQLReflection.invokeMethod( getQueryEngine(db, classLoader), "query", new Object[] {cypher, null}, String.class, Map.class); // $NON-NLS-1$ resultIterator = (Iterator<Map<String, Object>>) NoSQLReflection.invokeMethod( queryResult, "iterator", //$NON-NLS-1$ new Object[0]); } else { Object executionResult = NoSQLReflection.invokeMethod( getExecutionEngine(db, classLoader), "execute", new Object[] {cypher}); // $NON-NLS-1$ resultIterator = (Iterator<Map<String, Object>>) NoSQLReflection.invokeMethod( executionResult, "iterator", //$NON-NLS-1$ new Object[0]); } } catch (NoSQLReflectionException e) { throw new NoSQLServerException(e); } return resultIterator; }
public static synchronized boolean checkConnection(NoSQLConnection connection) throws NoSQLServerException { boolean canConnect = true; final ClassLoader classLoader = NoSQLClassLoaderFactory.getClassLoader(connection); try { final Object db = getDB(connection); boolean isRemote = Boolean.valueOf(connection.getAttributes().get(INeo4jAttributes.REMOTE_SERVER)); if (isRemote) { NoSQLReflection.invokeMethod( db, "getAllNodes", //$NON-NLS-1$ new Object[0]); } else { if (isVersion1(connection)) { doCheck(db, classLoader); } else { new ExecutionUnitWithTransaction() { @Override protected Object run() throws Exception { doCheck(db, classLoader); return null; } }.execute(db); } } } catch (Exception e) { canConnect = false; resetAll(); throw new NoSQLServerException( Messages.getString("Neo4jConnectionUtil.cannotConnectDatabase"), e); // $NON-NLS-1$ } return canConnect; }
public static boolean isVersion1(NoSQLConnection connection) { String dbVersion = connection.getAttributes().get(INeo4jAttributes.DB_VERSION); return "NEO4J_1_X_X".equals(dbVersion); // $NON-NLS-1$ }
public static synchronized Object getDB(NoSQLConnection connection) throws NoSQLServerException { 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.getAttributes().get(INeo4jAttributes.DB_VERSION))) { 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( "org.neo4j.rest.graphdb.RestGraphDatabase", new Object[] {serverUrl, usename, password}, // $NON-NLS-1$ classLoader); } else { db = NoSQLReflection.newInstance( "org.neo4j.rest.graphdb.RestGraphDatabase", new Object[] {serverUrl}, // $NON-NLS-1$ 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 = NoSQLReflection.newInstance( "org.neo4j.graphdb.factory.GraphDatabaseFactory", new Object[0], // $NON-NLS-1$ classLoader); db = NoSQLReflection.invokeMethod( dbFactory, "newEmbeddedDatabase", //$NON-NLS-1$ new Object[] {dbPath}); } registerShutdownHook(db); } catch (NoSQLReflectionException e) { throw new NoSQLServerException(e); } return graphDb = db; }