Anonymous avatar Anonymous committed 1804e79

Making sure InputStreams are closed

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk/src@15 e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (3)

java/com/opensymphony/xwork/config/providers/XmlConfigurationProvider.java

 
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
     //~ Instance fields ////////////////////////////////////////////////////////
 
     private Configuration configuration;
+    private List includedFileNames = new ArrayList();
     private String configFileName = "xwork.xml";
 
     //~ Constructors ///////////////////////////////////////////////////////////
 
     public void init(Configuration configuration) throws ConfigurationException {
         this.configuration = configuration;
+        includedFileNames.clear();
 
         DocumentBuilder db;
 
     * @return true if the file has been changed since the last time we read it
     */
     public boolean needsReload() {
-        return FileManager.fileNeedsReloading(configFileName);
+        boolean needsReload = FileManager.fileNeedsReloading(configFileName);
+        Iterator fileNameIterator = includedFileNames.iterator();
+
+        while (!needsReload && (fileNameIterator.hasNext())) {
+            String fileName = (String) fileNameIterator.next();
+            needsReload = FileManager.fileNeedsReloading(fileName);
+        }
+
+        return needsReload;
     }
 
     protected InputStream getInputStream(String fileName) {
         for (int i = 0; i < includeList.getLength(); i++) {
             Element includeElement = (Element) includeList.item(i);
             String fileName = includeElement.getAttribute("file");
+            includedFileNames.add(fileName);
             loadConfigurationFile(fileName, db);
         }
     }
 
     private void loadConfigurationFile(String fileName, DocumentBuilder db) throws Exception {
         Document doc = null;
+        InputStream is = null;
 
         try {
-            InputStream is = getInputStream(fileName);
+            is = getInputStream(fileName);
 
             if (is == null) {
                 throw new Exception("Could not open file " + fileName);
 
             doc = db.parse(is);
         } catch (Exception e) {
-            LOG.error("Caught exception while loading file " + fileName);
+            LOG.error("Caught exception while loading file " + fileName, e);
+        } finally {
+            if (is != null) {
+                try {
+                    is.close();
+                } catch (IOException e) {
+                    LOG.error("Unable to close input stream", e);
+                }
+            }
         }
 
         Element rootElement = doc.getDocumentElement();

java/com/opensymphony/xwork/config/providers/dsx/DelegatingSaxXmlProvider.java

         SAXParser saxParser;
 
         DefaultDelegatingHandler handler;
+        InputStream is = null;
 
         try {
             saxParser = SAXParserFactory.newInstance().newSAXParser();
                     }
                 });
 
-            InputStream is = getInputStream(configFileName);
+            is = getInputStream(configFileName);
 
             if (is == null) {
                 throw new Exception("Could not open file " + configFileName);
             LOG.fatal("Could not load XWork configuration file, failing", e);
 
             return;
+        } finally {
+            if (is != null) {
+                try {
+                    is.close();
+                } catch (IOException e) {
+                    LOG.error("Unable to close input stream", e);
+                }
+            }
         }
 
         if ((handler != null) && (handler.getException() != null) && (handler.getException().hasErrors())) {

java/com/opensymphony/xwork/validator/ActionValidatorManager.java

 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import java.io.IOException;
 import java.io.InputStream;
 
 import java.util.ArrayList;
     }
 
     private static List loadFile(String fileName, Class clazz, boolean checkFile) {
-        List retList;
+        List retList = Collections.EMPTY_LIST;
 
         if ((checkFile && FileManager.fileNeedsReloading(fileName)) || !validatorFileCache.containsKey(fileName)) {
-            InputStream is = FileManager.loadFile(fileName, clazz);
-
-            if (is != null) {
-                retList = new ArrayList(ValidatorFileParser.parseActionValidators(is));
-            } else {
-                retList = Collections.EMPTY_LIST;
+            InputStream is = null;
+
+            try {
+                is = FileManager.loadFile(fileName, clazz);
+
+                if (is != null) {
+                    retList = new ArrayList(ValidatorFileParser.parseActionValidators(is));
+                }
+            } catch (Exception e) {
+                LOG.error("Caught exception while loading file " + fileName, e);
+            } finally {
+                if (is != null) {
+                    try {
+                        is.close();
+                    } catch (IOException e) {
+                        LOG.error("Unable to close input stream", e);
+                    }
+                }
             }
 
             validatorFileCache.put(fileName, retList);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.