View Javadoc
1   /**
2    * Copyright 2005-2014 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.xml.spring;
17  
18  import java.util.Properties;
19  
20  import org.kuali.common.util.execute.Executable;
21  import org.kuali.common.util.properties.spring.EnvironmentPropertySourceConfig;
22  import org.kuali.common.util.runonce.smart.RunOnce;
23  import org.kuali.common.util.runonce.smart.RunOnceExecutable;
24  import org.kuali.common.util.spring.SpringExecUtils;
25  import org.kuali.common.util.spring.event.ApplicationEventListenerConfig;
26  import org.kuali.common.util.spring.event.ExecutableApplicationEventListener;
27  import org.kuali.common.util.spring.service.SpringService;
28  import org.kuali.common.util.spring.service.SpringServiceConfig;
29  import org.kuali.rice.core.api.CoreConstants;
30  import org.kuali.rice.core.api.config.property.ConfigContext;
31  import org.kuali.rice.sql.spring.SourceSqlExecConfig;
32  import org.kuali.rice.xml.ingest.ParameterServiceRunOnce;
33  import org.springframework.beans.factory.annotation.Autowired;
34  import org.springframework.context.annotation.Bean;
35  import org.springframework.context.annotation.Configuration;
36  import org.springframework.context.annotation.Import;
37  import org.springframework.context.event.ContextRefreshedEvent;
38  import org.springframework.context.event.SmartApplicationListener;
39  import org.springframework.core.Ordered;
40  import org.springframework.core.env.PropertySource;
41  
42  /**
43   * Set up an {@code SmartApplicationListener} that ingests XML when it receives a {@code ContextRefreshedEvent}.
44   * 
45   * @author Kuali Rice Team (rice.collab@kuali.org)
46   */
47  @Configuration
48  @Import({ SpringServiceConfig.class, EnvironmentPropertySourceConfig.class })
49  public class IngestXmlRunOnceConfig implements ApplicationEventListenerConfig {
50  
51      @Autowired
52      SpringService service;
53  
54      @Autowired
55      PropertySource<?> propertySource;
56  
57  	private static final String ORDER_KEY = "rice.ingest.order";
58  	private static final String NAMESPACE_KEY = "rice.ingest.param.namespace";
59  	private static final String COMPONENT_KEY = "rice.ingest.param.component";
60  	private static final String NAME_KEY = "rice.ingest.param.name";
61  	private static final String DESCRIPTION_KEY = "rice.ingest.param.description";
62  	private static final String RUN_ON_MISSING_PARAMETER_KEY = "rice.ingest.runOnMissingParameter";
63  
64  	private static final String NAMESPACE = "KR-WKFLW";
65  	private static final String COMPONENT = "All";
66  	private static final String NAME = "INGEST_XML_AT_STARTUP_IND";
67  	private static final String DESCRIPTION = "Set this to 'Y' to ingest XML documents at application startup";
68  	private static final Boolean RUN_ON_MISSING_PARAMETER = Boolean.FALSE;
69  
70  	@Override
71  	@Bean
72  	public SmartApplicationListener applicationEventListener() {
73  		// This needs to come from ConfigContext instead of EnvironmentService for the scenario where nobody
74  		// has wired in a bootstrap PSC in order to help manage the ingestion of XML at startup via RunOnce
75  		Properties properties = ConfigContext.getCurrentContextConfig().getProperties();
76  		String applicationId = properties.getProperty(CoreConstants.Config.APPLICATION_ID);
77  		String namespace = properties.getProperty(NAMESPACE_KEY, NAMESPACE);
78  		String component = properties.getProperty(COMPONENT_KEY, COMPONENT);
79  		String name = properties.getProperty(NAME_KEY, NAME);
80  		String description = properties.getProperty(DESCRIPTION_KEY, DESCRIPTION);
81  		boolean runOnMissingParameter = Boolean.parseBoolean(properties.getProperty(RUN_ON_MISSING_PARAMETER_KEY, RUN_ON_MISSING_PARAMETER.toString()));
82  
83  		RunOnce runOnce = ParameterServiceRunOnce.builder().applicationId(applicationId).namespace(namespace).component(component).name(name).description(description)
84  				.runOnMissingParameter(runOnMissingParameter).build();
85  		Executable springExecutable = SpringExecUtils.getSpringExecutable(service, propertySource, IngestXmlExecConfig.class);
86          Executable executable = RunOnceExecutable.builder(springExecutable, runOnce).build();
87  
88  		// Setup the application event listener
89  		int order = Integer.parseInt(properties.getProperty(ORDER_KEY, Ordered.LOWEST_PRECEDENCE + ""));
90  		return ExecutableApplicationEventListener.builder(executable, ContextRefreshedEvent.class).order(order).build();
91  	}
92  }