Coverage Report - org.kuali.rice.core.framework.persistence.jpa.DevHibernatePersistence
 
Classes in this File Line Coverage Branch Coverage Complexity
DevHibernatePersistence
0%
0/30
0%
0/10
2.75
 
 1  
 /*
 2  
  * Copyright 2007-2008 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.core.framework.persistence.jpa;
 17  
 
 18  
 import java.io.File;
 19  
 import java.io.FileInputStream;
 20  
 import java.io.FileOutputStream;
 21  
 import java.io.IOException;
 22  
 import java.io.ObjectInputStream;
 23  
 import java.io.ObjectOutputStream;
 24  
 import java.util.Map;
 25  
 
 26  
 import javax.persistence.EntityManagerFactory;
 27  
 import javax.persistence.spi.PersistenceUnitInfo;
 28  
 
 29  
 import org.apache.log4j.Logger;
 30  
 import org.hibernate.ejb.Ejb3Configuration;
 31  
 import org.hibernate.ejb.HibernatePersistence;
 32  
 
 33  
 /*
 34  
  * This is a temporary hack until we find a better way to override
 35  
  * createContainerEntityManagerFactory(...) below so that we can serialize the
 36  
  * configuration and cache the factory.
 37  
  */
 38  0
 public class DevHibernatePersistence extends HibernatePersistence {
 39  
 
 40  0
         private static final Logger LOG = Logger.getLogger(DevHibernatePersistence.class);
 41  
         
 42  
         private String serializationFilename;
 43  
 
 44  
         private boolean useSerialization;
 45  
 
 46  
         // Caching the factory does increase speed, but then it uses the first set of jpa properties specified rather than jpa properties per factory
 47  
         // private static EntityManagerFactory factory = null;
 48  
         public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map) {
 49  
                 //if (factory != null) {
 50  
                 //        return factory;
 51  
                 //}
 52  0
                 EntityManagerFactory factory = null;                
 53  0
                 Ejb3Configuration cfg = null;
 54  0
                 ObjectInputStream in = null;
 55  
                 try {
 56  0
                         if (useSerialization && new File(serializationFilename).exists()) {
 57  0
                                 in = new ObjectInputStream(new FileInputStream(serializationFilename));
 58  0
                                 cfg = (Ejb3Configuration) in.readObject();
 59  
                         } else {
 60  0
                                 cfg = configure(info, map);
 61  
                         }
 62  0
                 } catch (Exception e) {
 63  0
                         LOG.error(e.getMessage(), e);
 64  
                 } finally {
 65  0
                         if (in != null) {
 66  
                                 try {
 67  0
                                         in.close();
 68  0
                                 } catch (IOException e) {
 69  0
                                         LOG.error(e.getMessage(), e);
 70  0
                                 }
 71  
                         }
 72  
                 }
 73  
                 
 74  0
                 if (cfg != null) {
 75  0
                         factory = cfg.buildEntityManagerFactory();
 76  
                 } else {
 77  0
                         LOG.error("Error creating Ejb3Configuration");
 78  
                 }
 79  0
                 return factory;
 80  
         }
 81  
 
 82  
         private Ejb3Configuration configure(PersistenceUnitInfo info, Map map) throws IOException {                
 83  0
                 Ejb3Configuration configured = new Ejb3Configuration().configure(info, map);
 84  0
                 if (useSerialization) {
 85  0
                         ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(serializationFilename));
 86  0
                         oos.writeObject(configured);
 87  0
                         oos.close();
 88  
                 }
 89  0
                 return configured;
 90  
         }
 91  
 
 92  
         public void setSerializationFilename(String serializationFilename) {
 93  0
                 this.serializationFilename = serializationFilename;
 94  0
         }
 95  
 
 96  
         public void setUseSerialization(boolean useSerialization) {
 97  0
                 this.useSerialization = useSerialization;
 98  0
         }
 99  
 
 100  
 }