93572ee9d4660990abddabd803af65941d63dd13
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * ECOMP Portal SDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
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  * ================================================================================
19  */
20 package org.openecomp.portalsdk.core.onboarding.util;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.Properties;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 /**
30  * Singleton Class representing portal properties. Searches the classpath for
31  * the file "portal.properties".
32  * 
33  * To put the file "portal.properties" on the classpath, it can be in the same
34  * directory where the first package folder is - 'myClasses' folder in the
35  * following case as an example:
36  * 
37  */
38 public class PortalApiProperties {
39
40         private static final Log logger = LogFactory.getLog(PortalApiProperties.class);
41
42         private static Properties properties;
43         private static String propertyFileName = "portal.properties";
44
45         /**
46          * Constructor is private.
47          */
48         private PortalApiProperties() {
49         }
50
51         /**
52          * Gets the property value for the specified key. If a value is found,
53          * leading and trailing space is trimmed.
54          *
55          * @param property
56          * @return Value for the named property; null if the property file was not
57          *         loaded or the key was not found.
58          */
59         public static String getProperty(String property) {
60                 if (properties == null) {
61                         synchronized (propertyFileName) {
62                                 try {
63                                         if (!initialize()) {
64                                                 logger.error("Failed to read property file " + propertyFileName);
65                                                 return null;
66                                         }
67                                 } catch (IOException e) {
68                                         logger.error("Failed to read property file " + propertyFileName, e);
69                                         return null;
70                                 }
71                         }
72                 }
73                 String value = properties.getProperty(property);
74                 if (value != null)
75                         value = value.trim();
76                 return value;
77         }
78
79         /**
80          * Reads properties from a portal.properties file on the classpath.
81          * 
82          * Clients do NOT need to call this method. Clients MAY call this method to
83          * test whether the properties file can be loaded successfully.
84          * 
85          * @return True if properties were successfully loaded, else false.
86          * @throws IOException
87          */
88         public static boolean initialize() throws IOException {
89                 if (properties != null)
90                         return true;
91                 InputStream in = PortalApiProperties.class.getClassLoader().getResourceAsStream(propertyFileName);
92                 if (in == null)
93                         return false;
94                 properties = new Properties();
95                 try {
96                         properties.load(in);
97                 } finally {
98                         in.close();
99                 }
100                 return true;
101         }
102 }