use of org.w3c.dom.ls.DOMImplementationLS in project tomcat by apache.
the class JreMemoryLeakPreventionListener method lifecycleEvent.
@Override
public void lifecycleEvent(LifecycleEvent event) {
// Initialise these classes when Tomcat starts
if (Lifecycle.BEFORE_INIT_EVENT.equals(event.getType())) {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
try {
// Use the system classloader as the victim for all this
// ClassLoader pinning we're about to do.
Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
/*
* First call to this loads all drivers in the current class
* loader
*/
if (driverManagerProtection) {
DriverManager.getDrivers();
}
// Note this issue is fixed in Java 8 update 05 onwards.
if (awtThreadProtection && !JreCompat.isJre9Available()) {
java.awt.Toolkit.getDefaultToolkit();
}
/*
* Several components end up calling
* sun.misc.GC.requestLatency(long) which creates a daemon
* thread without setting the TCCL.
*
* Those libraries / components known to trigger memory leaks
* due to eventual calls to requestLatency(long) are:
* - javax.management.remote.rmi.RMIConnectorServer.start()
*
* Note: Long.MAX_VALUE is a special case that causes the thread
* to terminate
*
* Fixed in Java 9 onwards (from early access build 130)
*/
if (gcDaemonProtection && !JreCompat.isJre9Available()) {
try {
Class<?> clazz = Class.forName("sun.misc.GC");
Method method = clazz.getDeclaredMethod("requestLatency", new Class[] { long.class });
method.invoke(null, Long.valueOf(Long.MAX_VALUE - 1));
} catch (ClassNotFoundException e) {
if (JreVendor.IS_ORACLE_JVM) {
log.error(sm.getString("jreLeakListener.gcDaemonFail"), e);
} else {
log.debug(sm.getString("jreLeakListener.gcDaemonFail"), e);
}
} catch (SecurityException | NoSuchMethodException | IllegalArgumentException | IllegalAccessException e) {
log.error(sm.getString("jreLeakListener.gcDaemonFail"), e);
} catch (InvocationTargetException e) {
ExceptionUtils.handleThrowable(e.getCause());
log.error(sm.getString("jreLeakListener.gcDaemonFail"), e);
}
}
/*
* Creating a MessageDigest during web application startup
* initializes the Java Cryptography Architecture. Under certain
* conditions this starts a Token poller thread with TCCL equal
* to the web application class loader.
*
* Instead we initialize JCA right now.
*
* Fixed in Java 9 onwards (from early access build 133)
*/
if (tokenPollerProtection && !JreCompat.isJre9Available()) {
java.security.Security.getProviders();
}
// Set the default URL caching policy to not to cache
if (urlCacheProtection) {
try {
// Doesn't matter that this JAR doesn't exist - just as
// long as the URL is well-formed
URL url = new URL("jar:file://dummy.jar!/");
URLConnection uConn = url.openConnection();
uConn.setDefaultUseCaches(false);
} catch (MalformedURLException e) {
log.error(sm.getString("jreLeakListener.jarUrlConnCacheFail"), e);
} catch (IOException e) {
log.error(sm.getString("jreLeakListener.jarUrlConnCacheFail"), e);
}
}
/*
* Fixed in Java 9 onwards (from early access build 133)
*/
if (xmlParsingProtection && !JreCompat.isJre9Available()) {
// There are two known issues with XML parsing that affect
// Java 8+. The issues both relate to cached Exception
// instances that retain a link to the TCCL via the
// backtrace field. Note that YourKit only shows this field
// when using the HPROF format memory snapshots.
// https://bz.apache.org/bugzilla/show_bug.cgi?id=58486
// https://bugs.openjdk.java.net/browse/JDK-8146961
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
// Issue 1
// com.sun.org.apache.xml.internal.serialize.DOMSerializerImpl
Document document = documentBuilder.newDocument();
document.createElement("dummy");
DOMImplementationLS implementation = (DOMImplementationLS) document.getImplementation();
implementation.createLSSerializer().writeToString(document);
// Issue 1
// com.sun.org.apache.xerces.internal.dom.DOMNormalizer
document.normalize();
} catch (ParserConfigurationException e) {
log.error(sm.getString("jreLeakListener.xmlParseFail"), e);
}
}
/*
* Fixed in Java 9 onwards (from early access build 130)
*/
if (ldapPoolProtection && !JreCompat.isJre9Available()) {
try {
Class.forName("com.sun.jndi.ldap.LdapPoolManager");
} catch (ClassNotFoundException e) {
if (JreVendor.IS_ORACLE_JVM) {
log.error(sm.getString("jreLeakListener.ldapPoolManagerFail"), e);
} else {
log.debug(sm.getString("jreLeakListener.ldapPoolManagerFail"), e);
}
}
}
/*
* Present in Java 8 onwards
*/
if (forkJoinCommonPoolProtection) {
// Don't override any explicitly set property
if (System.getProperty(FORK_JOIN_POOL_THREAD_FACTORY_PROPERTY) == null) {
System.setProperty(FORK_JOIN_POOL_THREAD_FACTORY_PROPERTY, SafeForkJoinWorkerThreadFactory.class.getName());
}
}
if (classesToInitialize != null) {
StringTokenizer strTok = new StringTokenizer(classesToInitialize, ", \r\n\t");
while (strTok.hasMoreTokens()) {
String classNameToLoad = strTok.nextToken();
try {
Class.forName(classNameToLoad);
} catch (ClassNotFoundException e) {
log.error(sm.getString("jreLeakListener.classToInitializeFail", classNameToLoad), e);
// continue with next class to load
}
}
}
} finally {
Thread.currentThread().setContextClassLoader(loader);
}
}
}
use of org.w3c.dom.ls.DOMImplementationLS in project openhab1-addons by openhab.
the class Helper method documentToString.
/***
* Helper method which converts XML Document into pretty formatted string
*
* @param doc to convert
* @return converted XML as String
*/
public static String documentToString(Document doc) {
String strMsg = "";
try {
DOMImplementation domImpl = doc.getImplementation();
DOMImplementationLS domImplLS = (DOMImplementationLS) domImpl.getFeature("LS", "3.0");
LSSerializer lsSerializer = domImplLS.createLSSerializer();
lsSerializer.getDomConfig().setParameter("format-pretty-print", true);
Writer stringWriter = new StringWriter();
LSOutput lsOutput = domImplLS.createLSOutput();
lsOutput.setEncoding("UTF-8");
lsOutput.setCharacterStream(stringWriter);
lsSerializer.write(doc, lsOutput);
strMsg = stringWriter.toString();
} catch (Exception e) {
logger.warn("Error occured when converting document to string", e);
}
return strMsg;
}
use of org.w3c.dom.ls.DOMImplementationLS in project CloudStack-archive by CloudStack-extras.
the class VsmCommand method serialize.
private static String serialize(DOMImplementation domImpl, Document document) {
DOMImplementationLS ls = (DOMImplementationLS) domImpl;
LSSerializer lss = ls.createLSSerializer();
return lss.writeToString(document);
}
use of org.w3c.dom.ls.DOMImplementationLS in project opennms by OpenNMS.
the class JaxbClassObjectAdapter method unmarshal.
@Override
public Object unmarshal(final Object from) throws Exception {
LOG.trace("unmarshal: from = ({}){}", (from == null ? null : from.getClass()), from);
if (from == null)
return null;
if (from instanceof Node) {
final Node e = (Node) from;
e.normalize();
final String nodeName = e.getNodeName();
final Class<?> clazz = getClassForElement(nodeName);
LOG.trace("class type = {} (node name = {})", clazz, nodeName);
if (clazz == null) {
LOG.warn("Unable to determine object type for node name {}. Known elements include: {}", nodeName, m_knownElementClasses);
return from;
}
final DOMImplementationLS lsImpl = (DOMImplementationLS) e.getOwnerDocument().getImplementation().getFeature("LS", "3.0");
LSSerializer serializer = lsImpl.createLSSerializer();
//by default its true, so set it to false to get String without xml-declaration
serializer.getDomConfig().setParameter("xml-declaration", false);
final String str = serializer.writeToString(e);
return JaxbUtils.unmarshal(clazz, str);
} else {
LOG.error("Unsure how to determine which class to use for unmarshaling object type {}", from.getClass());
throw new IllegalArgumentException("Unsure how to determine which class to use for unmarshaling object type " + from.getClass());
}
}
use of org.w3c.dom.ls.DOMImplementationLS in project wildfly by wildfly.
the class UsernameTokenCallbackHandler method toString.
private String toString(Node node) {
String str = null;
if (node != null) {
DOMImplementationLS lsImpl = (DOMImplementationLS) node.getOwnerDocument().getImplementation().getFeature("LS", "3.0");
LSSerializer serializer = lsImpl.createLSSerializer();
//by default its true, so set it to false to get String without xml-declaration
serializer.getDomConfig().setParameter("xml-declaration", false);
str = serializer.writeToString(node);
}
return str;
}
Aggregations