Add a QueryCommandParser

In this section, you will learn how to add or remove a query command parser at runtime using the SampleServiceImpl code.

QueryParserService

The QueryParserService service supports adding or removing parsers through the two methods below.

  • addCommandParser(): Adds a new query command parser.
  • removeCommandParser(): Removes an existing query command parser.

Plugin in or out parsers

The SampleServiceImpl component is implemented to add all query command parsers to the QueryParserService on startup and remove all query commands from the QueryParserService on stop.

@Component(name = "sample-service")
@Instantiate
public class SampleServiceImpl {

	@Requires
	private QueryParserService queryParserService;

	@Requires
	private ConnectProfileService connectProfileService;

	private SampleQueryCommandParser subnetGroupsParser = new SampleSubnetGroupsCommandParser();
	private SampleQueryCommandParser createSubnetGroupParser = new SampleCreateSubnetGroupCommandParser();

	@Validate
	public void start() {
		for (SampleQueryCommandParser parser : getParsers()) {
			parser.setConnectProfileService(connectProfileService);
			queryParserService.addCommandParser(parser);
		}
	}

	@Invalidate
	public void stop() {
		if (queryParserService == null)
			return;

		for (SampleQueryCommandParser parser : getParsers()) {
			parser.setConnectProfileService(null);
			queryParserService.removeCommandParser(parser);
		}
	}

	private List<SampleQueryCommandParser> getParsers() {
		return Arrays.asList(subnetGroupsParser, createSubnetGroupParser);
	}
}

Note that because the SampleQueryCommandParser class inherits from ConnectProfileQueryCommandParser, you must set the ConnectProfileService object at the time of initializing each parser.

In the Logpresso shell, iterate through bundle.start and bundle.stop to see how the sample-subnet-groups query is executed. For example, if your com.logpresso.sonar.sample bundle is number 133, you can stop it as follows.

logpresso> bundle.stop 133
bundle 133 stopped.