001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.krad.util;
017
018import org.apache.log4j.Logger;
019import org.apache.ojb.broker.metadata.ConnectionRepository;
020import org.apache.ojb.broker.metadata.DescriptorRepository;
021import org.apache.ojb.broker.metadata.MetadataManager;
022import org.kuali.rice.core.api.util.ClassLoaderUtils;
023import org.springframework.beans.factory.InitializingBean;
024import org.springframework.core.io.DefaultResourceLoader;
025
026import java.io.InputStream;
027import java.util.ArrayList;
028import java.util.List;
029
030public class OjbMetadataLoader implements InitializingBean {
031    
032    private static final Logger LOG = Logger.getLogger(OjbMetadataLoader.class);
033
034    private List<String> repositoryDescriptors = new ArrayList<String>();
035    private List<String> connectionDescriptors = new ArrayList<String>(); 
036    
037    public List<String> getConnectionDescriptors() {
038        return connectionDescriptors;
039    }
040
041    public void setConnectionDescriptors(List<String> connectionDescriptors) {
042        this.connectionDescriptors = connectionDescriptors;
043    }
044
045    public List<String> getRepositoryDescriptors() {
046        return repositoryDescriptors;
047    }
048
049    public void setRepositoryDescriptors(List<String> repositoryDescriptors) {
050        this.repositoryDescriptors = repositoryDescriptors;
051    }
052
053    public void afterPropertiesSet() throws Exception {
054        
055        MetadataManager mm = MetadataManager.getInstance();
056        DefaultResourceLoader resourceLoader = new DefaultResourceLoader(ClassLoaderUtils.getDefaultClassLoader());
057        
058        for (String repositoryDescriptor : repositoryDescriptors) {
059            InputStream is = resourceLoader.getResource(repositoryDescriptor).getInputStream();
060            DescriptorRepository dr = mm.readDescriptorRepository(is);
061            mm.mergeDescriptorRepository(dr);
062            if (LOG.isDebugEnabled()) {
063                LOG.debug("--------------------------------------------------------------------------");
064                LOG.debug("Merging repository descriptor: " + repositoryDescriptor);
065                LOG.debug("--------------------------------------------------------------------------");
066            }
067            try {
068                is.close();
069            } catch (Exception e) {
070                LOG.warn("Failed to close stream to file " + repositoryDescriptor, e);
071            }
072        }
073        
074        for (String connectionDesciptor : connectionDescriptors) {
075            InputStream is = resourceLoader.getResource(connectionDesciptor).getInputStream();
076            ConnectionRepository cr = mm.readConnectionRepository(is);
077            mm.mergeConnectionRepository(cr);
078            if (LOG.isDebugEnabled()) {
079                LOG.debug("--------------------------------------------------------------------------");
080                LOG.debug("Merging connection descriptor: " + connectionDesciptor);
081                LOG.debug("--------------------------------------------------------------------------");
082            }
083            try {
084                is.close();
085            } catch (Exception e) {
086                LOG.warn("Failed to close stream to file " + connectionDesciptor, e);
087            }
088        }
089        
090    }
091
092}