1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.List;
21
22 import org.apache.log4j.Logger;
23 import org.apache.ojb.broker.metadata.ConnectionRepository;
24 import org.apache.ojb.broker.metadata.DescriptorRepository;
25 import org.apache.ojb.broker.metadata.MetadataManager;
26 import org.kuali.rice.core.api.exception.RiceRuntimeException;
27 import org.kuali.rice.core.api.util.ClassLoaderUtils;
28 import org.springframework.beans.factory.InitializingBean;
29 import org.springframework.core.io.DefaultResourceLoader;
30
31 public class SpringBasedOJBRepositoryLoader implements InitializingBean {
32
33 private static final Logger LOG = Logger.getLogger(SpringBasedOJBRepositoryLoader.class);
34 private static final String CLASSPATH_RESOURCE_PREFIX = "classpath:";
35
36 private List<String> pathLocations;
37
38 public void afterPropertiesSet() throws Exception {
39
40 for (String pathLocation : pathLocations) {
41 loadRepositoryDescriptor(pathLocation);
42 }
43
44 }
45
46 public void loadRepositoryDescriptor(String ojbRepositoryFilePath) {
47 LOG.info("Begin loading OJB Metadata for: " + ojbRepositoryFilePath);
48 DefaultResourceLoader resourceLoader = new DefaultResourceLoader(ClassLoaderUtils.getDefaultClassLoader());
49 InputStream is = null;
50 try {
51 is = resourceLoader.getResource(CLASSPATH_RESOURCE_PREFIX + ojbRepositoryFilePath).getInputStream();
52 ConnectionRepository cr = MetadataManager.getInstance().readConnectionRepository(is);
53 MetadataManager.getInstance().mergeConnectionRepository(cr);
54
55 is = resourceLoader.getResource(CLASSPATH_RESOURCE_PREFIX + ojbRepositoryFilePath).getInputStream();
56 DescriptorRepository dr = MetadataManager.getInstance().readDescriptorRepository(is);
57 MetadataManager.getInstance().mergeDescriptorRepository(dr);
58
59 if (LOG.isDebugEnabled()) {
60 LOG.debug("--------------------------------------------------------------------------");
61 LOG.debug("Merging repository descriptor: " + ojbRepositoryFilePath);
62 LOG.debug("--------------------------------------------------------------------------");
63 }
64 } catch (IOException ioe) {
65 if (is != null) {
66 try {
67 is.close();
68 } catch (IOException e) {
69 LOG.info("Failed to close InputStream on OJB repository path " + ojbRepositoryFilePath, e);
70 }
71 }
72 throw new RiceRuntimeException(ioe);
73 } finally {
74 if (is != null) {
75 try {
76 is.close();
77 } catch (IOException e) {
78 LOG.info("Failed to close InputStream on OJB repository path " + ojbRepositoryFilePath, e);
79 }
80 }
81 }
82 LOG.info("Finished loading OJB Metadata for: " + ojbRepositoryFilePath);
83 }
84
85 public List<String> getPathLocations() {
86 return pathLocations;
87 }
88
89 public void setPathLocations(List<String> pathLocations) {
90 this.pathLocations = pathLocations;
91 }
92
93 }