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.data.gemfire.support.SpringContextBootstrappingInitializer.java

/**
 * Registers the given Spring annotated (@Configuration) POJO classes with the specified
 * AnnotationConfigApplicationContext.//from   ww  w .  j  a v  a2 s.  c  om
 *
 * @param applicationContext the AnnotationConfigApplicationContext used to register the Spring annotated,
 * POJO classes.
 * @param annotatedClasses a Class array of Spring annotated (@Configuration) classes used to configure
 * and initialize the Spring AnnotationConfigApplicationContext.
 * @return the given AnnotationConfigApplicationContext.
 * @see org.springframework.context.annotation.AnnotationConfigApplicationContext#register(Class[])
 */
AnnotationConfigApplicationContext registerAnnotatedClasses(
        AnnotationConfigApplicationContext applicationContext, Class<?>[] annotatedClasses) {
    if (!ObjectUtils.isEmpty(annotatedClasses)) {
        applicationContext.register(annotatedClasses);
    }

    return applicationContext;
}

From source file:org.springframework.data.gemfire.support.SpringContextBootstrappingInitializer.java

/**
 * Configures classpath component scanning using the specified base packages on the specified
 * AnnotationConfigApplicationContext.//from w w w  .ja v  a  2s  .  co  m
 *
 * @param applicationContext the AnnotationConfigApplicationContext to setup with classpath component scanning
 * using the specified base packages.
 * @param basePackages an array of Strings indicating the base packages to use in the classpath component scan.
 * @return the given AnnotationConfigApplicationContext.
 * @see org.springframework.context.annotation.AnnotationConfigApplicationContext#scan(String...)
 */
AnnotationConfigApplicationContext scanBasePackages(AnnotationConfigApplicationContext applicationContext,
        String[] basePackages) {
    if (!ObjectUtils.isEmpty(basePackages)) {
        applicationContext.scan(basePackages);
    }

    return applicationContext;
}

From source file:org.springframework.data.gemfire.support.SpringContextBootstrappingInitializerTest.java

@Test
public void testCreateAnnotationApplicationContext() {
    final ConfigurableApplicationContext mockXmlApplicationContext = mock(ConfigurableApplicationContext.class,
            "testCreateAnnotationApplicationContext.MockXmlApplicationContext");

    final AnnotationConfigApplicationContext mockAnnotationApplicationContext = mock(
            AnnotationConfigApplicationContext.class,
            "testCreateAnnotationApplicationContext.MockAnnotationApplicationContext");

    String[] basePackages = { "org.example.app" };

    SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
        @Override/*w  w w  . ja  v a 2s . c  om*/
        ConfigurableApplicationContext createApplicationContext(final String[] configLocations) {
            return (ObjectUtils.isEmpty(configLocations) ? mockAnnotationApplicationContext
                    : mockXmlApplicationContext);
        }
    };

    ConfigurableApplicationContext actualApplicationContext = initializer.createApplicationContext(basePackages,
            null);

    assertSame(mockAnnotationApplicationContext, actualApplicationContext);

    verify(mockAnnotationApplicationContext, times(1)).scan(eq("org.example.app"));
}

From source file:org.springframework.data.gemfire.support.SpringContextBootstrappingInitializerTest.java

@Test
public void testCreateXmlApplicationContext() {
    final ConfigurableApplicationContext mockXmlApplicationContext = mock(ConfigurableApplicationContext.class,
            "testCreateXmlApplicationContext.MockXmlApplicationContext");

    final ConfigurableApplicationContext mockAnnotationApplicationContext = mock(
            ConfigurableApplicationContext.class,
            "testCreateXmlApplicationContext.MockAnnotationApplicationContext");

    SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
        @Override//ww  w. j a v  a  2 s .c o  m
        ConfigurableApplicationContext createApplicationContext(final String[] configLocations) {
            return (ObjectUtils.isEmpty(configLocations) ? mockAnnotationApplicationContext
                    : mockXmlApplicationContext);
        }
    };

    ConfigurableApplicationContext actualApplicationContext = initializer.createApplicationContext(null,
            new String[] { "/path/to/application/context.xml" });

    assertSame(mockXmlApplicationContext, actualApplicationContext);
}

