List of usage examples for java.lang ClassLoader equals
public boolean equals(Object obj)
From source file:Main.java
/** * Override the thread context ClassLoader with the environment's bean ClassLoader * if necessary, i.e. if the bean ClassLoader is not equivalent to the thread * context ClassLoader already./* w w w . j a v a2s . c o m*/ * * @param classLoaderToUse the actual ClassLoader to use for the thread context * @return the original thread context ClassLoader, or {@code null} if not overridden */ public static ClassLoader overrideThreadContextClassLoader(ClassLoader classLoaderToUse) { Thread currentThread = Thread.currentThread(); ClassLoader threadContextClassLoader = currentThread.getContextClassLoader(); if (classLoaderToUse != null && !classLoaderToUse.equals(threadContextClassLoader)) { currentThread.setContextClassLoader(classLoaderToUse); return threadContextClassLoader; } else { return null; } }
From source file:com.gigaspaces.internal.utils.ClassLoaderCleaner.java
private static void clearThreadLocalMap(Object map, Field internalTableField, ClassLoader classLoader) throws NoSuchMethodException, IllegalAccessException, NoSuchFieldException, InvocationTargetException { if (map != null) { Method mapRemove = map.getClass().getDeclaredMethod("remove", ThreadLocal.class); mapRemove.setAccessible(true);/*from w w w . ja va 2 s . c o m*/ final Object[] table = (Object[]) internalTableField.get(map); int staleEntriesCount = 0; if (table != null) { for (int j = 0; j < table.length; j++) { final Object tableEntry = table[j]; if (tableEntry != null) { boolean remove = false; // Check the key Object key = ((Reference<?>) tableEntry).get(); if (classLoader.equals(key) || (key != null && classLoader == key.getClass().getClassLoader())) { remove = true; } // Check the value Field valueField = tableEntry.getClass().getDeclaredField("value"); valueField.setAccessible(true); Object value = valueField.get(tableEntry); if (classLoader.equals(value) || (value != null && classLoader == value.getClass().getClassLoader())) { remove = true; } if (remove) { Object[] args = new Object[4]; if (key != null) { args[0] = key.getClass().getCanonicalName(); args[1] = key.toString(); } if (value != null) { args[2] = value.getClass().getCanonicalName(); args[3] = value.toString(); } if (value == null) { if (logger.isLoggable(Level.FINE)) { logger.fine("A created a ThreadLocal with key of type [" + args[0] + "] (value [" + args[1] + "]). The ThreadLocal has been correctly set to null and the key will be removed by GC. However, to simplify the process of tracing memory leaks, the key has been forcibly removed."); } } else { if (logger.isLoggable(Level.FINE)) logger.fine("A created ThreadLocal with key of type [" + args[0] + "] (value [" + args[1] + "]) and a value of type [" + args[2] + "] (value [" + args[3] + "]) but failed to remove it when class loader is removed. To prevent a memory leak, the ThreadLocal has been forcibly removed."); } if (key == null) { staleEntriesCount++; } else { mapRemove.invoke(map, key); } } } } } if (staleEntriesCount > 0) { Method mapRemoveStale = map.getClass().getDeclaredMethod("expungeStaleEntries"); mapRemoveStale.setAccessible(true); mapRemoveStale.invoke(map); } } }
From source file:com.freetmp.common.util.ClassUtils.java
public static ClassLoader overrideThreadContextClassLoader(ClassLoader classLoaderToUse) { Thread currentThread = Thread.currentThread(); ClassLoader threadContextClassLoader = currentThread.getContextClassLoader(); if (classLoaderToUse != null && !classLoaderToUse.equals(threadContextClassLoader)) { currentThread.setContextClassLoader(classLoaderToUse); return threadContextClassLoader; } else {//from w w w . j av a2 s .c o m return null; } }
From source file:net.sf.jasperreports.util.CastorUtil.java
/** * *//* ww w.j a v a 2 s. co m*/ private XMLContext getXmlContext(String contextCacheKey, String version) { ClassLoader castorClassLoader = Mapping.class.getClassLoader(); ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader(); Object cacheKey; ClassLoader contextClassLoader; if (threadClassLoader == null || threadClassLoader.equals(castorClassLoader)) { cacheKey = CONTEXT_KEY_NULL; contextClassLoader = castorClassLoader; } else { cacheKey = threadClassLoader; contextClassLoader = new CompositeClassloader(castorClassLoader, threadClassLoader); } Map<Object, XMLContext> xmlContextCache = getXmlContextCache(contextCacheKey); XMLContext xmlContext = xmlContextCache.get(cacheKey); if (xmlContext == null) { xmlContext = new XMLContext(); xmlContext.setClassLoader(contextClassLoader); Mapping mapping = new Mapping(contextClassLoader); List<CastorMapping> castorMappings = getMappings(version); for (CastorMapping castorMapping : castorMappings) { loadMapping(mapping, castorMapping.getPath()); } try { xmlContext.addMapping(mapping); } catch (MappingException e) { throw new JRRuntimeException(EXCEPTION_MESSAGE_KEY_MAPPINGS_LOADING_ERROR, (Object[]) null, e); } xmlContextCache.put(cacheKey, xmlContext); } return xmlContext; }
From source file:com.googlecode.arit.jul.HandlerResourceScanner.java
public void clean(ClassLoader classLoader) { Enumeration<String> loggerNames = logManager.getLoggerNames(); while (loggerNames.hasMoreElements()) { Logger logger = logManager.getLogger(loggerNames.nextElement()); // On some JREs, Logger instances may be garbage collected. In this case, // the enumeration returned by getLoggerNames may contain names of garbage // collected loggers, and getLogger will return null for these names. // This was observed with Sun JRE 1.6. Loggers are not garbage collectable // with Sun JRE 1.5, IBM JRE 1.5 and IBM JRE 1.6 (WAS 7.0). if (logger != null) { Handler[] handlers = logger.getHandlers(); for (Handler handler : handlers) { if (classLoader.equals(handler.getClass().getClassLoader())) { logger.removeHandler(handler); LOG.info("Removed JUL handler " + handler.getClass().getName()); }/*w ww . ja va 2 s. c om*/ } } } }
From source file:org.apache.axis2.jaxws.client.async.AsyncResponse.java
/** * Check for a valid relationship between unmarshalling classloader and * Application's current context classloader * @param cl/* w w w.j a v a 2 s . co m*/ * @param contextCL */ private void checkClassLoader(final ClassLoader cl, final ClassLoader contextCL) { // Ensure that the classloader (cl) used for unmarshalling is the same // or a parent of the current context classloader. Otherwise // ClassCastExceptions can occur if (log.isDebugEnabled()) { log.debug("AsyncResponse ClassLoader is:"); log.debug(cl.toString()); } if (cl.equals(contextCL)) { if (log.isDebugEnabled()) { log.debug("AsyncResponse ClassLoader matches Context ClassLoader"); } return; } else { if (log.isDebugEnabled()) { log.debug("Context ClassLoader is:"); log.debug(contextCL.toString()); } ClassLoader parent = getParentClassLoader(contextCL); while (parent != null) { if (parent.equals(cl)) { return; } if (log.isDebugEnabled()) { log.debug("AsyncResponse ClassLoader is an ancestor of the Context ClassLoader"); } parent = getParentClassLoader(parent); } } throw ExceptionFactory.makeWebServiceException(Messages.getMessage("threadClsLoaderErr", contextCL.getClass().toString(), cl.getClass().toString())); }
From source file:org.codelibs.fess.servlet.Tomcat6ConfigServlet.java
@SuppressWarnings("deprecation") private void cleanupAllThreads() { final Thread[] threads = getThreads(); final ClassLoader cl = this.getClass().getClassLoader(); try {/*from w w w .ja v a2 s .c om*/ cl.getResource(null); } catch (final Exception e) { } final List<String> jvmThreadGroupList = new ArrayList<String>(); jvmThreadGroupList.add("system"); jvmThreadGroupList.add("RMI Runtime"); // Iterate over the set of threads for (final Thread thread : threads) { if (thread != null) { final ClassLoader ccl = thread.getContextClassLoader(); if (ccl != null && ccl.equals(cl)) { // Don't warn about this thread if (thread == Thread.currentThread()) { continue; } // Don't warn about JVM controlled threads final ThreadGroup tg = thread.getThreadGroup(); if (tg != null && jvmThreadGroupList.contains(tg.getName())) { continue; } waitThread(thread); // Skip threads that have already died if (!thread.isAlive()) { continue; } if (logger.isInfoEnabled()) { logger.info("Interrupting a thread [" + thread.getName() + "]..."); } thread.interrupt(); waitThread(thread); // Skip threads that have already died if (!thread.isAlive()) { continue; } if (logger.isInfoEnabled()) { logger.info("Stopping a thread [" + thread.getName() + "]..."); } thread.stop(); } } } Field threadLocalsField = null; Field inheritableThreadLocalsField = null; Field tableField = null; try { threadLocalsField = Thread.class.getDeclaredField("threadLocals"); threadLocalsField.setAccessible(true); inheritableThreadLocalsField = Thread.class.getDeclaredField("inheritableThreadLocals"); inheritableThreadLocalsField.setAccessible(true); // Make the underlying array of ThreadLoad.ThreadLocalMap.Entry objects // accessible final Class<?> tlmClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap"); tableField = tlmClass.getDeclaredField("table"); tableField.setAccessible(true); } catch (final Exception e) { // ignore } for (final Thread thread : threads) { if (thread != null) { Object threadLocalMap; try { // Clear the first map threadLocalMap = threadLocalsField.get(thread); clearThreadLocalMap(cl, threadLocalMap, tableField); } catch (final Exception e) { // ignore } try { // Clear the second map threadLocalMap = inheritableThreadLocalsField.get(thread); clearThreadLocalMap(cl, threadLocalMap, tableField); } catch (final Exception e) { // ignore } } } }
From source file:org.codelibs.fess.servlet.Tomcat6ConfigServlet.java
private void clearThreadLocalMap(final ClassLoader cl, final Object map, final Field internalTableField) throws NoSuchMethodException, IllegalAccessException, NoSuchFieldException, InvocationTargetException { if (map != null) { final Method mapRemove = map.getClass().getDeclaredMethod("remove", ThreadLocal.class); mapRemove.setAccessible(true);// ww w.java 2 s . com final Object[] table = (Object[]) internalTableField.get(map); if (table != null) { for (final Object element : table) { if (element != null) { boolean remove = false; // Check the key final Field keyField = Reference.class.getDeclaredField("referent"); keyField.setAccessible(true); final Object key = keyField.get(element); if (cl.equals(key) || key != null && cl == key.getClass().getClassLoader()) { remove = true; } // Check the value final Field valueField = element.getClass().getDeclaredField("value"); valueField.setAccessible(true); final Object value = valueField.get(element); if (cl.equals(value) || value != null && cl == value.getClass().getClassLoader()) { remove = true; } if (remove) { final Object entry = ((Reference<?>) element).get(); if (logger.isInfoEnabled()) { logger.info("Removing " + key.toString() + " from a thread local..."); } mapRemove.invoke(map, entry); } } } } } }
From source file:org.echocat.jomon.runtime.i18n.RecursiveResourceBundleFactory.java
public void flushEntriesOf(@Nonnull ClassLoader classLoader) { synchronized (this) { _classLoaderToPackageNameAndNotRecursiveBundleCache.remove(classLoader); final Iterator<Class<?>> i = _typeToNotRecursiveBundleCache.keySet().iterator(); while (i.hasNext()) { final Class<?> type = i.next(); if (classLoader.equals(type.getClassLoader())) { i.remove();// www . j ava 2 s. co m } } } }