List of usage examples for java.lang Thread getContextClassLoader
@CallerSensitive
public ClassLoader getContextClassLoader()
From source file:com.liferay.portal.util.LocalizationImpl.java
private String _getRootAttribute(String xml, String name, String defaultValue) { String value = null;// w w w .j av a 2 s . c o m XMLStreamReader xmlStreamReader = null; ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader(); Thread currentThread = Thread.currentThread(); ClassLoader contextClassLoader = currentThread.getContextClassLoader(); try { if (contextClassLoader != portalClassLoader) { currentThread.setContextClassLoader(portalClassLoader); } XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); xmlStreamReader = xmlInputFactory.createXMLStreamReader(new UnsyncStringReader(xml)); if (xmlStreamReader.hasNext()) { xmlStreamReader.nextTag(); value = xmlStreamReader.getAttributeValue(null, name); } } catch (Exception e) { if (_log.isWarnEnabled()) { _log.warn(e, e); } } finally { if (contextClassLoader != portalClassLoader) { currentThread.setContextClassLoader(contextClassLoader); } if (xmlStreamReader != null) { try { xmlStreamReader.close(); } catch (Exception e) { } } } if (Validator.isNull(value)) { value = defaultValue; } return value; }
From source file:net.lightbody.bmp.proxy.jetty.jetty.servlet.WebApplicationContext.java
/** Start the Web Application. * @exception IOException // w w w . j av a2 s.c om */ protected void doStart() throws Exception { if (isStarted()) return; // save context classloader Thread thread = Thread.currentThread(); ClassLoader lastContextLoader = thread.getContextClassLoader(); MultiException mex = null; try { // Find the webapp resolveWebApp(); // Get the handler getServletHandler(); _configurations = loadConfigurations(); // initialize the classloader configureClassPath(); initClassLoader(true); thread.setContextClassLoader(getClassLoader()); initialize(); // Do the default configuration configureDefaults(); // Set classpath for Jasper. Map.Entry entry = _webAppHandler.getHolderEntry("test.jsp"); if (entry != null) { ServletHolder jspHolder = (ServletHolder) entry.getValue(); if (jspHolder != null && jspHolder.getInitParameter("classpath") == null) { String fileClassPath = getFileClassPath(); jspHolder.setInitParameter("classpath", fileClassPath); if (log.isDebugEnabled()) log.debug("Set classpath=" + fileClassPath + " for " + jspHolder); } } // configure webapp configureWebApp(); // If we have servlets, don't init them yet _webAppHandler.setAutoInitializeServlets(false); // Start handlers super.doStart(); mex = new MultiException(); // Context listeners if (_contextListeners != null && _webAppHandler != null) { ServletContextEvent event = new ServletContextEvent(getServletContext()); for (int i = 0; i < LazyList.size(_contextListeners); i++) { try { ((ServletContextListener) LazyList.get(_contextListeners, i)).contextInitialized(event); } catch (Exception ex) { mex.add(ex); } } } // OK to Initialize servlets now if (_webAppHandler != null && _webAppHandler.isStarted()) { try { _webAppHandler.initializeServlets(); } catch (Exception ex) { mex.add(ex); } } } catch (Exception e) { log.warn("Configuration error on " + _war, e); throw e; } finally { thread.setContextClassLoader(lastContextLoader); } if (mex != null) mex.ifExceptionThrow(); }
From source file:com.saysth.commons.quartz.SchedulerFactoryBean.java
/** * Create the Scheduler instance for the given factory and scheduler name. * Called by {@link #afterPropertiesSet}. * <p>//from w w w . ja v a2 s . c om * The default implementation invokes SchedulerFactory's * <code>getScheduler</code> method. Can be overridden for custom Scheduler * creation. * * @param schedulerFactory * the factory to create the Scheduler with * @param schedulerName * the name of the scheduler to create * @return the Scheduler instance * @throws SchedulerException * if thrown by Quartz methods * @see #afterPropertiesSet * @see org.quartz.SchedulerFactory#getScheduler */ protected Scheduler createScheduler(SchedulerFactory schedulerFactory, String schedulerName) throws SchedulerException { // Override thread context ClassLoader to work around naive Quartz // ClassLoadHelper loading. Thread currentThread = Thread.currentThread(); ClassLoader threadContextClassLoader = currentThread.getContextClassLoader(); boolean overrideClassLoader = (this.resourceLoader != null && !this.resourceLoader.getClassLoader().equals(threadContextClassLoader)); if (overrideClassLoader) { currentThread.setContextClassLoader(this.resourceLoader.getClassLoader()); } try { SchedulerRepository repository = SchedulerRepository.getInstance(); synchronized (repository) { Scheduler existingScheduler = (schedulerName != null ? repository.lookup(schedulerName) : null); Scheduler newScheduler = schedulerFactory.getScheduler(); if (newScheduler == existingScheduler) { throw new IllegalStateException("Active Scheduler of name '" + schedulerName + "' already registered " + "in Quartz SchedulerRepository. Cannot create a new Spring-managed Scheduler of the same name!"); } if (!this.exposeSchedulerInRepository) { // Need to remove it in this case, since Quartz shares the // Scheduler instance by default! SchedulerRepository.getInstance().remove(newScheduler.getSchedulerName()); } return newScheduler; } } finally { if (overrideClassLoader) { // Reset original thread context ClassLoader. currentThread.setContextClassLoader(threadContextClassLoader); } } }
From source file:net.lightbody.bmp.proxy.jetty.jetty.servlet.AbstractSessionManager.java
/** Find sessions that have timed out and invalidate them. * This runs in the SessionScavenger thread. *///from w w w .j a v a 2 s . co m private void scavenge() { Thread thread = Thread.currentThread(); ClassLoader old_loader = thread.getContextClassLoader(); try { if (_handler == null) return; ClassLoader loader = _handler.getClassLoader(); if (loader != null) thread.setContextClassLoader(loader); long now = System.currentTimeMillis(); // Since Hashtable enumeration is not safe over deletes, // we build a list of stale sessions, then go back and invalidate them Object stale = null; synchronized (AbstractSessionManager.this) { // For each session for (Iterator i = _sessions.values().iterator(); i.hasNext();) { Session session = (Session) i.next(); long idleTime = session._maxIdleMs; if (idleTime > 0 && session._accessed + idleTime < now) { // Found a stale session, add it to the list stale = LazyList.add(stale, session); } } } // Remove the stale sessions for (int i = LazyList.size(stale); i-- > 0;) { // check it has not been accessed in the meantime Session session = (Session) LazyList.get(stale, i); long idleTime = session._maxIdleMs; if (idleTime > 0 && session._accessed + idleTime < System.currentTimeMillis()) { session.invalidate(); int nbsess = this._sessions.size(); if (nbsess < this._minSessions) this._minSessions = nbsess; } } } finally { thread.setContextClassLoader(old_loader); } }
From source file:com.liferay.portal.util.LocalizationImpl.java
public String removeLocalization(String xml, String key, String requestedLanguageId, boolean cdata, boolean localized) { if (Validator.isNull(xml)) { return StringPool.BLANK; }/*from w w w . j av a 2 s .com*/ xml = _sanitizeXML(xml); String systemDefaultLanguageId = LocaleUtil.toLanguageId(LocaleUtil.getDefault()); XMLStreamReader xmlStreamReader = null; XMLStreamWriter xmlStreamWriter = null; ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader(); Thread currentThread = Thread.currentThread(); ClassLoader contextClassLoader = currentThread.getContextClassLoader(); try { if (contextClassLoader != portalClassLoader) { currentThread.setContextClassLoader(portalClassLoader); } XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); xmlStreamReader = xmlInputFactory.createXMLStreamReader(new UnsyncStringReader(xml)); String availableLocales = StringPool.BLANK; String defaultLanguageId = StringPool.BLANK; // Read root node if (xmlStreamReader.hasNext()) { xmlStreamReader.nextTag(); availableLocales = xmlStreamReader.getAttributeValue(null, _AVAILABLE_LOCALES); defaultLanguageId = xmlStreamReader.getAttributeValue(null, _DEFAULT_LOCALE); if (Validator.isNull(defaultLanguageId)) { defaultLanguageId = systemDefaultLanguageId; } } if ((availableLocales != null) && (availableLocales.indexOf(requestedLanguageId) != -1)) { availableLocales = StringUtil.remove(availableLocales, requestedLanguageId, StringPool.COMMA); UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter(); XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance(); xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(unsyncStringWriter); xmlStreamWriter.writeStartDocument(); xmlStreamWriter.writeStartElement(_ROOT); if (localized) { xmlStreamWriter.writeAttribute(_AVAILABLE_LOCALES, availableLocales); xmlStreamWriter.writeAttribute(_DEFAULT_LOCALE, defaultLanguageId); } _copyNonExempt(xmlStreamReader, xmlStreamWriter, requestedLanguageId, defaultLanguageId, cdata); xmlStreamWriter.writeEndElement(); xmlStreamWriter.writeEndDocument(); xmlStreamWriter.close(); xmlStreamWriter = null; xml = unsyncStringWriter.toString(); } } catch (Exception e) { if (_log.isWarnEnabled()) { _log.warn(e, e); } } finally { if (contextClassLoader != portalClassLoader) { currentThread.setContextClassLoader(contextClassLoader); } if (xmlStreamReader != null) { try { xmlStreamReader.close(); } catch (Exception e) { } } if (xmlStreamWriter != null) { try { xmlStreamWriter.close(); } catch (Exception e) { } } } return xml; }
From source file:com.liferay.portal.util.LocalizationImpl.java
public String updateLocalization(String xml, String key, String value, String requestedLanguageId, String defaultLanguageId, boolean cdata, boolean localized) { xml = _sanitizeXML(xml);//from w w w.j av a 2 s. c om XMLStreamReader xmlStreamReader = null; XMLStreamWriter xmlStreamWriter = null; ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader(); Thread currentThread = Thread.currentThread(); ClassLoader contextClassLoader = currentThread.getContextClassLoader(); try { if (contextClassLoader != portalClassLoader) { currentThread.setContextClassLoader(portalClassLoader); } XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); xmlStreamReader = xmlInputFactory.createXMLStreamReader(new UnsyncStringReader(xml)); String availableLocales = StringPool.BLANK; // Read root node if (xmlStreamReader.hasNext()) { xmlStreamReader.nextTag(); availableLocales = xmlStreamReader.getAttributeValue(null, _AVAILABLE_LOCALES); if (Validator.isNull(availableLocales)) { availableLocales = defaultLanguageId; } if (availableLocales.indexOf(requestedLanguageId) == -1) { availableLocales = StringUtil.add(availableLocales, requestedLanguageId, StringPool.COMMA); } } UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter(); XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance(); xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(unsyncStringWriter); xmlStreamWriter.writeStartDocument(); xmlStreamWriter.writeStartElement(_ROOT); if (localized) { xmlStreamWriter.writeAttribute(_AVAILABLE_LOCALES, availableLocales); xmlStreamWriter.writeAttribute(_DEFAULT_LOCALE, defaultLanguageId); } _copyNonExempt(xmlStreamReader, xmlStreamWriter, requestedLanguageId, defaultLanguageId, cdata); xmlStreamWriter.writeStartElement(key); if (localized) { xmlStreamWriter.writeAttribute(_LANGUAGE_ID, requestedLanguageId); } if (cdata) { xmlStreamWriter.writeCData(value); } else { xmlStreamWriter.writeCharacters(value); } xmlStreamWriter.writeEndElement(); xmlStreamWriter.writeEndElement(); xmlStreamWriter.writeEndDocument(); xmlStreamWriter.close(); xmlStreamWriter = null; xml = unsyncStringWriter.toString(); } catch (Exception e) { if (_log.isWarnEnabled()) { _log.warn(e, e); } } finally { if (contextClassLoader != portalClassLoader) { currentThread.setContextClassLoader(contextClassLoader); } if (xmlStreamReader != null) { try { xmlStreamReader.close(); } catch (Exception e) { } } if (xmlStreamWriter != null) { try { xmlStreamWriter.close(); } catch (Exception e) { } } } return xml; }
From source file:org.openvpms.component.business.dao.hibernate.im.query.QueryBuilder.java
private void addActiveConstraint(BaseArchetypeConstraint constraint, QueryContext context) { if (constraint.getState() != State.BOTH) { String alias = constraint.getAlias(); TypeSet set = context.getTypeSet(alias); boolean add = true; if (set != null) { // determine if the object has an active flag try { Thread thread = Thread.currentThread(); ClassLoader loader = thread.getContextClassLoader(); Class type = loader.loadClass(set.getClassName()); if (PeriodRelationshipDO.class.isAssignableFrom(type)) { // no active flag add = false;// ww w .j ava 2s.c o m } } catch (ClassNotFoundException ignore) { // do nothing } } if (add) { boolean active = constraint.getState() == State.ACTIVE; context.addConstraint(alias, "active", RelationalOp.EQ, active); } } }
From source file:com.liferay.portal.util.LocalizationImpl.java
public String getLocalization(String xml, String requestedLanguageId, boolean useDefault) { String value = _getCachedValue(xml, requestedLanguageId, useDefault); if (value != null) { return value; } else {//from ww w. jav a2s. c o m value = StringPool.BLANK; } String systemDefaultLanguageId = LocaleUtil.toLanguageId(LocaleUtil.getDefault()); String priorityLanguageId = null; Locale requestedLocale = LocaleUtil.fromLanguageId(requestedLanguageId); if (useDefault && LanguageUtil.isDuplicateLanguageCode(requestedLocale.getLanguage())) { Locale priorityLocale = LanguageUtil.getLocale(requestedLocale.getLanguage()); if (!requestedLanguageId.equals(priorityLanguageId)) { priorityLanguageId = LocaleUtil.toLanguageId(priorityLocale); } } if (!Validator.isXml(xml)) { if (useDefault || requestedLanguageId.equals(systemDefaultLanguageId)) { value = xml; } _setCachedValue(xml, requestedLanguageId, useDefault, value); return value; } XMLStreamReader xmlStreamReader = null; ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader(); Thread currentThread = Thread.currentThread(); ClassLoader contextClassLoader = currentThread.getContextClassLoader(); try { if (contextClassLoader != portalClassLoader) { currentThread.setContextClassLoader(portalClassLoader); } XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); xmlStreamReader = xmlInputFactory.createXMLStreamReader(new UnsyncStringReader(xml)); String defaultLanguageId = StringPool.BLANK; // Skip root node if (xmlStreamReader.hasNext()) { xmlStreamReader.nextTag(); defaultLanguageId = xmlStreamReader.getAttributeValue(null, _DEFAULT_LOCALE); if (Validator.isNull(defaultLanguageId)) { defaultLanguageId = systemDefaultLanguageId; } } // Find specified language and/or default language String defaultValue = StringPool.BLANK; String priorityValue = StringPool.BLANK; while (xmlStreamReader.hasNext()) { int event = xmlStreamReader.next(); if (event == XMLStreamConstants.START_ELEMENT) { String languageId = xmlStreamReader.getAttributeValue(null, _LANGUAGE_ID); if (Validator.isNull(languageId)) { languageId = defaultLanguageId; } if (languageId.equals(defaultLanguageId) || languageId.equals(priorityLanguageId) || languageId.equals(requestedLanguageId)) { String text = xmlStreamReader.getElementText(); if (languageId.equals(defaultLanguageId)) { defaultValue = text; } if (languageId.equals(priorityLanguageId)) { priorityValue = text; } if (languageId.equals(requestedLanguageId)) { value = text; } if (Validator.isNotNull(value)) { break; } } } else if (event == XMLStreamConstants.END_DOCUMENT) { break; } } if (useDefault && Validator.isNotNull(priorityLanguageId) && Validator.isNull(value) && Validator.isNotNull(priorityValue)) { value = priorityValue; } if (useDefault && Validator.isNull(value)) { value = defaultValue; } } catch (Exception e) { if (_log.isWarnEnabled()) { _log.warn(e, e); } } finally { if (contextClassLoader != portalClassLoader) { currentThread.setContextClassLoader(contextClassLoader); } if (xmlStreamReader != null) { try { xmlStreamReader.close(); } catch (Exception e) { } } } _setCachedValue(xml, requestedLanguageId, useDefault, value); return value; }
From source file:org.apache.hadoop.hbase.master.MasterCoprocessorHost.java
private boolean execOperation(final CoprocessorOperation ctx) throws IOException { if (ctx == null) return false; boolean bypass = false; for (MasterEnvironment env : coprocessors) { if (env.getInstance() instanceof MasterObserver) { ctx.prepare(env);/*from w w w . j a v a 2 s . co m*/ Thread currentThread = Thread.currentThread(); ClassLoader cl = currentThread.getContextClassLoader(); try { currentThread.setContextClassLoader(env.getClassLoader()); ctx.call((MasterObserver) env.getInstance(), ctx); } catch (Throwable e) { handleCoprocessorThrowable(env, e); } finally { currentThread.setContextClassLoader(cl); } bypass |= ctx.shouldBypass(); if (ctx.shouldComplete()) { break; } } ctx.postEnvCall(env); } return bypass; }