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);
    }
  }
 @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, 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 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);
 }
 /** @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int) */
 @Override
 public String getColumnText(final Object element, final int columnIndex) {
   String text;
   IContextParameter parameter = (IContextParameter) element;
   switch (columnIndex) {
     case 0:
       text = parameter.getName();
       break;
     case 1:
       if (ContextParameterUtils.isPasswordType(parameter)) {
         // see bug 0005661: In Run, Context password fields appear
         // in plaintext
         text = "****"; // $NON-NLS-1$
       } else {
         text = parameter.getValue();
       }
       break;
     default:
       text = super.getText(element);
   }
   return text;
 }
  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;
  }