Author: jleroux
Date: Thu Aug 10 07:52:16 2017
New Revision: 1804639

URL: http://svn.apache.org/viewvc?rev=1804639&view=rev
Log:
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 

Modified:
    
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/config/MainResourceHandler.java
    
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/config/ResourceLoader.java

Modified: 
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/config/MainResourceHandler.java
URL: 
http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/config/MainResourceHandler.java?rev=1804639&r1=1804638&r2=1804639&view=diff
==============================================================================
--- 
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/config/MainResourceHandler.java
 (original)
+++ 
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/config/MainResourceHandler.java
 Thu Aug 10 07:52:16 2017
@@ -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 i
     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 i
     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 i
 
     @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

Modified: 
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/config/ResourceLoader.java
URL: 
http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/config/ResourceLoader.java?rev=1804639&r1=1804638&r2=1804639&view=diff
==============================================================================
--- 
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/config/ResourceLoader.java
 (original)
+++ 
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/config/ResourceLoader.java
 Thu Aug 10 07:52:16 2017
@@ -58,25 +58,32 @@ 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;
     }
 
@@ -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);
         }
     }
 


Reply via email to