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.net.URL;
22  import java.util.ArrayList;
23  import java.util.Enumeration;
24  import java.util.List;
25  import java.util.StringTokenizer;
26  
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  import org.springframework.core.io.ClassPathResource;
30  import org.springframework.core.io.Resource;
31  import org.springframework.util.ClassUtils;
32  import org.springframework.util.StringUtils;
33  
34  import ch.elca.el4j.core.io.ExplicitClassPathResource;
35  
36  /**
37   * This class represents a simplified view of a module (EL4Ant build system
38   * unit). It contains its name, the configuration files and the dependencies.
39   *
40   * @svnLink $Revision: 3881 $;$Date: 2009-08-04 15:22:05 +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/Module.java $
41   *
42   * @author Andreas Bur (ABU)
43   */
44  public class Module {
45  	
46  	/** The delimiter used to separate configuration files and dependencies. */
47  	public static final String DELIMITER = ",";
48  	
49  	/** Private logger. */
50  	private static Logger s_logger = LoggerFactory.getLogger(Module.class);
51  	
52  	/**
53  	 * Is the used class loader.
54  	 */
55  	protected final ClassLoader m_classLoader;
56  	
57  	/**
58  	 * The module's name.
59  	 */
60  	private final String m_name;
61  	
62  	/**
63  	 * The module's location.
64  	 */
65  	private final String m_moduleLocation;
66  	
67  	/**
68  	 * The module's configuration files.
69  	 */
70  	private List<String> m_configFiles;
71  	
72  	/**
73  	 * The module's config file resources.
74  	 * Like {@link #m_configFiles} but with resolved resources.
75  	 */
76  	private List<Resource> m_configFileResources;
77  	
78  	/**
79  	 * The module's dependencies.
80  	 */
81  	private List<String> m_dependencies;
82  	
83  	/**
84  	 * Creates a new module with the given name.
85  	 *
86  	 * @param name
87  	 *      The module's name.
88  	 */
89  	public Module(String name) {
90  		this(name, null, null);
91  	}
92  	
93  	/**
94  	 * Creates a new module with the given name and the module's location.
95  	 *
96  	 * @param name The module's name.
97  	 * @param moduleLocation The module's location.
98  	 */
99  	public Module(String name, String moduleLocation) {
100 		this(name, moduleLocation, null);
101 	}
102 	
103 	/**
104 	 * Creates a new module with the given name, the module's location
105 	 * and resolves resources by using the given class loader.
106 	 *
107 	 * @param name The module's name.
108 	 * @param moduleLocation The module's location.
109 	 * @param classLoader The class loader to resolve resources.
110 	 */
111 	public Module(String name, String moduleLocation, ClassLoader classLoader) {
112 		m_name = name;
113 		m_moduleLocation = StringUtils.hasText(moduleLocation)
114 			? moduleLocation : null;
115 		m_configFiles = new ArrayList<String>();
116 		m_configFileResources = new ArrayList<Resource>();
117 		m_dependencies = new ArrayList<String>();
118 		m_classLoader = classLoader != null
119 			? classLoader : ClassUtils.getDefaultClassLoader();
120 	}
121 	
122 	/**
123 	 * Adds all items -- separated by {@link #DELIMITER} -- to the configuration
124 	 * file list.
125 	 *
126 	 * @param configFiles
127 	 *      The list of configuration files to add.
128 	 */
129 	public void addAllConfigFiles(String configFiles) {
130 		StringTokenizer tokenizer = new StringTokenizer(configFiles, DELIMITER);
131 		while (tokenizer.hasMoreTokens()) {
132 			addConfigFile(tokenizer.nextToken());
133 		}
134 	}
135 	
136 	/**
137 	 * Adds the given configuration file to the module.
138 	 *
139 	 * @param configFile
140 	 *      The configuration file to add.
141 	 */
142 	public void addConfigFile(String configFile) {
143 		m_configFiles.add(configFile);
144 		m_configFileResources.add(getConfigFileAsResource(configFile));
145 	}
146 	
147 	/**
148 	 * Adds all item -- separated by {@link #DELIMITER} -- to the dependency
149 	 * list.
150 	 *
151 	 * @param dependencies
152 	 *      The list of dependencies to add.
153 	 */
154 	public void addAllDependencies(String dependencies) {
155 		StringTokenizer tokenizer = new StringTokenizer(
156 				dependencies, DELIMITER);
157 		while (tokenizer.hasMoreTokens()) {
158 			addDependency(tokenizer.nextToken());
159 		}
160 	}
161 	
162 	/**
163 	 * Adds the given dependency to the module.
164 	 *
165 	 * @param dependency
166 	 *      The dependency to add.
167 	 */
168 	public void addDependency(String dependency) {
169 		m_dependencies.add(dependency);
170 	}
171 	
172 	/**
173 	 * @return Returns all configuration files.
174 	 */
175 	public String[] getConfigFiles() {
176 		return (String[]) m_configFiles.toArray(
177 				new String[m_configFiles.size()]);
178 	}
179 	
180 	/**
181 	 * @return Returns all dependencies.
182 	 */
183 	public String[] getDependencies() {
184 		return (String[]) m_dependencies.toArray(
185 				new String[m_dependencies.size()]);
186 	}
187 	
188 	/**
189 	 * @return Returns the configuration file list.
190 	 */
191 	public List<String> getConfigFilesAsList() {
192 		return new ArrayList<String>(m_configFiles);
193 	}
194 	
195 	/**
196 	 * @return Returns the dependency list.
197 	 */
198 	public List<String> getDependenciesAsList() {
199 		return new ArrayList<String>(m_dependencies);
200 	}
201 	
202 	/**
203 	 * @return Returns the module's name.
204 	 */
205 	public String getName() {
206 		return m_name;
207 	}
208 	
209 	/**
210 	 * @return Returns the module's location.
211 	 */
212 	public String getModuleLocation() {
213 		return m_moduleLocation;
214 	}
215 	
216 	/**
217 	 * @return Returns the configuration files resolved as resources in a list.
218 	 */
219 	public List<Resource> getConfigFileResourcesAsList() {
220 		return new ArrayList<Resource>(m_configFileResources);
221 	}
222 	
223 	/**
224 	 * Converts the given config file in a resource that takes place in
225 	 * the current module.
226 	 *
227 	 * @param configFile The config file to convert.
228 	 * @return Returns the converted resource.
229 	 */
230 	public Resource getConfigFileAsResource(String configFile) {
231 		Resource resource = null;
232 		if (m_moduleLocation == null) {
233 			resource = new ClassPathResource(configFile);
234 		} else {
235 			try {
236 				Enumeration<URL> resources
237 					= m_classLoader.getResources(configFile);
238 				while (resources.hasMoreElements() && resource == null) {
239 					URL url = resources.nextElement();
240 					String urlString = url.getFile();
241 					if (urlString.startsWith(m_moduleLocation)) {
242 						resource = new ExplicitClassPathResource(
243 							url, configFile, m_classLoader);
244 					}
245 				}
246 			} catch (IOException e) {
247 				resource = null;
248 			}
249 			if (resource == null) {
250 				s_logger.warn("Config file '" + configFile
251 					+ "' could not be resolved with current class loader.");
252 				resource = new ClassPathResource(configFile);
253 			}
254 		}
255 		return resource;
256 	}
257 
258 	/**
259 	 * {@inheritDoc}
260 	 */
261 	public String toString() {
262 		if (m_moduleLocation != null) {
263 			return "Module '" + m_name + "' at '" + m_moduleLocation + "'";
264 		} else {
265 			return "Module '" + m_name + "'";
266 		}
267 	}
268 }