Coverage Report - net.admin4j.dao.xml.BaseDAOXml
 
Classes in this File Line Coverage Branch Coverage Complexity
BaseDAOXml
52%
25/48
35%
5/14
5
 
 1  
 /*
 2  
  * This software is licensed under the Apache License, Version 2.0
 3  
  * (the "License") agreement; you may not use this file except in compliance with
 4  
  * the License.  You may obtain a copy of the License at
 5  
  * 
 6  
  *      http://www.apache.org/licenses/LICENSE-2.0
 7  
  * 
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS,
 10  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 11  
  * See the License for the specific language governing permissions and
 12  
  * limitations under the License.
 13  
  */
 14  
 package net.admin4j.dao.xml;
 15  
 
 16  
 import java.beans.XMLDecoder;
 17  
 import java.io.BufferedInputStream;
 18  
 import java.io.File;
 19  
 import java.io.FileInputStream;
 20  
 
 21  
 import net.admin4j.util.Admin4jRuntimeException;
 22  
 
 23  
 import org.slf4j.Logger;
 24  
 import org.slf4j.LoggerFactory;
 25  
 
 26  24
 public abstract class BaseDAOXml {
 27  
     
 28  24
     private Logger logger = LoggerFactory.getLogger(this.getClass());
 29  
     
 30  
     protected void versionOutputFile(String xmlFileName, String tempFileName,
 31  
             String previousFileName) {
 32  
         File file;
 33  
         // Rename configured Xml file to .previous
 34  6
         file = new File(previousFileName);
 35  6
         if (file.exists()) {
 36  0
             file.delete();
 37  
         }
 38  6
         file = new File(xmlFileName);
 39  6
         if (file.exists()) {
 40  0
             file.renameTo(new File(previousFileName));
 41  
         }
 42  
         
 43  
         // Rename .temp file to configured Xml File Name
 44  6
         file = new File(tempFileName);
 45  6
         file.renameTo(new File(xmlFileName));
 46  6
     }
 47  
     
 48  
     protected String derivePreviousFileName(String xmlFileName) {
 49  6
         return xmlFileName + ".previous";
 50  
     }
 51  
     
 52  
     protected Object readXmlFile(String xmlFileName) {
 53  9
         Object result = null;
 54  9
         XMLDecoder decoder = null;
 55  9
         boolean exceptionThrown = false;
 56  
         
 57  
         /*
 58  
          * Note: classloader assigning business to work around JDK bug
 59  
          * http://bugs.sun.com/view_bug.do?bug_id=6329581
 60  
          */
 61  9
         ClassLoader currentContextLoader = Thread.currentThread().getContextClassLoader();
 62  
         try {
 63  9
             Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
 64  9
             DefaultExceptionListener errorListener = new DefaultExceptionListener(xmlFileName);
 65  9
             decoder = new XMLDecoder(
 66  
                 new BufferedInputStream(
 67  
                     new FileInputStream(xmlFileName)));
 68  9
             decoder.setExceptionListener(errorListener);
 69  9
             try {result = decoder.readObject();}
 70  0
             catch (Exception e) {
 71  0
                 logger.error("Error reading file " + xmlFileName, e);
 72  0
                 exceptionThrown = true;
 73  9
             }
 74  
             
 75  9
             if (errorListener.isError() || exceptionThrown) {
 76  0
                 exceptionThrown = false;
 77  0
                 String previousFileName = derivePreviousFileName(xmlFileName);
 78  0
                 errorListener = new DefaultExceptionListener(previousFileName);
 79  0
                 decoder = new XMLDecoder(
 80  
                         new BufferedInputStream(
 81  
                             new FileInputStream(previousFileName)));
 82  0
                 decoder.setExceptionListener(errorListener);
 83  0
                 try {result = decoder.readObject();}
 84  0
                 catch (Exception e) {
 85  0
                     logger.error("Error reading file " + previousFileName, e);
 86  0
                     exceptionThrown = true;
 87  0
                 }
 88  
                 
 89  0
                 if (errorListener.isError() || exceptionThrown) {
 90  0
                     Admin4jRuntimeException ae = new Admin4jRuntimeException("Error reading xml Input after two attempts");
 91  0
                     ae.addContextValue("xmlFileName", xmlFileName);
 92  0
                     ae.addContextValue("previousFileName", previousFileName);
 93  
                     
 94  0
                     throw ae;
 95  
                 }
 96  
                 else {
 97  0
                     logger.warn("Error reading xmlFile {}.  Back up copy used.", xmlFileName);
 98  
                 }
 99  
             }
 100  
         }
 101  0
         catch (Throwable t) {
 102  0
             throw new Admin4jRuntimeException("Error reading XML Exception Information.", t)
 103  
                 .addContextValue("xmlFileName", xmlFileName);
 104  
         }
 105  
         finally {
 106  9
             if (decoder != null) decoder.close();
 107  9
             Thread.currentThread().setContextClassLoader(currentContextLoader);
 108  9
         }
 109  
         
 110  9
         return result;
 111  
     }
 112  
 
 113  
 }