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 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; }