aboutsummaryrefslogtreecommitdiffstats
path: root/ncomp-utils-java/src/main/java/org/openecomp/ncomp/utils/PropertyUtil.java
blob: c5e0bdde6424adf014c6fa4d2a9ddbb56ae265cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*-
 * ============LICENSE_START==========================================
 * OPENECOMP - DCAE
 * ===================================================================
 * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
 * ===================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 */
	
package org.openecomp.ncomp.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.URL;
import java.util.Properties;

import org.apache.log4j.Logger;



public class PropertyUtil { 
	public static final Logger logger = Logger.getLogger(PropertyUtil.class);

	static public Properties getPropertiesFromClasspath(String propFileName) throws IOException {
		Properties props = new Properties();
		if (propFileName == null) return props;

		InputStream inputStream = null;
		
		File f = new File(propFileName);
		if (f.isAbsolute()) {
			logger.info("Loading absolute " + f);
			inputStream = new FileInputStream(f);
		} else {
			// loading xmlProfileGen.properties from the classpath
			URL url = PropertyUtil.class.getClassLoader().getResource(propFileName);
			if (url == null) {
				throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath (null url)");
			}
			logger.info("Loading " + url);
			inputStream = PropertyUtil.class.getClassLoader().getResourceAsStream(propFileName);
			if (inputStream == null) {
				throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
			}
		}
		try {
			props.load(inputStream);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			if (inputStream != null)
				inputStream.close();
		}

		for (Object k : System.getProperties().keySet()) {
			Object v = props.get(p(k));
			if (v != null) {
				logger.info("Overwriting property from system property: " + p(k) + " = " + p(v));
			}
			props.put(k, System.getProperties().get(k));
		}
		String hostname = SecurityUtils.getHostName();
		props.put("user.hostname", hostname);
		return props;
	}

	private static String p(Object v) {
		return SecurityUtils.logForcingProtection(v);
	}

}