01: /**********************************************************************************
02: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-api/api/src/java/org/sakaiproject/metaobj/utils/ioc/ApplicationContextFactory.java $
03: * $Id: ApplicationContextFactory.java 9469 2006-05-15 14:52:05Z chmaurer@iupui.edu $
04: ***********************************************************************************
05: *
06: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
07: *
08: * Licensed under the Educational Community License, Version 1.0 (the "License");
09: * you may not use this file except in compliance with the License.
10: * You may obtain a copy of the License at
11: *
12: * http://www.opensource.org/licenses/ecl1.php
13: *
14: * Unless required by applicable law or agreed to in writing, software
15: * distributed under the License is distributed on an "AS IS" BASIS,
16: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: * See the License for the specific language governing permissions and
18: * limitations under the License.
19: *
20: **********************************************************************************/package org.sakaiproject.metaobj.utils.ioc;
21:
22: import java.io.IOException;
23: import java.util.Collection;
24: import java.util.Enumeration;
25: import java.util.Properties;
26: import java.util.SortedMap;
27: import java.util.TreeMap;
28:
29: import org.apache.commons.logging.Log;
30: import org.apache.commons.logging.LogFactory;
31: import org.springframework.context.ApplicationContext;
32: import org.springframework.context.support.ClassPathXmlApplicationContext;
33:
34: /**
35: * <p>Loads the ApplicationContext from xml files listed in a property file.
36: * The xml files referenced in the properties should be in the classpath.
37: * The format of the property file should be as follows:</p>
38: * <p/>
39: * <br>1=help-config.xml
40: * <br>2=help-web-config.xml
41: * <br>3=home-config.xml
42: * <br>...
43: * <p/>
44: * <p>The numbering controls the loading order of the files. This
45: * allows for overriding of beans configured in earlier xml definition files.
46: * Without the numbering we can not gaurantee what order the files will load. </p>
47: */
48: public class ApplicationContextFactory {
49: private Log logger = LogFactory.getLog(this .getClass());
50: private static ApplicationContextFactory factory = new ApplicationContextFactory();
51:
52: private ApplicationContextFactory() {
53: }
54:
55: public static ApplicationContextFactory getInstance() {
56: return factory;
57: }
58:
59: /**
60: * @param properties
61: * @return
62: */
63: public ApplicationContext createContext(Properties properties) {
64: try {
65: return new ClassPathXmlApplicationContext(
66: getConfigLocations(properties));
67: } catch (Exception e) {
68: throw new RuntimeException(
69: "problem loading ApplicationContext: "
70: + e.getMessage(), e);
71: }
72: }
73:
74: public String[] getConfigLocations(Properties props)
75: throws IOException {
76: SortedMap configFiles = new TreeMap();
77: for (Enumeration e = props.keys(); e.hasMoreElements();) {
78: Integer key = new Integer((String) e.nextElement());
79: String curFile = props.getProperty(key.toString());
80: curFile = (!curFile.startsWith("/")) ? "/" + curFile
81: : curFile;
82: configFiles.put(key, curFile);
83: logger.info("registering '" + curFile + "' in position "
84: + key + " as spring bean definition file");
85: }
86: return convertToArray(configFiles.values());
87: }
88:
89: protected String[] convertToArray(Collection collection) {
90: return (String[]) collection.toArray(new String[collection
91: .size()]);
92: }
93: }
|