001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.time;
017    
018    import java.io.IOException;
019    import java.io.InputStream;
020    import java.util.List;
021    
022    import org.apache.log4j.Logger;
023    import org.apache.ojb.broker.metadata.ConnectionRepository;
024    import org.apache.ojb.broker.metadata.DescriptorRepository;
025    import org.apache.ojb.broker.metadata.MetadataManager;
026    import org.kuali.rice.core.api.exception.RiceRuntimeException;
027    import org.kuali.rice.core.api.util.ClassLoaderUtils;
028    import org.springframework.beans.factory.InitializingBean;
029    import org.springframework.core.io.DefaultResourceLoader;
030    
031    public class SpringBasedOJBRepositoryLoader implements InitializingBean {
032    
033        private static final Logger LOG = Logger.getLogger(SpringBasedOJBRepositoryLoader.class);
034        private static final String CLASSPATH_RESOURCE_PREFIX = "classpath:";
035    
036        private List<String> pathLocations;
037    
038        public void afterPropertiesSet() throws Exception {
039    
040            for (String pathLocation : pathLocations) {
041                loadRepositoryDescriptor(pathLocation);
042            }
043    
044        }
045    
046        public void loadRepositoryDescriptor(String ojbRepositoryFilePath) {
047            LOG.info("Begin loading OJB Metadata for: " + ojbRepositoryFilePath);
048            DefaultResourceLoader resourceLoader = new DefaultResourceLoader(ClassLoaderUtils.getDefaultClassLoader());
049            InputStream is = null;
050            try {
051                is = resourceLoader.getResource(CLASSPATH_RESOURCE_PREFIX + ojbRepositoryFilePath).getInputStream();
052                ConnectionRepository cr = MetadataManager.getInstance().readConnectionRepository(is);
053                MetadataManager.getInstance().mergeConnectionRepository(cr);
054    
055                is = resourceLoader.getResource(CLASSPATH_RESOURCE_PREFIX + ojbRepositoryFilePath).getInputStream();
056                DescriptorRepository dr = MetadataManager.getInstance().readDescriptorRepository(is);
057                MetadataManager.getInstance().mergeDescriptorRepository(dr);
058    
059                if (LOG.isDebugEnabled()) {
060                    LOG.debug("--------------------------------------------------------------------------");
061                    LOG.debug("Merging repository descriptor: " + ojbRepositoryFilePath);
062                    LOG.debug("--------------------------------------------------------------------------");
063                }
064            } catch (IOException ioe) {
065                if (is != null) {
066                    try {
067                        is.close();
068                    } catch (IOException e) {
069                        LOG.info("Failed to close InputStream on OJB repository path " + ojbRepositoryFilePath, e);
070                    }
071                }
072                throw new RiceRuntimeException(ioe);
073            } finally {
074                if (is != null) {
075                    try {
076                        is.close();
077                    } catch (IOException e) {
078                        LOG.info("Failed to close InputStream on OJB repository path " + ojbRepositoryFilePath, e);
079                    }
080                }
081            }
082            LOG.info("Finished loading OJB Metadata for: " + ojbRepositoryFilePath);
083        }
084    
085        public List<String> getPathLocations() {
086            return pathLocations;
087        }
088    
089        public void setPathLocations(List<String> pathLocations) {
090            this.pathLocations = pathLocations;
091        }
092    
093    }