Change the header to SO
[so.git] / common / src / main / java / org / openecomp / mso / properties / MsoPropertyInitializer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.properties;
22
23
24 import javax.servlet.ServletContextEvent;
25 import javax.servlet.ServletContextListener;
26 import javax.servlet.annotation.WebListener;
27
28 import org.openecomp.mso.logger.MessageEnum;
29 import org.openecomp.mso.logger.MsoLogger;
30
31 /**
32  * This class will attempt to initialize MSO Properties when part of a web application.
33  * It will look for the configuration file mso.properties in the
34  * following order:
35  * 1. In an init-param "mso.configuration" in web.xml
36  * 2. In a system property "mso.configuration"
37  * 3. In the default location "/etc/ecomp/mso/config/mso.properties"
38  *
39  * If all else fails, the MSO Properties will go uninitialized, and will
40  * attempt to use the default constructors within the MsoProperties class.
41  *
42  *
43  */
44 @WebListener
45 public class MsoPropertyInitializer implements ServletContextListener
46 {
47
48         private MsoPropertiesFactory msoPropertiesFactory=new MsoPropertiesFactory();
49
50         public MsoPropertyInitializer () {
51         }
52
53         @Override
54         public void contextDestroyed(ServletContextEvent event) {
55                 // Nothing to do...
56         }
57
58
59         @Override
60         public void contextInitialized(ServletContextEvent event)
61         {
62
63                 // Note - this logger may be before or after MSO Logging configuration applied
64                 MsoLogger initLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL);
65                 try {
66                         // Look first in the init-parameters
67                         String msoPropConfigParam = event.getServletContext().getInitParameter("mso.configuration");
68                         if (msoPropConfigParam != null && !msoPropConfigParam.isEmpty() ) {
69                                 String[] configFileSplit = msoPropConfigParam.split(",");
70                                 for (String msoPropConfig:configFileSplit) {
71                                         String[] msoPropDecoded = msoPropConfig.split("=");
72         
73                                         try {
74                                                 msoPropertiesFactory.initializeMsoProperties(msoPropDecoded[0], msoPropDecoded[1]);
75                                                 initLogger.info(MessageEnum.LOAD_PROPERTIES_SUC, msoPropDecoded[1], "", "");
76                                                 initLogger.debug("Mso properties successfully loaded:"+msoPropDecoded[1]+",ID:"+msoPropDecoded[0]+")");
77                                         } catch (MsoPropertiesException e) {
78                                                 initLogger.error(MessageEnum.LOAD_PROPERTIES_FAIL, msoPropDecoded[1] + ". MSO Properties failed due to an mso properties exception", "", "", MsoLogger.ErrorCode.DataError, "Error in contextInitialized", e);
79                                         }
80                                 }
81                         }
82                 }
83                 catch (Exception e) {
84                         initLogger.error(MessageEnum.LOAD_PROPERTIES_FAIL, "Unknown. MSO Properties failed to initialize completely", "", "", MsoLogger.ErrorCode.DataError, "", e);
85                 }
86         }
87 }