View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.util;
17  
18  import org.apache.log4j.Logger;
19  import org.apache.ojb.broker.metadata.ConnectionRepository;
20  import org.apache.ojb.broker.metadata.DescriptorRepository;
21  import org.apache.ojb.broker.metadata.MetadataManager;
22  import org.kuali.rice.core.api.util.ClassLoaderUtils;
23  import org.springframework.beans.factory.InitializingBean;
24  import org.springframework.core.io.DefaultResourceLoader;
25  
26  import java.io.InputStream;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  public class OjbMetadataLoader implements InitializingBean {
31      
32      private static final Logger LOG = Logger.getLogger(OjbMetadataLoader.class);
33  
34      private List<String> repositoryDescriptors = new ArrayList<String>();
35      private List<String> connectionDescriptors = new ArrayList<String>(); 
36      
37      public List<String> getConnectionDescriptors() {
38          return connectionDescriptors;
39      }
40  
41      public void setConnectionDescriptors(List<String> connectionDescriptors) {
42          this.connectionDescriptors = connectionDescriptors;
43      }
44  
45      public List<String> getRepositoryDescriptors() {
46          return repositoryDescriptors;
47      }
48  
49      public void setRepositoryDescriptors(List<String> repositoryDescriptors) {
50          this.repositoryDescriptors = repositoryDescriptors;
51      }
52  
53      public void afterPropertiesSet() throws Exception {
54          
55          MetadataManager mm = MetadataManager.getInstance();
56          DefaultResourceLoader resourceLoader = new DefaultResourceLoader(ClassLoaderUtils.getDefaultClassLoader());
57          
58          for (String repositoryDescriptor : repositoryDescriptors) {
59              InputStream is = resourceLoader.getResource(repositoryDescriptor).getInputStream();
60              DescriptorRepository dr = mm.readDescriptorRepository(is);
61              mm.mergeDescriptorRepository(dr);
62              if (LOG.isDebugEnabled()) {
63                  LOG.debug("--------------------------------------------------------------------------");
64                  LOG.debug("Merging repository descriptor: " + repositoryDescriptor);
65                  LOG.debug("--------------------------------------------------------------------------");
66              }
67              try {
68                  is.close();
69              } catch (Exception e) {
70                  LOG.warn("Failed to close stream to file " + repositoryDescriptor, e);
71              }
72          }
73          
74          for (String connectionDesciptor : connectionDescriptors) {
75              InputStream is = resourceLoader.getResource(connectionDesciptor).getInputStream();
76              ConnectionRepository cr = mm.readConnectionRepository(is);
77              mm.mergeConnectionRepository(cr);
78              if (LOG.isDebugEnabled()) {
79                  LOG.debug("--------------------------------------------------------------------------");
80                  LOG.debug("Merging connection descriptor: " + connectionDesciptor);
81                  LOG.debug("--------------------------------------------------------------------------");
82              }
83              try {
84                  is.close();
85              } catch (Exception e) {
86                  LOG.warn("Failed to close stream to file " + connectionDesciptor, e);
87              }
88          }
89          
90      }
91  
92  }