View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.config;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.config.module.RunMode;
20  import org.kuali.rice.core.api.config.property.ConfigContext;
21  import org.kuali.rice.core.api.lifecycle.Lifecycle;
22  import org.kuali.rice.core.api.resourceloader.ResourceLoader;
23  import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
24  import org.kuali.rice.core.impl.config.module.ModuleConfigurer;
25  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
26  import org.kuali.rice.core.api.resourceloader.ResourceLoader;
27  import org.kuali.rice.core.impl.resourceloader.RiceResourceLoaderFactory;
28  import org.kuali.rice.core.impl.resourceloader.RiceResourceLoaderFactory;
29  import org.kuali.rice.kew.lifecycle.EmbeddedLifeCycle;
30  import org.kuali.rice.kew.plugin.PluginRegistry;
31  import org.kuali.rice.kew.plugin.PluginRegistryFactory;
32  import org.kuali.rice.kew.resourceloader.CoreResourceLoader;
33  import org.kuali.rice.kew.api.KewApiConstants.ClientProtocol;
34  
35  import javax.sql.DataSource;
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.Collections;
39  import java.util.LinkedList;
40  import java.util.List;
41  
42  
43  /**
44   * Configures the KEW Rice module.  KEW module initiation proceeds as follows:
45   *
46   * <ol>
47   *   <li>Parse and load configuration for:</li>
48   *     <ul>
49   *       <li>Client Protocol</li>
50   *       <li>Database</li>
51   *	   </ul>
52   *   </li>
53   *   <li>Configure and startup KEW for "Thin Client" mode OR</li>
54   *   <li>Configure and startup KEW for "Embedded Mode"</li>
55   * </ol>
56   *
57   * @author Kuali Rice Team (rice.collab@kuali.org)
58   */
59  public class KEWConfigurer extends ModuleConfigurer {
60  
61  	public static final String KEW_DATASOURCE_OBJ = "org.kuali.workflow.datasource";
62  
63  	private DataSource dataSource;
64  	
65  	@Override
66  	public List<String> getPrimarySpringFiles() {
67  		final List<String> springFileLocations;
68  		if (RunMode.REMOTE.equals(getRunMode()) || RunMode.THIN.equals(getRunMode()) ||
69  				ClientProtocol.WEBSERVICE.equals(getClientProtocol())) {
70  			springFileLocations = Collections.emptyList();
71  		} else {
72  			springFileLocations = getEmbeddedSpringFileLocation();
73  		}
74  
75  		return springFileLocations;
76  	}
77  	
78      private List<String> getEmbeddedSpringFileLocation(){
79      	final List<String> springFileLocations = new ArrayList<String>();
80      	springFileLocations.add("classpath:org/kuali/rice/kew/config/KEWSpringBeans.xml");
81  
82          if ( isExposeServicesOnBus() ) {
83              springFileLocations.add("classpath:org/kuali/rice/kew/config/KEWServiceBusSpringBeans.xml");
84          }
85          
86          if (OrmUtils.isJpaEnabled("rice.kew")) {
87          	springFileLocations.add("classpath:org/kuali/rice/kew/config/KEWJPASpringBeans.xml");
88          }
89          else {
90          	springFileLocations.add("classpath:org/kuali/rice/kew/config/KEWOJBSpringBeans.xml");
91          }
92  
93      	return springFileLocations;
94      }
95  
96  	@Override
97  	public List<Lifecycle> loadLifecycles() throws Exception {
98  		
99  		List<Lifecycle> lifecycles = new LinkedList<Lifecycle>();
100 		if ( getRunMode().equals( RunMode.THIN ) ) {
101 			lifecycles.add(createThinClientLifecycle());
102 		} else if ( !getRunMode().equals( RunMode.REMOTE ) ) { // local or embedded
103 			lifecycles.add(createEmbeddedLifeCycle());
104 		}
105 		return lifecycles;
106 	}
107 
108 	/**
109 	 * TODO Because a lot of our lifecycles live behind the embedded plugin and the KEWConfigurer does not, this is a simple
110 	 * measure to load these without having to deal with the removal of the embedded plugin right away.
111      * @return Life Cycle
112      * @throws Exception if life cycle not created
113      */
114 	private Lifecycle createEmbeddedLifeCycle() throws Exception {
115 		return new EmbeddedLifeCycle();
116 	}
117 
118 	private Lifecycle createThinClientLifecycle() throws Exception {
119 		return new ThinClientLifecycle();
120 	}
121 
122 	@Override
123 	public void addAdditonalToConfig() {
124 		configureDataSource();
125 	}
126 
127 	private void configureDataSource() {
128 		if (getDataSource() != null) {
129 			ConfigContext.getCurrentContextConfig().putObject(KEW_DATASOURCE_OBJ, getDataSource());
130 		}
131 	}
132 
133 	@Override
134 	public Collection<ResourceLoader> getResourceLoadersToRegister() throws Exception {
135 		// create the plugin registry
136 		PluginRegistry registry = null;
137 		String pluginRegistryEnabled = ConfigContext.getCurrentContextConfig().getProperty("plugin.registry.enabled");
138 		if (!StringUtils.isBlank(pluginRegistryEnabled) && Boolean.valueOf(pluginRegistryEnabled).booleanValue()) {
139 			registry = new PluginRegistryFactory().createPluginRegistry();
140 		}
141 
142 		final Collection<ResourceLoader> rls = new ArrayList<ResourceLoader>();
143 		for (ResourceLoader rl : RiceResourceLoaderFactory.getSpringResourceLoaders()) {
144 			CoreResourceLoader coreResourceLoader = 
145 				new CoreResourceLoader(rl, registry);
146 			coreResourceLoader.start();
147 
148 			//wait until core resource loader is started to attach to GRL;  this is so startup
149 			//code can depend on other things hooked into GRL without incomplete KEW resources
150 			//messing things up.
151 
152 			GlobalResourceLoader.addResourceLoader(coreResourceLoader);
153 
154 			// now start the plugin registry if there is one
155 			if (registry != null) {
156 				registry.start();
157 				// the registry resourceloader is now being handled by the CoreResourceLoader
158 				//GlobalResourceLoader.addResourceLoader(registry);
159 			}
160 			rls.add(coreResourceLoader);
161 		}
162 
163 		return rls;
164 	}
165 
166 	private ClientProtocol getClientProtocol() {
167 		return ClientProtocol.valueOf(ConfigContext.getCurrentContextConfig().getProperty("client.protocol"));
168 	}
169 
170 	public DataSource getDataSource() {
171 		return dataSource;
172 	}
173 
174 	public void setDataSource(DataSource dataSource) {
175 		this.dataSource = dataSource;
176 	}
177 }