From source file:org.springframework.data.hadoop.fs.DistributedCacheFactoryBean.java

@Override
public void afterPropertiesSet() throws Exception {
    Assert.notNull(conf, "A Hadoop configuration is required");
    Assert.notEmpty(entries, "No entries specified");

    // fall back to system discovery
    if (fs == null) {
        fs = FileSystem.get(conf);
    }//from  www  .j  av a 2  s.  c o m

    ds = new DistributedCache();

    if (createSymlink) {
        DistributedCache.createSymlink(conf);
    }

    HdfsResourceLoader loader = new HdfsResourceLoader(conf);

    boolean warnCpEntry = !":".equals(System.getProperty("path.separator"));

    try {
        for (CacheEntry entry : entries) {
            Resource[] resources = loader.getResources(entry.value);
            if (!ObjectUtils.isEmpty(resources)) {
                for (Resource resource : resources) {
                    HdfsResource res = (HdfsResource) resource;

                    URI uri = res.getURI();
                    String path = getPathWithFragment(uri);

                    String defaultLink = resource.getFilename();
                    boolean isArchive = (defaultLink.endsWith(".tgz") || defaultLink.endsWith(".tar")
                            || defaultLink.endsWith(".tar.gz") || defaultLink.endsWith(".zip"));

                    switch (entry.type) {
                    case CP:
                        // Path does not handle fragments so use the URI instead
                        Path p = new Path(URI.create(path));

                        if (FILE_SEPARATOR_WARNING && warnCpEntry) {
                            LogFactory.getLog(DistributedCacheFactoryBean.class).warn(
                                    "System path separator is not ':' - this will likely cause invalid classpath entries within the DistributedCache. See the docs and HADOOP-9123 for more information.");
                            // show the warning once per CL
                            FILE_SEPARATOR_WARNING = false;
                        }

                        if (isArchive) {
                            DistributedCache.addArchiveToClassPath(p, conf, fs);
                        } else {
                            DistributedCache.addFileToClassPath(p, conf, fs);
                        }

                        break;

                    case LOCAL:

                        if (isArchive) {
                            if (VersionUtils.isHadoop2X()) {
                                // TODO - Need to figure out how to add local archive
                            } else {
                                Method addLocalArchives = ReflectionUtils.findMethod(DistributedCache.class,
                                        "addLocalArchives", Configuration.class, String.class);
                                addLocalArchives.invoke(null, conf, path);
                            }
                        } else {
                            if (VersionUtils.isHadoop2X()) {
                                // TODO - Need to figure out how to add local files
                            } else {
                                Method addLocalFiles = ReflectionUtils.findMethod(DistributedCache.class,
                                        "addLocalFiles", Configuration.class, String.class);
                                addLocalFiles.invoke(null, conf, path);
                            }
                        }

                        break;

                    case CACHE:

                        if (!path.contains("#")) {
                            // use the path to avoid adding the host:port into the uri
                            uri = URI.create(path + "#" + defaultLink);
                        }

                        if (isArchive) {
                            DistributedCache.addCacheArchive(uri, conf);
                        } else {
                            DistributedCache.addCacheFile(uri, conf);
                        }

                        break;
                    }
                }
            }
        }
    } finally {
        loader.close();
    }
}

From source file:org.springframework.http.codec.json.Jackson2CodecSupport.java

/**
 * Constructor with a Jackson {@link ObjectMapper} to use.
 *//*from ww  w  .  j  av a  2 s  .  com*/
protected Jackson2CodecSupport(ObjectMapper objectMapper, MimeType... mimeTypes) {
    Assert.notNull(objectMapper, "ObjectMapper must not be null");
    this.objectMapper = objectMapper;
    this.mimeTypes = !ObjectUtils.isEmpty(mimeTypes) ? Collections.unmodifiableList(Arrays.asList(mimeTypes))
            : DEFAULT_MIME_TYPES;
}

