Coverage Report - net.admin4j.dao.xml.TaskTimerDAOXml
 
Classes in this File Line Coverage Branch Coverage Complexity
TaskTimerDAOXml
94%
36/38
16%
1/6
4
 
 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.XMLDecoder;
 18  
 import java.beans.XMLEncoder;
 19  
 import java.io.BufferedInputStream;
 20  
 import java.io.BufferedOutputStream;
 21  
 import java.io.FileInputStream;
 22  
 import java.io.FileOutputStream;
 23  
 import java.util.Set;
 24  
 
 25  
 import net.admin4j.config.Admin4JConfiguration;
 26  
 import net.admin4j.dao.TaskTimerDAO;
 27  
 import net.admin4j.timer.BasicTaskTimer;
 28  
 import net.admin4j.timer.SummaryDataMeasure;
 29  
 import net.admin4j.timer.TaskTimer;
 30  
 import net.admin4j.util.Admin4jRuntimeException;
 31  
 
 32  
 /**
 33  
  * DAO implementation for reading/writing performance measurement information.
 34  
  * @author D. Ashmore
 35  
  * @since 1.0
 36  
  */
 37  12
 public class TaskTimerDAOXml extends BaseDAOXml implements TaskTimerDAO {
 38  
     
 39  6
     private static final Object SAVE_LOCK = new Object();
 40  
 
 41  
     @SuppressWarnings("unchecked")
 42  
     public Set<TaskTimer> findAll() {
 43  9
         Set<TaskTimer> result = null;
 44  9
         XMLDecoder decoder = null;
 45  9
         String xmlFileName = Admin4JConfiguration.getPerformanceInformationXmlFileName();
 46  
         
 47  
         /*
 48  
          * Note: classloader assigning business to work around JDK bug
 49  
          * http://bugs.sun.com/view_bug.do?bug_id=6329581
 50  
          */
 51  9
         ClassLoader currentContextLoader = Thread.currentThread().getContextClassLoader();
 52  
         try {
 53  9
             Thread.currentThread().setContextClassLoader(TaskTimer.class.getClassLoader());
 54  9
             decoder = new XMLDecoder(
 55  
                 new BufferedInputStream(
 56  
                     new FileInputStream(xmlFileName)));
 57  6
             decoder.setExceptionListener(new DefaultExceptionListener(xmlFileName));
 58  6
             result = (Set<TaskTimer>)decoder.readObject();
 59  
         }
 60  3
         catch (Throwable t) {
 61  3
             throw new Admin4jRuntimeException("Error reading XML Performance Information.", t)
 62  
                 .addContextValue("xmlFileName", xmlFileName);
 63  
         }
 64  
         finally {
 65  9
             if (decoder != null) decoder.close();
 66  9
             Thread.currentThread().setContextClassLoader(currentContextLoader);
 67  6
         }
 68  
         
 69  6
         return result;
 70  
     }
 71  
 
 72  
     public void saveAll(Set<TaskTimer> exceptionList) {
 73  3
         XMLEncoder encoder = null;
 74  3
         String xmlFileName = Admin4JConfiguration.getPerformanceInformationXmlFileName();
 75  3
         String tempFileName = xmlFileName + ".temp";
 76  3
         String previousFileName = derivePreviousFileName( ".previous" );
 77  
 
 78  3
         boolean encoderClosed = false;
 79  
         
 80  
         /*
 81  
          * Write to a .temp file
 82  
          * Rename configured xml file to .previous
 83  
          * Rename new .temp to the configured file name.
 84  
          * 
 85  
          * Note: classloader assigning business to work around JDK bug
 86  
          * http://bugs.sun.com/view_bug.do?bug_id=6329581
 87  
          */
 88  3
         synchronized (SAVE_LOCK) {
 89  
             
 90  3
             ClassLoader currentContextLoader = Thread.currentThread().getContextClassLoader();
 91  
             try {
 92  
                 
 93  3
                 Thread.currentThread().setContextClassLoader(TaskTimer.class.getClassLoader());
 94  3
                 encoder = new XMLEncoder(
 95  
                         new BufferedOutputStream(
 96  
                             new FileOutputStream(tempFileName)));
 97  3
                 encoder.setExceptionListener(new DefaultExceptionListener(xmlFileName));
 98  3
                 encoder.setPersistenceDelegate(BasicTaskTimer.class, 
 99  
                         new DefaultPersistenceDelegate(
 100  
                                 new String[]{"label","dataMeasures"}));
 101  3
                 encoder.setPersistenceDelegate(SummaryDataMeasure.class, 
 102  
                         new DefaultPersistenceDelegate(
 103  
                                 new String[]{"firstObservationTime"}));
 104  
                
 105  3
                 encoder.writeObject(exceptionList);
 106  3
                 encoder.close();
 107  3
                 encoderClosed = true;
 108  
                 
 109  3
                 versionOutputFile(xmlFileName, tempFileName, previousFileName);
 110  
                 
 111  
             }
 112  0
             catch (Throwable t) {
 113  0
                 if (encoder != null && !encoderClosed) encoder.close();
 114  
             }
 115  
             finally {
 116  3
                 Thread.currentThread().setContextClassLoader(currentContextLoader);
 117  3
             }
 118  3
         }
 119  
 
 120  3
     }
 121  
 
 122  
 }