Coverage Report - net.admin4j.dao.xml.ExceptionInfoDAOXml
 
Classes in this File Line Coverage Branch Coverage Complexity
ExceptionInfoDAOXml
92%
24/26
0%
0/4
2.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.DefaultPersistenceDelegate;
 17  
 import java.beans.XMLEncoder;
 18  
 import java.io.BufferedOutputStream;
 19  
 import java.io.FileOutputStream;
 20  
 import java.util.Set;
 21  
 
 22  
 import net.admin4j.config.Admin4JConfiguration;
 23  
 import net.admin4j.dao.ExceptionInfoDAO;
 24  
 import net.admin4j.entity.ExceptionInfo;
 25  
 
 26  
 /**
 27  
  * DAO implementation for reading and writing Exception information.
 28  
  * @author D. Ashmore
 29  
  * @since 1.0
 30  
  */
 31  12
 public class ExceptionInfoDAOXml extends BaseDAOXml implements ExceptionInfoDAO {
 32  
     
 33  6
     private static final Object SAVE_LOCK = new Object();
 34  
 
 35  
     @SuppressWarnings("unchecked")
 36  
     public Set<ExceptionInfo> findAll() {
 37  9
         return (Set<ExceptionInfo>) readXmlFile(Admin4JConfiguration.getExceptionInformationXmlFileName());
 38  
     }
 39  
 
 40  
     public void saveAll(Set<ExceptionInfo> exceptionList) {
 41  3
         XMLEncoder encoder = null;
 42  3
         String xmlFileName = Admin4JConfiguration.getExceptionInformationXmlFileName();
 43  3
         String tempFileName = xmlFileName + ".temp";
 44  3
         String previousFileName = derivePreviousFileName(xmlFileName);
 45  
 
 46  3
         boolean encoderClosed = false;
 47  
         
 48  3
         synchronized (SAVE_LOCK) {
 49  
             /*
 50  
              * Write to a .temp file
 51  
              * Rename configured xml file to .previous
 52  
              * Rename new .temp to the configured file name.
 53  
              * 
 54  
              * Note: classloader assigning business to work around JDK bug
 55  
              * http://bugs.sun.com/view_bug.do?bug_id=6329581
 56  
              */
 57  3
             ClassLoader currentContextLoader = Thread.currentThread().getContextClassLoader();
 58  
             try {
 59  3
                 Thread.currentThread().setContextClassLoader(ExceptionInfo.class.getClassLoader());
 60  3
                 encoder = new XMLEncoder(
 61  
                         new BufferedOutputStream(
 62  
                             new FileOutputStream(tempFileName)));
 63  3
                 encoder.setExceptionListener(new DefaultExceptionListener(xmlFileName));
 64  3
                 encoder.setPersistenceDelegate(ExceptionInfo.class, 
 65  
                         new DefaultPersistenceDelegate(
 66  
                                 new String[]{"exceptionClassName","stackTrace", "alternateId"}));
 67  3
                 encoder.setPersistenceDelegate(ExceptionInfo.TimeStamp.class, 
 68  
                         new DefaultPersistenceDelegate(
 69  
                                 new String[]{"timestampInMillis"}));
 70  3
                 encoder.setPersistenceDelegate(StackTraceElement.class, 
 71  
                         new DefaultPersistenceDelegate(
 72  
                                 new String[]{"className","methodName",
 73  
                                         "fileName","lineNumber"}));
 74  3
                 encoder.writeObject(exceptionList);
 75  3
                 encoder.close();
 76  3
                 encoderClosed = true;
 77  
                 
 78  3
                 versionOutputFile(xmlFileName, tempFileName, previousFileName);
 79  
                 
 80  
             }
 81  0
             catch (Throwable t) {
 82  0
                 if (encoder != null && !encoderClosed) encoder.close();
 83  
             }
 84  
             finally {
 85  3
                 Thread.currentThread().setContextClassLoader(currentContextLoader);
 86  3
             }
 87  3
         }
 88  
 
 89  3
     }
 90  
 
 91  
 }