From source file:org.springframework.http.HttpLog.java

/**
 * Create a composite logger that uses the given primary logger, if enabled,
 * or falls back on one of the given secondary loggers..
 * @param primaryLogger the primary logger
 * @param secondaryLoggers fallback loggers
 * @return a composite logger/* w w w  . jav a  2 s .com*/
 */
public static Log createWith(Log primaryLogger, Log... secondaryLoggers) {
    if (ObjectUtils.isEmpty(secondaryLoggers)) {
        return primaryLogger;
    }
    List<Log> loggers = new ArrayList<>(1 + secondaryLoggers.length);
    loggers.add(primaryLogger);
    Collections.addAll(loggers, secondaryLoggers);
    return new HttpLog(loggers);
}

From source file:org.springframework.integration.config.annotation.AbstractMethodAnnotationPostProcessor.java

protected AbstractEndpoint doCreateEndpoint(MessageHandler handler, MessageChannel inputChannel,
        List<Annotation> annotations) {
    AbstractEndpoint endpoint;//from   ww w  .j  a v a  2  s  . c  o m
    if (inputChannel instanceof PollableChannel) {
        PollingConsumer pollingConsumer = new PollingConsumer((PollableChannel) inputChannel, handler);
        configurePollingEndpoint(pollingConsumer, annotations);
        endpoint = pollingConsumer;
    } else {
        Poller[] pollers = MessagingAnnotationUtils.resolveAttribute(annotations, "poller", Poller[].class);
        Assert.state(ObjectUtils.isEmpty(pollers), "A '@Poller' should not be specified for Annotation-based "
                + "endpoint, since '" + inputChannel + "' is a SubscribableChannel (not pollable).");
        if (inputChannel instanceof Publisher) {
            endpoint = new ReactiveConsumer(inputChannel, handler);
        } else {
            endpoint = new EventDrivenConsumer((SubscribableChannel) inputChannel, handler);
        }
    }
    return endpoint;
}

From source file:org.springframework.integration.config.annotation.AbstractMethodAnnotationPostProcessor.java

