0c6fba9a7992b357c7b957d67ade3e2d06b21763
[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.DependsOn;
61 import org.springframework.context.annotation.Import;
62 import org.springframework.context.annotation.Profile;
63 import org.springframework.context.annotation.PropertySource;
64 import org.springframework.scheduling.annotation.EnableAsync;
65 import org.springframework.scheduling.annotation.EnableScheduling;
66 import org.springframework.scheduling.quartz.SchedulerFactoryBean;
67 import org.springframework.scheduling.quartz.SpringBeanJobFactory;
68 import org.springframework.web.multipart.commons.CommonsMultipartResolver;
69 import org.springframework.web.servlet.ViewResolver;
70 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
71 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
72 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
73 import org.springframework.core.io.Resource;
74 import org.springframework.jdbc.datasource.init.DataSourceInitializer;
75 import org.springframework.jdbc.datasource.init.DatabasePopulator;
76 import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
77
78 /**
79  * ONAP Portal SDK sample application. Extends core AppConfig class to
80  * reuse interceptors, view resolvers and other features defined there.
81  */
82 @Configuration
83 @EnableWebMvc
84 @ComponentScan(basePackages = {"org.onap"})
85 @PropertySource(value = { "${container.classpath:}/WEB-INF/conf/app/test.properties" }, ignoreResourceNotFound = true)
86 @Profile("src")
87 @EnableAsync
88 @EnableScheduling
89 public class ExternalAppConfig extends AppConfig implements Configurable {
90
91         private RegistryAdapter schedulerRegistryAdapter;
92     /** The Constant LOG. */
93     private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(ExternalAppConfig.class);
94  
95     /** The vid schema script. */
96     @Value("classpath:vid-schema.sql")
97     private Resource vidSchemaScript;
98
99     /** The vid data script. */
100     @Value("classpath:vid-data.sql")
101     private Resource vidDataScript;
102     
103     /**
104      * The Class InnerConfiguration.
105      */
106         @Configuration
107         @Import(SystemProperties.class)
108         static class InnerConfiguration {
109         }
110
111         /**
112          * @see org.onap.portalsdk.core.conf.AppConfig#viewResolver()
113          */
114         @Override
115         public ViewResolver viewResolver() {
116                 return super.viewResolver();
117         }
118
119         /**
120          * @see org.onap.portalsdk.core.conf.AppConfig#addResourceHandlers(ResourceHandlerRegistry)
121          * 
122          * @param registry
123          */
124         @Override
125         public void addResourceHandlers(ResourceHandlerRegistry registry) {
126                 super.addResourceHandlers(registry);
127         }
128
129         /**
130          * @see org.onap.portalsdk.core.conf.AppConfig#dataAccessService()
131          */
132         @Override
133         public DataAccessService dataAccessService() {
134                 // Echo the JDBC URL to assist developers when starting the app.
135                 System.out.println("ExternalAppConfig: " + SystemProperties.DB_CONNECTIONURL + " is "
136                                 + SystemProperties.getProperty(SystemProperties.DB_CONNECTIONURL));
137                 return super.dataAccessService();
138         }
139
140         /**
141          * Creates a new list with a single entry that is the external app
142          * definitions.xml path.
143          * 
144          * @return List of String, size 1
145          */
146         @Override
147         public List<String> addTileDefinitions() {
148                 List<String> definitions = new ArrayList<>();
149                 definitions.add("/WEB-INF/defs/definitions.xml");
150                 return definitions;
151         }
152
153         /**
154          * Adds request interceptors to the specified registry by calling
155          * {@link AppConfig#addInterceptors(InterceptorRegistry)}, but excludes
156          * certain paths from the session timeout interceptor.
157          */
158         @Override
159         public void addInterceptors(InterceptorRegistry registry) {
160                 super.setExcludeUrlPathsForSessionTimeout("/login_external", "*/login_external.htm", "login", "/login.htm",
161                                 "/api*", "/single_signon.htm", "/single_signon");
162                 super.addInterceptors(registry);
163         }
164
165         /**
166          * Creates and returns a new instance of a {@link CacheManager} class.
167          * 
168          * @return New instance of {@link CacheManager}
169          */
170         @Bean
171         public AbstractCacheManager cacheManager() {
172                 return new CacheManager();
173         }
174
175         /**
176          * Creates and returns a new instance of a {@link SchedulerFactoryBean} and
177          * populates it with triggers.
178          * 
179          * @return New instance of {@link SchedulerFactoryBean}
180          * @throws Exception
181          */
182         @Bean // ANNOTATION COMMENTED OUT
183         // APPLICATIONS REQUIRING QUARTZ SHOULD RESTORE ANNOTATION
184         @DependsOn("dataSourceInitializer")
185         public SchedulerFactoryBean schedulerFactoryBean() throws Exception {
186                 SchedulerFactoryBean scheduler = new SchedulerFactoryBean();
187                 scheduler.setTriggers(schedulerRegistryAdapter.getTriggers());
188                 scheduler.setConfigLocation(appApplicationContext.getResource("WEB-INF/conf/quartz.properties"));
189                 scheduler.setDataSource(dataSource());
190             scheduler.setJobFactory(new SpringBeanJobFactory());
191                 return scheduler;
192         }
193
194         
195         /**
196      * Data source initializer.
197      *
198      * @param dataSource the data source
199      * @return the data source initializer
200      */
201     @Bean
202     public DataSourceInitializer dataSourceInitializer(DataSource dataSource) {
203         
204         LOG.info("Initializing VID data source");
205         
206         final DataSourceInitializer initializer = new DataSourceInitializer();
207         initializer.setDataSource(dataSource);
208         initializer.setDatabasePopulator(databasePopulator());
209         return initializer;
210     }
211     
212     /**
213      * Database populator.
214      *
215      * @return the database populator
216      */
217     public DatabasePopulator databasePopulator() {
218         LOG.info("Populating VID data source");
219         
220         final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
221         populator.addScript(vidSchemaScript);
222         populator.addScript(vidDataScript);
223         return populator;
224     }
225         
226
227         /*@Bean
228         public SpringLiquibase liquibaseBean(DataSource dataSource) {
229                 SpringLiquibase springLiquibase = new SpringLiquibase();
230                 springLiquibase.setDataSource(dataSource);
231                 springLiquibase.setChangeLog("classpath:db-master-changelog.xml");
232                 return springLiquibase;
233         }*/
234         
235         /**
236          * Sets the scheduler registry adapter.
237          * 
238          * @param schedulerRegistryAdapter
239          */
240         @Autowired
241         public void setSchedulerRegistryAdapter(final RegistryAdapter schedulerRegistryAdapter) {
242                 this.schedulerRegistryAdapter = schedulerRegistryAdapter;
243         }
244
245         @Bean
246         public LoginStrategy loginStrategy() {
247                 return new LoginStrategyImpl();
248         }
249
250         @Bean
251         public CommonsMultipartResolver multipartResolver() {
252                 CommonsMultipartResolver resolver=new CommonsMultipartResolver();
253                 resolver.setDefaultEncoding("utf-8");
254                 return resolver;
255         }
256
257 }