Example usage for java.lang Thread getContextClassLoader

List of usage examples for java.lang Thread getContextClassLoader

Introduction

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

Prototype

@CallerSensitive
public ClassLoader getContextClassLoader() 

Source Link

Document

Returns the context ClassLoader for this thread.

Usage

From source file:be.fedict.trust.BelgianTrustValidatorFactory.java

private static X509Certificate loadCertificate(String resourceName) {
    LOG.debug("loading certificate: " + resourceName);
    Thread currentThread = Thread.currentThread();
    ClassLoader classLoader = currentThread.getContextClassLoader();
    InputStream certificateInputStream = classLoader.getResourceAsStream(resourceName);
    if (null == certificateInputStream) {
        throw new IllegalArgumentException("resource not found: " + resourceName);
    }/*from   ww  w . ja v  a 2 s. c o  m*/
    try {
        CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
        X509Certificate certificate = (X509Certificate) certificateFactory
                .generateCertificate(certificateInputStream);
        return certificate;
    } catch (CertificateException e) {
        throw new RuntimeException("X509 error: " + e.getMessage(), e);
    }
}

From source file:org.apache.axis2.jaxws.message.databinding.JAXBContextFromClasses.java

/**
 * Utility method that creates a JAXBContext from the 
 * class[] and ClassLoader.// w  w w .  j a v a  2s .co m
 * 
 * @param classArray
 * @param cl
 * @return JAXBContext
 * @throws Throwable
 */
private static JAXBContext _newInstance(final Class[] classArray, final ClassLoader cl,
        final Map<String, ?> properties) throws Throwable {
    JAXBContext jaxbContext;
    try {
        jaxbContext = (JAXBContext) AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws JAXBException {
                // Unlike the JAXBContext.newInstance(Class[]) method
                // does now accept a classloader.  To workaround this
                // issue, the classloader is temporarily changed to cl
                Thread currentThread = Thread.currentThread();
                ClassLoader savedClassLoader = currentThread.getContextClassLoader();
                try {
                    currentThread.setContextClassLoader(cl);
                    return JAXBContext.newInstance(classArray, properties);
                } finally {
                    currentThread.setContextClassLoader(savedClassLoader);
                }
            }
        });
    } catch (PrivilegedActionException e) {
        throw ((PrivilegedActionException) e).getException();
    } catch (Throwable t) {
        throw t;
    }
    return jaxbContext;
}

From source file:org.talend.mdm.repository.core.service.ModelImpactAnalyseService.java

public static Result readResponseMessage(String message) {
    if (message != null) {
        if (message.trim().isEmpty()) {
            // first time deploy
            return null;
        }/*  ww  w  .j a  va2s  .  c om*/
        Thread cur = Thread.currentThread();
        ClassLoader save = cur.getContextClassLoader();
        cur.setContextClassLoader(RepositoryPlugin.getDefault().getClass().getClassLoader());
        try {
            Result result = (Result) getParser().fromXML(message);
            return result;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        } finally {
            cur.setContextClassLoader(save);

        }
    }
    return null;
}

From source file:be.fedict.eid.applet.service.signer.time.TSPTimeStampService.java

private static X509Certificate loadCertificate(String resourceName) {
    LOG.debug("loading certificate: " + resourceName);
    Thread currentThread = Thread.currentThread();
    ClassLoader classLoader = currentThread.getContextClassLoader();
    InputStream certificateInputStream = classLoader.getResourceAsStream(resourceName);
    if (null == certificateInputStream) {
        throw new IllegalArgumentException("resource not found: " + resourceName);
    }//from  w  w w  .  ja v a2 s  .com
    try {
        CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
        return (X509Certificate) certificateFactory.generateCertificate(certificateInputStream);
    } catch (CertificateException e) {
        throw new RuntimeException("X509 error: " + e.getMessage(), e);
    }
}

From source file:com.huawei.streaming.cql.DriverContext.java

/**
 * classpathjar/*  w ww . j  av  a  2s .c  o m*/
 *
 * @param pathsToRemove jar
 * @throws IOException jar
 */
private static void removeFromClassPath(String[] pathsToRemove) throws IOException {
    Thread curThread = Thread.currentThread();
    URLClassLoader loader = (URLClassLoader) curThread.getContextClassLoader();
    Set<URL> newPath = new HashSet<URL>(Arrays.asList(loader.getURLs()));

    if (pathsToRemove != null) {
        for (String onestr : pathsToRemove) {
            if (StringUtils.indexOf(onestr, FILE_PREFIX) == 0) {
                onestr = StringUtils.substring(onestr, CQLConst.I_7);
            }

            URL oneurl = (new File(onestr)).toURI().toURL();
            newPath.remove(oneurl);
        }
    }

    loader = new URLClassLoader(newPath.toArray(new URL[0]));
    curThread.setContextClassLoader(loader);
}

