Java Source Code: ome.tools.hibernate.OmeroSessionFactoryBean


   1: /*
   2:  * ome.tools.hibernate.OmeroSessionFactoryBean
   3:  *
   4:  *------------------------------------------------------------------------------
   5:  *
   6:  *  Copyright (C) 2005 Open Microscopy Environment
   7:  *      Massachusetts Institute of Technology,
   8:  *      National Institutes of Health,
   9:  *      University of Dundee
  10:  *
  11:  *
  12:  *
  13:  *    This library is free software; you can redistribute it and/or
  14:  *    modify it under the terms of the GNU Lesser General Public
  15:  *    License as published by the Free Software Foundation; either
  16:  *    version 2.1 of the License, or (at your option) any later version.
  17:  *
  18:  *    This library is distributed in the hope that it will be useful,
  19:  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  20:  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  21:  *    Lesser General Public License for more details.
  22:  *
  23:  *    You should have received a copy of the GNU Lesser General Public
  24:  *    License along with this library; if not, write to the Free Software
  25:  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  26:  *
  27:  *------------------------------------------------------------------------------
  28:  */
  29: 
  30: package ome.tools.hibernate;
  31: 
  32: //Java imports
  33: 
  34: //Third-party libraries
  35: import org.hibernate.HibernateException;
  36: import org.hibernate.cfg.Configuration;
  37: import org.springframework.core.io.ClassPathResource;
  38: import org.springframework.core.io.Resource;
  39: import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
  40: 
  41: //Application-internal dependencies
  42: 
  43: 
  44: 
  45: /** 
  46:  * provides a cacheable Hibernate configuration to speed up development.
  47:  * 
  48:  * @author  Josh Moore     
  49:  *                 <a href="mailto:josh.moore@gmx.de">josh.moore@gmx.de</a>
  50:  * @version 1.0 
  51:  * <small>
  52:  * (<b>Internal version:</b> $Rev$ $Date$)
  53:  * </small>
  54:  * @since 1.0
  55:  */
  56:	  public class OmeroSessionFactoryBean extends LocalSessionFactoryBean{
  57: 
  58:    private Resource[] cachedMappingResources; 
  59:    
  60:    /**
  61:     * Set Hibernate mapping resources to be found in the class path,
  62:     * like "example.hbm.xml" or "mypackage/example.hbm.xml". Resources stored
  63:     * as <b>cached</b>MappingResouces will use the Configuration.addCacheableFile
  64:     * call.
  65:     * @see org.hibernate.cfg.Configuration#addCacheableFile
  66:     * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#setMappingResources
  67:     */
  68:	      public void setCachedMappingResources(String[] mappingResources) {
  69:            this.cachedMappingResources = new Resource[mappingResources.length];
  70:	              for (int i = 0; i < mappingResources.length; i++) {
  71:                    this.cachedMappingResources[i] = new ClassPathResource(mappingResources[i].trim());
  72:            }
  73:    }
  74:           
  75:    /**
  76:     * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#postProcessConfiguration(org.hibernate.cfg.Configuration)
  77:     */
  78:    protected void postProcessConfiguration(Configuration config)
  79:	              throws HibernateException {
  80:        
  81:	          if (this.cachedMappingResources != null) {
  82:	              for (int i = 0; i < this.cachedMappingResources.length; i++) {
  83:	                      try {
  84:                        config.addCacheableFile(this.cachedMappingResources[i].getFile());
  85:                    } catch (Exception e) {
  86:                        throw new RuntimeException("Failed to load Hibernate mapping resource "+this.cachedMappingResources[i],e);
  87:                    }
  88:            }
  89:        }
  90:            
  91:    }
  92:     
  93:}