2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.sli.plugins.prop;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
28 import java.util.Properties;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class PropertiesNode implements SvcLogicJavaPlugin {
38 private static final Logger log = LoggerFactory.getLogger(PropertiesNode.class);
40 public void readProperties(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
41 String fileName = parseParam(paramMap, "fileName", true, null);
42 String contextPrefix = parseParam(paramMap, "contextPrefix", false, null);
45 Properties pp = new Properties();
46 InputStream in = new FileInputStream(fileName);
48 for (Object key : pp.keySet()) {
49 String pfx = contextPrefix != null ? contextPrefix + '.' : "";
50 String name = (String) key;
51 String value = pp.getProperty(name);
52 if (value != null && value.trim().length() > 0) {
53 ctx.setAttribute(pfx + name, value.trim());
54 log.info("+++ " + pfx + name + ": [" + value + "]");
57 } catch (IOException e) {
58 throw new SvcLogicException("Cannot read property file: " + fileName + ": " + e.getMessage(), e);
62 private String parseParam(Map<String, String> paramMap, String name, boolean required, String def)
63 throws SvcLogicException {
64 String s = paramMap.get(name);
66 if (s == null || s.trim().length() == 0) {
69 throw new SvcLogicException("Parameter " + name + " is required in PropertiesNode");
75 int i1 = s.indexOf('%');
77 int i2 = s.indexOf('%', i1 + 1);
79 throw new SvcLogicException("Cannot parse parameter " + name + ": " + s + ": no matching %");
81 String varName = s.substring(i1 + 1, i2);
82 String varValue = System.getenv(varName);
86 value += s.substring(i, i1);
90 i1 = s.indexOf('%', i);
92 value += s.substring(i);
94 log.info("Parameter " + name + ": " + value);