Author: alessio.soldano(a)jboss.com
Date: 2011-09-05 09:21:31 -0400 (Mon, 05 Sep 2011)
New Revision: 14935
Added:
common/branches/jbossws-common-1.0.0.GA_CP/src/main/java/org/jboss/wsf/common/SecurityActions.java
Modified:
common/branches/jbossws-common-1.0.0.GA_CP/src/main/java/org/jboss/wsf/common/DOMUtils.java
Log:
[JBPAPP-7126] Caching default document builder factory to improve performances
Modified:
common/branches/jbossws-common-1.0.0.GA_CP/src/main/java/org/jboss/wsf/common/DOMUtils.java
===================================================================
---
common/branches/jbossws-common-1.0.0.GA_CP/src/main/java/org/jboss/wsf/common/DOMUtils.java 2011-09-05
12:08:31 UTC (rev 14934)
+++
common/branches/jbossws-common-1.0.0.GA_CP/src/main/java/org/jboss/wsf/common/DOMUtils.java 2011-09-05
13:21:31 UTC (rev 14935)
@@ -76,7 +76,30 @@
private static final String DEFER_NODE_EXPANSION_FEATURE =
"http://apache.org/xml/features/dom/defer-node-expansion";
private static final String ENABLE_DOCTYPE_DECL =
"org.jboss.ws.enable_doctype_decl";
private static final String DISALLOW_DOCTYPE_DECL_FEATURE =
"http://apache.org/xml/features/disallow-doctype-decl";
+
+ private static DocumentBuilderFactory documentBuilderFactory;
+
+ private static final boolean disableDeferedNodeExpansion =
Boolean.getBoolean(DISABLE_DEFERRED_NODE_EXPANSION);
+ private static final boolean enableDoctypeDeclaration =
Boolean.getBoolean(ENABLE_DOCTYPE_DECL);
+
+ static
+ {
+ //load default document builder factory using the DOMUtils' defining
classloader
+ final ClassLoader classLoader = SecurityActions.getContextClassLoader();
+ SecurityActions.setContextClassLoader(DOMUtils.class.getClassLoader());
+ try
+ {
+ final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ initializeFactory(factory);
+ documentBuilderFactory = factory;
+ }
+ finally
+ {
+ SecurityActions.setContextClassLoader(classLoader);
+ }
+ }
+
// All elements created by the same thread are created by the same builder and belong
to the same doc
private static ThreadLocal<Document> documentThreadLocal = new
ThreadLocal<Document>();
private static ThreadLocal<DocumentBuilder> builderThreadLocal = new
ThreadLocal<DocumentBuilder>() {
@@ -85,29 +108,21 @@
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- factory.setValidating(false);
- factory.setNamespaceAware(true);
- factory.setExpandEntityReferences(false);
- try
+ //check if the factory we'd get for this thread is equivalent to the
default one;
+ //in that case re-use the default one and skip the initialization, which is
time-consuming
+ final DocumentBuilderFactory threadFactory ;
+ if (factory.getClass().getClassLoader() ==
documentBuilderFactory.getClass().getClassLoader())
{
- factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
- if (Boolean.getBoolean(DISABLE_DEFERRED_NODE_EXPANSION))
- {
- factory.setFeature(DEFER_NODE_EXPANSION_FEATURE, false);
- }
-
- if (!Boolean.getBoolean(ENABLE_DOCTYPE_DECL))
- {
- factory.setFeature(DISALLOW_DOCTYPE_DECL_FEATURE, true);
- }
+ threadFactory = documentBuilderFactory ;
}
- catch (ParserConfigurationException pce)
+ else
{
- log.error(pce);
+ threadFactory = factory ;
+ initializeFactory(threadFactory) ;
}
- DocumentBuilder builder = factory.newDocumentBuilder();
+ DocumentBuilder builder = threadFactory.newDocumentBuilder();
setEntityResolver(builder);
return builder;
}
@@ -122,7 +137,7 @@
String[] resolvers = new String[] {
"org.jboss.ws.core.utils.JBossWSEntityResolver",
"org.jboss.util.xml.JBossEntityResolver" };
EntityResolver entityResolver = null;
- ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ ClassLoader loader = SecurityActions.getContextClassLoader();
for (String resolver : resolvers)
{
try
@@ -140,7 +155,31 @@
builder.setEntityResolver(entityResolver);
}
};
+
+ private static void initializeFactory(final DocumentBuilderFactory factory)
+ {
+ factory.setValidating(false);
+ factory.setNamespaceAware(true);
+ factory.setExpandEntityReferences(false);
+ try
+ {
+ factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
+ if (disableDeferedNodeExpansion)
+ {
+ factory.setFeature(DEFER_NODE_EXPANSION_FEATURE, false);
+ }
+ if (!enableDoctypeDeclaration)
+ {
+ factory.setFeature(DISALLOW_DOCTYPE_DECL_FEATURE, true);
+ }
+ }
+ catch (ParserConfigurationException pce)
+ {
+ log.error(pce);
+ }
+ }
+
public static void clearThreadLocals()
{
documentThreadLocal.remove();
Added:
common/branches/jbossws-common-1.0.0.GA_CP/src/main/java/org/jboss/wsf/common/SecurityActions.java
===================================================================
---
common/branches/jbossws-common-1.0.0.GA_CP/src/main/java/org/jboss/wsf/common/SecurityActions.java
(rev 0)
+++
common/branches/jbossws-common-1.0.0.GA_CP/src/main/java/org/jboss/wsf/common/SecurityActions.java 2011-09-05
13:21:31 UTC (rev 14935)
@@ -0,0 +1,135 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.wsf.common;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
+
+/**
+ * Security actions for this package
+ *
+ * @author alessio.soldano(a)jboss.com
+ * @since 19-Jun-2009
+ *
+ */
+class SecurityActions
+{
+ /**
+ * Get context classloader.
+ *
+ * @return the current context classloader
+ */
+ static ClassLoader getContextClassLoader()
+ {
+ SecurityManager sm = System.getSecurityManager();
+ if (sm == null)
+ {
+ return Thread.currentThread().getContextClassLoader();
+ }
+ else
+ {
+ return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>()
{
+ public ClassLoader run()
+ {
+ return Thread.currentThread().getContextClassLoader();
+ }
+ });
+ }
+ }
+
+ /**
+ * Set context classloader.
+ *
+ * @param classLoader the context classloader
+ */
+ static void setContextClassLoader(final ClassLoader classLoader)
+ {
+ SecurityManager sm = System.getSecurityManager();
+ if (sm == null)
+ {
+ Thread.currentThread().setContextClassLoader(classLoader);
+ }
+ else
+ {
+ AccessController.doPrivileged(new PrivilegedAction<Object>() {
+ public Object run()
+ {
+ Thread.currentThread().setContextClassLoader(classLoader);
+ return null;
+ }
+ });
+ }
+ }
+
+ /**
+ * Load a class using the provided classloader
+ *
+ * @param name
+ * @return
+ * @throws PrivilegedActionException
+ */
+ static Class<?> loadClass(final ClassLoader cl, final String name) throws
PrivilegedActionException, ClassNotFoundException
+ {
+ SecurityManager sm = System.getSecurityManager();
+ if (sm == null)
+ {
+ return cl.loadClass(name);
+ }
+ else
+ {
+ return AccessController.doPrivileged(new
PrivilegedExceptionAction<Class<?>>() {
+ public Class<?> run() throws PrivilegedActionException
+ {
+ try
+ {
+ return cl.loadClass(name);
+ }
+ catch (Exception e)
+ {
+ throw new PrivilegedActionException(e);
+ }
+ }
+ });
+ }
+ }
+
+ /**
+ * Return the current value of the specified system property
+ *
+ * @param name
+ * @param defaultValue
+ * @return
+ */
+ static String getSystemProperty(final String name, final String defaultValue)
+ {
+ PrivilegedAction<String> action = new PrivilegedAction<String>()
+ {
+ public String run()
+ {
+ return System.getProperty(name, defaultValue);
+ }
+ };
+ return AccessController.doPrivileged(action);
+ }
+}
\ No newline at end of file