Coverage Report - br.com.arsmachina.accesslogger.hibernate.HibernateAccessLogger
 
Classes in this File Line Coverage Branch Coverage Complexity
HibernateAccessLogger
0%
0/21
0%
0/10
4,5
 
 1  
 // Copyright 2008 Thiago H. de Paula Figueiredo
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package br.com.arsmachina.accesslogger.hibernate;
 16  
 
 17  
 import org.hibernate.HibernateException;
 18  
 import org.hibernate.Session;
 19  
 import org.hibernate.SessionFactory;
 20  
 import org.hibernate.Transaction;
 21  
 import org.slf4j.Logger;
 22  
 import org.slf4j.LoggerFactory;
 23  
 
 24  
 import br.com.arsmachina.accesslogger.Access;
 25  
 import br.com.arsmachina.accesslogger.AccessLogger;
 26  
 
 27  
 /**
 28  
  * {@link AccessLogger} implementation using Hibernate to persist {@link Access} instances in a
 29  
  * relationational database.
 30  
  * 
 31  
  * @author Thiago H. de Paula Figueiredo
 32  
  */
 33  
 public class HibernateAccessLogger implements AccessLogger {
 34  
         
 35  0
         private Logger logger = LoggerFactory.getLogger(HibernateAccessLogger.class);
 36  
 
 37  
         private SessionFactory sessionFactory;
 38  
 
 39  
         /**
 40  
          * Single constructor of this class.
 41  
          * 
 42  
          * @param sessionFactory a {@link SessionFactory}. It cannot be null.
 43  
          */
 44  0
         public HibernateAccessLogger(SessionFactory sessionFactory) {
 45  
 
 46  0
                 if (sessionFactory == null) {
 47  0
                         throw new IllegalArgumentException("Parameter sessionFactory cannot be null");
 48  
                 }
 49  
 
 50  0
                 this.sessionFactory = sessionFactory;
 51  
 
 52  0
         }
 53  
 
 54  
         /**
 55  
          * @see br.com.arsmachina.accesslogger.AccessLogger#log(br.com.arsmachina.accesslogger.Access)
 56  
          */
 57  
         public void log(Access access) {
 58  
                 
 59  0
                 final Session session = sessionFactory.openSession();
 60  0
                 Transaction transaction = null;
 61  
 
 62  0
                 if (access instanceof br.com.arsmachina.accesslogger.hibernate.Access == false) {
 63  0
                         access = new br.com.arsmachina.accesslogger.hibernate.Access(access);
 64  
                 }
 65  
                 
 66  
                 try {
 67  0
                         transaction = session.beginTransaction();
 68  0
                         session.save(access);
 69  0
                         transaction.commit();
 70  
                 }
 71  0
                 catch (HibernateException e) {
 72  
                         
 73  0
                         if (logger.isErrorEnabled()) {
 74  0
                                 logger.error("Exception while inserting access", e);
 75  
                         }
 76  
                         
 77  0
                         if (transaction != null) {
 78  0
                                 transaction.rollback();
 79  
                         }
 80  
                         
 81  
                 }
 82  
                 finally {
 83  
                         
 84  0
                         if (session != null) {
 85  0
                                 session.close();
 86  
                         }
 87  
                         
 88  
                 }
 89  
 
 90  0
         }
 91  
 
 92  
 }