Java Code Examples for org.apache.karaf.shell.api.console.Session#get()

The following examples show how to use org.apache.karaf.shell.api.console.Session#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KeySpaceCompleter.java    From Karaf-Cassandra with Apache License 2.0 6 votes vote down vote up
public int complete(Session session, CommandLine commandLine,
		List<String> candidates) {
	StringsCompleter delegate = new StringsCompleter();

	com.datastax.driver.core.Session cassandraSession = (com.datastax.driver.core.Session) session
			.get(SessionParameter.CASSANDRA_SESSION);

	if (cassandraSession == null) {
		System.err
				.println("No active session found--run the connect command first");
		return 0;
		// return delegate.complete(session, commandLine, candidates);
	}

	CompleterCommons.completeKeySpace(delegate, cassandraSession);

	return delegate.complete(session, commandLine, candidates);
}
 
Example 2
Source File: DropCompleter.java    From Karaf-Cassandra with Apache License 2.0 4 votes vote down vote up
public int complete(Session session, CommandLine commandLine,
		List<String> candidates) {
	if (session != null) {

		com.datastax.driver.core.Session cassandraSession = (com.datastax.driver.core.Session) session
				.get(SessionParameter.CASSANDRA_SESSION);

		if (cassandraSession == null) {
			System.err
					.println("No active session found--run the connect command first");
			return 0;
		}

		if (commandLine instanceof ArgumentCommandLine) {
			delegate.getStrings().add(commandLine.getCursorArgument());

		} else {
			/*
			 * DROP ( KEYSPACE | SCHEMA ) IF EXISTS keyspace_name
			 */

			/*
			 * DROP TABLE IF EXISTS keyspace_name.table_name
			 */

			List<String> arguments = Arrays.asList(commandLine
					.getArguments());
			int cursorArgumentIndex = commandLine.getCursorArgumentIndex();
			String validArgument; 
			if (cursorArgumentIndex > 1)
				validArgument = arguments.get(cursorArgumentIndex -1);
			else
				validArgument = arguments.get(0);
			
			if (cursorArgumentIndex <= 1) {
				delegate.getStrings().add("KEYSPACE");
				delegate.getStrings().add("SCHEMA");
				// alternative drop a table
				delegate.getStrings().add("TABLE");
			} else if (cursorArgumentIndex >= 2) {
				delegate.getStrings().clear();
				if (cursorArgumentIndex == 2)
					delegate.getStrings().add("IF EXISTS");

				// tables are needed.
				if (validArgument.equalsIgnoreCase("KEYSPACE")
						|| validArgument.equalsIgnoreCase("SCHEMA")
						|| cassandraSession.getLoggedKeyspace() == null) {
					CompleterCommons.completeKeySpace(delegate,
							cassandraSession);
				} else {
					String keyspace = cassandraSession.getLoggedKeyspace();
					ResultSet execute = cassandraSession
							.execute(String
									.format("select columnfamily_name from system.schema_columnfamilies where keyspace_name = '%s';",
											keyspace));
					for (Row row : execute) {
						String table = row.getString("columnfamily_name");
						delegate.getStrings().add(table);
					}
				}
			} 
		}
	}
	return delegate.complete(session, commandLine, candidates);

}
 
Example 3
Source File: CreateCompleter.java    From Karaf-Cassandra with Apache License 2.0 4 votes vote down vote up
public int complete(Session session, CommandLine commandLine,
		List<String> candidates) {
	if (session != null) {

		com.datastax.driver.core.Session cassandraSession = (com.datastax.driver.core.Session) session
				.get(SessionParameter.CASSANDRA_SESSION);

		if (cassandraSession == null) {
			System.err
					.println("No active session found--run the connect command first");
			return 0;
		}

		if (commandLine instanceof ArgumentCommandLine) {
			delegate.getStrings().add(commandLine.getCursorArgument());

		} else {

			/*
			 * CREATE ( KEYSPACE | SCHEMA ) IF NOT EXISTS keyspace_name WITH
			 * REPLICATION = map AND DURABLE_WRITES = ( true | false )
			 */

			/*
			 * CREATE TABLE IF NOT EXISTS keyspace_name.table_name
			 * (column_definition, column_definition, ...) WITH property AND
			 * property ...
			 */

			List<String> arguments = Arrays.asList(commandLine
					.getArguments());
			int cursorArgumentIndex = commandLine.getCursorArgumentIndex();

			if (cursorArgumentIndex <= 1) {
				delegate.getStrings().add("KEYSPACE");
				delegate.getStrings().add("TABLE");
			}

			if (cursorArgumentIndex >= 2) {

				String prevArg = commandLine.getArguments()[1];
				switch (prevArg) {
				case "TABLE":
					int index = arguments.indexOf("TABLE");
					if (arguments.contains("TABLE")
							&& arguments.size() > index + 1) {
						delegate.getStrings().add(arguments.get(index + 1));
					}
					delegate.getStrings().add("TABLE");
					delegate.getStrings().remove("KEYSPACE");
					delegate.getStrings().add("(");
					delegate.getStrings().add(")");
					delegate.getStrings().add("PRIMARY KEY");
					break;
				case "KEYSPACE":
				default:
					if (cursorArgumentIndex == 2) {
						delegate.getStrings().clear();
						delegate.getStrings().add("IF NOT EXISTS");
					} else if (cursorArgumentIndex > 2) {
						delegate.getStrings().clear();
						index = arguments.indexOf("KEYSPACE");
						if (arguments.contains("KEYSPACE")
								&& arguments.size() > index + 1) {
							delegate.getStrings().add(
									arguments.get(index + 1));
						}
						delegate.getStrings().add("WITH REPLICATION = {");
						delegate.getStrings().add("'class' :");
						delegate.getStrings().add("'SimpleStrategy'");
						delegate.getStrings().add(",");
						delegate.getStrings().add("'replication_factor' :");
						delegate.getStrings().add("}");
					}
					break;
				}
			}
		}
	}
	return delegate.complete(session, commandLine, candidates);

}