• R/O
  • SSH

JdbcAcsess2: 提交

全てを再実装したJdbcAcsessです。


Commit MetaInfo

修訂8003c5eee50815a9fd774e94deb12c10412440ca (tree)
時間2012-04-20 22:47:33
作者shimakazuro
Commitershimakazuro

Log Message

接続情報の自動バックアップ

Change Summary

差異

diff -r 1e05c669aed3 -r 8003c5eee508 src/jdbcacsess2/connect/ConfigConnect.java
--- a/src/jdbcacsess2/connect/ConfigConnect.java Sat Apr 07 10:11:57 2012 +0900
+++ b/src/jdbcacsess2/connect/ConfigConnect.java Fri Apr 20 22:47:33 2012 +0900
@@ -1,15 +1,15 @@
11 /*
22 * Copyright 2011 Kazuhiro Shimada
33 *
4- * Licensed under the Apache License, Version 2.0 (the "License");
5- * you may not use this file except in compliance with the License.
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
66 * You may obtain a copy of the License at
77 *
8- * http://www.apache.org/licenses/LICENSE-2.0
8+ * http://www.apache.org/licenses/LICENSE-2.0
99 *
10- * Unless required by applicable law or agreed to in writing, software
10+ * Unless required by applicable law or agreed to in writing, software
1111 * distributed under the License is distributed on an "AS IS" BASIS,
12- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
@@ -17,8 +17,11 @@
1717
1818 import java.util.Date;
1919
20+import jdbcacsess2.connect.ConfigConnect.ConfigConnectImpl;
2021 import net.java.ao.Entity;
22+import net.java.ao.Implementation;
2123 import net.java.ao.Preload;
24+import net.java.ao.schema.Ignore;
2225 import net.java.ao.schema.Unique;
2326
2427 /**
@@ -27,6 +30,7 @@
2730 *
2831 */
2932 @Preload
33+@Implementation(ConfigConnectImpl.class)
3034 public interface ConfigConnect extends Entity {
3135
3236 @Unique
@@ -74,6 +78,12 @@
7478
7579 void setLastConnectdate(Date lastConnectdate);
7680
81+ @Ignore
82+ public StringBuilder convertRowheader();
83+
84+ @Ignore
85+ public StringBuilder convertRow();
86+
7787 public static String CONNECTNAME = "connectname";
7888 public static String DBMS = "dbms";
7989 public static String CONNECTUSER = "connectuser";
@@ -86,4 +96,45 @@
8696 public static String URLOPTION = "urloption";
8797 public static String LASTCONNECTDATE = "lastconnectdate";
8898
99+ public class ConfigConnectImpl {
100+ private final ConfigConnect configConnect;
101+
102+ public ConfigConnectImpl(ConfigConnect configConnect) {
103+ this.configConnect = configConnect;
104+ }
105+
106+ public StringBuilder convertRowheader() {
107+ StringBuilder sb = new StringBuilder();
108+ sb.append('"' + CONNECTNAME + '"');
109+ sb.append("," + '"' + DBMS + '"');
110+ sb.append("," + '"' + CONNECTUSER + '"');
111+ sb.append("," + '"' + PASSWORD + '"');
112+ sb.append("," + '"' + HOSTNAME + '"');
113+ sb.append("," + '"' + PORTNUMBER + '"');
114+ sb.append("," + '"' + DATABASENAME + '"');
115+ sb.append("," + '"' + CLASSNAME + '"');
116+ sb.append("," + '"' + DRIVERFILE + '"');
117+ sb.append("," + '"' + URLOPTION + '"');
118+ sb.append("," + '"' + LASTCONNECTDATE + '"');
119+ return sb;
120+ }
121+
122+ public StringBuilder convertRow() {
123+ StringBuilder sb = new StringBuilder();
124+ sb.append('"' + configConnect.getConnectName() + '"');
125+ sb.append("," + '"' + configConnect.getConfigDbms().getDbmsName() + '"');
126+ sb.append("," + '"' + configConnect.getConnectUser() + '"');
127+ sb.append("," + '"' + configConnect.getPassword() + '"');
128+ sb.append("," + '"' + configConnect.getHostName() + '"');
129+ sb.append("," + '"' + configConnect.getPortNumber() + '"');
130+ sb.append("," + '"' + configConnect.getDatabaseName() + '"');
131+ sb.append("," + '"' + configConnect.getClassName() + '"');
132+ sb.append("," + '"' + configConnect.getDriverFile() + '"');
133+ sb.append("," + '"' + configConnect.getUrlOption() + '"');
134+ sb.append("," + '"' + configConnect.getLastConnectdate().toString() + '"');
135+ return sb;
136+ }
137+
138+ }
139+
89140 }
diff -r 1e05c669aed3 -r 8003c5eee508 src/jdbcacsess2/main/Config.java
--- a/src/jdbcacsess2/main/Config.java Sat Apr 07 10:11:57 2012 +0900
+++ b/src/jdbcacsess2/main/Config.java Fri Apr 20 22:47:33 2012 +0900
@@ -15,11 +15,16 @@
1515 */
1616 package jdbcacsess2.main;
1717
18+import java.io.BufferedWriter;
1819 import java.io.File;
20+import java.io.FileWriter;
21+import java.io.FilenameFilter;
1922 import java.io.IOException;
2023 import java.sql.SQLException;
2124 import java.util.ArrayList;
25+import java.util.Arrays;
2226 import java.util.Collections;
27+import java.util.Comparator;
2328 import java.util.Enumeration;
2429 import java.util.Properties;
2530 import java.util.logging.ConsoleHandler;
@@ -27,6 +32,7 @@
2732 import java.util.logging.Formatter;
2833 import java.util.logging.Handler;
2934 import java.util.logging.Level;
35+import java.util.regex.Pattern;
3036
3137 import jdbcacsess2.connect.ConfigConnect;
3238 import jdbcacsess2.connect.ConfigDbms;
@@ -124,6 +130,28 @@
124130 Jdbcacsess2.logger.info("Migrate end");
125131 setUpConfigDbms();
126132
133+ // 接続情報をファイルにバックアップ
134+ try {
135+ File file = getBackupFile(CONFIG_DIR, "BackupConfigConnect", 5);
136+ Jdbcacsess2.logger.info("BackupConfigConnect:" + file.getAbsolutePath());
137+
138+ ConfigConnect[] ccs = getEntityManager().find(ConfigConnect.class,
139+ Query.select());
140+ if (ccs.length != 0) {
141+ try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
142+ bw.write(ccs[0].convertRowheader().toString());
143+ bw.newLine();
144+ for (ConfigConnect configConnect : ccs) {
145+ bw.write(configConnect.convertRow().toString());
146+ bw.newLine();
147+ }
148+ }
149+ }
150+
151+ } catch (IOException e) {
152+ ShowDialog.errorMessage(e);
153+ }
154+
127155 // プロパティの作成
128156 optionValues = new OptionValues(getEntityManager());
129157
@@ -136,6 +164,53 @@
136164 }
137165
138166 /**
167+ * 世代管理されたファイル名を生成する
168+ *
169+ * @param dir
170+ * ディレクトリ名
171+ * @param name
172+ * ファイル名
173+ * @param cnt
174+ * 世代数
175+ * @return 次回作成すべきファイル
176+ * @throws IOException
177+ */
178+ private File getBackupFile(String dir, String name, int cnt) throws IOException {
179+
180+ final Pattern backupFilePattern = Pattern.compile(name + "\\.[0-" + (cnt - 1) + "]");
181+ File[] f = new File(dir).listFiles(new FilenameFilter() {
182+ @Override
183+ public boolean accept(File dir, String name) {
184+ return backupFilePattern.matcher(name).find();
185+ }
186+ });
187+
188+ Arrays.sort(f, new Comparator<File>() {
189+ @Override
190+ public int compare(File o1, File o2) {
191+ if (o1.lastModified() < o2.lastModified()) {
192+ return -1;
193+ }
194+ if (o1.lastModified() > o2.lastModified()) {
195+ return 1;
196+ }
197+ return 0;
198+ }
199+ });
200+
201+ File backup;
202+ if (f.length == cnt) {
203+ backup = f[0];
204+ backup.delete();
205+ } else {
206+ backup = new File(dir + System.getProperty("file.separator") + name + "." + f.length);
207+ }
208+ backup.createNewFile();
209+
210+ return backup;
211+ }
212+
213+ /**
139214 * ActiveObjects の migrate を呼ぶ。SuppressWarningsでワーニングを抑止する為にメソッド抽出。
140215 *
141216 * @throws SQLException
@@ -219,22 +294,22 @@
219294 {"Symfoware","symford","//%HostName%%PortNumber%/%DataBaseName%","com.fujitsu.symfoware.jdbc.SYMDriver","","00.0"},
220295 {"HiRDB","hitachi:hirdb","//DBID=%PortNumberRAW%,DBHOST=%HostName%","JP.co.Hitachi.soft.HiRDB.JDBC.HiRDBDriver","","00.0"},
221296 {"ODBC(TYPE1)","odbc","%DataBaseName%","sun.jdbc.odbc.JdbcOdbcDriver","","00.0"},
222- {"free format","","","","","00.0"}
297+ {"free format","","","","","00.1"}
223298 };
224299
225300 for (String[] row : rows) {
226301 ConfigDbms[] dbms = getEntityManager().find(ConfigDbms.class,
227302 Query.select().where(ConfigDbms.DBMSNAME + "=?", row[0]));
228303 if (dbms.length == 0) {
229- ConfigDbms configDbms =
230- getEntityManager().create(ConfigDbms.class, new DBParam(ConfigDbms.DBMSNAME, row[0]));
304+ getEntityManager().create(ConfigDbms.class,
305+ new DBParam(ConfigDbms.DBMSNAME, row[0]),
306+ new DBParam(ConfigDbms.SUBPROTOCOL, row[1]),
307+ new DBParam(ConfigDbms.SUBNAME, row[2]),
308+ new DBParam(ConfigDbms.DRIVERCLASS, row[3]),
309+ new DBParam(ConfigDbms.URLOPTION, row[4]),
310+ new DBParam(ConfigDbms.VERSION, row[5])
311+ );
231312 Jdbcacsess2.logger.info("not exists:" + row[0]);
232- configDbms.setSubprotocol(row[1]);
233- configDbms.setSubname(row[2]);
234- configDbms.setDriverclass(row[3]);
235- configDbms.setUrlOption(row[4]);
236- configDbms.setVersion(row[5]);
237- configDbms.save();
238313 } else {
239314 if (dbms[0].getVersion().compareTo(row[5]) < 0) {
240315 dbms[0].setSubprotocol(row[1]);
@@ -243,6 +318,7 @@
243318 dbms[0].setUrlOption(row[4]);
244319 dbms[0].setVersion(row[5]);
245320 dbms[0].save();
321+ Jdbcacsess2.logger.info("update:" + row[0]);
246322 }
247323 }
248324 }
diff -r 1e05c669aed3 -r 8003c5eee508 src/jdbcacsess2/main/OptionValues.java
--- a/src/jdbcacsess2/main/OptionValues.java Sat Apr 07 10:11:57 2012 +0900
+++ b/src/jdbcacsess2/main/OptionValues.java Fri Apr 20 22:47:33 2012 +0900
@@ -77,10 +77,10 @@
7777 type = Type.BOOLEAN;
7878 }
7979 ConfigProperty rtn =
80- entityManager.create(ConfigProperty.class,
81- new DBParam("NAME", name),
82- new DBParam("TYPE", type),
83- new DBParam("VALUE", defaultValue));
80+ entityManager.create(ConfigProperty.class,
81+ new DBParam("NAME", name),
82+ new DBParam("TYPE", type),
83+ new DBParam("VALUE", defaultValue));
8484 } catch (SQLException e1) {
8585 ShowDialog.errorMessage(e1);
8686 }
diff -r 1e05c669aed3 -r 8003c5eee508 src/jdbcacsess2/main/ShowDialog.java
--- a/src/jdbcacsess2/main/ShowDialog.java Sat Apr 07 10:11:57 2012 +0900
+++ b/src/jdbcacsess2/main/ShowDialog.java Fri Apr 20 22:47:33 2012 +0900
@@ -43,10 +43,11 @@
4343 nextSqlException = ((SQLException) t).getNextException();
4444 }
4545
46- Jdbcacsess2.logger.log(Level.SEVERE, "ERROR", t.getStackTrace());
46+ Jdbcacsess2.logger.log(Level.SEVERE, "ERROR", t);
4747
4848 final String msg = t.toString() + (nextSqlException != null ? "\n" + nextSqlException.toString() : "");
4949
50+
5051 // EDT以外で呼ばれる事も想定し、EDTで実行する。
5152 SwingUtilities.invokeLater(new Runnable() {
5253 public void run() {
Show on old repository browser