티스토리 뷰
반응형
외부에 있는 json 파일을 읽어서
특정 위치의 특정 값만 바꾸어 값을 넘겨주는 작업이 필요해짐에 따라
아래 내용을 수행했다.
먼저, FileUtils 자바 파일을 생성하여 파일을 읽어오거나 삭제할 수 있는 메소드를 작성한다.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.core.io.ClassPathResource;
public class FileUtils {
private static final Logger log = LogManager.getLogger(FileUtils.class);
private FileUtils() {
}
public static String randomFilename() {
return String.format("rnd_%d", System.nanoTime());
}
public static boolean exists(String filepath) {
File f = new File(filepath);
return f.isFile();
}
public static boolean saveStringToFile(String str, String filepath) {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(filepath));
writer.write(str);
} catch (IOException var12) {
log.error("", var12);
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException var11) {
log.info(var11);
}
}
return true;
}
public static String readfile(String filepath) {
Path path = Paths.get(filepath);
try {
return new String(Files.readAllBytes(path));
} catch (IOException var3) {
log.error("", var3);
return null;
}
}
public static String readResource(String resourceName) {
ClassPathResource resource = new ClassPathResource(resourceName);
try {
StringBuilder textBuilder = new StringBuilder();
Reader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), Charset.forName(StandardCharsets.UTF_8.name())));
Throwable var4 = null;
try {
boolean var5 = false;
int c;
while((c = reader.read()) != -1) {
textBuilder.append((char)c);
}
} catch (Throwable var14) {
var4 = var14;
throw var14;
} finally {
if (reader != null) {
if (var4 != null) {
try {
reader.close();
} catch (Throwable var13) {
var4.addSuppressed(var13);
}
} else {
reader.close();
}
}
}
return textBuilder.toString();
} catch (IOException var16) {
log.error("", var16);
return null;
}
}
public static boolean deleteFile(String filepath) {
File file = new File(filepath);
return file.delete();
}
public static boolean mkdir(String path) {
File file = new File(path);
return file.mkdirs();
}
public static boolean rmdir(String path) {
File filePath = new File(path);
File[] allContents = filePath.listFiles();
if (allContents != null) {
File[] var3 = allContents;
int var4 = allContents.length;
for(int var5 = 0; var5 < var4; ++var5) {
File file = var3[var5];
rmdir(file.toString());
}
}
return filePath.delete();
}
}
필요한 json 파일을 resources 디렉터리 안에 생성해준다.
{
"coreThing" : {
"caPath" : "root.ca.pem",
"certPath" : "{{CERT_ID}}.cert.pem",
"keyPath" : "{{CERT_ID}}.private.key",
"thingArn" : "{{IOT_ARN}}:thing/{{CORE_NAME}}",
"iotHost" : "{{ENDPOINT}}",
"ggHost" : "greengrass-ats.iot.ap-northeast-2.amazonaws.com",
"keepAlive" : 600
},
"runtime" : {
"cgroup" : {
"useSystemd" : "yes"
}
},
"managedRespawn" : false,
"crypto" : {
"principals" : {
"SecretsManager" : {
"privateKeyPath" : "file:///greengrass/certs/{{CERT_ID}}.private.key"
},
"IoTCertificate" : {
"privateKeyPath" : "file:///greengrass/certs/{{CERT_ID}}.private.key",
"certificatePath" : "file:///greengrass/certs/{{CERT_ID}}.cert.pem"
}
},
"caPath" : "file:///greengrass/certs/root.ca.pem"
}
}
특정 부분의 값을 대체한 후 리턴해주는 메소드를 생성해준다.
public class GreengrassSourceBuilder {
public String makeCorePolicyDocument(String iotArn, String groupId, String coreName) {
String document = FileUtils.readResource("json/core-policy.json");
document = document.replace("{{IOT_ARN}}", iotArn);
document = document.replace("{{GROUP_ID}}", groupId);
document = document.replace("{{CORE_NAME}}", coreName);
return document;
}
public String makeDevicePolicyDocument(String iotArn, String groupId, String deviceName) {
String document = FileUtils.readResource("json/core-policy.json");
document = document.replace("{{IOT_ARN}}", iotArn);
document = document.replace("{{GROUP_ID}}", groupId);
document = document.replace("{{DEVICE_NAME}}", deviceName);
return document;
}
public String makeCoreConfigFile(String endpoint, String iotArn, String certId, String coreName) {
String document = FileUtils.readResource("json/core-config.json");
document = document.replace("{{ENDPOINT}}", endpoint);
document = document.replace("{{IOT_ARN}}", iotArn);
document = document.replace("{{CERT_ID}}", certId);
document = document.replace("{{CORE_NAME}}", coreName);
return document;
}
}
반응형
'Java' 카테고리의 다른 글
[Java] Enum class (0) | 2021.02.10 |
---|---|
[Java] @Builder (0) | 2021.02.10 |
[Java] Collection - Iterator (0) | 2021.02.10 |
[Java] 정규표현식 Pattern, Matcher (0) | 2021.02.10 |
[Java] Object의 Field 값 얻는 방법 (0) | 2021.02.10 |