Example usage for org.springframework.util ObjectUtils isEmpty

List of usage examples for org.springframework.util ObjectUtils isEmpty

Introduction

In this page you can find the example usage for org.springframework.util ObjectUtils isEmpty.

Prototype

@SuppressWarnings("rawtypes")
public static boolean isEmpty(@Nullable Object obj) 

Source Link

Document

Determine whether the given object is empty.

Usage

From source file:org.springframework.cloud.iot.coap.method.ResolvableMethod.java

private static ResolvableType toResolvableType(Class<?> type, Class<?>... generics) {
    return (ObjectUtils.isEmpty(generics) ? ResolvableType.forClass(type)
            : ResolvableType.forClassWithGenerics(type, generics));
}

From source file:org.springframework.cloud.stream.app.http.source.DefaultMixedCaseContentTypeHttpHeaderMapper.java

private void setMessageHeader(Map<String, Object> target, String name, Object value) {
    if (ObjectUtils.isArray(value)) {
        Object[] values = ObjectUtils.toObjectArray(value);
        if (!ObjectUtils.isEmpty(values)) {
            if (values.length == 1) {
                target.put(name, values);
            } else {
                target.put(name, values[0]);
            }/*from  w  w  w .j av a 2 s  . c  o  m*/
        }
    } else if (value instanceof Collection<?>) {
        Collection<?> values = (Collection<?>) value;
        if (!CollectionUtils.isEmpty(values)) {
            if (values.size() == 1) {
                target.put(name, values.iterator().next());
            } else {
                target.put(name, values);
            }
        }
    } else if (value != null) {
        target.put(name, value);
    }
}

From source file:org.springframework.cloud.stream.binder.kafka.config.KafkaBinderConfiguration.java

@Bean
KafkaBinderHealthIndicator healthIndicator(KafkaMessageChannelBinder kafkaMessageChannelBinder) {
    Map<String, Object> props = new HashMap<>();
    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, ByteArrayDeserializer.class);
    props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ByteArrayDeserializer.class);
    if (!ObjectUtils.isEmpty(configurationProperties.getConsumerConfiguration())) {
        props.putAll(configurationProperties.getConsumerConfiguration());
    }/*from   w ww.j  a  va 2 s  .c  o m*/
    if (!props.containsKey(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG)) {
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
                this.configurationProperties.getKafkaConnectionString());
    }
    ConsumerFactory<?, ?> consumerFactory = new DefaultKafkaConsumerFactory<>(props);
    return new KafkaBinderHealthIndicator(kafkaMessageChannelBinder, consumerFactory);
}

From source file:org.springframework.cloud.stream.binder.kafka.KafkaBinderMetrics.java

private ConsumerFactory<?, ?> createConsumerFactory() {
    if (this.defaultConsumerFactory == null) {
        synchronized (this) {
            if (this.defaultConsumerFactory == null) {
                Map<String, Object> props = new HashMap<>();
                props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, ByteArrayDeserializer.class);
                props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ByteArrayDeserializer.class);
                Map<String, Object> mergedConfig = this.binderConfigurationProperties
                        .mergedConsumerConfiguration();
                if (!ObjectUtils.isEmpty(mergedConfig)) {
                    props.putAll(mergedConfig);
                }/*w  w  w  .  j ava 2 s  .com*/
                if (!props.containsKey(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG)) {
                    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
                            this.binderConfigurationProperties.getKafkaConnectionString());
                }
                this.defaultConsumerFactory = new DefaultKafkaConsumerFactory<>(props);
            }
        }
    }

    return this.defaultConsumerFactory;
}

From source file:org.springframework.cloud.stream.binder.kafka.streams.KafkaStreamsDlqDispatch.java