protected void configurePollingEndpoint(AbstractPollingEndpoint pollingEndpoint, List<Annotation> annotations) {
    PollerMetadata pollerMetadata = null;
    Poller[] pollers = MessagingAnnotationUtils.resolveAttribute(annotations, "poller", Poller[].class);
    if (!ObjectUtils.isEmpty(pollers)) {
        Assert.state(pollers.length == 1,
                "The 'poller' for an Annotation-based endpoint can have only one '@Poller'.");
        Poller poller = pollers[0];/*ww  w  . ja  v a2s. co m*/

        String ref = poller.value();
        String triggerRef = poller.trigger();
        String executorRef = poller.taskExecutor();
        String fixedDelayValue = this.beanFactory.resolveEmbeddedValue(poller.fixedDelay());
        String fixedRateValue = this.beanFactory.resolveEmbeddedValue(poller.fixedRate());
        String maxMessagesPerPollValue = this.beanFactory.resolveEmbeddedValue(poller.maxMessagesPerPoll());
        String cron = this.beanFactory.resolveEmbeddedValue(poller.cron());
        String errorChannel = this.beanFactory.resolveEmbeddedValue(poller.errorChannel());

        if (StringUtils.hasText(ref)) {
            Assert.state(!StringUtils.hasText(triggerRef) && !StringUtils.hasText(executorRef)
                    && !StringUtils.hasText(cron) && !StringUtils.hasText(fixedDelayValue)
                    && !StringUtils.hasText(fixedRateValue) && !StringUtils.hasText(maxMessagesPerPollValue),
                    "The '@Poller' 'ref' attribute is mutually exclusive with other attributes.");
            pollerMetadata = this.beanFactory.getBean(ref, PollerMetadata.class);
        } else {
            pollerMetadata = new PollerMetadata();
            if (StringUtils.hasText(maxMessagesPerPollValue)) {
                pollerMetadata.setMaxMessagesPerPoll(Long.parseLong(maxMessagesPerPollValue));
            } else if (pollingEndpoint instanceof SourcePollingChannelAdapter) {
                // SPCAs default to 1 message per poll
                pollerMetadata.setMaxMessagesPerPoll(1);
            }

            if (StringUtils.hasText(executorRef)) {
                pollerMetadata.setTaskExecutor(this.beanFactory.getBean(executorRef, TaskExecutor.class));
            }

            Trigger trigger = null;
            if (StringUtils.hasText(triggerRef)) {
                Assert.state(
                        !StringUtils.hasText(cron) && !StringUtils.hasText(fixedDelayValue)
                                && !StringUtils.hasText(fixedRateValue),
                        "The '@Poller' 'trigger' attribute is mutually exclusive with other attributes.");
                trigger = this.beanFactory.getBean(triggerRef, Trigger.class);
            } else if (StringUtils.hasText(cron)) {
                Assert.state(!StringUtils.hasText(fixedDelayValue) && !StringUtils.hasText(fixedRateValue),
                        "The '@Poller' 'cron' attribute is mutually exclusive with other attributes.");
                trigger = new CronTrigger(cron);
            } else if (StringUtils.hasText(fixedDelayValue)) {
                Assert.state(!StringUtils.hasText(fixedRateValue),
                        "The '@Poller' 'fixedDelay' attribute is mutually exclusive with other attributes.");
                trigger = new PeriodicTrigger(Long.parseLong(fixedDelayValue));
            } else if (StringUtils.hasText(fixedRateValue)) {
                trigger = new PeriodicTrigger(Long.parseLong(fixedRateValue));
                ((PeriodicTrigger) trigger).setFixedRate(true);
            }
            //'Trigger' can be null. 'PollingConsumer' does fallback to the 'new PeriodicTrigger(10)'.
            pollerMetadata.setTrigger(trigger);

            if (StringUtils.hasText(errorChannel)) {
                MessagePublishingErrorHandler errorHandler = new MessagePublishingErrorHandler();
                errorHandler.setDefaultErrorChannelName(errorChannel);
                errorHandler.setBeanFactory(this.beanFactory);
                pollerMetadata.setErrorHandler(errorHandler);
            }
        }
    } else {
        pollerMetadata = PollerMetadata.getDefaultPollerMetadata(this.beanFactory);
        Assert.notNull(pollerMetadata, "No poller has been defined for Annotation-based endpoint, "
                + "and no default poller is available within the context.");
    }
    pollingEndpoint.setTaskExecutor(pollerMetadata.getTaskExecutor());
    pollingEndpoint.setTrigger(pollerMetadata.getTrigger());
    pollingEndpoint.setAdviceChain(pollerMetadata.getAdviceChain());
    pollingEndpoint.setMaxMessagesPerPoll(pollerMetadata.getMaxMessagesPerPoll());
    pollingEndpoint.setErrorHandler(pollerMetadata.getErrorHandler());
    if (pollingEndpoint instanceof PollingConsumer) {
        ((PollingConsumer) pollingEndpoint).setReceiveTimeout(pollerMetadata.getReceiveTimeout());
    }
    pollingEndpoint.setTransactionSynchronizationFactory(pollerMetadata.getTransactionSynchronizationFactory());
}

From source file:org.springframework.integration.config.annotation.AbstractMethodAnnotationPostProcessor.java

protected String resolveTargetBeanName(Method method) {
    String id = null;/* www .  j a v  a  2  s .  co  m*/
    String[] names = AnnotationUtils.getAnnotation(method, Bean.class).name();
    if (!ObjectUtils.isEmpty(names)) {
        id = names[0];
    }
    if (!StringUtils.hasText(id)) {
        id = method.getName();
    }
    return id;
}