| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 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 net.sourceforge.hivetranse.transaction.hibernate3; |
| 16 | |
| 17 | import java.lang.reflect.Method; |
| 18 | import java.net.URL; |
| 19 | import java.util.List; |
| 20 | import java.util.Properties; |
| 21 | |
| 22 | import org.apache.commons.logging.Log; |
| 23 | import org.apache.hivemind.ApplicationRuntimeException; |
| 24 | import org.apache.hivemind.Resource; |
| 25 | import org.hibernate.HibernateException; |
| 26 | import org.hibernate.Interceptor; |
| 27 | import org.hibernate.SessionFactory; |
| 28 | import org.hibernate.cfg.Configuration; |
| 29 | import org.hibernate.cfg.NamingStrategy; |
| 30 | |
| 31 | /** |
| 32 | * Utility class to build Hibernate <code>SessionFactory</code>. Can be used |
| 33 | * as a HiveMind service but does not have to. |
| 34 | * <p> |
| 35 | * Please note that this interface will not be stable for some time, so avoid |
| 36 | * using it directly. Anyway, it should be considered to be reserved for |
| 37 | * <code>hivetranse.hibernate</code> internal use only. |
| 38 | * |
| 39 | * @author Jean-Francois Poilpret |
| 40 | */ |
| 41 | public class SessionFactoryBuilderImpl implements SessionFactoryBuilder |
| 42 | { |
| 43 | public SessionFactoryBuilderImpl(Log logger) |
| 44 | { |
| 45 | _logger = logger; |
| 46 | } |
| 47 | |
| 48 | public SessionFactory buildSessionFactory(Resource config, |
| 49 | Properties properties, |
| 50 | List<String> mappingPackages, |
| 51 | List<Class> mappingClasses, |
| 52 | List<URL> mappingFiles, |
| 53 | Interceptor interceptor, |
| 54 | NamingStrategy namingStrategy) |
| 55 | { |
| 56 | try |
| 57 | { |
| 58 | // Fix suggested by Vinicius Caldeira Carvalho to support Hibernate3 |
| 59 | // Annotations. We just test if the Hibernate Annotations jars are in |
| 60 | // the classpath, if so we instantiate AnnotationConfiguration through |
| 61 | // reflection, else fall back to "normal Configuration. |
| 62 | Configuration conf = initConfiguration(); |
| 63 | if (config != null) |
| 64 | { |
| 65 | if (config.getResourceURL() == null) |
| 66 | { |
| 67 | _logger.error( "buildSessionFactory() bad configuration file " + |
| 68 | config.getName()); |
| 69 | throw new ApplicationRuntimeException( "Bad configuration file " + |
| 70 | config.getName()); |
| 71 | } |
| 72 | conf.configure(config.getResourceURL()); |
| 73 | } |
| 74 | if (namingStrategy != null) |
| 75 | { |
| 76 | conf.setNamingStrategy(namingStrategy); |
| 77 | } |
| 78 | for (String pack: mappingPackages) |
| 79 | { |
| 80 | addPackage(conf, pack); |
| 81 | } |
| 82 | for (Class clazz: mappingClasses) |
| 83 | { |
| 84 | addClass(conf, clazz); |
| 85 | } |
| 86 | for (URL url: mappingFiles) |
| 87 | { |
| 88 | conf.addURL(url); |
| 89 | } |
| 90 | conf.addProperties(properties); |
| 91 | if (interceptor != null) |
| 92 | { |
| 93 | conf.setInterceptor(interceptor); |
| 94 | } |
| 95 | return conf.buildSessionFactory(); |
| 96 | } |
| 97 | catch (HibernateException e) |
| 98 | { |
| 99 | if (config != null) |
| 100 | { |
| 101 | _logger.error( |
| 102 | "buildSessionFactory() could not build SessionFactory from file " + |
| 103 | config.getName(), e); |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | _logger.error("buildSessionFactory() could not build SessionFactory", e); |
| 108 | } |
| 109 | throw new ApplicationRuntimeException("Could not build SessionFactory", e); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | //CSOFF: IllegalCatchCheck |
| 114 | private Configuration initConfiguration() |
| 115 | { |
| 116 | try |
| 117 | { |
| 118 | // Try to instantiate AnnotationConfiguration by reflection |
| 119 | Class clazz = Class.forName("org.hibernate.cfg.AnnotationConfiguration"); |
| 120 | Configuration config = (Configuration) clazz.newInstance(); |
| 121 | // Init special hibernate 3.1 annotations Methods |
| 122 | _addAnnotatedClass = clazz.getMethod("addAnnotatedClass", Class.class); |
| 123 | _addPackage = clazz.getMethod("addPackage", String.class); |
| 124 | return config; |
| 125 | } |
| 126 | catch (Exception e) |
| 127 | { |
| 128 | // Fall back to normal configuration |
| 129 | _logger.info("initConfiguration", e); |
| 130 | return new Configuration(); |
| 131 | } |
| 132 | } |
| 133 | //CSON: IllegalCatchCheck |
| 134 | |
| 135 | //CSOFF: IllegalCatchCheck |
| 136 | private void addClass(Configuration config, Class clazz) |
| 137 | { |
| 138 | if (_addAnnotatedClass != null) |
| 139 | { |
| 140 | try |
| 141 | { |
| 142 | _addAnnotatedClass.invoke(config, clazz); |
| 143 | } |
| 144 | catch (Exception e) |
| 145 | { |
| 146 | if (_logger.isErrorEnabled()) |
| 147 | { |
| 148 | _logger.error("addClass(" + clazz + ")", e); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | config.addClass(clazz); |
| 155 | } |
| 156 | } |
| 157 | //CSON: IllegalCatchCheck |
| 158 | |
| 159 | //CSOFF: IllegalCatchCheck |
| 160 | private void addPackage(Configuration config, String pack) |
| 161 | { |
| 162 | if (_addPackage != null) |
| 163 | { |
| 164 | try |
| 165 | { |
| 166 | _addPackage.invoke(config, pack); |
| 167 | } |
| 168 | catch (Exception e) |
| 169 | { |
| 170 | if (_logger.isErrorEnabled()) |
| 171 | { |
| 172 | _logger.error("addPackage(" + pack + ")", e); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | //CSON: IllegalCatchCheck |
| 178 | |
| 179 | private final Log _logger; |
| 180 | private Method _addAnnotatedClass = null; |
| 181 | private Method _addPackage = null; |
| 182 | } |