87d4c1fdd5a8ee894dccc0848e6efa23857b3e01
[portal/sdk.git] /
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright (C) 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  * 
37  */
38 package org.onap.portalsdk.external.authorization.util;
39
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.util.Properties;
43
44 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
45
46
47 public class EcompExternalAuthProperties {
48
49         public static final String EXTERNAL_AUTH_USER_NAME = "extern_auth_user_name";
50
51         public static final String EXTERNAL_AUTH_PASSWORD = "extern_auth_password";
52
53         public static final String EXTERNAL_AUTH_URL = "extern_auth_url";
54
55         public static final String EXTERNAL_AUTH_USER_DOMAIN = "extern_auth_user_domain";
56         
57         public static final String EXTERNAL_AUTH_NAMESPACE = "extern_auth_namespace";
58
59         private EcompExternalAuthProperties(){}
60         
61         private static Properties properties;
62         
63         private static String propertyFileName = "external-auth.properties";
64         
65         private static final Object lockObject = new Object();
66         
67         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EcompExternalAuthProperties.class);
68         
69         /**
70          * Gets the property value for the specified key. If a value is found, leading
71          * and trailing space is trimmed.
72          *
73          * @param property
74          *            Property key
75          * @return Value for the named property; null if the property file was not
76          *         loaded or the key was not found.
77          */
78         public static String getProperty(String property) {
79                 if (properties == null) {
80                         synchronized (lockObject) {
81                                 try {
82                                         if (!initialize()) {
83                                                 logger.error(EELFLoggerDelegate.errorLogger,"Failed to read property file " + propertyFileName);
84                                                 return null;
85                                         }
86                                 } catch (IOException e) {
87                                         logger.error(EELFLoggerDelegate.errorLogger,"Failed to read property file " + propertyFileName ,e);
88                                         return null;
89                                 }
90                         }
91                 }
92                 String value = properties.getProperty(property);
93                 if (value != null)
94                         value = value.trim();
95                 return value;
96         }
97         
98         /**
99          * Reads properties from a portal.properties file on the classpath.
100          * 
101          * Clients do NOT need to call this method. Clients MAY call this method to test
102          * whether the properties file can be loaded successfully.
103          * 
104          * @return True if properties were successfully loaded, else false.
105          * @throws IOException
106          *             On failure
107          */
108         private static boolean initialize() throws IOException {
109                 if (properties != null)
110                         return true;
111                 InputStream in = EcompExternalAuthProperties.class.getClassLoader().getResourceAsStream(propertyFileName);
112                 if (in == null)
113                         return false;
114                 properties = new Properties();
115                 try {
116                         properties.load(in);
117                 } finally {
118                         in.close();
119                 }
120                 return true;
121         }
122         
123 }