Example usage for java.lang ThreadLocal get

List of usage examples for java.lang ThreadLocal get

Introduction

In this page you can find the example usage for java.lang ThreadLocal get.

Prototype

public T get() 

Source Link

Document

Returns the value in the current thread's copy of this thread-local variable.

Usage

From source file:com.github.fharms.camel.route.CamelEntityManagerTestRouteTest.java

private Object getCamelEntityManagerThreadLocal() throws IllegalAccessException, NoSuchFieldException {
    CamelEntityManagerHandler handler = applicationContext.getBean(CamelEntityManagerHandler.class);
    Field entityManagerLocalField = handler.getClass().getDeclaredField("entityManagerLocal");
    entityManagerLocalField.setAccessible(true);
    ThreadLocal tl = (ThreadLocal) entityManagerLocalField.get(handler);
    return tl.get();
}

From source file:org.codice.alliance.nsili.common.ResultDAGConverter.java

private static boolean processEntry(String entryName, List<String> requiredAttrs, List<String> parsedAttrs) {
    final ThreadLocal<Boolean> dataIsValid = new ThreadLocal<>();
    dataIsValid.set(true);//from   w ww .j  a v a2 s . c  om

    if (requiredAttrs != null) {
        requiredAttrs.stream().filter(requiredAttr -> !parsedAttrs.contains(requiredAttr))
                .forEach(missingAttr -> {
                    dataIsValid.set(false);
                    LOGGER.warn("Node: {} is missing attribute: {}", entryName, missingAttr);
                });
    }

    return dataIsValid.get();
}

From source file:com.github.mjeanroy.springmvc.view.mustache.core.DefaultTemplateLoaderTest.java

@Test
public void it_should_add_temporary_partial_aliases() throws Exception {
    String k1 = "foo";
    String v1 = "bar";
    String k2 = "bar";
    String v2 = "foo";

    Map<String, String> aliases = new HashMap<String, String>();
    aliases.put(k1, v1);/*from w  w  w. ja v a  2 s .co m*/
    aliases.put(k2, v2);

    DefaultTemplateLoader loader = new DefaultTemplateLoader(resourceLoader, prefix, suffix);
    loader.addTemporaryPartialAliases(aliases);

    ThreadLocal<Map<String, String>> tl = (ThreadLocal<Map<String, String>>) readField(loader,
            "temporaryPartialAliases", true);
    Map<String, String> partialsAliases = tl.get();
    assertThat(partialsAliases).isNotNull().isNotEmpty().hasSize(aliases.size()).containsOnly(entry(k1, v1),
            entry(k2, v2));
}

From source file:com.github.mjeanroy.springmvc.view.mustache.core.DefaultMustacheTemplateLoaderTest.java

@Test
public void it_should_add_temporary_partial_aliases() throws Exception {
    String k1 = "foo";
    String v1 = "bar";
    String k2 = "bar";
    String v2 = "foo";

    Map<String, String> aliases = new HashMap<String, String>();
    aliases.put(k1, v1);/*from  w  w  w  .ja  v  a  2  s  . c o  m*/
    aliases.put(k2, v2);

    DefaultMustacheTemplateLoader loader = new DefaultMustacheTemplateLoader(resourceLoader, prefix, suffix);
    loader.addTemporaryPartialAliases(aliases);

    ThreadLocal<Map<String, String>> tl = (ThreadLocal<Map<String, String>>) readField(loader,
            "temporaryPartialAliases", true);
    Map<String, String> partialsAliases = tl.get();
    assertThat(partialsAliases).isNotNull().isNotEmpty().hasSize(aliases.size()).containsOnly(entry(k1, v1),
            entry(k2, v2));
}

From source file:com.github.mjeanroy.springmvc.view.mustache.core.DefaultTemplateLoaderTest.java

