• R/O
  • SSH

JdbcAcsess2: Commit

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


Commit MetaInfo

Revision0af782ed162e7ad675ff2b979e5a1c095dbff719 (tree)
Time2012-03-04 22:44:40
Authorshimakazuro
Commitershimakazuro

Log Message

Eclipseコードスタイル

Change Summary

Incremental Difference

diff -r 4b85c7dfefc0 -r 0af782ed162e src/jdbcacsess2/main/Config.java
--- a/src/jdbcacsess2/main/Config.java Sun Mar 04 22:43:19 2012 +0900
+++ b/src/jdbcacsess2/main/Config.java Sun Mar 04 22:44:40 2012 +0900
@@ -42,241 +42,243 @@
4242
4343 public class Config {
4444
45- private static final String CONFIG_DIR = System.getProperty("user.home") + System.getProperty("file.separator")
46- + ".jdbcacsess2";
47-
48- private static final String CONFIG_DB = "config.derby";
49- private static final String CONFIG_JDBCURL = "jdbc:derby:" + CONFIG_DIR + System.getProperty("file.separator")
50- + CONFIG_DB + ";create=true";
51- private static final String CONFIG_JDBCCLASS = "org.apache.derby.jdbc.EmbeddedDriver";
52- private static final String USER = "";
53- private static final String PASS = "";
54-
55- private final EntityManager entityManager;
56-
57- /**
58- * コンストラクタ
59- *
60- * @throws IOException
61- * @throws ClassNotFoundException
62- * @throws SQLException
63- */
64- public Config() throws IOException, ClassNotFoundException, SQLException {
65-
66- // コンソールとファイルのハンドラーで使用するログフォーマッター
67- Formatter oneLine = new OneLinenFormatter();
68-
69- // コンソールハンドラー
70- Handler consoleHandler = new ConsoleHandler();
71- consoleHandler.setFormatter(oneLine);
72- consoleHandler.setLevel(Level.ALL);
73-
74- Jdbcacsess2.logger.addHandler(consoleHandler);
75- Jdbcacsess2.logger.setUseParentHandlers(false);
76- Jdbcacsess2.loggerActiveObjects.addHandler(consoleHandler);
77- Jdbcacsess2.loggerActiveObjects.setUseParentHandlers(false);
78-
79- // DB作成
80- File configDir = new File(CONFIG_DIR);
81- if (!configDir.exists()) {
82- if (!configDir.mkdir()) {
83- throw new IOException("Couldnt create diretrory:" + configDir.getAbsolutePath());
84- }
85- }
86-
87- // ログファイルハンドラー
88- Handler fileHandler = new FileHandler(CONFIG_DIR + File.separator + "message.log", 1024 * 1024, 5);
89- fileHandler.setFormatter(oneLine);
90- fileHandler.setLevel(Level.ALL);
91-
92- Jdbcacsess2.logger.addHandler(fileHandler);
93- Jdbcacsess2.loggerActiveObjects.addHandler(fileHandler);
94-
95- Jdbcacsess2.logger.info("file logger start");
96- Jdbcacsess2.logger.info("configure directory:" + CONFIG_DIR);
97-
98- // システムプロパティを表示する
99- if (Jdbcacsess2.logger.isLoggable(Level.FINE)) {
100- Properties prop = System.getProperties();
101- ArrayList<String> aList = Collections.list(extracted(prop));
102- Collections.sort(aList);
103-
104- for (Iterator<String> iterator = aList.iterator(); iterator.hasNext();) {
105- String string = iterator.next();
106- Jdbcacsess2.logger.fine(string + ":" + System.getProperty(string));
107- }
108- } else {
109- Jdbcacsess2.logger.info(System.getProperty("java.vm.vendor") + " " + System.getProperty("java.vm.name")
110- + " " + System.getProperty("java.vm.version"));
111- Jdbcacsess2.logger.info(System.getProperty("java.home"));
112- Jdbcacsess2.logger.info(System.getProperty("java.class.path"));
113- Jdbcacsess2.logger.info(System.getProperty("java.library.path"));
114- Jdbcacsess2.logger.info(System.getProperty("java.ext.dirs"));
115- }
116-
117- // ActibObjectsの初期設定
118- Class.forName(CONFIG_JDBCCLASS);
119-
120- this.entityManager = new EntityManager(CONFIG_JDBCURL, USER, PASS);
121- Jdbcacsess2.loggerActiveObjects.setLevel(Level.WARNING);
122- // Jdbcacsess2.loggerActiveObjects.setLevel(Level.FINE);
123-
124- migrate();
125- setUpConfigDbms();
126-
127- Jdbcacsess2.logger.info("Config end");
128- }
129-
130- @SuppressWarnings("unchecked")
131- private Enumeration<String> extracted(Properties prop) {
132- return (Enumeration<String>) prop.propertyNames();
133- }
134-
135- /**
136- * ActiveObjects の migrate を呼ぶ。SuppressWarningsでワーニングを抑止する為にメソッド抽出。
137- *
138- * @throws SQLException
139- */
140- @SuppressWarnings("unchecked")
141- private void migrate() throws SQLException {
142- getEntityManager().migrate(ConfigDbms.class,
143- ConfigConnect.class,
144- ConnectView.ConfigViewConnect.class,
145- MainView.ConfigViewMain.class,
146- ExecHistory.class);
147- }
148-
149- /**
150- * {@link ConfigDbms#DBMSNAME}カラム順のDBMSの一覧
151- *
152- * @return {@link ConfigDbms}の配列
153- * @throws SQLException
154- */
155- public ConfigDbms[] getConfigDbms() throws SQLException {
156- return getEntityManager().find(ConfigDbms.class, Query.select().order(ConfigDbms.DBMSNAME));
157- }
45+ private static final String CONFIG_DIR = System.getProperty("user.home") + System.getProperty("file.separator")
46+ + ".jdbcacsess2";
15847
159- /**
160- * {@link ConfigConnect#CONNECTNAME}カラム順の接続情報の一覧
161- *
162- * @return {@link ConfigConnect}の配列
163- * @throws SQLException
164- */
165- public ConfigConnect[] getConfigConnect() throws SQLException {
166- return getEntityManager().find(ConfigConnect.class, Query.select());
167- }
168-
169- /**
170- * 接続名称で検索し、存在する・しないに関わらず、保存用の{@link ConfigConnect}を返却します。
171- * @param connectName 接続名称
172- * @return データがあった場合は検索結果の{@link ConfigConnect}、なかった場合は挿入用の{@link ConfigConnect}
173- * @throws SQLException
174- */
175- public ConfigConnect getConfigConnectRow(String connectName) throws SQLException {
176- ConfigConnect[] configConnects = getEntityManager().find(ConfigConnect.class, Query.select().where(ConfigConnect.CONNECTNAME + "=?", connectName));
177-
178- ConfigConnect rtn;
179- if (configConnects.length==0l){
180- rtn = getEntityManager().create(ConfigConnect.class,
181- new DBParam(ConfigConnect.CONNECTNAME,
182- connectName));
183- }else{
184- rtn = configConnects[0];
185- }
186- return rtn;
187- }
188-
189- /**
190- * component属性を保管するテーブル検索し、先頭1件だけに絞る。
191- *
192- * @param <T>
193- * @param <K>
194- * @param type
195- * 検索対象componentのテーブルクラス
196- * @return データが存在しない時は、
197- * {@link EntityManager#create(Class, net.java.ao.DBParam...)}
198- * を実行し空行を取得します。
199- * @throws SQLException
200- */
201- public <T extends RawEntity<K>, K> T getConfigWindow(Class<T> type) throws SQLException {
202- T[] e = getEntityManager().find(type);
203- if (e.length != 0) {
204- return e[0];
205- }
206- return getEntityManager().create(type);
207- }
48+ private static final String CONFIG_DB = "config.derby";
49+ private static final String CONFIG_JDBCURL = "jdbc:derby:" + CONFIG_DIR + System.getProperty("file.separator")
50+ + CONFIG_DB + ";create=true";
51+ private static final String CONFIG_JDBCCLASS = "org.apache.derby.jdbc.EmbeddedDriver";
52+ private static final String USER = "";
53+ private static final String PASS = "";
20854
209- /**
210- * @throws SQLException
211- */
212- private void setUpConfigDbms() throws SQLException {
213- String[] colnames = { ConfigDbms.DBMSNAME, ConfigDbms.SUBPROTOCOL, ConfigDbms.SUBNAME, ConfigDbms.DRIVERCLASS,
214- ConfigDbms.URLOPTION };
215- String[][] rows = {
216- { "PostgreSQL", "postgresql", "//%HostName%%PortNumber%/%DataBaseName%",
217- "org.postgresql.Driver", "" },
218- { "MySQL", "mysql", "//%HostName%%PortNumber%/%DataBaseName%", "org.gjt.mm.mysql.Driver", "" },
219- { "MySQL(com)", "mysql", "//%HostName%%PortNumber%/%DataBaseName%", "com.mysql.jdbc.Driver",
220- "" },
221- { "HSQL", "hsqldb:hsql", "//%HostName%%PortNumber%/%DataBaseName%", "org.hsqldb.jdbcDriver",
222- "" },
223- { "Oracle(TYPE4:Pure Java)", "oracle:thin", "@%HostName%%PortNumber%:%DataBaseName%",
224- "oracle.jdbc.driver.OracleDriver", "" },
225- { "Oracle(TYPE2:NET)", "oracle:oci", "@%DataBaseName%", "oracle.jdbc.driver.OracleDriver",
226- "" },
227- { "Oracle(TYPE2:NET8)", "oracle:oci8", "@%DataBaseName%", "oracle.jdbc.driver.OracleDriver",
228- "" },
229- { "DB2(TYPE4:Universal)", "db2", "//%HostName%%PortNumber%/%DataBaseName%",
230- "com.ibm.db2.jcc.DB2Driver", "" },
231- { "DB2(TYPE2:Universal)", "db2", "%DataBaseName%", "com.ibm.db2.jcc.DB2Driver", "" },
232- { "DB2(TYPE2:App)", "db2", "%DataBaseName%", "COM.ibm.db2.jdbc.app.DB2Driver", "" },
233- { "DB2(TYPE3:Net)", "db2", "//%HostName%%PortNumber%/%DataBaseName%",
234- "COM.ibm.db2.jdbc.net.DB2Driver", "" },
235- { "SQL Server", "sqlserver",
236- "//%HostName%%PortNumber%;databaseName=%DataBaseName%",
237- "com.microsoft.sqlserver.jdbc.SQLServerDriver", "" },
238- { "SQL Server(2000)", "microsoft:sqlserver",
239- "//%HostName%%PortNumber%;databaseName=%DataBaseName%",
240- "com.microsoft.jdbc.sqlserver.SQLServerDriver", "" },
241- { "Derby(Embedded)", "derby", "%DataBaseName%", "org.apache.derby.jdbc.EmbeddedDriver", "" },
242- { "Derby(Client)", "derby", "%DataBaseName%", "org.apache.derby.jdbc.ClientDriver", "" },
243- { "SQLite", "sqlite", "/%DataBaseName%", "org.sqlite.JDBC", "" },
244- { "Firebird", "firebirdsql", "//%HostName%%PortNumber%/%DataBaseName%",
245- "org.firebirdsql.jdbc.FBDriver", "" },
246- { "Symfoware", "symford", "//%HostName%%PortNumber%/%DataBaseName%",
247- "com.fujitsu.symfoware.jdbc.SYMDriver", "" },
248- { "HiRDB", "hitachi:hirdb", "//DBID=%PortNumberRAW%,DBHOST=%HostName%",
249- "JP.co.Hitachi.soft.HiRDB.JDBC.HiRDBDriver", "" },
250- { "ODBC(TYPE1)", "odbc", "%DataBaseName%", "sun.jdbc.odbc.JdbcOdbcDriver", "" },
251- { "free format", "", "", "", "" } };
55+ private final EntityManager entityManager;
25256
253- for (int i = 0; i < rows.length; i++) {
254- ConfigDbms[] dbms = getEntityManager().find(ConfigDbms.class,
255- Query.select().where(ConfigDbms.DBMSNAME + "=?", rows[i][0]));
256- if (dbms.length == 0) {
257- Map<String, Object> m = new HashMap<String, Object>();
258- for (int j = 0; j < colnames.length; j++) {
259- m.put(colnames[j], rows[i][j]);
260- }
261- getEntityManager().create(ConfigDbms.class, m);
262- Jdbcacsess2.logger.info("not exists:" + rows[i][0]);
263- }
264- }
265- }
57+ /**
58+ * コンストラクタ
59+ *
60+ * @throws IOException
61+ * @throws ClassNotFoundException
62+ * @throws SQLException
63+ */
64+ public Config() throws IOException, ClassNotFoundException, SQLException {
26665
267- /**
268- * 設定情報保管用のDBをクローズ
269- */
270- public void close() {
271- getEntityManager().flushAll();
272- getEntityManager().getProvider().dispose();
273- }
66+ // コンソールとファイルのハンドラーで使用するログフォーマッター
67+ Formatter oneLine = new OneLinenFormatter();
27468
275- /**
276- * @return entityManager
277- */
278- public EntityManager getEntityManager() {
279- return entityManager;
280- }
69+ // コンソールハンドラー
70+ Handler consoleHandler = new ConsoleHandler();
71+ consoleHandler.setFormatter(oneLine);
72+ consoleHandler.setLevel(Level.ALL);
73+
74+ Jdbcacsess2.logger.addHandler(consoleHandler);
75+ Jdbcacsess2.logger.setUseParentHandlers(false);
76+ Jdbcacsess2.loggerActiveObjects.addHandler(consoleHandler);
77+ Jdbcacsess2.loggerActiveObjects.setUseParentHandlers(false);
78+
79+ // DB作成
80+ File configDir = new File(CONFIG_DIR);
81+ if (!configDir.exists()) {
82+ if (!configDir.mkdir()) {
83+ throw new IOException("Couldnt create diretrory:" + configDir.getAbsolutePath());
84+ }
85+ }
86+
87+ // ログファイルハンドラー
88+ Handler fileHandler = new FileHandler(CONFIG_DIR + File.separator + "message.log", 1024 * 1024, 5);
89+ fileHandler.setFormatter(oneLine);
90+ fileHandler.setLevel(Level.ALL);
91+
92+ Jdbcacsess2.logger.addHandler(fileHandler);
93+ Jdbcacsess2.loggerActiveObjects.addHandler(fileHandler);
94+
95+ Jdbcacsess2.logger.info("file logger start");
96+ Jdbcacsess2.logger.info("configure directory:" + CONFIG_DIR);
97+
98+ // システムプロパティを表示する
99+ if (Jdbcacsess2.logger.isLoggable(Level.FINE)) {
100+ Properties prop = System.getProperties();
101+ ArrayList<String> aList = Collections.list(extracted(prop));
102+ Collections.sort(aList);
103+
104+ for (Iterator<String> iterator = aList.iterator(); iterator.hasNext();) {
105+ String string = iterator.next();
106+ Jdbcacsess2.logger.fine(string + ":" + System.getProperty(string));
107+ }
108+ } else {
109+ Jdbcacsess2.logger.info(System.getProperty("java.vm.vendor") + " " + System.getProperty("java.vm.name")
110+ + " " + System.getProperty("java.vm.version"));
111+ Jdbcacsess2.logger.info(System.getProperty("java.home"));
112+ Jdbcacsess2.logger.info(System.getProperty("java.class.path"));
113+ Jdbcacsess2.logger.info(System.getProperty("java.library.path"));
114+ Jdbcacsess2.logger.info(System.getProperty("java.ext.dirs"));
115+ }
116+
117+ // ActibObjectsの初期設定
118+ Class.forName(CONFIG_JDBCCLASS);
119+
120+ this.entityManager = new EntityManager(CONFIG_JDBCURL, USER, PASS);
121+ Jdbcacsess2.loggerActiveObjects.setLevel(Level.WARNING);
122+ // Jdbcacsess2.loggerActiveObjects.setLevel(Level.FINE);
123+
124+ migrate();
125+ setUpConfigDbms();
126+
127+ Jdbcacsess2.logger.info("Config end");
128+ }
129+
130+ @SuppressWarnings("unchecked")
131+ private Enumeration<String> extracted(Properties prop) {
132+ return (Enumeration<String>) prop.propertyNames();
133+ }
134+
135+ /**
136+ * ActiveObjects の migrate を呼ぶ。SuppressWarningsでワーニングを抑止する為にメソッド抽出。
137+ *
138+ * @throws SQLException
139+ */
140+ @SuppressWarnings("unchecked")
141+ private void migrate() throws SQLException {
142+ getEntityManager().migrate(ConfigDbms.class,
143+ ConfigConnect.class,
144+ ConnectView.ConfigViewConnect.class,
145+ MainView.ConfigViewMain.class,
146+ ExecHistory.class);
147+ }
148+
149+ /**
150+ * {@link ConfigDbms#DBMSNAME}カラム順のDBMSの一覧
151+ *
152+ * @return {@link ConfigDbms}の配列
153+ * @throws SQLException
154+ */
155+ public ConfigDbms[] getConfigDbms() throws SQLException {
156+ return getEntityManager().find(ConfigDbms.class, Query.select().order(ConfigDbms.DBMSNAME));
157+ }
158+
159+ /**
160+ * {@link ConfigConnect#CONNECTNAME}カラム順の接続情報の一覧
161+ *
162+ * @return {@link ConfigConnect}の配列
163+ * @throws SQLException
164+ */
165+ public ConfigConnect[] getConfigConnect() throws SQLException {
166+ return getEntityManager().find(ConfigConnect.class, Query.select());
167+ }
168+
169+ /**
170+ * 接続名称で検索し、存在する・しないに関わらず、保存用の{@link ConfigConnect}を返却します。
171+ *
172+ * @param connectName
173+ * 接続名称
174+ * @return データがあった場合は検索結果の{@link ConfigConnect}、なかった場合は挿入用の
175+ * {@link ConfigConnect}
176+ * @throws SQLException
177+ */
178+ public ConfigConnect getConfigConnectRow(String connectName) throws SQLException {
179+ ConfigConnect[] configConnects = getEntityManager().find(ConfigConnect.class,
180+ Query.select().where(ConfigConnect.CONNECTNAME + "=?",
181+ connectName));
182+
183+ ConfigConnect rtn;
184+ if (configConnects.length == 0l) {
185+ rtn = getEntityManager().create(ConfigConnect.class, new DBParam(ConfigConnect.CONNECTNAME, connectName));
186+ } else {
187+ rtn = configConnects[0];
188+ }
189+ return rtn;
190+ }
191+
192+ /**
193+ * component属性を保管するテーブル検索し、先頭1件だけに絞る。
194+ *
195+ * @param <T>
196+ * @param <K>
197+ * @param type
198+ * 検索対象componentのテーブルクラス
199+ * @return データが存在しない時は、
200+ * {@link EntityManager#create(Class, net.java.ao.DBParam...)}
201+ * を実行し空行を取得します。
202+ * @throws SQLException
203+ */
204+ public <T extends RawEntity<K>, K> T getConfigWindow(Class<T> type) throws SQLException {
205+ T[] e = getEntityManager().find(type);
206+ if (e.length != 0) {
207+ return e[0];
208+ }
209+ return getEntityManager().create(type);
210+ }
211+
212+ /**
213+ * @throws SQLException
214+ */
215+ private void setUpConfigDbms() throws SQLException {
216+ String[] colnames = { ConfigDbms.DBMSNAME, ConfigDbms.SUBPROTOCOL, ConfigDbms.SUBNAME, ConfigDbms.DRIVERCLASS,
217+ ConfigDbms.URLOPTION };
218+ String[][] rows = {
219+ { "PostgreSQL", "postgresql", "//%HostName%%PortNumber%/%DataBaseName%",
220+ "org.postgresql.Driver", "" },
221+ { "MySQL", "mysql", "//%HostName%%PortNumber%/%DataBaseName%", "org.gjt.mm.mysql.Driver", "" },
222+ { "MySQL(com)", "mysql", "//%HostName%%PortNumber%/%DataBaseName%", "com.mysql.jdbc.Driver",
223+ "" },
224+ { "HSQL", "hsqldb:hsql", "//%HostName%%PortNumber%/%DataBaseName%", "org.hsqldb.jdbcDriver",
225+ "" },
226+ { "Oracle(TYPE4:Pure Java)", "oracle:thin", "@%HostName%%PortNumber%:%DataBaseName%",
227+ "oracle.jdbc.driver.OracleDriver", "" },
228+ { "Oracle(TYPE2:NET)", "oracle:oci", "@%DataBaseName%", "oracle.jdbc.driver.OracleDriver",
229+ "" },
230+ { "Oracle(TYPE2:NET8)", "oracle:oci8", "@%DataBaseName%", "oracle.jdbc.driver.OracleDriver",
231+ "" },
232+ { "DB2(TYPE4:Universal)", "db2", "//%HostName%%PortNumber%/%DataBaseName%",
233+ "com.ibm.db2.jcc.DB2Driver", "" },
234+ { "DB2(TYPE2:Universal)", "db2", "%DataBaseName%", "com.ibm.db2.jcc.DB2Driver", "" },
235+ { "DB2(TYPE2:App)", "db2", "%DataBaseName%", "COM.ibm.db2.jdbc.app.DB2Driver", "" },
236+ { "DB2(TYPE3:Net)", "db2", "//%HostName%%PortNumber%/%DataBaseName%",
237+ "COM.ibm.db2.jdbc.net.DB2Driver", "" },
238+ { "SQL Server", "sqlserver", "//%HostName%%PortNumber%;databaseName=%DataBaseName%",
239+ "com.microsoft.sqlserver.jdbc.SQLServerDriver", "" },
240+ { "SQL Server(2000)", "microsoft:sqlserver",
241+ "//%HostName%%PortNumber%;databaseName=%DataBaseName%",
242+ "com.microsoft.jdbc.sqlserver.SQLServerDriver", "" },
243+ { "Derby(Embedded)", "derby", "%DataBaseName%", "org.apache.derby.jdbc.EmbeddedDriver", "" },
244+ { "Derby(Client)", "derby", "%DataBaseName%", "org.apache.derby.jdbc.ClientDriver", "" },
245+ { "SQLite", "sqlite", "/%DataBaseName%", "org.sqlite.JDBC", "" },
246+ { "Firebird", "firebirdsql", "//%HostName%%PortNumber%/%DataBaseName%",
247+ "org.firebirdsql.jdbc.FBDriver", "" },
248+ { "Symfoware", "symford", "//%HostName%%PortNumber%/%DataBaseName%",
249+ "com.fujitsu.symfoware.jdbc.SYMDriver", "" },
250+ { "HiRDB", "hitachi:hirdb", "//DBID=%PortNumberRAW%,DBHOST=%HostName%",
251+ "JP.co.Hitachi.soft.HiRDB.JDBC.HiRDBDriver", "" },
252+ { "ODBC(TYPE1)", "odbc", "%DataBaseName%", "sun.jdbc.odbc.JdbcOdbcDriver", "" },
253+ { "free format", "", "", "", "" } };
254+
255+ for (int i = 0; i < rows.length; i++) {
256+ ConfigDbms[] dbms = getEntityManager().find(ConfigDbms.class,
257+ Query.select().where(ConfigDbms.DBMSNAME + "=?", rows[i][0]));
258+ if (dbms.length == 0) {
259+ Map<String, Object> m = new HashMap<String, Object>();
260+ for (int j = 0; j < colnames.length; j++) {
261+ m.put(colnames[j], rows[i][j]);
262+ }
263+ getEntityManager().create(ConfigDbms.class, m);
264+ Jdbcacsess2.logger.info("not exists:" + rows[i][0]);
265+ }
266+ }
267+ }
268+
269+ /**
270+ * 設定情報保管用のDBをクローズ
271+ */
272+ public void close() {
273+ getEntityManager().flushAll();
274+ getEntityManager().getProvider().dispose();
275+ }
276+
277+ /**
278+ * @return entityManager
279+ */
280+ public EntityManager getEntityManager() {
281+ return entityManager;
282+ }
281283
282284 }
Show on old repository browser