private DefaultKafkaProducerFactory<byte[], byte[]> getProducerFactory(
        ExtendedProducerProperties<KafkaProducerProperties> producerProperties,
        KafkaBinderConfigurationProperties configurationProperties) {
    Map<String, Object> props = new HashMap<>();
    props.put(ProducerConfig.RETRIES_CONFIG, 0);
    props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 33554432);
    props.put(ProducerConfig.ACKS_CONFIG, configurationProperties.getRequiredAcks());
    Map<String, Object> mergedConfig = configurationProperties.mergedProducerConfiguration();
    if (!ObjectUtils.isEmpty(mergedConfig)) {
        props.putAll(mergedConfig);//from w w w  .  j  av  a  2 s .  co m
    }
    if (ObjectUtils.isEmpty(props.get(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG))) {
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, configurationProperties.getKafkaConnectionString());
    }
    if (ObjectUtils.isEmpty(props.get(ProducerConfig.BATCH_SIZE_CONFIG))) {
        props.put(ProducerConfig.BATCH_SIZE_CONFIG,
                String.valueOf(producerProperties.getExtension().getBufferSize()));
    }
    if (ObjectUtils.isEmpty(props.get(ProducerConfig.LINGER_MS_CONFIG))) {
        props.put(ProducerConfig.LINGER_MS_CONFIG,
                String.valueOf(producerProperties.getExtension().getBatchTimeout()));
    }
    if (ObjectUtils.isEmpty(props.get(ProducerConfig.COMPRESSION_TYPE_CONFIG))) {
        props.put(ProducerConfig.COMPRESSION_TYPE_CONFIG,
                producerProperties.getExtension().getCompressionType().toString());
    }
    if (!ObjectUtils.isEmpty(producerProperties.getExtension().getConfiguration())) {
        props.putAll(producerProperties.getExtension().getConfiguration());
    }
    //Always send as byte[] on dlq (the same byte[] that the consumer received)
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class);
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class);

    return new DefaultKafkaProducerFactory<>(props);
}

From source file:org.springframework.cloud.stream.binder.kafka.streams.KafkaStreamsStreamListenerSetupMethodOrchestrator.java

private static String[] getOutboundBindingTargetNames(Method method) {
    SendTo sendTo = AnnotationUtils.findAnnotation(method, SendTo.class);
    if (sendTo != null) {
        Assert.isTrue(!ObjectUtils.isEmpty(sendTo.value()), StreamListenerErrorMessages.ATLEAST_ONE_OUTPUT);
        Assert.isTrue(sendTo.value().length >= 1, "At least one outbound destination need to be provided.");
        return sendTo.value();
    }/*from ww w  .  ja  va2 s .c o m*/
    return null;
}

From source file:org.springframework.cloud.stream.binder.kafka.streams.KafkaStreamsStreamListenerSetupMethodOrchestrator.java

@SuppressWarnings({ "unchecked" })
private static KafkaStreamsStateStoreProperties buildStateStoreSpec(Method method) {
    KafkaStreamsStateStore spec = AnnotationUtils.findAnnotation(method, KafkaStreamsStateStore.class);
    if (spec != null) {
        Assert.isTrue(!ObjectUtils.isEmpty(spec.name()), "name cannot be empty");
        Assert.isTrue(spec.name().length() >= 1, "name cannot be empty.");
        KafkaStreamsStateStoreProperties props = new KafkaStreamsStateStoreProperties();
        props.setName(spec.name());//from   www .j  ava  2  s .  co m
        props.setType(spec.type());
        props.setLength(spec.lengthMs());
        props.setKeySerdeString(spec.keySerde());
        props.setRetention(spec.retentionMs());
        props.setValueSerdeString(spec.valueSerde());
        props.setCacheEnabled(spec.cache());
        props.setLoggingDisabled(!spec.logging());
        return props;
    }
    return null;
}

From source file:org.springframework.cloud.stream.binding.BinderAwareChannelResolver.java

@SuppressWarnings("unchecked")
@Override//  w w  w.  j  av  a 2  s. c  o m
public MessageChannel resolveDestination(String channelName) {
    try {
        return super.resolveDestination(channelName);
    } catch (DestinationResolutionException e) {
        // intentionally empty; will check again while holding the monitor
    }
    synchronized (this) {
        BindingServiceProperties bindingServiceProperties = this.bindingService.getBindingServiceProperties();
        String[] dynamicDestinations = bindingServiceProperties.getDynamicDestinations();
        boolean dynamicAllowed = ObjectUtils.isEmpty(dynamicDestinations)
                || ObjectUtils.containsElement(dynamicDestinations, channelName);
        try {
            return super.resolveDestination(channelName);
        } catch (DestinationResolutionException e) {
            if (!dynamicAllowed) {
                throw e;
            }
        }

        MessageChannel channel = this.bindingTargetFactory.createOutput(channelName);
        this.beanFactory.registerSingleton(channelName, channel);

        //TODO: Investigate if the following call is necessary.
        //initializeBean call on the next line also calling the addMatchingInterceptors method in GlobalChannelInterceptorProcessor
        //this.instrumentChannelWithGlobalInterceptors(channel, channelName);

        channel = (MessageChannel) this.beanFactory.initializeBean(channel, channelName);
        if (this.newBindingCallback != null) {
            ProducerProperties producerProperties = bindingServiceProperties.getProducerProperties(channelName);
            Object extendedProducerProperties = this.bindingService.getExtendedProducerProperties(channel,
                    channelName);
            this.newBindingCallback.configure(channelName, channel, producerProperties,
                    extendedProducerProperties);
            bindingServiceProperties.updateProducerProperties(channelName, producerProperties);
        }
        Binding<MessageChannel> binding = this.bindingService.bindProducer(channel, channelName);
        this.dynamicDestinationsBindable.addOutputBinding(channelName, binding);

        return channel;
    }
}

