Improved: [FB] Package org.apache.ofbiz.base.config

(OFBIZ-9566)

MainResourceHandler
 The protected fields were set to private since the class is not extended 
 anywhere. If they need to be accessed in the future, getter that return an 
 unmodifiable version should be implemented (Collections.unmodifiableXXX).

Refactor:
 Uses multi exceptions catch (explicitly to not catch, hence somehow swallow, 
 RuntimeException)
 Uses return instead of else when appropriate
 Extracts a getLoader() handy method
 Removes an useless thrown exception in invalidateDocument()
 Removes an useless Document cast
 Removes an useless ResourceLoader null initialisation 

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1804639 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jacques Le Roux
2017-08-10 07:52:16 +00:00
parent 98e89f33ff
commit 6d67c194a0
2 changed files with 48 additions and 56 deletions
@@ -35,9 +35,9 @@ import org.w3c.dom.Element;
public final class MainResourceHandler implements ResourceHandler {
public static final String module = MainResourceHandler.class.getName();
protected final String xmlFilename;
protected final String loaderName;
protected final String location;
private final String xmlFilename;
private final String loaderName;
private final String location;
public MainResourceHandler(String xmlFilename, Element element) {
this.xmlFilename = xmlFilename;
@@ -63,11 +63,7 @@ public final class MainResourceHandler implements ResourceHandler {
public Document getDocument() throws GenericConfigException {
try {
return UtilXml.readXmlDocument(this.getStream(), this.xmlFilename, true);
} catch (org.xml.sax.SAXException e) {
throw new GenericConfigException("Error reading " + this.toString(), e);
} catch (javax.xml.parsers.ParserConfigurationException e) {
throw new GenericConfigException("Error reading " + this.toString(), e);
} catch (java.io.IOException e) {
} catch (org.xml.sax.SAXException | javax.xml.parsers.ParserConfigurationException | java.io.IOException e) {
throw new GenericConfigException("Error reading " + this.toString(), e);
}
}
@@ -83,11 +79,7 @@ public final class MainResourceHandler implements ResourceHandler {
public boolean isFileResource() throws GenericConfigException {
ResourceLoader loader = ResourceLoader.getLoader(this.xmlFilename, this.loaderName);
if (loader instanceof FileLoader) {
return true;
} else {
return false;
}
return loader instanceof FileLoader;
}
public String getFullLocation() throws GenericConfigException {
@@ -98,16 +90,14 @@ public final class MainResourceHandler implements ResourceHandler {
@Override
public boolean equals(Object obj) {
if (obj instanceof MainResourceHandler) {
MainResourceHandler other = (MainResourceHandler) obj;
if (this.loaderName.equals(other.loaderName) &&
this.xmlFilename.equals(other.xmlFilename) &&
this.location.equals(other.location)) {
return true;
}
if (!(obj instanceof MainResourceHandler)) {
return false;
}
return false;
MainResourceHandler other = (MainResourceHandler) obj;
return this.loaderName.equals(other.loaderName) &&
this.xmlFilename.equals(other.xmlFilename) &&
this.location.equals(other.location);
}
@Override
@@ -58,28 +58,35 @@ public abstract class ResourceLoader {
String cacheKey = xmlFilename.concat("#").concat(loaderName);
ResourceLoader loader = loaderCache.get(cacheKey);
if (loader == null) {
Element rootElement = null;
URL xmlUrl = UtilURL.fromResource(xmlFilename);
if (xmlUrl == null) {
throw new GenericConfigException("Could not find the " + xmlFilename + " file");
}
try {
rootElement = UtilXml.readXmlDocument(xmlUrl, true, true).getDocumentElement();
} catch (Exception e) {
throw new GenericConfigException("Exception thrown while reading " + xmlFilename + ": ", e);
}
Element loaderElement = UtilXml.firstChildElement(rootElement, "resource-loader", "name", loaderName);
if (loaderElement == null) {
throw new GenericConfigException("The " + xmlFilename + " file is missing the <resource-loader> element with the name " + loaderName);
}
if (loaderElement.getAttribute("class").isEmpty()) {
throw new GenericConfigException("The " + xmlFilename + " file <resource-loader> element with the name " + loaderName + " is missing the class attribute");
}
loader = loaderCache.putIfAbsentAndGet(cacheKey, makeLoader(loaderElement));
loader = getLoader(xmlFilename, loaderName, cacheKey);
}
return loader;
}
private static ResourceLoader getLoader(String xmlFilename, String loaderName, String cacheKey)
throws GenericConfigException {
ResourceLoader loader;
Element rootElement;
URL xmlUrl = UtilURL.fromResource(xmlFilename);
if (xmlUrl == null) {
throw new GenericConfigException("Could not find the " + xmlFilename + " file");
}
try {
rootElement = UtilXml.readXmlDocument(xmlUrl, true, true).getDocumentElement();
} catch (Exception e) {
throw new GenericConfigException("Exception thrown while reading " + xmlFilename + ": ", e);
}
Element loaderElement = UtilXml.firstChildElement(rootElement, "resource-loader", "name", loaderName);
if (loaderElement == null) {
throw new GenericConfigException("The " + xmlFilename + " file is missing the <resource-loader> element with the name " + loaderName);
}
if (loaderElement.getAttribute("class").isEmpty()) {
throw new GenericConfigException("The " + xmlFilename + " file <resource-loader> element with the name " + loaderName + " is missing the class attribute");
}
loader = loaderCache.putIfAbsentAndGet(cacheKey, makeLoader(loaderElement));
return loader;
}
/** This method should be avoided. DOM object trees take a lot of memory and they are not
* thread-safe, so they should not be cached.
* @deprecated use {@link #readXmlRootElement(String)}
@@ -88,24 +95,22 @@ public abstract class ResourceLoader {
public static Element getXmlRootElement(String xmlFilename) throws GenericConfigException {
Document document = ResourceLoader.getXmlDocument(xmlFilename);
if (document != null) {
return document.getDocumentElement();
} else {
if (document == null) {
return null;
}
return document.getDocumentElement();
}
public static Element readXmlRootElement(String xmlFilename) throws GenericConfigException {
Document document = ResourceLoader.readXmlDocument(xmlFilename);
if (document != null) {
return document.getDocumentElement();
} else {
if (document == null) {
return null;
}
return document.getDocumentElement();
}
public static void invalidateDocument(String xmlFilename) throws GenericConfigException {
public static void invalidateDocument(String xmlFilename) {
UtilCache.clearCachesThatStartWith(xmlFilename);
}
@@ -121,7 +126,7 @@ public abstract class ResourceLoader {
document = readXmlDocument(xmlFilename);
if (document != null) {
document = (Document) domCache.putIfAbsentAndGet(xmlFilename, document);
document = domCache.putIfAbsentAndGet(xmlFilename, document);
}
}
return document;
@@ -136,11 +141,7 @@ public abstract class ResourceLoader {
try {
return UtilXml.readXmlDocument(confUrl, true, true);
} catch (org.xml.sax.SAXException e) {
throw new GenericConfigException("Error reading " + xmlFilename + "", e);
} catch (javax.xml.parsers.ParserConfigurationException e) {
throw new GenericConfigException("Error reading " + xmlFilename + "", e);
} catch (java.io.IOException e) {
} catch (org.xml.sax.SAXException | javax.xml.parsers.ParserConfigurationException | java.io.IOException e) {
throw new GenericConfigException("Error reading " + xmlFilename + "", e);
}
}
@@ -148,7 +149,7 @@ public abstract class ResourceLoader {
private static ResourceLoader makeLoader(Element loaderElement) throws GenericConfigException {
String loaderName = loaderElement.getAttribute("name");
String className = loaderElement.getAttribute("class");
ResourceLoader loader = null;
ResourceLoader loader;
try {
Class<?> lClass = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
@@ -156,8 +157,9 @@ public abstract class ResourceLoader {
loader = (ResourceLoader) lClass.newInstance();
loader.init(loaderName, loaderElement.getAttribute("prefix"), loaderElement.getAttribute("prepend-env"));
return loader;
} catch (Exception e) {
throw new GenericConfigException("Exception thrown while loading ResourceLoader class \"" + className + "\" ", e);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
throw new GenericConfigException("Exception thrown while loading ResourceLoader class \"" + className
+ "\" ", e);
}
}