View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  
18  package ch.elca.el4j.core.io.support;
19  
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.commons.lang.ArrayUtils;
25  import org.springframework.core.io.Resource;
26  
27  /**
28   * This class simplifies writing ordered configuration location providers.
29   *
30   * @svnLink $Revision: 3873 $;$Date: 2009-08-04 13:59:45 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/core/io/support/AbstractOrderedConfigLocationProvider.java $
31   *
32   * @author Andreas Bur (ABU)
33   */
34  public abstract class AbstractOrderedConfigLocationProvider
35  	implements ConfigLocationProvider {
36  
37  	/**
38  	 * The module sorter that computes the list of modules preserving their
39  	 * hierarchical constraints.
40  	 */
41  	private ModuleSorter m_moduleSorter = new DefaultModuleSorter();
42  
43  	/**
44  	 * The sorted list of modules.
45  	 */
46  	private Module[] m_sortedModules;
47  
48  	/**
49  	 * Sets the module sorter used to compute the list of modules while
50  	 * preserving their hierarchical constraints. Default sorter is a
51  	 * {@link DefaultModuleSorter}.
52  	 *
53  	 * @param moduleSorter
54  	 *      The module sorter to use.
55  	 *
56  	 * @see ModuleSorter
57  	 */
58  	public void setModuleSorter(ModuleSorter moduleSorter) {
59  		m_moduleSorter = moduleSorter;
60  	}
61  
62  	/**
63  	 * @return Returns the sorted list of modules.
64  	 * @throws IOException On any io problem while working with modules.
65  	 */
66  	protected Module[] getSortedModules() throws IOException {
67  		if (m_sortedModules == null) {
68  			Module[] modules = createModules();
69  			m_sortedModules = sortModules(modules);
70  		}
71  		return m_sortedModules;
72  	}
73  	
74  	/**
75  	 * @return Returns the created list of modules.
76  	 * @throws IOException On any io problem while module creation.
77  	 */
78  	protected abstract Module[] createModules() throws IOException;
79  	
80  	/**
81  	 * Sorts an unordered list of modules using the hierarchical constraints
82  	 * defined across the modules.
83  	 *
84  	 * @param modules
85  	 *      The list of modules to sort.
86  	 *
87  	 * @return Returns an ordered list of modules that fulfill the partial
88  	 *      order defined by the module's hierarchical constraints.
89  	 */
90  	protected Module[] sortModules(Module[] modules) {
91  		return m_moduleSorter.sortModules(modules);
92  	}
93  	
94  	/**
95  	 * Merges the configuration locations of the provided list of modules,
96  	 * preserving the module's order.
97  	 *
98  	 * @param modules
99  	 *      The ordered list of modules which configuration locations has to be
100 	 *      merged.
101 	 *
102 	 * @return Returns the ordered list of configuration locations declared
103 	 *      by the modules. Most specific location comes first.
104 	 */
105 	protected String[] mergeConfigLocations(Module[] modules) {
106 		List<String> configLocations = new ArrayList<String>();
107 		for (int i = 0; i < modules.length; i++) {
108 			configLocations.addAll(modules[i].getConfigFilesAsList());
109 		}
110 		
111 		String[] result = (String[]) configLocations.toArray(
112 				new String[configLocations.size()]);
113 		ArrayUtils.reverse(result);
114 		return result;
115 	}
116 	
117 	/**
118 	 * Merges the configuration location resources of the provided list of
119 	 * modules, preserving the module's order.
120 	 *
121 	 * @param modules
122 	 *      The ordered list of modules which configuration location resources
123 	 *      has to be merged.
124 	 *
125 	 * @return Returns the ordered list of configuration location resources
126 	 *      declared by the modules. Most specific location comes first.
127 	 */
128 	protected Resource[] mergeConfigLocationResources(Module[] modules) {
129 		List<Resource> configLocationResources = new ArrayList<Resource>();
130 		for (Module module : modules) {
131 			configLocationResources.addAll(
132 				module.getConfigFileResourcesAsList());
133 		}
134 		Resource[] result = (Resource[]) configLocationResources.toArray(
135 				new Resource[configLocationResources.size()]);
136 		ArrayUtils.reverse(result);
137 		return result;
138 	}
139 }