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