@Override public Set<String> getColumnFamilyNames(NoSQLConnection connection, String ksName) 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 { Collection tables = (Collection) NoSQLReflection.invokeMethod(keySpace, "getTables"); // $NON-NLS-1$ for (Object table : tables) { String cfName = (String) NoSQLReflection.invokeMethod(table, "getName"); // $NON-NLS-1$ cfNames.add(cfName); } } } catch (Exception e) { throw new NoSQLServerException(e); } return cfNames; }
@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 List<Object> getKeySpaces(NoSQLConnection connection) throws NoSQLServerException { List<Object> keySpaces = new ArrayList<Object>(); initCluster(connection); try { Object metadata = NoSQLReflection.invokeMethod(cluster, "getMetadata"); // $NON-NLS-1$ keySpaces.addAll( (List) NoSQLReflection.invokeMethod(metadata, "getKeyspaces")); // $NON-NLS-1$ } catch (NoSQLReflectionException e) { throw new NoSQLServerException(e); } return keySpaces; }
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); } }
private static void collectNodeProperties( Map<String, Object> propertiesMap, Object node, ClassLoader classLoader) throws NoSQLReflectionException { Iterable<String> propertieKeys = (Iterable<String>) NoSQLReflection.invokeMethod(node, "getPropertyKeys", new Object[0]); // $NON-NLS-1$ Iterator<String> propertyKeysIter = propertieKeys.iterator(); while (propertyKeysIter.hasNext()) { String proKey = propertyKeysIter.next(); Object proValue = NoSQLReflection.invokeMethod(node, "getProperty", new Object[] {proKey}); if (proKey != null || proValue != null) { propertiesMap.put(proKey, proValue); } } }
private static void doCheck(Object db, ClassLoader classLoader) throws NoSQLReflectionException, ClassNotFoundException { Object ggo = NoSQLReflection.invokeStaticMethod( "org.neo4j.tooling.GlobalGraphOperations", "at", //$NON-NLS-1$ //$NON-NLS-2$ new Object[] {db}, classLoader, Class.forName( "org.neo4j.graphdb.GraphDatabaseService", true, classLoader)); // $NON-NLS-1$ NoSQLReflection.invokeMethod( ggo, "getAllNodes", //$NON-NLS-1$ new Object[0]); }
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; }
@Override public String getColumnDbType(Object column) throws NoSQLServerException { String dbType = null; try { Object type = NoSQLReflection.invokeMethod(column, "getType"); // $NON-NLS-1$ if (type != null) { Object typeName = NoSQLReflection.invokeMethod(type, "getName"); // $NON-NLS-1$ dbType = ((String) NoSQLReflection.invokeMethod(typeName, "toString")); // $NON-NLS-1$ if (ICassandraConstants.DBM_ID.equals(dbmsId)) { dbType = dbType.toUpperCase(); } } } catch (Exception e) { throw new NoSQLServerException(e); } return dbType; }
private static void shutdownNeo4JDb(final Object db) { try { NoSQLReflection.invokeMethod(db, "shutdown", new Object[0]); // $NON-NLS-1$ } catch (NoSQLReflectionException e) { // Nothing we can do here. e.printStackTrace(); } }
public static synchronized void closeConnections() throws NoSQLServerException { if (graphDb != null) { try { NoSQLReflection.invokeMethod(graphDb, "shutdown", new Object[0]); // $NON-NLS-1$ } catch (NoSQLReflectionException e) { throw new NoSQLServerException(e); } } resetAll(); }
@Override public String getColumnName(NoSQLConnection connection, Object column) throws NoSQLServerException { String columnName = ""; // $NON-NLS-1$ try { columnName = (String) NoSQLReflection.invokeMethod(column, "getName"); // $NON-NLS-1$ } catch (Exception e) { throw new NoSQLServerException(e); } return columnName; }
public static boolean isNode(Object obj, ClassLoader classLoader) throws NoSQLServerException { if (obj == null) { return false; } try { return NoSQLReflection.isInstance( obj, Class.forName("org.neo4j.graphdb.Node", true, classLoader)); // $NON-NLS-1$ } catch (Exception e) { throw new NoSQLServerException(e); } }
@Override public void closeConnections() throws NoSQLServerException { try { if (cluster != null) { NoSQLReflection.invokeMethod(cluster, "close"); // $NON-NLS-1$ cluster = null; } } catch (NoSQLReflectionException 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; }
private static Object getQueryEngine(Object db, ClassLoader classLoader) throws NoSQLReflectionException { Object qe = queryEngine; if (qe != null) { return qe; } Object restAPI = NoSQLReflection.invokeMethod(db, "getRestAPI", new Object[0]); // $NON-NLS-1$ try { qe = NoSQLReflection.newInstance( "org.neo4j.rest.graphdb.query.RestCypherQueryEngine", //$NON-NLS-1$ new Object[] {restAPI}, classLoader, Class.forName("org.neo4j.rest.graphdb.RestAPI", true, classLoader)); // $NON-NLS-1$ } catch (ClassNotFoundException e) { throw new NoSQLReflectionException(e); } return queryEngine = qe; }
@Override public List<Object> getColumns(NoSQLConnection connection, String ksName, String cfName) throws NoSQLServerException { List<Object> columns = new ArrayList<Object>(); try { Object keySpace = getKeySpace(connection, ksName); if (keySpace == null) { return columns; } Collection<Object> tables = (Collection) NoSQLReflection.invokeMethod(keySpace, "getTables"); // $NON-NLS-1$ for (Object table : tables) { String name = (String) NoSQLReflection.invokeMethod(table, "getName"); // $NON-NLS-1$ if (name != null && cfName.equals(name)) { columns.addAll((List) NoSQLReflection.invokeMethod(table, "getColumns")); // $NON-NLS-1$ break; } } } catch (Exception e) { throw new NoSQLServerException(e); } return columns; }
@Override public List<String> getKeySpaceNames(NoSQLConnection connection) throws NoSQLServerException { List<String> ksNames = new ArrayList<String>(); initCluster(connection); try { List<Object> keySpaces = getKeySpaces(connection); for (Object keySpace : keySpaces) { String ksName = (String) NoSQLReflection.invokeMethod(keySpace, "getName"); // $NON-NLS-1$ ksNames.add(ksName); } } catch (Exception e) { throw new NoSQLServerException(e); } return ksNames; }
private static Object getExecutionEngine(Object db, ClassLoader classLoader) throws NoSQLReflectionException { Object ee = executionEngine; if (ee != null) { return ee; } try { ee = NoSQLReflection.newInstance( "org.neo4j.cypher.javacompat.ExecutionEngine", new Object[] {db}, // $NON-NLS-1$ classLoader, Class.forName( "org.neo4j.graphdb.GraphDatabaseService", true, classLoader)); // $NON-NLS-1$ } catch (ClassNotFoundException e) { throw new NoSQLReflectionException(e); } return executionEngine = ee; }
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; }
@Override public Set<String> getSuperColumnFamilyNames(NoSQLConnection connection, String ksName) throws NoSQLServerException { Set<String> scfNames = new HashSet<String>(); Object session = null; ClassLoader classLoader = NoSQLClassLoaderFactory.getClassLoader(connection); try { if (ksName == null) { List<String> ksNames = getKeySpaceNames(connection); for (String name : ksNames) { scfNames.addAll(getColumnFamilyNames(connection, name)); } } else { initCluster(connection); session = NoSQLReflection.invokeMethod( cluster, "connect", new Object[] {"system"}); // $NON-NLS-1$ //$NON-NLS-2$ Object toPrepare = NoSQLReflection.newInstance( "com.datastax.driver.core.SimpleStatement", //$NON-NLS-1$ new Object[] {"select * from schema_columnfamilies where keyspace_name =?"}, classLoader); //$NON-NLS-1$ Object prepared = NoSQLReflection.invokeMethod( session, "prepare", new Object[] {toPrepare}, // $NON-NLS-1$ Class.forName( "com.datastax.driver.core.RegularStatement", false, classLoader)); //$NON-NLS-1$ Object statement = NoSQLReflection.invokeMethod( prepared, "bind", new Object[] {new Object[] {ksName}}, // $NON-NLS-1$ Object[].class); // Statement Object resultSet = NoSQLReflection.invokeMethod( session, "execute", new Object[] {statement}, // $NON-NLS-1$ Class.forName( "com.datastax.driver.core.Statement", false, classLoader)); // $NON-NLS-1$ Iterator iterator = (Iterator) NoSQLReflection.invokeMethod(resultSet, "iterator"); // $NON-NLS-1$ while (iterator.hasNext()) { Object row = iterator.next(); // String type = row.getString("type");s String type = (String) NoSQLReflection.invokeMethod( row, "getString", new Object[] {"type"}); // $NON-NLS-1$ //$NON-NLS-2$ String scfName = (String) NoSQLReflection.invokeMethod( row, "getString", new Object[] {"columnfamily_name"}); // $NON-NLS-1$ //$NON-NLS-2$ if (type.equalsIgnoreCase("super")) { // $NON-NLS-1$ scfNames.add(scfName); } } } } catch (Exception e) { throw new NoSQLServerException(e); } finally { try { if (session != null) { NoSQLReflection.invokeMethod(session, "close"); // $NON-NLS-1$ closeConnections(); } } catch (NoSQLReflectionException e) { // only for debug e.printStackTrace(); } } return scfNames; }
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; }