View Javadoc

1   /**
2    * Copyright 2004-2012 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.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  }