001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/trunk/sakai/admin-tools/su/src/java/org/sakaiproject/tool/su/SuTool.java $
003: * $Id: SuTool.java 5970 2006-02-15 03:07:19Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.springframework.orm.hibernate;
021:
022: import java.io.IOException;
023: import java.util.ArrayList;
024: import java.util.List;
025: import java.util.Iterator;
026: import java.util.Collections;
027:
028: import org.hibernate.HibernateException;
029: import org.hibernate.cfg.Configuration;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.springframework.beans.BeansException;
034: import org.springframework.context.ApplicationContext;
035: import org.springframework.context.ApplicationContextAware;
036: import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
037:
038: public class AddableSessionFactoryBean extends LocalSessionFactoryBean
039: implements ApplicationContextAware {
040: protected final transient Log logger = LogFactory
041: .getLog(getClass());
042:
043: private ApplicationContext applicationContext;
044:
045: /**
046: * To be implemented by subclasses that want to to perform custom post-processing of the Configuration object after this FactoryBean performed its default initialization.
047: *
048: * @param config
049: * the current Configuration object
050: * @throws org.hibernate.HibernateException
051: * in case of Hibernate initialization errors
052: */
053: protected void postProcessConfiguration(Configuration config)
054: throws HibernateException {
055: super .postProcessConfiguration(config);
056:
057: String[] names = applicationContext.getBeanNamesForType(
058: AdditionalHibernateMappings.class, false, false);
059:
060: try {
061: List beans = new ArrayList();
062: for (int i = 0; i < names.length; i++) {
063: AdditionalHibernateMappings mappings = (AdditionalHibernateMappings) applicationContext
064: .getBean(names[i]);
065:
066: beans.add(mappings);
067: }
068:
069: Collections.sort(beans);
070:
071: for (Iterator i = beans.iterator(); i.hasNext();) {
072: AdditionalHibernateMappings mappings = (AdditionalHibernateMappings) i
073: .next();
074: mappings.processConfig(config);
075: }
076:
077: } catch (IOException e) {
078: logger.error(e.getMessage(), e);
079: throw new RuntimeException(e);
080: }
081: }
082:
083: /**
084: * Set the ApplicationContext that this object runs in. Normally this call will be used to initialize the object.
085: * <p>
086: * Invoked after population of normal bean properties but before an init callback like InitializingBean's afterPropertiesSet or a custom init-method. Invoked after ResourceLoaderAware's setResourceLoader.
087: *
088: * @param applicationContext
089: * ApplicationContext object to be used by this object
090: * @throws org.springframework.context.ApplicationContextException
091: * in case of applicationContext initialization errors
092: * @throws org.springframework.beans.BeansException
093: * if thrown by application applicationContext methods
094: * @see org.springframework.beans.factory.BeanInitializationException
095: */
096: public void setApplicationContext(
097: ApplicationContext applicationContext)
098: throws BeansException {
099: this.applicationContext = applicationContext;
100: }
101: }
|