1 4 package xpetstore.web.servlet; 5 6 import java.io.IOException ; 7 import java.io.InputStream ; 8 9 import java.util.Properties ; 10 11 import javax.servlet.ServletConfig ; 12 import javax.servlet.ServletException ; 13 14 import org.apache.log4j.Logger; 15 16 import cirrus.hibernate.Datastore; 17 import cirrus.hibernate.Hibernate; 18 import cirrus.hibernate.HibernateException; 19 import cirrus.hibernate.SessionFactory; 20 21 import webwork.dispatcher.ServletDispatcher; 22 23 import xpetstore.domain.Account; 24 import xpetstore.domain.Category; 25 import xpetstore.domain.Customer; 26 import xpetstore.domain.Item; 27 import xpetstore.domain.Order; 28 import xpetstore.domain.OrderItem; 29 import xpetstore.domain.Product; 30 31 32 56 public class ActionServlet 57 extends ServletDispatcher 58 { 59 61 private static final Logger __logger = Logger.getLogger( ActionServlet.class ); 62 public static final String SESSION_FACTORY_KEY = "cirrus.hibernate.SessionFactory"; 63 64 66 69 public void init( ServletConfig config ) 70 throws ServletException 71 { 72 super.init( config ); 73 74 __logger.info( "Initializing" ); 75 76 try 77 { 78 Datastore ds = Hibernate.createDatastore( ); 79 80 81 load( Account.class, ds ); 82 load( Category.class, ds ); 83 load( Customer.class, ds ); 84 load( Item.class, ds ); 85 load( Order.class, ds ); 86 load( OrderItem.class, ds ); 87 load( Product.class, ds ); 88 89 90 __logger.info( "...loading: hibernate.properties" ); 91 92 InputStream in = getClass( ).getClassLoader( ).getResourceAsStream( "hibernate.properties" ); 93 Properties props = new Properties ( ); 94 props.load( in ); 95 96 97 __logger.info( "...initializing the Hibernate SessionFactory" ); 98 99 SessionFactory sessionFactory = ds.buildSessionFactory( props ); 100 config.getServletContext( ).setAttribute( SESSION_FACTORY_KEY, sessionFactory ); 101 102 __logger.info( "Initialized" ); 103 } 104 catch ( HibernateException h ) 105 { 106 throw new ServletException ( h ); 107 } 108 catch ( IOException io ) 109 { 110 throw new ServletException ( "Unable to load: hibernate.properties", io ); 111 } 112 } 113 114 private void load( Class type, 115 Datastore ds ) 116 throws ServletException 117 { 118 try 119 { 120 __logger.info( "... loading mapping for: " + type ); 121 ds.storeClass( type ); 122 } 123 catch ( Exception e ) 124 { 125 throw new ServletException ( "Unable to load Hibernate mapping:" + type, e ); 126 } 127 } 128 129 132 public void destroy( ) 133 { 134 getServletContext( ).removeAttribute( SESSION_FACTORY_KEY ); 135 136 super.destroy( ); 137 } 138 } 139 | Popular Tags |