private WeatherSearcher searcher() { if (provider != null) { return registry.providerOf(WeatherSearcher.class, provider) .orElseThrow(() -> new MessageProcessingException(String.format("Provider %s not found", provider))) .get(); } else { return registry.providerOf(WeatherSearcher.class) .orElseThrow(() -> new MessageProcessingException("No provider for weather forecast")) .get(); } }
@Override public void process(IncomeMessage message) { message.command().as(LookupCommand.class) .lookup() .and(message::reply) .orElseThrow(() -> new MessageProcessingException("Definition not found")); }
@Override public void process(IncomeMessage message) { message.command().as(WeatherCommand.class).search() .and(weather -> message.reply(buildResponse(weather))) .orElseThrow(() -> new MessageProcessingException("No weather forecast found")); }
User user = message.user(); if (user == null) { throw new MessageProcessingException("You're not registered.");
private <T> T convert(String string, Class<T> type) { try { return new ParameterBinder<>(type) .context(context -> { OsgiServiceRegistry registry = new OsgiServiceRegistry(); OsgiParameterResolver osgiResolver = new OsgiParameterResolver(registry); context.use(this.incomeMessage) .when(type(IncomeMessage.class)) .use(this.incomeMessage.channel()) .when(type(Channel.class)) .use(this.incomeMessage.sender()) .when(type(Sender.class)) .use(this.incomeMessage.location()) .when(type(MessageLocation.class)) .use(registry) .when(type(ServiceRegistry.class)) // try to lookup implementations by default using the osgi registry .use(osgiResolver) .byDefault(); if (this.incomeMessage.user() != null) { context.use(parameter -> this.incomeMessage.user()) .when(type(User.class)); } }).apply(string); } catch (Exception e) { throw new MessageProcessingException("Invalid command parameters."); } }