全てを再実装したJdbcAcsessです。
修订版 | 8003c5eee50815a9fd774e94deb12c10412440ca (tree) |
---|---|
时间 | 2012-04-20 22:47:33 |
作者 | shimakazuro |
Commiter | shimakazuro |
接続情報の自動バックアップ
@@ -1,15 +1,15 @@ | ||
1 | 1 | /* |
2 | 2 | * Copyright 2011 Kazuhiro Shimada |
3 | 3 | * |
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. | |
6 | 6 | * You may obtain a copy of the License at |
7 | 7 | * |
8 | - * http://www.apache.org/licenses/LICENSE-2.0 | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | 9 | * |
10 | - * Unless required by applicable law or agreed to in writing, software | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | 11 | * 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. | |
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
@@ -17,8 +17,11 @@ | ||
17 | 17 | |
18 | 18 | import java.util.Date; |
19 | 19 | |
20 | +import jdbcacsess2.connect.ConfigConnect.ConfigConnectImpl; | |
20 | 21 | import net.java.ao.Entity; |
22 | +import net.java.ao.Implementation; | |
21 | 23 | import net.java.ao.Preload; |
24 | +import net.java.ao.schema.Ignore; | |
22 | 25 | import net.java.ao.schema.Unique; |
23 | 26 | |
24 | 27 | /** |
@@ -27,6 +30,7 @@ | ||
27 | 30 | * |
28 | 31 | */ |
29 | 32 | @Preload |
33 | +@Implementation(ConfigConnectImpl.class) | |
30 | 34 | public interface ConfigConnect extends Entity { |
31 | 35 | |
32 | 36 | @Unique |
@@ -74,6 +78,12 @@ | ||
74 | 78 | |
75 | 79 | void setLastConnectdate(Date lastConnectdate); |
76 | 80 | |
81 | + @Ignore | |
82 | + public StringBuilder convertRowheader(); | |
83 | + | |
84 | + @Ignore | |
85 | + public StringBuilder convertRow(); | |
86 | + | |
77 | 87 | public static String CONNECTNAME = "connectname"; |
78 | 88 | public static String DBMS = "dbms"; |
79 | 89 | public static String CONNECTUSER = "connectuser"; |
@@ -86,4 +96,45 @@ | ||
86 | 96 | public static String URLOPTION = "urloption"; |
87 | 97 | public static String LASTCONNECTDATE = "lastconnectdate"; |
88 | 98 | |
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 | + | |
89 | 140 | } |
@@ -15,11 +15,16 @@ | ||
15 | 15 | */ |
16 | 16 | package jdbcacsess2.main; |
17 | 17 | |
18 | +import java.io.BufferedWriter; | |
18 | 19 | import java.io.File; |
20 | +import java.io.FileWriter; | |
21 | +import java.io.FilenameFilter; | |
19 | 22 | import java.io.IOException; |
20 | 23 | import java.sql.SQLException; |
21 | 24 | import java.util.ArrayList; |
25 | +import java.util.Arrays; | |
22 | 26 | import java.util.Collections; |
27 | +import java.util.Comparator; | |
23 | 28 | import java.util.Enumeration; |
24 | 29 | import java.util.Properties; |
25 | 30 | import java.util.logging.ConsoleHandler; |
@@ -27,6 +32,7 @@ | ||
27 | 32 | import java.util.logging.Formatter; |
28 | 33 | import java.util.logging.Handler; |
29 | 34 | import java.util.logging.Level; |
35 | +import java.util.regex.Pattern; | |
30 | 36 | |
31 | 37 | import jdbcacsess2.connect.ConfigConnect; |
32 | 38 | import jdbcacsess2.connect.ConfigDbms; |
@@ -124,6 +130,28 @@ | ||
124 | 130 | Jdbcacsess2.logger.info("Migrate end"); |
125 | 131 | setUpConfigDbms(); |
126 | 132 | |
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 | + | |
127 | 155 | // プロパティの作成 |
128 | 156 | optionValues = new OptionValues(getEntityManager()); |
129 | 157 |
@@ -136,6 +164,53 @@ | ||
136 | 164 | } |
137 | 165 | |
138 | 166 | /** |
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 | + /** | |
139 | 214 | * ActiveObjects の migrate を呼ぶ。SuppressWarningsでワーニングを抑止する為にメソッド抽出。 |
140 | 215 | * |
141 | 216 | * @throws SQLException |
@@ -219,22 +294,22 @@ | ||
219 | 294 | {"Symfoware","symford","//%HostName%%PortNumber%/%DataBaseName%","com.fujitsu.symfoware.jdbc.SYMDriver","","00.0"}, |
220 | 295 | {"HiRDB","hitachi:hirdb","//DBID=%PortNumberRAW%,DBHOST=%HostName%","JP.co.Hitachi.soft.HiRDB.JDBC.HiRDBDriver","","00.0"}, |
221 | 296 | {"ODBC(TYPE1)","odbc","%DataBaseName%","sun.jdbc.odbc.JdbcOdbcDriver","","00.0"}, |
222 | - {"free format","","","","","00.0"} | |
297 | + {"free format","","","","","00.1"} | |
223 | 298 | }; |
224 | 299 | |
225 | 300 | for (String[] row : rows) { |
226 | 301 | ConfigDbms[] dbms = getEntityManager().find(ConfigDbms.class, |
227 | 302 | Query.select().where(ConfigDbms.DBMSNAME + "=?", row[0])); |
228 | 303 | 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 | + ); | |
231 | 312 | 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(); | |
238 | 313 | } else { |
239 | 314 | if (dbms[0].getVersion().compareTo(row[5]) < 0) { |
240 | 315 | dbms[0].setSubprotocol(row[1]); |
@@ -243,6 +318,7 @@ | ||
243 | 318 | dbms[0].setUrlOption(row[4]); |
244 | 319 | dbms[0].setVersion(row[5]); |
245 | 320 | dbms[0].save(); |
321 | + Jdbcacsess2.logger.info("update:" + row[0]); | |
246 | 322 | } |
247 | 323 | } |
248 | 324 | } |
@@ -77,10 +77,10 @@ | ||
77 | 77 | type = Type.BOOLEAN; |
78 | 78 | } |
79 | 79 | 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)); | |
84 | 84 | } catch (SQLException e1) { |
85 | 85 | ShowDialog.errorMessage(e1); |
86 | 86 | } |
@@ -43,10 +43,11 @@ | ||
43 | 43 | nextSqlException = ((SQLException) t).getNextException(); |
44 | 44 | } |
45 | 45 | |
46 | - Jdbcacsess2.logger.log(Level.SEVERE, "ERROR", t.getStackTrace()); | |
46 | + Jdbcacsess2.logger.log(Level.SEVERE, "ERROR", t); | |
47 | 47 | |
48 | 48 | final String msg = t.toString() + (nextSqlException != null ? "\n" + nextSqlException.toString() : ""); |
49 | 49 | |
50 | + | |
50 | 51 | // EDT以外で呼ばれる事も想定し、EDTで実行する。 |
51 | 52 | SwingUtilities.invokeLater(new Runnable() { |
52 | 53 | public void run() { |