From source file:org.springframework.context.support.AbstractMessageSource.java

/**
 * Resolve the given code and arguments as message in the given Locale,
 * returning null if not found. Does <i>not</i> fall back to the code
 * as default message. Invoked by getMessage methods.
 * @param code the code to lookup up, such as 'calculator.noRateSet'
 * @param args array of arguments that will be filled in for params
 * within the message/*from   w  w  w  .  j a  v a  2s. c  om*/
 * @param locale the Locale in which to do the lookup
 * @return the resolved message, or <code>null</code> if not found
 * @see #getMessage(String, Object[], String, Locale)
 * @see #getMessage(String, Object[], Locale)
 * @see #getMessage(MessageSourceResolvable, Locale)
 * @see #setUseCodeAsDefaultMessage
 */
protected String getMessageInternal(String code, Object[] args, Locale locale) {
    if (code == null) {
        return null;
    }
    if (locale == null) {
        locale = Locale.getDefault();
    }
    Object[] argsToUse = args;

    if (!isAlwaysUseMessageFormat() && ObjectUtils.isEmpty(args)) {
        // Optimized resolution: no arguments to apply,
        // therefore no MessageFormat needs to be involved.
        // Note that the default implementation still uses MessageFormat;
        // this can be overridden in specific subclasses.
        String message = resolveCodeWithoutArguments(code, locale);
        if (message != null) {
            return message;
        }
    }

    else {
        // Resolve arguments eagerly, for the case where the message
        // is defined in a parent MessageSource but resolvable arguments
        // are defined in the child MessageSource.
        argsToUse = resolveArguments(args, locale);

        MessageFormat messageFormat = resolveCode(code, locale);
        if (messageFormat != null) {
            synchronized (messageFormat) {
                return messageFormat.format(argsToUse);
            }
        }
    }

    // Not found -> check parent, if any.
    return getMessageFromParent(code, argsToUse, locale);
}

From source file:org.springframework.context.support.MessageSourceSupport.java

/**
 * Format the given message String, using cached MessageFormats.
 * By default invoked for passed-in default messages, to resolve
 * any argument placeholders found in them.
 * @param msg the message to format/*from  w  w w .ja v a 2s .  c om*/
 * @param args array of arguments that will be filled in for params within
 * the message, or {@code null} if none
 * @param locale the Locale used for formatting
 * @return the formatted message (with resolved arguments)
 */
protected String formatMessage(String msg, @Nullable Object[] args, Locale locale) {
    if (!isAlwaysUseMessageFormat() && ObjectUtils.isEmpty(args)) {
        return msg;
    }
    MessageFormat messageFormat = null;
    synchronized (this.messageFormatsPerMessage) {
        Map<Locale, MessageFormat> messageFormatsPerLocale = this.messageFormatsPerMessage.get(msg);
        if (messageFormatsPerLocale != null) {
            messageFormat = messageFormatsPerLocale.get(locale);
        } else {
            messageFormatsPerLocale = new HashMap<>();
            this.messageFormatsPerMessage.put(msg, messageFormatsPerLocale);
        }
        if (messageFormat == null) {
            try {
                messageFormat = createMessageFormat(msg, locale);
            } catch (IllegalArgumentException ex) {
                // Invalid message format - probably not intended for formatting,
                // rather using a message structure with no arguments involved...
                if (isAlwaysUseMessageFormat()) {
                    throw ex;
                }
                // Silently proceed with raw message if format not enforced...
                messageFormat = INVALID_MESSAGE_FORMAT;
            }
            messageFormatsPerLocale.put(locale, messageFormat);
        }
    }
    if (messageFormat == INVALID_MESSAGE_FORMAT) {
        return msg;
    }
    synchronized (messageFormat) {
        return messageFormat.format(resolveArguments(args, locale));
    }
}