View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.core.config;
18  
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import javax.sql.DataSource;
24  import javax.transaction.TransactionManager;
25  import javax.transaction.UserTransaction;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.apache.log4j.Logger;
29  import org.kuali.rice.core.exception.RiceRuntimeException;
30  import org.kuali.rice.core.lifecycle.Lifecycle;
31  import org.kuali.rice.core.resourceloader.GlobalResourceLoader;
32  import org.kuali.rice.core.security.credentials.CredentialsSourceFactory;
33  import org.kuali.rice.core.util.ClassLoaderUtils;
34  import org.kuali.rice.core.util.RiceConstants;
35  import org.kuali.rice.kcb.config.KCBConfigurer;
36  import org.kuali.rice.ken.config.KENConfigurer;
37  import org.kuali.rice.kew.config.KEWConfigurer;
38  import org.kuali.rice.kim.config.KIMConfigurer;
39  import org.kuali.rice.kns.config.KNSConfigurer;
40  import org.kuali.rice.kns.web.servlet.dwr.GlobalResourceDelegatingSpringCreator;
41  import org.kuali.rice.ksb.messaging.MessageFetcher;
42  import org.kuali.rice.ksb.messaging.config.KSBConfigurer;
43  import org.kuali.rice.ksb.service.KSBServiceLocator;
44  
45  /**
46   * Used to configure common Rice configuration properties.
47   *
48   * @author Kuali Rice Team (rice.collab@kuali.org)
49   *
50   */
51  public class RiceConfigurer extends RiceConfigurerBase {
52  
53  	private static final Logger LOG = Logger.getLogger(RiceConfigurer.class);
54  
55  	private DataSource dataSource;
56  	private DataSource nonTransactionalDataSource;
57  	private DataSource serverDataSource;
58  	private String platform;
59  	private UserTransaction userTransaction;
60  	private TransactionManager transactionManager;
61      private String dataSourceJndiLocation;
62      private String nonTransactionalDataSourceJndiLocation;
63      private String serverDataSourceJndiLocation;
64  	private String userTransactionJndiLocation;
65  	private String transactionManagerJndiLocation;
66  	private CredentialsSourceFactory credentialsSourceFactory;
67  	
68  	private KSBConfigurer ksbConfigurer;
69  	private KNSConfigurer knsConfigurer;
70  	private KIMConfigurer kimConfigurer;
71  	private KCBConfigurer kcbConfigurer;
72  	private KEWConfigurer kewConfigurer;
73  	private KENConfigurer kenConfigurer;
74  		
75  	/***
76  	 * @see org.kuali.rice.core.lifecycle.BaseCompositeLifecycle#start()
77  	 */
78  	public void start() throws Exception {
79  		//Add the configurers to modules list in the desired sequence.
80  		// and at the beginning if any other modules were specified
81  
82  		int index = 0;
83  		if(getKsbConfigurer()!=null) getModules().add(index++,getKsbConfigurer());
84  		if(getKnsConfigurer()!=null) getModules().add(index++,getKnsConfigurer());
85  		if(getKimConfigurer()!=null) getModules().add(index++,getKimConfigurer());
86  		if(getKcbConfigurer()!=null) getModules().add(index++,getKcbConfigurer());
87  		if(getKewConfigurer()!=null) getModules().add(index++,getKewConfigurer());
88  		if(getKenConfigurer()!=null) getModules().add(index++,getKenConfigurer());
89  		// now execute the super class's start method which will initialize configuration and resource loaders
90  		super.start();
91  
92  		
93  		//automatically requeue documents sitting with status of 'R'
94  		MessageFetcher messageFetcher = new MessageFetcher((Integer) null);
95  		KSBServiceLocator.getThreadPool().execute(messageFetcher);
96  
97  	}
98  	
99  	/**
100 	 * 
101 	 * This method decides the sequence of module resource loaders to be added to global resource loader (GRL).
102 	 * It asks the individual module configurers for the resource loader they want to register and adds them to GRL.
103 	 * 
104 	 * <p>TODO: the implementation of this method seems like a total HACK, it seems like the implementation on
105 	 * RiceConfigurerBase makes more sense since it is more general, also, very strange how the
106 	 * getResourceLoaderToRegister method on KEWConfigurer is side-affecting.  This whole thing looks like a mess.
107 	 * Somebody untangle this, please!
108 	 * 
109 	 * @throws Exception
110 	 */
111 	@Override
112 	protected void addModulesResourceLoaders() throws Exception {
113 		if(getKewConfigurer()!=null){
114 			// TODO: Check - In the method getResourceLoaderToRegister of KewConfigurer, 
115 			// does the call registry.start() depend on the preceding line GlobalResourceLoader.addResourceLoader(coreResourceLoader)?
116 			// Ideally we would like to register the resource loader into GRL over here
117 			getKewConfigurer().getResourceLoaderToRegister();
118 		}
119 		if(getKsbConfigurer()!=null){
120 			GlobalResourceLoader.addResourceLoader(getKsbConfigurer().getResourceLoaderToRegister());
121 		}
122 	}
123 
124 
125 	/***
126 	 * @see org.kuali.rice.core.lifecycle.BaseCompositeLifecycle#loadLifecycles()
127 	 */
128 	protected List<Lifecycle> loadLifecycles() throws Exception {
129 		 GlobalResourceDelegatingSpringCreator.APPLICATION_BEAN_FACTORY = getBeanFactory();
130 		 return super.loadLifecycles();
131 	}
132 		    
133 
134 	@SuppressWarnings("unchecked")
135 	@Override
136 	protected void initializeBaseConfiguration(Config currentConfig) throws Exception {
137 		super.initializeBaseConfiguration(currentConfig);
138 		configureJta(currentConfig);
139 		configureDataSource(currentConfig);
140 		configurePlatform(currentConfig);
141 		configureCredentialsSourceFactory(currentConfig);
142 	}
143 
144 	protected void configureCredentialsSourceFactory(final Config rootConfig) {
145 		if (credentialsSourceFactory != null) {
146 			rootConfig.putObject(Config.CREDENTIALS_SOURCE_FACTORY, this.credentialsSourceFactory);
147 		}
148 		
149 	}
150 
151 	protected void configurePlatform(Config config) {
152 		if (!StringUtils.isBlank(this.platform)) {
153 			String platformClassName = "org.kuali.rice.core.database.platform."+this.platform+"DatabasePlatform";
154 			config.putProperty(Config.DATASOURCE_PLATFORM, platformClassName);
155 			config.putProperty(Config.OJB_PLATFORM, this.platform);
156 		}
157 	}
158  
159 	protected void configureDataSource(Config config) {
160 		if (this.dataSource != null) {
161 			config.putObject(RiceConstants.DATASOURCE_OBJ, this.dataSource);
162 		} else if (!StringUtils.isBlank(this.dataSourceJndiLocation)) {
163 			config.putProperty(RiceConstants.DATASOURCE_JNDI, this.dataSourceJndiLocation);
164 		}
165         if (this.nonTransactionalDataSource != null) {
166             config.putObject(RiceConstants.NON_TRANSACTIONAL_DATASOURCE_OBJ, this.nonTransactionalDataSource);
167         } else if (!StringUtils.isBlank(this.nonTransactionalDataSourceJndiLocation)) {
168             config.putProperty(RiceConstants.NON_TRANSACTIONAL_DATASOURCE_JNDI, this.nonTransactionalDataSourceJndiLocation);
169         }
170         if (this.serverDataSource != null) {
171         	config.putObject(RiceConstants.SERVER_DATASOURCE_OBJ, this.serverDataSource);
172         }  else if (!StringUtils.isBlank(this.serverDataSourceJndiLocation)) {
173         	config.putProperty(RiceConstants.SERVER_DATASOURCE_JNDI, this.serverDataSourceJndiLocation);
174         }
175 	}
176 
177 	/**
178 	 * If the user injected JTA classes into this configurer, verify that both the
179 	 * UserTransaction and TransactionManager are set and then attach them to
180 	 * the configuration.
181 	 */
182 	protected void configureJta(Config config) {
183 		if (this.userTransaction != null) {
184 			config.putObject(RiceConstants.USER_TRANSACTION_OBJ, this.userTransaction);
185 		}
186 		if (this.transactionManager != null) {
187 			config.putObject(RiceConstants.TRANSACTION_MANAGER_OBJ, this.transactionManager);
188 		}
189 		if (!StringUtils.isEmpty(this.userTransactionJndiLocation)) {
190 			config.putProperty(RiceConstants.USER_TRANSACTION_JNDI, this.userTransactionJndiLocation);
191 		}
192 		if (!StringUtils.isEmpty(this.transactionManagerJndiLocation)) {
193 			config.putProperty(RiceConstants.TRANSACTION_MANAGER_JNDI, this.transactionManagerJndiLocation);
194 		}
195 		boolean userTransactionConfigured = this.userTransaction != null || !StringUtils.isEmpty(this.userTransactionJndiLocation);
196 		boolean transactionManagerConfigured = this.transactionManager != null || !StringUtils.isEmpty(this.transactionManagerJndiLocation);
197 		if (userTransactionConfigured && !transactionManagerConfigured) {
198 			throw new ConfigurationException("When configuring JTA, both a UserTransaction and a TransactionManager are required.  Only the UserTransaction was configured.");
199 		}
200 		if (transactionManagerConfigured && !userTransactionConfigured) {
201 			throw new ConfigurationException("When configuring JTA, both a UserTransaction and a TransactionManager are required.  Only the TransactionManager was configured.");
202 		}
203 	}
204 
205 	public DataSource getDataSource() {
206 		return this.dataSource;
207 	}
208 
209 	public void setDataSource(DataSource dataSource) {
210 		this.dataSource = dataSource;
211 	}
212 
213     public DataSource getNonTransactionalDataSource() {
214         return this.nonTransactionalDataSource;
215     }
216 
217     public void setNonTransactionalDataSource(DataSource nonTransactionalDataSource) {
218         this.nonTransactionalDataSource = nonTransactionalDataSource;
219     }
220 
221     public DataSource getServerDataSource() {
222 		return this.serverDataSource;
223 	}
224 
225 	public void setServerDataSource(DataSource serverDataSource) {
226 		this.serverDataSource = serverDataSource;
227 	}
228 
229 	public String getPlatform() {
230 		return this.platform;
231 	}
232 
233 	public void setPlatform(String platform) {
234 		this.platform = platform;
235 	}
236 
237 	public TransactionManager getTransactionManager() {
238 		return this.transactionManager;
239 	}
240 
241 	public void setTransactionManager(TransactionManager transactionManager) {
242 		this.transactionManager = transactionManager;
243 	}
244 
245 	public UserTransaction getUserTransaction() {
246 		return this.userTransaction;
247 	}
248 
249 	public void setUserTransaction(UserTransaction userTransaction) {
250 		this.userTransaction = userTransaction;
251 	}
252 
253 	public void setDataSourceJndiLocation(String dataSourceJndiLocation) {
254 		this.dataSourceJndiLocation = dataSourceJndiLocation;
255 	}
256 
257     public void setNonTransactionalDataSourceJndiLocation(String nonTransactionalDataSourceJndiLocation) {
258         this.nonTransactionalDataSourceJndiLocation = nonTransactionalDataSourceJndiLocation;
259     }
260 
261 	public void setServerDataSourceJndiLocation(String serverDataSourceJndiLocation) {
262 		this.serverDataSourceJndiLocation = serverDataSourceJndiLocation;
263 	}
264 
265 	public String getTransactionManagerJndiLocation() {
266 		return this.transactionManagerJndiLocation;
267 	}
268 
269     public void setTransactionManagerJndiLocation(String transactionManagerJndiLocation) {
270 		this.transactionManagerJndiLocation = transactionManagerJndiLocation;
271 	}
272 
273 	public String getUserTransactionJndiLocation() {
274 		return this.userTransactionJndiLocation;
275 	}
276 
277 	public void setUserTransactionJndiLocation(String userTransactionJndiLocation) {
278 		this.userTransactionJndiLocation = userTransactionJndiLocation;
279 	}
280 
281 	public CredentialsSourceFactory getCredentialsSourceFactory() {
282 		return credentialsSourceFactory;
283 	}
284 
285 	public void setCredentialsSourceFactory(
286 			final CredentialsSourceFactory credentialsSourceFactory) {
287 		this.credentialsSourceFactory = credentialsSourceFactory;
288 	}
289 
290 	/**
291 	 * @return the kcbConfigurer
292 	 */
293 	public KCBConfigurer getKcbConfigurer() {
294 		return this.kcbConfigurer;
295 	}
296 
297 	/**
298 	 * @param kcbConfigurer the kcbConfigurer to set
299 	 */
300 	public void setKcbConfigurer(KCBConfigurer kcbConfigurer) {
301 		this.kcbConfigurer = kcbConfigurer;
302 	}
303 
304 	/**
305 	 * @return the kenConfigurer
306 	 */
307 	public KENConfigurer getKenConfigurer() {
308 		return this.kenConfigurer;
309 	}
310 
311 	/**
312 	 * @param kenConfigurer the kenConfigurer to set
313 	 */
314 	public void setKenConfigurer(KENConfigurer kenConfigurer) {
315 		this.kenConfigurer = kenConfigurer;
316 	}
317 
318 	/**
319 	 * @return the kewConfigurer
320 	 */
321 	public KEWConfigurer getKewConfigurer() {
322 		return this.kewConfigurer;
323 	}
324 
325 	/**
326 	 * @param kewConfigurer the kewConfigurer to set
327 	 */
328 	public void setKewConfigurer(KEWConfigurer kewConfigurer) {
329 		this.kewConfigurer = kewConfigurer;
330 	}
331 
332 	/**
333 	 * @return the kimConfigurer
334 	 */
335 	public KIMConfigurer getKimConfigurer() {
336 		return this.kimConfigurer;
337 	}
338 
339 	/**
340 	 * @param kimConfigurer the kimConfigurer to set
341 	 */
342 	public void setKimConfigurer(KIMConfigurer kimConfigurer) {
343 		this.kimConfigurer = kimConfigurer;
344 	}
345 
346 	/**
347 	 * @return the knsConfigurer
348 	 */
349 	public KNSConfigurer getKnsConfigurer() {
350 		return this.knsConfigurer;
351 	}
352 
353 	/**
354 	 * @param knsConfigurer the knsConfigurer to set
355 	 */
356 	public void setKnsConfigurer(KNSConfigurer knsConfigurer) {
357 		this.knsConfigurer = knsConfigurer;
358 	}
359 
360 	/**
361 	 * @return the ksbConfigurer
362 	 */
363 	public KSBConfigurer getKsbConfigurer() {
364 		return this.ksbConfigurer;
365 	}
366 
367 	/**
368 	 * @param ksbConfigurer the ksbConfigurer to set
369 	 */
370 	public void setKsbConfigurer(KSBConfigurer ksbConfigurer) {
371 		this.ksbConfigurer = ksbConfigurer;
372 	}
373 
374 }