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  package ch.elca.el4j.tests.core;
18  
19  import java.util.Arrays;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import org.springframework.context.ConfigurableApplicationContext;
24  
25  import ch.elca.el4j.core.context.ModuleApplicationContext;
26  
27  /**
28   * This class implements a cache for {@link ModuleApplicationContext}s.
29   *
30   * @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/test/java/ch/elca/el4j/tests/core/ModuleTestContextCache.java $
31   *
32   * @author Stefan Wismer (SWI)
33   */
34  public final class ModuleTestContextCache {
35  	/**
36  	 * The actual cache.
37  	 */
38  	private static Map<Configuration, ConfigurableApplicationContext> s_cache;
39  	
40  	
41  	/**
42  	 * The hidden constructor.
43  	 */
44  	private ModuleTestContextCache() {
45  		throw new AssertionError();
46  	}
47  	
48  	/**
49  	 * @param config    a {@link ModuleApplicationContext} configuration
50  	 * @return          the corresponding {@link ModuleApplicationContext}
51  	 */
52  	public static ConfigurableApplicationContext get(ModuleTestContextConfiguration config) {
53  		return get(new Configuration(config));
54  	}
55  	
56  	/**
57  	 * @param inclusiveConfigLocations         the string array with inclusive locations
58  	 * @param exclusiveConfigLocations         the string array with exclusive locations
59  	 * @param allowBeanDefinitionOverriding    <code>true</code> if bean definition overriding should be allowed
60  	 * @return                                 the corresponding {@link ModuleApplicationContext}
61  	 */
62  	public static ConfigurableApplicationContext get(String[] inclusiveConfigLocations,
63  			String[] exclusiveConfigLocations, boolean allowBeanDefinitionOverriding) {
64  		
65  		return get(new Configuration(inclusiveConfigLocations, exclusiveConfigLocations,
66  			allowBeanDefinitionOverriding));
67  	}
68  	
69  	/**
70  	 * Clear this ModuleApplicationContext cache.
71  	 */
72  	public static void clear() {
73  		if (s_cache != null) {
74  			for (ConfigurableApplicationContext context : s_cache.values()) {
75  				context.close();
76  			}
77  			s_cache.clear();
78  		}
79  	}
80  	
81  	/**
82  	 * @param config    a {@link ModuleApplicationContext} configuration
83  	 * @return          the corresponding {@link ModuleApplicationContext}
84  	 */
85  	private static synchronized ConfigurableApplicationContext get(Configuration config) {
86  		if (s_cache == null) {
87  			s_cache = new HashMap<Configuration, ConfigurableApplicationContext>();
88  		}
89  		if (s_cache.containsKey(config)) {
90  			ConfigurableApplicationContext loadedContext = s_cache.get(config);
91  			// refresh context if necessary
92  			if (!loadedContext.isActive()) {
93  				loadedContext.refresh();
94  			}
95  			return loadedContext;
96  		} else {
97  			ConfigurableApplicationContext newContext = new ModuleApplicationContext(
98  				config.getInclusiveConfigLocations(), config.getExclusiveConfigLocations(),
99  				config.isBeanOverridingAllowed(), (ConfigurableApplicationContext) null);
100 			
101 			s_cache.put(config, newContext);
102 			return newContext;
103 		}
104 	}
105 	
106 	/**
107 	 * Data holder for {@link ModuleApplicationContext} configurations that can be used as key in {@link Map}s.
108 	 */
109 	private static class Configuration implements ModuleTestContextConfiguration {
110 		private final String[] m_inclusiveConfigLocations;
111 		private final String[] m_exclusiveConfigLocations;
112 		private final boolean m_allowBeanDefinitionOverriding;
113 		
114 		Configuration(String[] inclusiveConfigLocations, String[] exclusiveConfigLocations,
115 			boolean allowBeanDefinitionOverriding) {
116 			
117 			m_inclusiveConfigLocations = inclusiveConfigLocations;
118 			m_exclusiveConfigLocations = exclusiveConfigLocations;
119 			m_allowBeanDefinitionOverriding = allowBeanDefinitionOverriding;
120 		}
121 		
122 		Configuration(ModuleTestContextConfiguration config) {
123 			m_inclusiveConfigLocations = config.getInclusiveConfigLocations();
124 			m_exclusiveConfigLocations = config.getExclusiveConfigLocations();
125 			m_allowBeanDefinitionOverriding = config.isBeanOverridingAllowed();
126 		}
127 		
128 		/** {@inheritDoc} */
129 		public String[] getInclusiveConfigLocations() {
130 			return m_inclusiveConfigLocations;
131 		}
132 		
133 		/** {@inheritDoc} */
134 		public String[] getExclusiveConfigLocations() {
135 			return m_exclusiveConfigLocations;
136 		}
137 		
138 		/** {@inheritDoc} */
139 		public boolean isBeanOverridingAllowed() {
140 			return m_allowBeanDefinitionOverriding;
141 		}
142 
143 		/** {@inheritDoc} */
144 		@Override
145 		public int hashCode() {
146 			final int prime = 31;
147 			int result = 1;
148 			result = prime * result
149 				+ (m_allowBeanDefinitionOverriding ? 1231 : 1237);
150 			result = prime * result
151 				+ Arrays.hashCode(m_exclusiveConfigLocations);
152 			result = prime * result
153 				+ Arrays.hashCode(m_inclusiveConfigLocations);
154 			return result;
155 		}
156 
157 		/** {@inheritDoc} */
158 		@Override
159 		public boolean equals(Object obj) {
160 			if (this == obj)
161 				return true;
162 			if (obj == null)
163 				return false;
164 			if (getClass() != obj.getClass())
165 				return false;
166 			final Configuration other = (Configuration) obj;
167 			if (m_allowBeanDefinitionOverriding != other.m_allowBeanDefinitionOverriding)
168 				return false;
169 			if (!Arrays.equals(m_exclusiveConfigLocations,
170 				other.m_exclusiveConfigLocations))
171 				return false;
172 			if (!Arrays.equals(m_inclusiveConfigLocations,
173 				other.m_inclusiveConfigLocations))
174 				return false;
175 			return true;
176 		}
177 	}
178 }