10000 Use reflection to remove runtime dependency on internal classes · senthgit/aws-sdk-java@78f8301 · GitHub
[go: up one dir, main page]

Skip to content

Commit 78f8301

Browse files
author
Sean Bright
committed
Use reflection to remove runtime dependency on internal classes
1 parent 83b94cf commit 78f8301

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

aws-java-sdk-core/src/main/java/com/amazonaws/util/XpathUtils.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import java.io.ByteArrayInputStream;
1717
import java.io.IOException;
1818
import java.io.InputStream;
19+
import java.lang.reflect.InvocationTargetException;
20+
import java.lang.reflect.Method;
1921
import java.net.URL;
2022
import java.nio.ByteBuffer;
2123
import java.util.Date;
@@ -34,11 +36,6 @@
3436
import org.w3c.dom.NodeList;
3537
import org.xml.sax.SAXException;
3638

37-
import com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl;
38-
import com.sun.org.apache.xml.internal.dtm.DTMManager;
39-
import com.sun.org.apache.xml.internal.dtm.ref.DTMManagerDefault;
40-
import com.sun.org.apache.xpath.internal.XPathContext;
41-
4239
/**
4340
* Utility methods for extracting data from XML documents using Xpath
4441
* expressions.
@@ -50,24 +47,36 @@ public class XpathUtils {
5047
/** The default property name to load the Xalan Document Builder Factory. */
5148
private static final String DOCUMENT_BUILDER_FACTORY_PROP_NAME =
5249
"javax.xml.parsers.DocumentBuilderFactory";
50+
/** The FQCN of the desired DocumentBuilderFactory implementation. */
51+
private static final String DOCUMENT_BUILDER_FACTORY_IMPL_CLASS_NAME =
52+
"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl";
53+
/** The FQCN of the internal XPathContext class. */
54+
private static final String XPATH_CONTEXT_CLASS_NAME =
55+
"com.sun.org.apache.xpath.internal.XPathContext";
56+
/** The FQCN of the desired DTMManager implementation. */
57+
private static final String DTM_MANAGER_IMPL_CLASS_NAME =
58+
"com.sun.org.apache.xml.internal.dtm.ref.DTMManagerDefault";
5359
private static final Log log = LogFactory.getLog(XpathUtils.class);
5460

5561
/**
5662
* Used to optimize performance by avoiding expensive file access every time
5763
* a DTMManager is constructed as a result of constructing a Xalan xpath
5864
* context!
5965
*/
60-
private static void speedUpDTMManager() {
66+
private static void speedUpDTMManager() throws Exception {
6167
// https://github.com/aws/aws-sdk-java/issues/238
6268
// http://stackoverflow.com/questions/6340802/java-xpath-apache-jaxp-implementation-performance
63-
String className = System.getProperty(DTM_MANAGER_DEFAULT_PROP_NAME);
64-
if (className == null) {
65-
DTMManager dtmManager = new XPathContext().getDTMManager();
66-
if (dtmManager instanceof DTMManagerDefault) {
69+
if (System.getProperty(DTM_MANAGER_DEFAULT_PROP_NAME) == null) {
70+
Class<?> XPathContextClass = Class.forName(XPATH_CONTEXT_CLASS_NAME);
71+
Method getDTMManager = XPathContextClass.getMethod("getDTMManager");
72+
Object XPathContext = XPathContextClass.newInstance();
73+
Object dtmManager = getDTMManager.invoke(XPathContext);
74+
75+
if (DTM_MANAGER_IMPL_CLASS_NAME.equals(dtmManager.getClass().getName())) {
6776
// This would avoid C222 the file system to be accessed every time
6877
// the internal XPathContext is instantiated.
6978
System.setProperty(DTM_MANAGER_DEFAULT_PROP_NAME,
70-
DTMManagerDefault.class.getName());
79+
DTM_MANAGER_IMPL_CLASS_NAME);
7180
}
7281
}
7382
}
@@ -78,14 +87,13 @@ private static void speedUpDTMManager() {
7887
* Xalan document factory.
7988
*/
8089
private static void speedUpDcoumentBuilderFactory() {
81-
String className = System.getProperty(DOCUMENT_BUILDER_FACTORY_PROP_NAME);
82-
if (className == null) {
90+
if (System.getProperty(DOCUMENT_BUILDER_FACTORY_PROP_NAME) == null) {
8391
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
84-
if (factory instanceof DocumentBuilderFactoryImpl) {
92+
if (DOCUMENT_BUILDER_FACTORY_IMPL_CLASS_NAME.equals(factory.getClass().getName())) {
8593
// This would avoid the file system to be accessed every time
8694
// the internal DocumentBuilderFactory is instantiated.
8795
System.setProperty(DOCUMENT_BUILDER_FACTORY_PROP_NAME,
88-
DocumentBuilderFactoryImpl.class.getName());
96+
DOCUMENT_BUILDER_FACTORY_IMPL_CLASS_NAME);
8997
}
9098
}
9199
}

0 commit comments

Comments
 (0)
0