Merge 1806 code of vid-common
[vid.git] / epsdk-app-onap / src / main / java / org / onap / portalapp / conf / ExternalAppConfig.java
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.conf;
39
40 import java.util.ArrayList;
41 import java.util.List;
42
43 import javax.sql.DataSource;
44
45 import org.onap.portalapp.login.LoginStrategyImpl;
46 import org.onap.portalapp.scheduler.RegistryAdapter;
47 import org.onap.portalsdk.core.auth.LoginStrategy;
48 import org.onap.portalsdk.core.conf.AppConfig;
49 import org.onap.portalsdk.core.conf.Configurable;
50 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
51 import org.onap.portalsdk.core.objectcache.AbstractCacheManager;
52 import org.onap.portalsdk.core.service.DataAccessService;
53 import org.onap.portalsdk.core.util.CacheManager;
54 import org.onap.portalsdk.core.util.SystemProperties;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.beans.factory.annotation.Value;
57 import org.springframework.context.annotation.Bean;
58 import org.springframework.context.annotation.ComponentScan;
59 import org.springframework.context.annotation.Configuration;
60 import org.springframework.context.annotation.Import;
61 import org.springframework.context.annotation.Profile;
62 import org.springframework.context.annotation.PropertySource;
63 import org.springframework.scheduling.annotation.EnableAsync;
64 import org.springframework.scheduling.annotation.EnableScheduling;
65 import org.springframework.scheduling.quartz.SchedulerFactoryBean;
66 import org.springframework.scheduling.quartz.SpringBeanJobFactory;
67 import org.springframework.web.multipart.commons.CommonsMultipartResolver;
68 import org.springframework.web.servlet.ViewResolver;
69 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
70 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
71 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
72 import org.springframework.core.io.Resource;
73 import org.springframework.jdbc.datasource.init.DataSourceInitializer;
74 import org.springframework.jdbc.datasource.init.DatabasePopulator;
75 import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
76
77 /**
78  * ONAP Portal SDK sample application. Extends core AppConfig class to
79  * reuse interceptors, view resolvers and other features defined there.
80  */
81 @Configuration
82 @EnableWebMvc
83 @ComponentScan(basePackages = {"org.onap"})
84 @PropertySource(value = { "${container.classpath:}/WEB-INF/conf/app/test.properties" }, ignoreResourceNotFound = true)
85 @Profile("src")
86 @EnableAsync
87 @EnableScheduling
88 public class ExternalAppConfig extends AppConfig implements Configurable {
89
90         private RegistryAdapter schedulerRegistryAdapter;
91     /** The Constant LOG. */
92     private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(ExternalAppConfig.class);
93  
94     /** The vid schema script. */
95     @Value("classpath:vid-schema.sql")
96     private Resource vidSchemaScript;
97
98     /** The vid data script. */
99     @Value("classpath:vid-data.sql")
100     private Resource vidDataScript;
101     
102     /**
103      * The Class InnerConfiguration.
104      */
105         @Configuration
106         @Import(SystemProperties.class)
107         static class InnerConfiguration {
108         }
109
110         /**
111          * @see org.onap.portalsdk.core.conf.AppConfig#viewResolver()
112          */
113         @Override
114         public ViewResolver viewResolver() {
115                 return super.viewResolver();
116         }
117
118         /**
119          * @see org.onap.portalsdk.core.conf.AppConfig#addResourceHandlers(ResourceHandlerRegistry)
120          * 
121          * @param registry
122          */
123         @Override
124         public void addResourceHandlers(ResourceHandlerRegistry registry) {
125                 super.addResourceHandlers(registry);
126         }
127
128         /**
129          * @see org.onap.portalsdk.core.conf.AppConfig#dataAccessService()
130          */
131         @Override
132         public DataAccessService dataAccessService() {
133                 // Echo the JDBC URL to assist developers when starting the app.
134                 System.out.println("ExternalAppConfig: " + SystemProperties.DB_CONNECTIONURL + " is "
135                                 + SystemProperties.getProperty(SystemProperties.DB_CONNECTIONURL));
136                 return super.dataAccessService();
137         }
138
139         /**
140          * Creates a new list with a single entry that is the external app
141          * definitions.xml path.
142          * 
143          * @return List of String, size 1
144          */
145         @Override
146         public List<String> addTileDefinitions() {
147                 List<String> definitions = new ArrayList<>();
148                 definitions.add("/WEB-INF/defs/definitions.xml");
149                 return definitions;
150         }
151
152         /**
153          * Adds request interceptors to the specified registry by calling
154          * {@link AppConfig#addInterceptors(InterceptorRegistry)}, but excludes
155          * certain paths from the session timeout interceptor.
156          */
157         @Override
158         public void addInterceptors(InterceptorRegistry registry) {
159                 super.setExcludeUrlPathsForSessionTimeout("/login_external", "*/login_external.htm", "login", "/login.htm",
160                                 "/api*", "/single_signon.htm", "/single_signon");
161                 super.addInterceptors(registry);
162         }
163
164         /**
165          * Creates and returns a new instance of a {@link CacheManager} class.
166          * 
167          * @return New instance of {@link CacheManager}
168          */
169         @Bean
170         public AbstractCacheManager cacheManager() {
171                 return new CacheManager();
172         }
173
174         /**
175          * Creates and returns a new instance of a {@link SchedulerFactoryBean} and
176          * populates it with triggers.
177          * 
178          * @return New instance of {@link SchedulerFactoryBean}
179          * @throws Exception
180          */
181         @Bean // ANNOTATION COMMENTED OUT
182         // APPLICATIONS REQUIRING QUARTZ SHOULD RESTORE ANNOTATION
183         public SchedulerFactoryBean schedulerFactoryBean() throws Exception {
184                 SchedulerFactoryBean scheduler = new SchedulerFactoryBean();
185                 scheduler.setTriggers(schedulerRegistryAdapter.getTriggers());
186                 scheduler.setConfigLocation(appApplicationContext.getResource("WEB-INF/conf/quartz.properties"));
187                 scheduler.setDataSource(dataSource());
188             scheduler.setJobFactory(new SpringBeanJobFactory());
189                 return scheduler;
190         }
191
192         
193         /**
194      * Data source initializer.
195      *
196      * @param dataSource the data source
197      * @return the data source initializer
198      */
199     @Bean
200     public DataSourceInitializer dataSourceInitializer(DataSource dataSource) {
201         
202         LOG.info("Initializing VID data source");
203         
204         final DataSourceInitializer initializer = new DataSourceInitializer();
205         initializer.setDataSource(dataSource);
206         initializer.setDatabasePopulator(databasePopulator());
207         return initializer;
208     }
209     
210     /**
211      * Database populator.
212      *
213      * @return the database populator
214      */
215     public DatabasePopulator databasePopulator() {
216         LOG.info("Populating VID data source");
217         
218         final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
219         populator.addScript(vidSchemaScript);
220         populator.addScript(vidDataScript);
221         return populator;
222     }
223         
224
225         /*@Bean
226         public SpringLiquibase liquibaseBean(DataSource dataSource) {
227                 SpringLiquibase springLiquibase = new SpringLiquibase();
228                 springLiquibase.setDataSource(dataSource);
229                 springLiquibase.setChangeLog("classpath:db-master-changelog.xml");
230                 return springLiquibase;
231         }*/
232         
233         /**
234          * Sets the scheduler registry adapter.
235          * 
236          * @param schedulerRegistryAdapter
237          */
238         @Autowired
239         public void setSchedulerRegistryAdapter(final RegistryAdapter schedulerRegistryAdapter) {
240                 this.schedulerRegistryAdapter = schedulerRegistryAdapter;
241         }
242
243         @Bean
244         public LoginStrategy loginStrategy() {
245                 return new LoginStrategyImpl();
246         }
247
248         @Bean
249         public CommonsMultipartResolver multipartResolver() {
250                 CommonsMultipartResolver resolver=new CommonsMultipartResolver();
251                 resolver.setDefaultEncoding("utf-8");
252                 return resolver;
253         }
254
255 }