From source file:org.springframework.data.hadoop.mapreduce.ExecutionUtils.java

private static void replaceTccl(ClassLoader leakedClassLoader, ClassLoader replacementClassLoader) {
    for (Thread thread : threads()) {
        if (thread != null) {
            ClassLoader cl = thread.getContextClassLoader();
            // do identity check to prevent expensive (and potentially dangerous) equals()
            if (leakedClassLoader == cl) {
                log.warn("Trying to patch leaked cl [" + leakedClassLoader + "] in thread [" + thread + "]");
                ThreadGroup tg = thread.getThreadGroup();
                // it's a JVM thread so use the System ClassLoader always
                boolean debug = log.isDebugEnabled();
                if (tg != null && JVM_THREAD_NAMES.contains(tg.getName())) {
                    thread.setContextClassLoader(ClassLoader.getSystemClassLoader());
                    if (debug) {
                        log.debug("Replaced leaked cl in thread [" + thread + "] with system classloader");
                    }//from w ww. ja  v a2s  . com
                } else {
                    thread.setContextClassLoader(replacementClassLoader);
                    if (debug) {
                        log.debug(
                                "Replaced leaked cl in thread [" + thread + "] with " + replacementClassLoader);
                    }
                }
            }
        }
    }
}

From source file:org.nuxeo.common.xmap.XMap.java

private static DocumentBuilderFactory initFactory() {
    Thread t = Thread.currentThread();
    ClassLoader cl = t.getContextClassLoader();
    t.setContextClassLoader(XMap.class.getClassLoader());
    try {//  w  w w . ja va2  s .c om
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        return factory;
    } finally {
        t.setContextClassLoader(cl);
    }
}

From source file:org.apache.jackrabbit.oak.plugins.index.lucene.LuceneIndexEditorContext.java

static IndexWriterConfig getIndexWriterConfig(IndexDefinition definition, boolean remoteDir) {
    // FIXME: Hack needed to make Lucene work in an OSGi environment
    Thread thread = Thread.currentThread();
    ClassLoader loader = thread.getContextClassLoader();
    thread.setContextClassLoader(IndexWriterConfig.class.getClassLoader());
    try {//from  w  w w.j av a2 s .c  om
        Analyzer definitionAnalyzer = definition.getAnalyzer();
        Map<String, Analyzer> analyzers = new HashMap<String, Analyzer>();
        analyzers.put(FieldNames.SPELLCHECK, new ShingleAnalyzerWrapper(LuceneIndexConstants.ANALYZER, 3));
        if (!definition.isSuggestAnalyzed()) {
            analyzers.put(FieldNames.SUGGEST, SuggestHelper.getAnalyzer());
        }
        Analyzer analyzer = new PerFieldAnalyzerWrapper(definitionAnalyzer, analyzers);
        IndexWriterConfig config = new IndexWriterConfig(VERSION, analyzer);
        if (remoteDir) {
            config.setMergeScheduler(new SerialMergeScheduler());
        }
        if (definition.getCodec() != null) {
            config.setCodec(definition.getCodec());
        }
        return config;
    } finally {
        thread.setContextClassLoader(loader);
    }
}

From source file:com.nubits.nubot.utils.Utils.java

public static void logActiveThreads() {
    int active = Thread.activeCount();
    LOG.trace("currently active threads: " + active);
    Thread allThreads[] = new Thread[active];
    Thread.enumerate(allThreads);

    for (int i = 0; i < active; i++) {
        Thread t = allThreads[i];
        LOG.trace(i + ": " + t + " id: " + t.getId() + " name: " + t.getName() + " " + t.getContextClassLoader()
                + " group: " + t.getThreadGroup() + " alive" + t.isAlive());
        LOG.trace("super: " + t.getClass().getSuperclass());
    }/*from  w w  w.  ja  v  a  2s .c  om*/

    if (active > maxThreadsError) {
        LOG.error("too many threads started");
    }
}

From source file:org.openspaces.test.client.executor.ExecutorUtils.java

/**
 * Loads the requested resource and returns its input straem
 *
 * @param name name of resource to get/*w  w  w.  java 2s.c om*/
 * @return URL containing the resource
 */
static public URL getResourceURL(String name) {
    URL result = null;

    //do not allow search using / prefix which does not work with classLoader.getResource()
    if (name.startsWith("/")) {
        name = name.substring(1);
    }
    Thread currentThread = Thread.currentThread();
    ClassLoader classLoader = currentThread.getContextClassLoader();
    result = classLoader.getResource(name);

    return result;
}