@Test
public void it_should_remove_temporary_partial_aliases() throws Exception {
    String k1 = "foo";
    String v1 = "bar";
    String k2 = "bar";
    String v2 = "foo";

    Map<String, String> aliases = new HashMap<String, String>();
    aliases.put(k1, v1);/* w w w.j  a  v a 2  s . c o m*/
    aliases.put(k2, v2);

    DefaultTemplateLoader loader = new DefaultTemplateLoader(resourceLoader, prefix, suffix);
    loader.addTemporaryPartialAliases(aliases);

    ThreadLocal<Map<String, String>> tl = (ThreadLocal<Map<String, String>>) readField(loader,
            "temporaryPartialAliases", true);
    Map<String, String> partialsAliases = tl.get();
    assertThat(partialsAliases).isNotNull().isNotEmpty().hasSize(aliases.size()).containsOnly(entry(k1, v1),
            entry(k2, v2));

    loader.removeTemporaryPartialAliases();

    tl = (ThreadLocal<Map<String, String>>) readField(loader, "temporaryPartialAliases", true);
    partialsAliases = tl.get();
    assertThat(partialsAliases).isNotNull().isEmpty();
}

From source file:com.github.mjeanroy.springmvc.view.mustache.core.DefaultMustacheTemplateLoaderTest.java

@Test
public void it_should_remove_temporary_partial_aliases() throws Exception {
    String k1 = "foo";
    String v1 = "bar";
    String k2 = "bar";
    String v2 = "foo";

    Map<String, String> aliases = new HashMap<String, String>();
    aliases.put(k1, v1);//from   w ww  . ja va 2s . c  o  m
    aliases.put(k2, v2);

    DefaultMustacheTemplateLoader loader = new DefaultMustacheTemplateLoader(resourceLoader, prefix, suffix);
    loader.addTemporaryPartialAliases(aliases);

    ThreadLocal<Map<String, String>> tl = (ThreadLocal<Map<String, String>>) readField(loader,
            "temporaryPartialAliases", true);
    Map<String, String> partialsAliases = tl.get();
    assertThat(partialsAliases).isNotNull().isNotEmpty().hasSize(aliases.size()).containsOnly(entry(k1, v1),
            entry(k2, v2));

    loader.removeTemporaryPartialAliases();

    tl = (ThreadLocal<Map<String, String>>) readField(loader, "temporaryPartialAliases", true);
    partialsAliases = tl.get();
    assertThat(partialsAliases).isNotNull().isEmpty();
}

From source file:jef.tools.DateUtils.java

/**
 * ?? ThreadLocal?//from  w  w  w . j  a  v a  2  s .c  om
 * 
 * @param d
 * @param format
 * @return
 */
public static String format(Date d, ThreadLocal<DateFormat> format) {
    if (d == null)
        return "";
    return format.get().format(d);
}

From source file:jef.tools.DateUtils.java

/**
 * ? ?/* w ww  .  ja  v a 2 s .c om*/
 * 
 * @param s
 * @param format
 * @return
 * @throws ParseException
 */
public static Date parse(String s, ThreadLocal<DateFormat> format) throws ParseException {
    if (StringUtils.isBlank(s))
        return null;
    return format.get().parse(s);
}

From source file:org.codice.alliance.transformer.nitf.image.NitfPreStoragePlugin.java

private BufferedImage renderImage(ContentItem contentItem)
        throws IOException, ParseException, NitfFormatException {

    final ThreadLocal<BufferedImage> bufferedImage = new ThreadLocal<>();

    if (contentItem != null && contentItem.getInputStream() != null) {
        NitfRenderer renderer = new NitfRenderer();

        new NitfParserInputFlow().inputStream(contentItem.getInputStream()).allData()
                .forEachImageSegment(segment -> {
                    if (bufferedImage.get() == null) {
                        try {
                            bufferedImage.set(renderer.render(segment));
                        } catch (IOException e) {
                            LOGGER.error(e.getMessage(), e);
                        }// w w  w . j a  v a2s.c  o m
                    }
                }).end();
    }

    return bufferedImage.get();
}

From source file:jef.tools.DateUtils.java

/**
 * ? ??/*  w  w  w .  j av a  2  s  .c  o m*/
 * 
 * @return defaultValue ?defaultValue
 * @throws?ParseException
 */
public static Date parse(String s, ThreadLocal<DateFormat> format, Date defaultValue) {
    if (StringUtils.isBlank(s))
        return defaultValue;
    try {
        return format.get().parse(s);
    } catch (ParseException e) {
        LogUtil.exception(e);
        return defaultValue;
    }
}