Example usage for java.lang ThreadLocal ThreadLocal

List of usage examples for java.lang ThreadLocal ThreadLocal

Introduction

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

Prototype

public ThreadLocal() 

Source Link

Document

Creates a thread local variable.

Usage

From source file:org.loginject.SpringLogInjectionService.java

@Override
public BeanFactoryPostProcessor getBindings(LogInject<_Logger_> logInject) {
    return new BeanFactoryPostProcessor() {
        ThreadLocal<Object> injectee = new ThreadLocal<>();

        @Override//  w  w  w  .  j  a v  a2s  . co m
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
            DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) beanFactory;
            AutowireCandidateResolver resolver = new ContextAnnotationAutowireCandidateResolver() {
                @Override
                public Object getSuggestedValue(DependencyDescriptor descriptor) {
                    if (descriptor.getDependencyType().equals(logInject.getLoggerClass())) {
                        return logInject.createLogger(injectee.get().getClass());
                    }
                    return null;
                }
            };
            AutowiredAnnotationBeanPostProcessor beanPostProcessor = new AutowiredAnnotationBeanPostProcessor() {
                @Override
                public PropertyValues postProcessPropertyValues(PropertyValues values,
                        PropertyDescriptor[] descriptors, Object bean, String beanName) throws BeansException {
                    injectee.set(bean);
                    return super.postProcessPropertyValues(values, descriptors, bean, beanName);
                }
            };
            beanPostProcessor.setBeanFactory(defaultListableBeanFactory);
            defaultListableBeanFactory.addBeanPostProcessor(beanPostProcessor);
            defaultListableBeanFactory.setAutowireCandidateResolver(resolver);
        }
    };
}

From source file:org.lilyproject.repository.bulk.serial.ThreadedRecordWriter.java

public ThreadedRecordWriter(String lilyZk, int numThreads, String repositoryName, String tableName,
        boolean bulkMode) {
    this.lilyZk = lilyZk;
    this.repositoryName = repositoryName;
    this.tableName = tableName;
    this.bulkMode = bulkMode;
    executor = new ThreadPoolExecutor(numThreads, numThreads, 10, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(5));
    executor.setRejectedExecutionHandler(new WaitPolicy());
    threadLocalBulkIngesters = new ThreadLocal<BulkIngester>();
    bulkIngesters = Collections.synchronizedList(Lists.<BulkIngester>newArrayList());
}

From source file:org.apache.jmeter.config.RandomVariableConfig.java

private ThreadLocal<Random> initThreadLocal() {
    return new ThreadLocal<Random>() {
        @Override//from w w w.j  a  v a2s.c  om
        protected Random initialValue() {
            init();
            return new Random(getRandomSeedAsLong());
        }
    };
}

From source file:com.skysql.manager.api.NodeInfo.java

private CachedData getLastModified() {
    if (lastModified == null || lastModified.get() == null) {
        lastModified = new ThreadLocal<CachedData>();
        lastModified.set(VaadinSession.getCurrent().getAttribute(CachedData.class));
    }//from   w w  w.j  av a  2  s.co m
    return lastModified.get();
}

From source file:org.apache.axis2.transaction.TransactionConfiguration.java

public TransactionConfiguration(ParameterInclude transactionParameters) throws DeploymentException {
    Iterator it = transactionParameters.getParameters().iterator();
    while (it.hasNext()) {
        Parameter parameter = (Parameter) it.next();
        jndiProperties.put(parameter.getName(), (String) parameter.getValue());
    }// w  ww.  java 2  s  .co m

    transactionManagerJNDIName = (String) transactionParameters.getParameter(TX_MANAGER_JNDI_NAME).getValue();

    if (transactionManagerJNDIName == null) {
        throw new DeploymentException("Required transaction parameter " + TX_MANAGER_JNDI_NAME + " missing");
    }

    threadTransactionManager = new ThreadLocal();
}

From source file:org.cyclop.service.common.FileStorage.java

@PostConstruct
protected void init() {
    supported = checkSupported();//from w  w  w. j a  va  2s  . com

    encoder = new ThreadLocal<CharsetEncoder>() {
        @Override
        protected CharsetEncoder initialValue() {
            Charset charset = Charset.forName("UTF-8");
            CharsetEncoder decoder = charset.newEncoder();
            return decoder;
        }
    };

    decoder = new ThreadLocal<CharsetDecoder>() {
        @Override
        protected CharsetDecoder initialValue() {
            Charset charset = Charset.forName("UTF-8");
            CharsetDecoder decoder = charset.newDecoder();
            return decoder;
        }
    };
}

From source file:marshalsec.gadgets.SpringUtil.java

public static BeanFactory makeMethodTrigger(Object o, String method) throws Exception {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    RootBeanDefinition caller = new RootBeanDefinition();

    caller.setFactoryBeanName("obj");
    caller.setFactoryMethodName(method);
    Reflections.setFieldValue(caller.getMethodOverrides(), "overrides", new HashSet<>());
    bf.registerBeanDefinition("caller", caller);

    Reflections.getField(DefaultListableBeanFactory.class, "beanClassLoader").set(bf, null);
    Reflections.getField(DefaultListableBeanFactory.class, "alreadyCreated").set(bf, new HashSet<>());
    Reflections.getField(DefaultListableBeanFactory.class, "singletonsCurrentlyInCreation").set(bf,
            new HashSet<>());
    Reflections.getField(DefaultListableBeanFactory.class, "inCreationCheckExclusions").set(bf,
            new HashSet<>());
    Reflections.getField(DefaultListableBeanFactory.class, "logger").set(bf, new NoOpLog());
    Reflections.getField(DefaultListableBeanFactory.class, "prototypesCurrentlyInCreation").set(bf,
            new ThreadLocal<>());

    @SuppressWarnings("unchecked")
    Map<String, Object> objs = (Map<String, Object>) Reflections.getFieldValue(bf, "singletonObjects");
    objs.put("obj", o);
    return bf;//from w w w.j a  va2s. c om
}

From source file:com.mmnaseri.dragonfly.data.impl.DelegatingConnection.java

public DelegatingConnection(Connection connection, Processor<Connection> closeCallback) {
    this.closeCallback = closeCallback;
    this.connection = connection;
    this.opened = new ThreadLocal<Long>() {
        @Override/*from w  w w .java2s  .  c o m*/
        protected Long initialValue() {
            return 0L;
        }
    };
    log.info("Connection requested");
}

From source file:org.apache.lens.cube.metadata.UpdatePeriod.java

private static DateFormat getMinutelyFormat() {
    if (minutelyFormat == null) {
        minutelyFormat = new ThreadLocal<DateFormat>() {
            @Override//from w w  w .ja v  a2  s. c om
            protected SimpleDateFormat initialValue() {
                return new SimpleDateFormat(MINUTELY.formatStr());
            }
        };
    }
    return minutelyFormat.get();
}

From source file:org.atomserver.core.validators.RelaxNGValidator.java

public void setSchemaLocation(final Resource schemaLocation) {
    validationDriver = new ThreadLocal<ValidationDriver>() {

        protected ValidationDriver initialValue() {
            ValidationDriver validationDriver = new ValidationDriver(ERROR_HANDLER_PROPERTY_MAP,
                    CompactSchemaReader.getInstance());
            try {
                validationDriver.loadSchema(new InputSource(schemaLocation.getInputStream()));
                return validationDriver;
            } catch (Exception e) {
                log.error("exception loading schema", e);
                throw new RuntimeException("exception loading schema", e);
            }//from www.  j ava 2s  . c o m
        }
    };
    validationDriver.get();
}