Skip to content

Commit 2990dfa

Browse files
committed
Remove statement that throw 'PropertyNotLoadedException'
1 parent 641b34e commit 2990dfa

File tree

4 files changed

+27
-90
lines changed

4 files changed

+27
-90
lines changed

src/main/java/root/applications/ConsoleApp.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import java.util.List;
99
import java.util.stream.Collectors;
1010

11-
import root.core.domain.exceptions.PropertyNotLoadedException;
11+
import root.core.repository.constracts.PropertyRepository;
12+
import root.core.repository.implement.PropertyRepositoryImpl;
1213
import root.core.service.contracts.PropertyService;
1314
import root.core.service.implement.FilePropertyService;
1415
import root.utils.PatternUtils;
@@ -25,7 +26,7 @@ public class ConsoleApp {
2526

2627
private static PropertyService propService;
2728

28-
public static void main(String[] args) throws IOException, PropertyNotLoadedException {
29+
public static void main(String[] args) throws IOException {
2930
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
3031

3132
// STEP1: 접속정보 설정파일 선택
@@ -64,7 +65,9 @@ public static void main(String[] args) throws IOException, PropertyNotLoadedExce
6465
// STEP2: 선택된 접속정보 설정파일 Load
6566
String propertiesFilePath = DEFAULT_CONFIG_DIR + "/" + selectedFile;
6667
try {
67-
propService = new FilePropertyService(propertiesFilePath);
68+
PropertyRepository propRepo = PropertyRepositoryImpl.getInstance();
69+
propRepo.loadConnectionInfoConfig(propertiesFilePath);
70+
propService = new FilePropertyService(propRepo);
6871
} catch (Exception e) {
6972
System.out.println("configuration loading error\n" + e + "\n");
7073
return;

src/main/java/root/core/repository/implement/PropertyRepositoryImpl.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class PropertyRepositoryImpl implements PropertyRepository {
4545

4646
// 생성자를 Private으로 선언함으로써 해당 객체를 생성할 수 있는 방법을 업애버림 => 안정적인 Singletone 관리방법
4747
private PropertyRepositoryImpl() {
48+
loadCombinedConfiguration();
4849
}
4950

5051
// propertyService Field에 접근할 수 있는 유일한 방법 (Static Factory Pattern)
@@ -81,8 +82,7 @@ public PropertiesConfiguration getConfiguration(String name) {
8182
}
8283

8384
/**
84-
* 주어진 경로에 PropertyConfiguration에 설정된 Key-Value를 저장한다. TODO PropertiesUtils
85-
* 클래스쪽의 메서드 제거 후 여기에서 구현하기 (일원화)
85+
* 주어진 경로에 PropertyConfiguration에 설정된 Key-Value를 저장한다.
8686
*/
8787
@Override
8888
public void save(String filePath, PropertiesConfiguration config) {
@@ -570,16 +570,18 @@ private AlertLogCommand getAlertLogCommand(String serverName) {
570570
alertLogDateFormatRegex);
571571
return alc;
572572
}
573-
573+
574574
/**
575575
* Properties 파일에서 모니터링할 서버명을 읽어온 후, 각 서버별 AlertLogCommand 객체를 생성한다.
576+
*
576577
* @return 각 DB별 JdbcConnectionInfo 객체를 담은 후 DB Name 순으로 정렬한 리스트
577578
*/
578579
@Override
579580
public Map<String, AlertLogCommand> getAlertLogCommandMap() {
580581
String[] serverNames = connInfoConfig.getStringArray("servernames");
581582
Map<String, AlertLogCommand> alcMap = new HashMap<>();
582-
for(String serverName : serverNames) alcMap.put(serverName, getAlertLogCommand(serverName));
583+
for (String serverName : serverNames)
584+
alcMap.put(serverName, getAlertLogCommand(serverName));
583585
return alcMap;
584586
}
585587
}

src/main/java/root/core/service/contracts/PropertyService.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,13 @@
33
import java.util.List;
44
import java.util.Map;
55

6-
import org.apache.commons.configuration2.ex.ConfigurationException;
7-
8-
import root.core.domain.exceptions.PropertyNotLoadedException;
9-
106
public interface PropertyService {
117

12-
void loadAppConfiguration(String filePath) throws ConfigurationException;
13-
14-
boolean isLoaded(String configName);
15-
16-
Map<String, String> getMonitoringPresetMap() throws PropertyNotLoadedException;
8+
Map<String, String> getMonitoringPresetMap();
179

18-
List<String> getMonitoringPresetFilePathList() throws PropertyNotLoadedException;
10+
List<String> getMonitoringPresetFilePathList();
1911

20-
List<String> getMonitoringPresetNameList() throws PropertyNotLoadedException;
12+
List<String> getMonitoringPresetNameList();
2113

22-
String getMonitoringPresetFilePath(String presetName) throws PropertyNotLoadedException;
14+
String getMonitoringPresetFilePath(String presetName);
2315
}
Lines changed: 11 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package root.core.service.implement;
22

3-
import java.io.File;
43
import java.util.ArrayList;
54
import java.util.List;
65
import java.util.Map;
@@ -11,74 +10,25 @@
1110
import java.util.stream.Collectors;
1211
import java.util.stream.StreamSupport;
1312

14-
import org.apache.commons.configuration2.PropertiesConfiguration;
15-
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
16-
import org.apache.commons.configuration2.builder.fluent.Parameters;
17-
import org.apache.commons.configuration2.builder.fluent.PropertiesBuilderParameters;
18-
import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
19-
import org.apache.commons.configuration2.convert.ListDelimiterHandler;
20-
import org.apache.commons.configuration2.ex.ConfigurationException;
21-
22-
import root.core.domain.exceptions.PropertyNotLoadedException;
13+
import root.core.repository.constracts.PropertyRepository;
2314
import root.core.service.contracts.PropertyService;
2415

2516
public class FilePropertyService implements PropertyService {
2617

2718
private static final String MONITORING_PRESET_KEY = "monitoring.setting.preset.(.*).filepath";
2819
private static final Pattern MONITORING_PRESET_KEY_PATTERN = Pattern.compile(MONITORING_PRESET_KEY);
2920

30-
private PropertiesConfiguration connInfoConfig = null; // DB, Server 접속정보 Configuration
31-
32-
public FilePropertyService(String filePath) throws ConfigurationException {
33-
loadAppConfiguration(filePath);
34-
}
35-
36-
/**
37-
* 매개변수로 주어진 경로에 저장된 설정파일을 읽어 [propConfig] PropertiesConfiguration 객체를 초기화한다.
38-
*
39-
* @param filePath
40-
* @throws ConfigurationException
41-
*/
42-
@Override
43-
public void loadAppConfiguration(String filePath) throws ConfigurationException {
44-
File file = new File(filePath);
45-
ListDelimiterHandler delimiter = new DefaultListDelimiterHandler(',');
46-
47-
PropertiesBuilderParameters propertyParameters = new Parameters().properties();
48-
propertyParameters.setFile(file);
49-
propertyParameters.setThrowExceptionOnMissing(true);
50-
propertyParameters.setListDelimiterHandler(delimiter);
51-
52-
FileBasedConfigurationBuilder<PropertiesConfiguration> builder = new FileBasedConfigurationBuilder<>(
53-
PropertiesConfiguration.class);
54-
builder.configure(propertyParameters);
55-
56-
connInfoConfig = builder.getConfiguration();
21+
private PropertyRepository propRepo;
22+
23+
public FilePropertyService(PropertyRepository propRepo) {
24+
this.propRepo = propRepo;
5725
}
5826

5927
@Override
60-
public boolean isLoaded(String configName) {
61-
boolean result = false;
62-
63-
switch (configName) {
64-
case "connInfoConfig":
65-
if (connInfoConfig != null) {
66-
result = true;
67-
}
68-
break;
69-
}
70-
71-
return result;
72-
}
73-
74-
@Override
75-
public Map<String, String> getMonitoringPresetMap() throws PropertyNotLoadedException {
76-
if (!isLoaded("connInfoConfig")) {
77-
throw new PropertyNotLoadedException("connInfoConfig");
78-
}
79-
28+
public Map<String, String> getMonitoringPresetMap() {
8029
return StreamSupport
81-
.stream(Spliterators.spliteratorUnknownSize(connInfoConfig.getKeys(), Spliterator.ORDERED), false)
30+
.stream(Spliterators.spliteratorUnknownSize(propRepo.getConfiguration("connInfoConfig").getKeys(),
31+
Spliterator.ORDERED), false)
8232
.filter(key -> key.matches(MONITORING_PRESET_KEY))
8333
.collect(Collectors.toUnmodifiableMap(key -> {
8434
Matcher m = MONITORING_PRESET_KEY_PATTERN.matcher(key);
@@ -87,27 +37,17 @@ public Map<String, String> getMonitoringPresetMap() throws PropertyNotLoadedExce
8737
}
8838

8939
@Override
90-
public List<String> getMonitoringPresetFilePathList() throws PropertyNotLoadedException {
91-
if (!isLoaded("connInfoConfig")) {
92-
throw new PropertyNotLoadedException("connInfoConfig");
93-
}
94-
40+
public List<String> getMonitoringPresetFilePathList() {
9541
return new ArrayList<>(getMonitoringPresetMap().values());
9642
}
9743

9844
@Override
99-
public List<String> getMonitoringPresetNameList() throws PropertyNotLoadedException {
100-
if (!isLoaded("connInfoConfig")) {
101-
throw new PropertyNotLoadedException("connInfoConfig");
102-
}
45+
public List<String> getMonitoringPresetNameList() {
10346
return new ArrayList<>(getMonitoringPresetMap().keySet());
10447
}
10548

10649
@Override
107-
public String getMonitoringPresetFilePath(String presetName) throws PropertyNotLoadedException {
108-
if (!isLoaded("connInfoConfig")) {
109-
throw new PropertyNotLoadedException("connInfoConfig");
110-
}
50+
public String getMonitoringPresetFilePath(String presetName) {
11151
return getMonitoringPresetMap().get(presetName);
11252
}
11353
}

0 commit comments

Comments
 (0)