1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.core.persistence;
16
17 import org.figure8.join.core.setup.BootstrapUtil;
18 import org.figure8.join.core.setup.BootstrapManager;
19 import org.figure8.join.core.setup.HibernateConfigurator;
20 import org.figure8.join.util.LogUtil;
21
22 import org.apache.commons.logging.Log;
23 import org.springframework.core.io.Resource;
24 import org.springframework.orm.hibernate.LocalSessionFactoryBean;
25 import net.sf.hibernate.HibernateException;
26 import net.sf.hibernate.cfg.Configuration;
27
28 import java.util.List;
29 import java.io.IOException;
30 /**
31 * This is an extension of Spring <code>LocalSessionFactoryBean</code> that
32 * can be further configured using extra properties provided by a
33 * <code>HibernateConfigurator</code> instance. The creation of the Hibernate
34 * session factory can be also conditioned by the fact that we are on an
35 * asynchronous side of the Join application.
36 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
37 * @version $Revision: 1.3 $
38 */
39 public class ConfigurableHibernateSessionFactoryBean extends LocalSessionFactoryBean{
40
41
42
43 /** Get a commons logger. */
44 protected static Log log = LogUtil.getLog(ConfigurableHibernateSessionFactoryBean.class);
45
46
47
48
49 /** Flag telling if we should start only if on asychronous side of application */
50 private boolean checkForAsync = false;
51 /** Flag telling if properties from Spring config should be overriden by those from HibernateConfigurator */
52 private boolean useHbnConfigurator = true;
53 /** Flag telling if property representing Url should be checked */
54 private boolean replaceHomeInUrl = false;
55
56 /** Hibernate mapping files resources. */
57 private List mappingResources = null;
58 /** Helper object for configuring Hibernate session factory. */
59 private HibernateConfigurator configurator = null;
60
61
62
63
64 /** Creates a new instance of ConfigurableHibernateSessionFactoryBean */
65 public ConfigurableHibernateSessionFactoryBean(){
66 super();
67 }
68
69
70
71
72 /** @param checkForAsync Flag telling if we should start only if on asychronous side of application */
73 public void setCheckForAsync(boolean checkForAsync){
74 this.checkForAsync = checkForAsync;
75 }
76 /** @param useHbnConfigurator Flag telling if properties from Spring should be overriden by HibernateConfigurator */
77 public void setUseHbnConfigurator(boolean useHbnConfigurator){
78 this.useHbnConfigurator = useHbnConfigurator;
79 }
80 /** @param replaceHomeInUrl Flag telling if property representing Url should be checked */
81 public void setReplaceHomeInUrl(boolean replaceHomeInUrl){
82 this.replaceHomeInUrl = replaceHomeInUrl;
83 }
84
85 /** @param resources List of Hibernate mapping resources */
86 public void setMappingResources(List resources){
87 this.mappingResources = resources;
88 }
89 /** @param configurator Helper for configuring session factory */
90 public void setHibernateConfigurator(HibernateConfigurator configurator){
91 this.configurator = configurator;
92 }
93
94
95
96
97 /**
98 * Initialize the SessionFactory for the given or the default location.
99 * This is only done if : <b>checkForAynch</b> is false or <b>checkForAsync</b>
100 * is true and we are on an asynchronous side of the application.
101 * @throws IllegalArgumentException in case of illegal property values
102 * @throws HibernateException in case of Hibernate initialization errors
103 * @throws IOException ...
104 */
105 public void afterPropertiesSet() throws IllegalArgumentException, HibernateException, IOException{
106
107 BootstrapManager bootManager = BootstrapUtil.getBootstrapManager();
108 if (checkForAsync && !bootManager.isAsynchronousSide())
109 return;
110
111 log.info("Creating a new Hibernate SessionFactory...");
112 super.afterPropertiesSet();
113 }
114
115 /**
116 * Custom initialization of the Configuration instance used for
117 * SessionFactory creation.
118 * @return A newly created Hibernate configuration
119 */
120 protected Configuration newConfiguration() throws HibernateException{
121 log.info("Creating a new Hibernate Configuration...");
122
123 Configuration config = new Configuration();
124 super.setConfigLocation((Resource)null);
125
126 if (useHbnConfigurator)
127 config.addProperties(configurator.getHibernateProperties());
128
129 if (mappingResources != null && mappingResources.size() > 0){
130 String[] resources = new String[mappingResources.size()];
131 for (int i=0; i<mappingResources.size(); i++)
132 resources[i] = (String)mappingResources.get(i);
133
134 super.setMappingResources(resources);
135 }
136 return config;
137 }
138
139 /**
140 * To be implemented by subclasses that want to to perform custom
141 * post-processing of the Configuration object after this FactoryBean
142 * performed its default initialization.
143 * @param config the current Configuration object
144 * @throws HibernateException in case of Hibernate initialization errors
145 */
146 protected void postProcessConfiguration(Configuration config) throws HibernateException{
147
148 if (replaceHomeInUrl){
149 int length = "${join.home}".length();
150 String connectionUrl = config.getProperty("hibernate.connection.url");
151 StringBuffer buffer = new StringBuffer(connectionUrl);
152
153 BootstrapManager bootManager = BootstrapUtil.getBootstrapManager();
154
155 for (int i=buffer.indexOf("${join.home}"); i!=-1; i=buffer.indexOf("${join.home}"))
156 buffer.replace(i, i + length, bootManager.getJoinHome());
157
158 config.setProperty("hibernate.connection.url", buffer.toString());
159 }
160 }
161 }