Skip to content

Commit 4320007

Browse files
committed
Merge pull request aws#558 from seanbright/no-static-internals
Make com.sun.* import resolution optional
2 parents 83b94cf + dc26048 commit 4320007

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.io.ByteArrayInputStream;
1717
import java.io.IOException;
1818
import java.io.InputStream;
19+
import java.lang.reflect.Method;
1920
import java.net.URL;
2021
import java.nio.ByteBuffer;
2122
import java.util.Date;
@@ -34,11 +35,6 @@
3435
import org.w3c.dom.NodeList;
3536
import org.xml.sax.SAXException;
3637

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-
4238
/**
4339
* Utility methods for extracting data from XML documents using Xpath
4440
* expressions.
@@ -50,24 +46,36 @@ public class XpathUtils {
5046
/** The default property name to load the Xalan Document Builder Factory. */
5147
private static final String DOCUMENT_BUILDER_FACTORY_PROP_NAME =
5248
"javax.xml.parsers.DocumentBuilderFactory";
49+
/** The FQCN of the desired DocumentBuilderFactory implementation. */
50+
private static final String DOCUMENT_BUILDER_FACTORY_IMPL_CLASS_NAME =
51+
"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl";
52+
/** The FQCN of the internal XPathContext class. */
53+
private static final String XPATH_CONTEXT_CLASS_NAME =
54+
"com.sun.org.apache.xpath.internal.XPathContext";
55+
/** The FQCN of the desired DTMManager implementation. */
56+
private static final String DTM_MANAGER_IMPL_CLASS_NAME =
57+
"com.sun.org.apache.xml.internal.dtm.ref.DTMManagerDefault";
5358
private static final Log log = LogFactory.getLog(XpathUtils.class);
5459

5560
/**
5661
* Used to optimize performance by avoiding expensive file access every time
5762
* a DTMManager is constructed as a result of constructing a Xalan xpath
5863
* context!
5964
*/
60-
private static void speedUpDTMManager() {
65+
private static void speedUpDTMManager() throws Exception {
6166
// https://github.com/aws/aws-sdk-java/issues/238
6267
// 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) {
68+
if (System.getProperty(DTM_MANAGER_DEFAULT_PROP_NAME) == null) {
69+
Class<?> XPathContextClass = Class.forName(XPATH_CONTEXT_CLASS_NAME);
70+
Method getDTMManager = XPathContextClass.getMethod("getDTMManager");
71+
Object XPathContext = XPathContextClass.newInstance();
72+
Object dtmManager = getDTMManager.invoke(XPathContext);
73+
74+
if (DTM_MANAGER_IMPL_CLASS_NAME.equals(dtmManager.getClass().getName())) {
6775
// This would avoid the file system to be accessed every time
6876
// the internal XPathContext is instantiated.
6977
System.setProperty(DTM_MANAGER_DEFAULT_PROP_NAME,
70-
DTMManagerDefault.class.getName());
78+
DTM_MANAGER_IMPL_CLASS_NAME);
7179
}
7280
}
7381
}
@@ -78,14 +86,13 @@ private static void speedUpDTMManager() {
7886
* Xalan document factory.
7987
*/
8088
private static void speedUpDcoumentBuilderFactory() {
81-
String className = System.getProperty(DOCUMENT_BUILDER_FACTORY_PROP_NAME);
82-
if (className == null) {
89+
if (System.getProperty(DOCUMENT_BUILDER_FACTORY_PROP_NAME) == null) {
8390
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
84-
if (factory instanceof DocumentBuilderFactoryImpl) {
91+
if (DOCUMENT_BUILDER_FACTORY_IMPL_CLASS_NAME.equals(factory.getClass().getName())) {
8592
// This would avoid the file system to be accessed every time
8693
// the internal DocumentBuilderFactory is instantiated.
8794
System.setProperty(DOCUMENT_BUILDER_FACTORY_PROP_NAME,
88-
DocumentBuilderFactoryImpl.class.getName());
95+
DOCUMENT_BUILDER_FACTORY_IMPL_CLASS_NAME);
8996
}
9097
}
9198
}

aws-java-sdk-osgi/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@
412412
<configuration>
413413
<instructions>
414414
<Export-Package>com.amazonaws.*</Export-Package>
415-
<Import-Package>!org.junit.*,!org.springframework.*,!org.apache.avalon.*,!org.apache.log.*,!org.aspectj.*,org.apache.http.conn.routing,*</Import-Package>
415+
<Import-Package>!org.junit.*,!org.springframework.*,!org.apache.avalon.*,!org.apache.log.*,!org.aspectj.*,org.apache.http.conn.routing,com.sun.org.apache.xerces.internal.jaxp.*;resolution:=optional,com.sun.org.apache.xml.internal.dtm.*;resolution:=optional,com.sun.org.apache.xml.internal.dtm.ref.*;resolution:=optional,com.sun.org.apache.xpath.internal.*;resolution:=optional,*</Import-Package>
416416
<Embed-Dependency>*;scope=compile;inline=true</Embed-Dependency>
417417
<Embed-Transitive>false</Embed-Transitive>
418418
</instructions>

0 commit comments

Comments
 (0)