View Javadoc
1   /**
2    * Copyright 2005-2016 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 com.google.common.collect.Lists;
21  import org.kuali.common.util.execute.Executable;
22  import org.kuali.common.util.properties.spring.EnvironmentPropertySourceConfig;
23  import org.kuali.common.util.runonce.smart.RunOnce;
24  import org.kuali.common.util.runonce.smart.RunOnceExecutable;
25  import org.kuali.common.util.spring.SpringExecUtils;
26  import org.kuali.common.util.spring.event.ApplicationEventListenerConfig;
27  import org.kuali.common.util.spring.event.ExecutableApplicationEventListener;
28  import org.kuali.common.util.spring.service.SpringService;
29  import org.kuali.common.util.spring.service.SpringServiceConfig;
30  import org.kuali.rice.core.api.CoreConstants;
31  import org.kuali.rice.core.api.config.property.ConfigContext;
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      private static final String ORDER_KEY = "rice.ingest.order";
52      private static final String NAMESPACE_KEY = "rice.ingest.param.namespace";
53      private static final String COMPONENT_KEY = "rice.ingest.param.component";
54      private static final String NAME_KEY = "rice.ingest.param.name";
55      private static final String DESCRIPTION_KEY = "rice.ingest.param.description";
56      private static final String RUN_ON_MISSING_PARAMETER_KEY = "rice.ingest.runOnMissingParameter";
57  
58      private static final String NAMESPACE = "KR-WKFLW";
59      private static final String COMPONENT = "All";
60      private static final String NAME = "INGEST_XML_AT_STARTUP_IND";
61      private static final String DESCRIPTION = "Set this to 'Y' to ingest XML documents at application startup";
62      private static final Boolean RUN_ON_MISSING_PARAMETER = Boolean.FALSE;
63  
64      /**
65       * The Spring loader.
66       */
67      @Autowired
68      SpringService service;
69  
70      /**
71       * The final list of properties.
72       */
73      @Autowired
74      PropertySource<?> propertySource;
75  
76      /**
77       * {@inheritDoc}
78       *
79       * <p>
80       * The {@link org.kuali.rice.coreservice.api.parameter.Parameter} properties need to come from the
81       * {@link ConfigContext} instead of the {@link org.kuali.common.util.spring.env.EnvironmentService} for the scenario
82       * where nobody has wired in a bootstrap PSC in order to help manage the resetting of the database via RunOnce.
83       * </p>
84       */
85  	@Override
86  	@Bean
87  	public SmartApplicationListener applicationEventListener() {
88  		Properties properties = ConfigContext.getCurrentContextConfig().getProperties();
89  		String applicationId = properties.getProperty(CoreConstants.Config.APPLICATION_ID);
90  		String namespace = properties.getProperty(NAMESPACE_KEY, NAMESPACE);
91  		String component = properties.getProperty(COMPONENT_KEY, COMPONENT);
92  		String name = properties.getProperty(NAME_KEY, NAME);
93  		String description = properties.getProperty(DESCRIPTION_KEY, DESCRIPTION);
94  		boolean runOnMissingParameter = Boolean.parseBoolean(properties.getProperty(RUN_ON_MISSING_PARAMETER_KEY, RUN_ON_MISSING_PARAMETER.toString()));
95          int order = Integer.parseInt(properties.getProperty(ORDER_KEY, String.valueOf(Ordered.LOWEST_PRECEDENCE)));
96  
97  		RunOnce runOnce = ParameterServiceRunOnce.builder(applicationId, namespace, component, name)
98                  .description(description).runOnMissingParameter(runOnMissingParameter).build();
99  		Executable springExecutable = SpringExecUtils.getSpringExecutable(service, propertySource, IngestXmlExecConfig.class,
100                 Lists.newArrayList("master"));
101         Executable executable = RunOnceExecutable.builder(springExecutable, runOnce).build();
102 
103 		return ExecutableApplicationEventListener.builder(executable, ContextRefreshedEvent.class).order(order).build();
104 	}
105 
106 }