11package root .core .repository .implement ;
22
33import java .io .File ;
4+ import java .io .FileWriter ;
45import java .util .ArrayList ;
56import java .util .LinkedHashMap ;
67import java .util .List ;
78import java .util .Map ;
9+ import java .util .regex .Matcher ;
10+ import java .util .regex .Pattern ;
811
912import org .apache .commons .configuration2 .Configuration ;
1013import org .apache .commons .configuration2 .PropertiesConfiguration ;
14+ import org .apache .commons .configuration2 .PropertiesConfiguration .PropertiesWriter ;
15+ import org .apache .commons .configuration2 .PropertiesConfigurationLayout ;
1116
17+ import lombok .extern .slf4j .Slf4j ;
18+ import root .core .domain .JdbcConnectionInfo ;
19+ import root .core .domain .JschConnectionInfo ;
1220import root .core .repository .constracts .PropertyRepository ;
1321import root .utils .PropertiesUtils ;
1422
23+ @ Slf4j
1524public class PropertyRepositoryImpl implements PropertyRepository {
1625
1726 // Private 필드로 선언 후 Singletone으로 관리
@@ -25,6 +34,9 @@ public static PropertyRepository getInstance() {
2534 return propertyService ;
2635 }
2736
37+ private static Pattern dbPropPattern = Pattern .compile ("(.*).jdbc.(.*)" );
38+ private static Pattern serverPropPattern = Pattern .compile ("(.*).server.(.*)" );
39+
2840 /****************************************************************************/
2941
3042 /**
@@ -45,6 +57,136 @@ public void save(String filePath, PropertiesConfiguration config) {
4557 PropertiesUtils .save (filePath , config );
4658 }
4759
60+ @ Override
61+ public void saveDBConnectionInfo (String filePath , Map <String , JdbcConnectionInfo > dbConfig ) {
62+ PropertiesConfiguration config = PropertiesUtils .connInfoConfig ;
63+
64+ // TODO dbnames property..
65+ String dbNames = "" ;
66+ for (String dbName : dbConfig .keySet ()) {
67+ dbNames += dbName + "," ;
68+
69+ JdbcConnectionInfo jdbc = dbConfig .get (dbName );
70+ config .setProperty (dbName + ".jdbc.alias" , jdbc .getJdbcDBName ());
71+ config .setProperty (dbName + ".jdbc.id" , jdbc .getJdbcId ());
72+ config .setProperty (dbName + ".jdbc.pw" , jdbc .getJdbcPw ());
73+ config .setProperty (dbName + ".jdbc.url" , jdbc .getJdbcUrl ());
74+ config .setProperty (dbName + ".jdbc.driver" , jdbc .getJdbcOracleDriver ());
75+ config .setProperty (dbName + ".jdbc.validation" , jdbc .getJdbcValidation ());
76+ config .setProperty (dbName + ".jdbc.connections" , jdbc .getJdbcConnections ());
77+ }
78+
79+ config .setProperty ("dbnames" , dbNames .substring (0 , dbNames .length ()-1 ));
80+
81+ PropertiesConfigurationLayout layout = config .getLayout ();
82+ try {
83+ PropertiesWriter writer = config .getIOFactory ()
84+ .createPropertiesWriter (new FileWriter (filePath , false ), config .getListDelimiterHandler ());
85+
86+ // Write Header Comment
87+ writer .writeln (layout .getHeaderComment ());
88+
89+ for (final String key : layout .getKeys ()) {
90+ Matcher m = dbPropPattern .matcher (key );
91+ if (m .matches ()) {
92+ String dbName = m .group (1 );
93+ if (!dbConfig .containsKey (dbName )) {
94+ continue ;
95+ }
96+ }
97+
98+ // Output blank lines before property
99+ for (int i = 0 ; i < layout .getBlancLinesBefore (key ); i ++) {
100+ writer .writeln (null );
101+ }
102+
103+ // Output the property and its value
104+ boolean singleLine = layout .isForceSingleLine () || layout .isSingleLine (key );
105+ writer .setCurrentSeparator (layout .getSeparator (key ));
106+ writer .writeProperty (key , config .getProperty (key ), singleLine );
107+ }
108+
109+ writer .writeln (layout .getCanonicalFooterCooment (true ));
110+ writer .flush ();
111+
112+ log .info ("[" + filePath + "] 파일 저장이 성공적으로 완료되었습니다." );
113+ } catch (Exception e ) {
114+ e .printStackTrace ();
115+ log .error ("[" + filePath + "] 파일 저장에 실패했습니다." );
116+ }
117+ }
118+
119+ @ Override
120+ public void saveServerConnectionInfo (String filePath , Map <String , JschConnectionInfo > serverConfig ) {
121+ PropertiesConfiguration config = PropertiesUtils .connInfoConfig ;
122+
123+ // TODO servernames property..
124+ String serverNames = "" ;
125+ for (String serverName : serverConfig .keySet ()) {
126+ serverNames += serverName + "," ;
127+
128+ JschConnectionInfo jsch = serverConfig .get (serverName );
129+ config .setProperty (serverName + ".server.servername" , jsch .getServerName ());
130+ config .setProperty (serverName + ".server.host" , jsch .getHost ());
131+ config .setProperty (serverName + ".server.port" , jsch .getPort ());
132+ config .setProperty (serverName + ".server.username" , jsch .getUserName ());
133+ config .setProperty (serverName + ".server.password" , jsch .getPassword ());
134+
135+ String dateFormat = jsch .getAlc ().getDateFormat ();
136+ String dateFormatRegex = "" ;
137+
138+ if (dateFormat .equals ("EEE MMM dd HH:mm:ss yyyy" )) {
139+ dateFormatRegex = "...\\ s...\\ s([0-2][0-9]|1[012])\\ s\\ d\\ d:\\ d\\ d:\\ d\\ d\\ s\\ d{4}" ;
140+ } else if (dateFormat .equals ("yyyy-MM-dd" )) {
141+ dateFormatRegex = "\\ d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T" ;
142+ }
143+
144+ config .setProperty (serverName + ".server.alertlog.dateformat" , dateFormat );
145+ config .setProperty (serverName + ".server.alertlog.dateformatregex" , dateFormatRegex );
146+ config .setProperty (serverName + ".server.alertlog.filepath" , jsch .getAlc ().getReadFilePath ());
147+ config .setProperty (serverName + ".server.alertlog.readLine" , 500 );
148+ }
149+
150+ config .setProperty ("servernames" , serverNames .substring (0 , serverNames .length ()-1 ));
151+
152+ PropertiesConfigurationLayout layout = config .getLayout ();
153+ try {
154+ PropertiesWriter writer = config .getIOFactory ()
155+ .createPropertiesWriter (new FileWriter (filePath , false ), config .getListDelimiterHandler ());
156+
157+ // Write Header Comment
158+ writer .writeln (layout .getHeaderComment ());
159+
160+ for (final String key : layout .getKeys ()) {
161+ Matcher m = serverPropPattern .matcher (key );
162+ if (m .matches ()) {
163+ String serverName = m .group (1 );
164+ if (!serverConfig .containsKey (serverName )) {
165+ continue ;
166+ }
167+ }
168+
169+ // Output blank lines before property
170+ for (int i = 0 ; i < layout .getBlancLinesBefore (key ); i ++) {
171+ writer .writeln (null );
172+ }
173+
174+ // Output the property and its value
175+ boolean singleLine = layout .isForceSingleLine () || layout .isSingleLine (key );
176+ writer .setCurrentSeparator (layout .getSeparator (key ));
177+ writer .writeProperty (key , config .getProperty (key ), singleLine );
178+ }
179+
180+ writer .writeln (layout .getCanonicalFooterCooment (true ));
181+ writer .flush ();
182+
183+ log .info ("[" + filePath + "] 파일 저장이 성공적으로 완료되었습니다." );
184+ } catch (Exception e ) {
185+ e .printStackTrace ();
186+ log .error ("[" + filePath + "] 파일 저장에 실패했습니다." );
187+ }
188+ }
189+
48190 /**
49191 * 접속정보 프로퍼티 파일을 Load한다.
50192 */
0 commit comments