1 16 17 package org.springframework.orm.jdo; 18 19 import java.io.IOException ; 20 import java.lang.reflect.Method ; 21 import java.util.Properties ; 22 23 import javax.jdo.JDOException; 24 import javax.jdo.JDOHelper; 25 import javax.jdo.PersistenceManagerFactory; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 import org.springframework.beans.factory.DisposableBean; 31 import org.springframework.beans.factory.FactoryBean; 32 import org.springframework.beans.factory.InitializingBean; 33 import org.springframework.core.io.Resource; 34 import org.springframework.core.io.support.PropertiesLoaderUtils; 35 import org.springframework.dao.DataAccessException; 36 import org.springframework.dao.support.PersistenceExceptionTranslator; 37 import org.springframework.util.ClassUtils; 38 import org.springframework.util.CollectionUtils; 39 import org.springframework.util.ReflectionUtils; 40 41 115 public class LocalPersistenceManagerFactoryBean 116 implements FactoryBean, InitializingBean, DisposableBean, PersistenceExceptionTranslator { 117 118 private final static Method getPersistenceManagerFactoryMethod = ClassUtils.getMethodIfAvailable( 121 JDOHelper.class, "getPersistenceManagerFactory", new Class [] {Properties .class}); 122 123 124 protected final Log logger = LogFactory.getLog(getClass()); 125 126 private Resource configLocation; 127 128 private Properties jdoProperties; 129 130 private PersistenceManagerFactory persistenceManagerFactory; 131 132 private JdoDialect jdoDialect; 133 134 135 141 public void setConfigLocation(Resource configLocation) { 142 this.configLocation = configLocation; 143 } 144 145 150 public void setJdoProperties(Properties jdoProperties) { 151 this.jdoProperties = jdoProperties; 152 } 153 154 158 public Properties getJdoProperties() { 159 if (this.jdoProperties == null) { 160 this.jdoProperties = new Properties (); 161 } 162 return this.jdoProperties; 163 } 164 165 174 public void setJdoDialect(JdoDialect jdoDialect) { 175 this.jdoDialect = jdoDialect; 176 } 177 178 179 185 public void afterPropertiesSet() throws IllegalArgumentException , IOException , JDOException { 186 if (this.configLocation == null && this.jdoProperties == null) { 187 throw new IllegalArgumentException ("Either configLocation or jdoProperties must be set"); 188 } 189 190 Properties mergedProps = new Properties (); 191 192 if (this.configLocation != null) { 193 if (logger.isInfoEnabled()) { 194 logger.info("Loading JDO config from [" + this.configLocation + "]"); 195 } 196 PropertiesLoaderUtils.fillProperties(mergedProps, this.configLocation); 197 } 198 199 CollectionUtils.mergePropertiesIntoMap(this.jdoProperties, mergedProps); 200 201 logger.info("Building new JDO PersistenceManagerFactory"); 203 this.persistenceManagerFactory = newPersistenceManagerFactory(mergedProps); 204 205 if (this.jdoDialect == null) { 207 this.jdoDialect = new DefaultJdoDialect(this.persistenceManagerFactory.getConnectionFactory()); 208 } 209 } 210 211 227 protected PersistenceManagerFactory newPersistenceManagerFactory(Properties props) { 228 if (getPersistenceManagerFactoryMethod != null) { 230 return (PersistenceManagerFactory) ReflectionUtils.invokeMethod( 231 getPersistenceManagerFactoryMethod, null, new Object [] {props}); 232 } 233 return JDOHelper.getPersistenceManagerFactory(props); 235 } 236 237 238 241 public Object getObject() { 242 return this.persistenceManagerFactory; 243 } 244 245 public Class getObjectType() { 246 return (this.persistenceManagerFactory != null ? 247 this.persistenceManagerFactory.getClass() : PersistenceManagerFactory.class); 248 } 249 250 public boolean isSingleton() { 251 return true; 252 } 253 254 255 264 public DataAccessException translateExceptionIfPossible(RuntimeException ex) { 265 if (ex instanceof JDOException) { 266 if (this.jdoDialect != null) { 267 return this.jdoDialect.translateException((JDOException) ex); 268 } 269 else { 270 return PersistenceManagerFactoryUtils.convertJdoAccessException((JDOException) ex); 271 } 272 } 273 return null; 274 } 275 276 277 280 public void destroy() { 281 logger.info("Closing JDO PersistenceManagerFactory"); 282 this.persistenceManagerFactory.close(); 283 } 284 285 } 286 | Popular Tags |