2 * Copyright 2016-2017, Nokia Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.common.util;
19 import java.io.BufferedInputStream;
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.io.InputStream;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public final class CommonUtil {
30 private static final Logger logger = LoggerFactory.getLogger(CommonUtil.class);
32 public static String getJsonStrFromFile(String filePath) throws IOException {
33 InputStream ins = null;
34 BufferedInputStream bins = null;
35 String fileContent = "";
36 String fileName = getAppRoot() + filePath;
39 ins = new FileInputStream(fileName);
40 bins = new BufferedInputStream(ins);
42 byte[] contentByte = new byte[ins.available()];
43 int num = bins.read(contentByte);
46 fileContent = new String(contentByte);
48 } catch(FileNotFoundException e) {
49 logger.error(fileName + "is not found!", e);
61 private static String getAppRoot() {
62 String appRoot = System.getProperty("catalina.base");
64 appRoot = getCanonicalPath(appRoot);
69 private static String getCanonicalPath(final String inPath) {
73 final File file = new File(inPath);
74 path = file.getCanonicalPath();
76 } catch(final IOException e) {
77 logger.error("file.getCanonicalPath() IOException:", e);