Yesterday I was writing code for parsing the the ejb descriptor file contained in a ejb jar file.
The code was something like this:
private void parseAndPopulateResourceReferences(File ejbJarFile) {
try {
JarFile jarFile = new JarFile(ejbJarFile);
JarEntry jarEntry = jarFile.getJarEntry("META-INF/ejb-jar.xml");
if (jarEntry != null) {
InputStream is = jarFile.getInputStream(jarEntry);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(is);
//do something with the doc
populateEntriesFromDoc(doc);
}
} catch (Exception e) {
throw new RuntimeException("Exception found while trying to parse ejb descriptor file for ejb named " + ejbJarFile.getName(), e);
}
}
The code worked fine in my office, but when I tried it from home in the evening, I started to get exception from the same code. The exception I am talking about is this one:
Caused by: java.net.UnknownHostException: java.sun.com
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
at sun.net.www.http.HttpClient.New(HttpClient.java:306)
at sun.net.www.http.HttpClient.New(HttpClient.java:323)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:860)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:726)
So it was clear that it was looking for something on internet.. and failing because there was no internet connection available.
I realized that this exception was thrown because the XML which was being parsed contained this at the top:
<!DOCTYPE ejb-jar PUBLIC “-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN” “http://java.sun.com/dtd/ejb-jar_2_0.dtd”>
Now there are were 2 solutions which I could think of: Either download the DTD files from internet and let the XML refer to the local copy of DTD, something like this:
<!DOCTYPE root-element SYSTEM “ejb-jar_2_0.dtd”>
But this option was not viable for me, as I didn’t know what XML I would be parsing at run time, so I opted for the second option of disabling the DTD checking. This can be done by setting a custom entity resolver on the DocumentBuilder object.
private static void disableDtdValidation(DocumentBuilder db) {
// disable the dtd validation by setting a custom entity resolver
db.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
System.out.println("Ignoring " + publicId + ", " + systemId);
return new InputSource(new StringReader(""));
}
});
}
Thanks boss it worked for us.
By: neha on February 1, 2011
at 8:18 PM