001 /* 002 * Copyright (c) 2009 The openGion Project. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 013 * either express or implied. See the License for the specific language 014 * governing permissions and limitations under the License. 015 */ 016 package org.opengion.fukurou.db; 017 018 import java.io.File; 019 import java.net.URL; 020 import java.util.ArrayList; 021 import java.util.Arrays; 022 import java.util.Comparator; 023 import java.util.LinkedHashMap; 024 import java.util.List; 025 import java.util.Locale; 026 import java.util.Map; 027 028 import org.opengion.fukurou.util.StringUtil; 029 import org.opengion.fukurou.util.FileUtil; 030 import org.opengion.fukurou.util.LogWriter; 031 import org.opengion.fukurou.xml.DomParser; 032 import org.w3c.dom.Document; 033 import org.w3c.dom.Element; 034 import org.w3c.dom.Node; 035 import org.w3c.dom.NodeList; 036 037 /** 038 * DB設定XMLの?をJAXBを利用してロードす? 039 * Driverをロードす? 040 * 上記2つの機?を備えたクラスで? 041 * 042 * 外部からはgetDbidメソ?を利用してDB設?ExpandedDbid?を取得します? 043 * DB設定情報が無??合にXMLを読みに?ます? 044 * こ?DBIDを決めるキーは、?部取り込み字に、大?変換されます?で、大?? 045 * 小文字?区別はありません? 046 * 047 * @og.rev 4.0.0.0 (2007/10/25) 新規作? 048 * @og.rev 5.1.7.0 (2010/06/01) org.opengion.fukurou.xml.jaxb.dbid 関?? 049 * @og.group 初期? 050 * 051 * @version 4.0 052 * @author 高橋正? 053 * @since JDK6.0, 054 */ 055 public class DatabaseConfig { 056 057 // fukurou?完結させるため、HybsDataからは読み込まずにここに書? 058 private static final String DEFAULT_DRIVER = "oracle.jdbc.OracleDriver"; 059 060 // XMLファイル関連 061 // private transient final String XmlFilename; 062 // private final String XmlFilename; 063 private final String xmlFilename; // 5.7.2.2 (2014/01/24) 064 // private static final String SCHEMA_FILENAME = "DBConfig.xsd";//xsdはfukurou.xml.jaxb.dbidパッケージに置? 065 066 // private transient final Map<String, EDbid> dbidMap = new HashMap<String, EDbid>(); 067 // private transient List<String> driverList = new ArrayList<String>(); 068 // private Map<String, EDbid> dbidMap = new HashMap<String, EDbid>(); 069 private Map<String, EDbid> dbidMap = new LinkedHashMap<String, EDbid>(); // 5.6.7.0 (2013/07/27) 070 // private List<String> driverList = new ArrayList<String>(); 071 private List<String> driverList = new ArrayList<String>(); 072 073 // 5.6.7.0 (2013/07/27) プル?ンメニュー用の??の、キャ?ュ用変数? 074 private String codeKeyVal = null; // 初めて要求されたときに、セ?します? 075 076 // private static final String CR = System.getProperty( "line.separator" ); 077 078 /* DBIDのキーの?を管?ます?5.1.9.0 (2010/08/01) */ 079 // 5.5.2.0 (2012/05/01) property追? 080 // private static final String[] DBID_INFO_KEYS 081 // = { "dbidKey", "url", "user", "password", "readonly" 082 // , "mincount", "maxcount", "pooltime", "applicationInfo" }; 083 // private static final String[] DBID_INFO_KEYS 084 // = { "dbidKey", "url", "user", "password", "readonly" 085 // , "mincount", "maxcount", "pooltime", "applicationInfo","property" }; 086 // 5.6.6.0 (2013/07/05) 表?title)属?を追? 087 private static final String[] DBID_INFO_KEYS 088 = { "dbidKey", "title", "url", "user", "password", "readonly" 089 , "mincount", "maxcount", "pooltime", "applicationInfo","property" }; 090 091 /* DBDRIVERのキーのを管?ます?5.1.9.0 (2010/08/01) */ 092 private static final String DBDRIVER_CLASS_KEY = "class"; 093 094 /** 095 * 初期値を使ってXMLを読み込? 096 * xmlFilenameの初期値は../DBConfig.xml 097 * 098 * @og.rev 4.3.1.1 (2008/08/23) 自??コンストラクターを呼ぶように修正 099 */ 100 public DatabaseConfig() { 101 this( "../DBConfig.xml" ); 102 } 103 104 /** 105 * XMLファイルの名前を指定して読み込? 106 * 107 * @og.rev 5.1.9.0 (2010/08/01) クラスロー??外からで?BConfig.xmlを取得できるようにする 108 * @og.rev 5.6.7.0 (2013/07/27) オブジェクト作?時に初期化も行っておきます? 109 * @og.rev 5.6.8.2 (2013/09/20) Tomcat8で、クラスロー??が変更されて?のでそ?対? 110 * @og.rev 5.7.2.2 (2014/01/24) WEB-INF/classes フォル?な?、xmlURL がnull になる対応? 111 * @og.rev 5.7.2.3 (2014/01/31) ファイルの存在チェ?を追?ます? 112 * 113 * @param xmlfile XMLファイルの名前 114 */ 115 public DatabaseConfig( final String xmlfile ) { 116 // xmlFilename = xmlfile; 117 String fileName = null; 118 119 ClassLoader clsl = getClass().getClassLoader(); 120 URL xmlURL = clsl.getResource( xmlfile ); 121 122 if( xmlURL != null ) { 123 fileName = xmlURL.getFile(); 124 // 5.7.2.3 (2014/01/31) ファイルの存在チェ?を追?ます? 125 if( ! new File( fileName ).exists() ) { fileName = null; } 126 } 127 // 5.6.8.2 (2013/09/20) Tomcat8で、xmlURL が取得できなくなって?・・・よう?? 128 else { 129 xmlURL = clsl.getResource( "/" ); // クラスパスのベ?スURL 130 // 5.7.2.2 (2014/01/24) Tomcat7で、WEB-INF/classes フォル?な?、xmlURL がnull になる? 131 if( xmlURL != null ) { 132 File temp = new File( xmlURL.getPath() , xmlfile ); 133 if( temp.exists() ) { fileName = temp.getAbsolutePath(); } 134 } 135 } 136 137 // 5.1.9.0 (2010/08/01) クラスロー??外からで?BConfig.xmlを取得できるようにする 138 if( fileName == null && new File( xmlfile ).exists() ) { 139 fileName = xmlfile; 140 } 141 142 if( fileName == null ) { 143 // 5.5.7.2 (2012/10/09) コメント追? 144 String errMsg = "DBConfig.xmlが見つかりません?ile=[" + xmlfile + "]\n" 145 + " WEB-INF/classes フォル?な?、相対パスで見つけることができません? ; 146 throw new RuntimeException( errMsg ); 147 // throw new RuntimeException( "DBConfig.xmlが見つかりません?ile=[" + xmlfile + "]" ); 148 } 149 150 xmlFilename = fileName; 151 // System.out.println( xmlFilename ); 152 153 init(); // 5.6.7.0 (2013/07/27) 154 } 155 156 /** 157 * dbidKeyをキーにしてExpandedDbid型でマップ??を返す? 158 * 存在しな??合?NULLを返します? 159 * キーが無??合に初期化を行う? 160 * 161 * @og.rev 4.0.0.1 (2007/12/04) EDbid#clone() ? 162 * @og.rev 5.6.7.0 (2013/07/27) synchronized メソ?にします? 163 * 164 * @param key XMLで登録したdbidKey 165 * 166 * @return EDbid型オブジェク? 167 */ 168 public synchronized EDbid getDbid( final String key ) { 169 // synchronized ( dbidMap ) { 170 // if( dbidMap.isEmpty() ) { 171 // init(); 172 // } 173 174 return dbidMap.get( key.toUpperCase( Locale.JAPAN ) ) ; 175 // } 176 } 177 178 /** 179 * マップをクリアします? 180 * XMLファイルを?読み込みする場合に使用します? 181 * 182 * @og.rev 5.1.9.0 (2010/08/01) ドライバ?のリストもクリアする? 183 * @og.rev 5.6.7.0 (2013/07/27) synchronized メソ?にします? 184 */ 185 public synchronized void reload() { 186 // synchronized ( dbidMap ) { 187 dbidMap.clear(); 188 // } 189 // synchronized ( driverList ) { 190 driverList.clear(); 191 // } 192 init(); 193 } 194 195 /** 196 * 初期化?? 197 * 198 * DB設定XMLファイル(DBConfig.xml)を読み込みます? 199 * こ?ファイルから、ドライバ?リスト?取得?DBIDのオブジェクト???の作?? 200 * 行います? 201 * EDbidオブジェク?は、環?数で、?通?初期値を定義しておくことが可能です? 202 * ?として、REALM_URL、REALM_NAME、REALM_PASSWORD が設定可能です? 203 * 204 * ドライバ?リスト?取得後?Class.forName で、ドライバ?登録も行います? 205 * 206 * @og.rev 5.1.7.0 (2010/06/01) org.opengion.fukurou.xml.jaxb.dbid 関?? 207 * @og.rev 5.6.7.0 (2013/07/27) dbidMap,driverList を書き込??ではなく?作?します? 208 */ 209 private void init() { 210 Document doc = DomParser.read( new File(xmlFilename) ) ; 211 Element firstRoot = doc.getDocumentElement(); 212 213 // List<String> driverList = getDriverList( firstRoot ); 214 // driverList = getDriverList( firstRoot ); 215 makeDriverList( firstRoot ); // 5.6.7.0 (2013/07/27) 216 217 // 5.6.7.0 (2013/07/27) を?かけておきます? 218 synchronized ( this ) { 219 for( String dr : driverList ) { 220 try { 221 Class.forName( dr ); 222 } catch ( ClassNotFoundException ex ) { 223 String errMsg = "ドライバクラスが見つかりません?" + dr + "]" ; 224 LogWriter.log( errMsg ); 225 LogWriter.log( ex ); 226 } 227 } 228 } 229 230 EDbid defDdbid = new EDbid(); // 初期値 231 defDdbid.setUrl( System.getenv( "REALM_URL" ) ); 232 defDdbid.setUser( System.getenv( "REALM_NAME" ) ); 233 defDdbid.setPassword( System.getenv( "REALM_PASSWORD" ) ); 234 235 // dbidMap = getDbidMap( firstRoot,defDdbid ); 236 makeDbidMap( firstRoot,defDdbid ); // 5.6.7.0 (2013/07/27) 237 } 238 239 /** 240 * ドライバ?リストを取得します? 241 * 242 * DB設定XMLファイル(DBConfig.xml)の、class タグを取り込みます? 243 * こ?ファイルから、ドライバ?リストを取得します? 244 * 245 * ???段階?処?実行されます? 246 * 第?Step:DBConfig.xml から、ドライバ?リストを取? 247 * 第?Step:ドライバ?リストが存在しな??合?環?数の REALM_DRIVER からドライバ?を取? 248 * 第?Step:それでも存在しな??合?こ?クラスの DEFAULT_DRIVER 定数 からドライバ?を取? 249 * 250 * @og.rev 5.1.7.0 (2010/06/01) org.opengion.fukurou.xml.jaxb.dbid 関?? 251 * @og.rev 5.1.9.0 (2010/08/01) ドライバ?のListをオブジェクト変数? 252 * @og.rev 5.6.7.0 (2013/07/27) driverList を書き込??ではなく?作?します? 253 * @og.rev 5.6.7.0 (2013/07/27) synchronized メソ?にします? 254 * 255 * @param element DB設定XMLファイルのElementオブジェク? 256 */ 257 // private static List<String> getDriverList( final Element element ) { 258 private void makeDriverList( final Element element ) { 259 // List<String> dList = new ArrayList<String>(); 260 261 NodeList list = element.getElementsByTagName( "class" ) ; 262 int num = list.getLength(); 263 for (int i = 0; i < num; i++) { 264 Element cls = (Element)list.item(i); 265 // dList.add( cls.getTextContent() ); 266 driverList.add( cls.getTextContent() ); 267 } 268 269 // if( dList.isEmpty() ) { 270 if( driverList.isEmpty() ) { 271 String realmDriver = System.getenv( "REALM_DRIVER" ); 272 if( realmDriver != null && realmDriver.length() > 0 ) { 273 // dList.add( realmDriver ); 274 driverList.add( realmDriver ); 275 } 276 } 277 278 // if( dList.isEmpty() ) { dList.add( DEFAULT_DRIVER ); } 279 if( driverList.isEmpty() ) { driverList.add( DEFAULT_DRIVER ); } 280 281 // return dList ; 282 } 283 284 /** 285 * EDbidオブジェクト?マップを取得します? 286 * 287 * DB設定XMLファイル(DBConfig.xml)の、dbid タグを取り込みます? 288 * こ?ファイルから、EDbidオブジェクト?属???を取得し、オブジェクトを構築します? 289 * 290 * EDbidオブジェク?は、?期?をコピ?して、作?して?ます? 291 * EDbidオブジェクトをマップから取り?すキーとなる?dbidKey は、大?化して設定します? 292 * 293 * @og.rev 5.1.7.0 (2010/06/01) org.opengion.fukurou.xml.jaxb.dbid 関?? 294 * @og.rev 5.1.9.0 (2010/08/01) Mapを返すように変更 295 * @og.rev 5.5.2.0 (2012/05/01) property追? 296 * @og.rev 5.6.6.0 (2013/07/05) 表?title)属?の取? 297 * @og.rev 5.6.7.0 (2013/07/27) ?MapをDBConfig.xmlの読み込み?変更? 298 * @og.rev 5.6.7.0 (2013/07/27) dbidMap を書き込??ではなく?作?します? 299 * @og.rev 5.6.7.0 (2013/07/27) synchronized メソ?にします? 300 * @og.rev 5.6.7.1 (2013/08/09) DEFAULT と、RESOURCE の DBIDキーがなければ、?部?作?します? 301 * @og.rev 5.6.8.0 (2013/09/06) RESOURCE の DBIDキーを?部作?時に、title も設定します? 302 * 303 * @param element DB設定XMLファイルのElementオブジェク? 304 * @param defDdbid 初期??の設定された、EDbidオブジェク? 305 */ 306 // private void makeDbidMap( final Element element , EDbid defDdbid ) { 307 // private static Map<String,EDbid> getDbidMap( final Element element , EDbid defDdbid ) { 308 private void makeDbidMap( final Element element , EDbid defDdbid ) { 309 // Map<String,EDbid> dMap = new HashMap<String,EDbid>(); // 5.6.7.0 (2013/07/27) 310 311 NodeList list = element.getElementsByTagName( "dbid" ) ; 312 int num = list.getLength(); 313 for (int i = 0; i < num; i++) { 314 Element ele = (Element)list.item(i); 315 NodeList childs = ele.getChildNodes(); 316 int numChild = childs.getLength(); 317 // EDbid dbid = new EDbid(); 318 EDbid dbid = defDdbid.clone(); // 初期値をコピ?して、作? 319 for (int j = 0; j < numChild; j++) { 320 Node nd = childs.item(j); 321 if( nd.getNodeType() == Node.ELEMENT_NODE ) { 322 Element el = (Element)nd; 323 String tag = el.getTagName(); 324 // dbidKey は、toUpperCase して、大??みとする? 325 if( "dbidKey".equals( tag ) ) { 326 String dbidKey = el.getTextContent(); 327 if( dbidKey != null && dbidKey.length() > 0 ) { 328 dbid.setDbidKey( dbidKey.toUpperCase( Locale.JAPAN ) ); 329 } 330 } 331 else if( "title".equals( tag ) ) { dbid.setTitle( el.getTextContent() ); } // 5.6.6.0 (2013/07/05) 表?title)属?の取? 332 else if( "url".equals( tag ) ) { dbid.setUrl( el.getTextContent() ); } 333 else if( "user".equals( tag ) ) { dbid.setUser( el.getTextContent() ); } 334 else if( "password".equals( tag ) ) { dbid.setPassword( el.getTextContent() ); } 335 else if( "readonly".equals( tag ) ) { dbid.setReadonly( el.getTextContent() ); } 336 else if( "mincount".equals( tag ) ) { dbid.setMincount( el.getTextContent() ); } 337 else if( "maxcount".equals( tag ) ) { dbid.setMaxcount( el.getTextContent() ); } 338 else if( "pooltime".equals( tag ) ) { dbid.setPooltime( el.getTextContent() ); } 339 else if( "applicationInfo".equals( tag ) ) { dbid.setApplicationInfo( el.getTextContent() ); } 340 else if ("property".equals( tag ) ) { dbid.addProp( el.getTextContent() ); } // 5.5.2.0 (2012/05/01) 341 else { 342 System.err.println( "警告:dbid に新しい属?が?追?れて?す?" ); 343 } 344 } 345 } 346 // dbidMap.put( dbid.getDbidKey(), dbid ); 347 // dMap.put( dbid.getDbidKey(), dbid ); 348 dbidMap.put( dbid.getDbidKey(), dbid ); // 5.6.7.0 (2013/07/27) 復活 349 } 350 351 // 5.6.7.1 (2013/08/09) DEFAULT と、RESOURCE の DBIDキーがなければ、?部?作?します? 352 EDbid dbid_D = dbidMap.get( "DEFAULT" ); // DEFAULT が存在するか確認する? 353 if( dbid_D == null ) { 354 dbid_D = defDdbid.clone(); // 初期値をコピ? 355 dbid_D.setDbidKey( "DEFAULT" ); 356 dbidMap.put( "DEFAULT", dbid_D ); 357 } 358 359 EDbid dbid_R = dbidMap.get( "RESOURCE" ); // RESOURCE が存在するか確認する? 360 if( dbid_R == null ) { 361 dbid_R = dbid_D.clone(); // DEFAULT の DBIDをコピ?(?存在するは? 362 dbid_R.setDbidKey( "RESOURCE" ); 363 dbid_R.setTitle( "RESOURCE" ); // 5.6.8.0 (2013/09/06) title も設定します? 364 dbidMap.put( "RESOURCE", dbid_R ); 365 } 366 367 // return dMap; 368 } 369 370 /* ------------------------------------------------------------------------------------ 371 * 372 * 以下?、DBConfig.xml編?のメソ?です? 373 * 編?のメソ?では、オブジェクト化されたDBID及?DBDRIVERの??は使用せずに? 374 * DBConfig.xmlからそ???を?度読み出して、SET/GETして?す? 375 * (オブジェクトとして依存して?のは、DBConfig.xmlのファイル名?みで? 376 * 377 * ------------------------------------------------------------------------------------- 378 */ 379 /** 380 * DBIDとして管?て??のキーの?を?列形式で返します? 381 * 382 * @og.rev 5.1.9.0 (2010/08/01) 新規作? 383 * 384 * @return ?のキー? 385 */ 386 public static String[] getDbidInfoKeys() { 387 // return DBID_INFO_KEYS; 388 return DBID_INFO_KEYS.clone(); 389 } 390 391 /** 392 * 全てのDBIDの属???のリス?配?)で返します? 393 * 394 * 値の?につ?は、{@link #getDbidInfoKeys()}で返されるキーの?と同じです? 395 * 396 * @og.rev 5.1.9.0 (2010/08/01) 新規作? 397 * @og.rev 5.5.2.1 (2012/05/07) propertiesを?? 398 * @og.rev 5.6.6.0 (2013/07/05) 表?title)属?を追? 399 * @og.rev 5.6.7.0 (2013/07/27) ?MapをDBConfig.xmlの読み込み?変更? 400 * 401 * @return 全てのDBIDの属???のリス?配?) 402 * @see #getDbidInfoKeys() 403 */ 404 public synchronized String[][] getDbidInfo() { 405 // Element ele = DomParser.read( new File(xmlFilename) ).getDocumentElement(); 406 // Map<String,EDbid> dMap = getDbidMap( ele , new EDbid() ); 407 // String[][] dbidInfo = new String[dMap.size()][DBID_INFO_KEYS.length]; 408 409 String[][] dbidInfo = new String[dbidMap.size()][DBID_INFO_KEYS.length]; 410 int idx = 0; 411 // for( EDbid dbid : dMap.values() ) { 412 for( EDbid dbid : dbidMap.values() ) { 413 dbidInfo[idx][0] = dbid.getDbidKey(); 414 dbidInfo[idx][1] = dbid.getTitle(); // 5.6.6.0 (2013/07/05) 表?title)属?を追? 415 dbidInfo[idx][2] = dbid.getUrl(); 416 dbidInfo[idx][3] = dbid.getUser(); 417 dbidInfo[idx][4] = dbid.getPassword(); 418 dbidInfo[idx][5] = String.valueOf( dbid.isReadonly() ); 419 dbidInfo[idx][6] = String.valueOf( dbid.getMincount() ); 420 dbidInfo[idx][7] = String.valueOf( dbid.getMaxcount() ); 421 dbidInfo[idx][8] = String.valueOf( dbid.getPooltime() ); 422 dbidInfo[idx][9] = String.valueOf( dbid.isApplicationInfo() ); 423 dbidInfo[idx][10]= String.valueOf( dbid.getProps().toString() ); // 5.5.2.1 (2012/05/07) 424 idx++; 425 } 426 // 5.6.7.0 (2013/07/27) ?MapをDBConfig.xmlの読み込み?変更(なので、?)? 427 // dbidSort( dbidInfo ); 428 return dbidInfo; 429 } 430 431 /** 432 * 全てのDBIDの属???のリス?配?)をセ?します? 433 * 434 * こ?メソ?を呼び出すと、DBConfig.xmlで定義されて?DBID?????削除?? 435 * そ?上で、引数のDBID???をDBConfig.xmlに書き込みます? 436 * 437 * 値の?につ?は、{@link #getDbidInfoKeys()}で返されるキーの?と同じです? 438 * 439 * 書き込みの直前に、同じフォル?タイ?タンプを付加したバックア??ファイルを作?します? 440 * 441 * @og.rev 5.1.9.0 (2010/08/01) 新規作? 442 * @og.rev 5.6.7.0 (2013/07/27) ?MapをDBConfig.xmlの読み込み?変更? 443 * 444 * @param dbidVals 全てのDBIDの属???のリス?配?) 445 * @see #getDbidInfoKeys() 446 */ 447 public void setDbidInfo( final String[][] dbidVals ) { 448 FileUtil.copy( xmlFilename, xmlFilename + "_" + System.currentTimeMillis() ); 449 450 Document doc = DomParser.read( new File(xmlFilename) ) ; 451 Element firstRoot = doc.getDocumentElement(); 452 deleteChildElements( firstRoot, "dbid" ); 453 454 if( dbidVals != null && dbidVals.length > 0 ) { 455 // 5.6.7.0 (2013/07/27) ?MapをDBConfig.xmlの読み込み?変更(なので、?)? 456 // dbidSort( dbidVals ); 457 for( int i=0; i<dbidVals.length; i++ ) { 458 Element newEle = doc.createElement( "dbid" ); 459 for( int j=0; j<dbidVals[i].length; j++ ) { 460 Element newChEle = doc.createElement( DBID_INFO_KEYS[j] ); 461 newChEle.setTextContent( dbidVals[i][j] ); 462 newEle.appendChild( newChEle ); 463 } 464 firstRoot.appendChild( newEle ); 465 firstRoot.appendChild( doc.createTextNode( "\n\n" ) ); 466 } 467 } 468 469 DomParser.write( new File(xmlFilename), doc ); 470 471 reload(); // 5.6.7.0 (2013/07/27) DBIDの属???のリストを更新後?初期化します? 472 } 473 474 /** 475 * DBIDの配?をソートします? 476 * ソート?方法としては? 477 * ?EFAULTのDBIDは?初め 478 * ②DEFAULT以外?、DBID? 479 * となります? 480 * 481 * @og.rev 5.6.7.0 (2013/07/27) ?MapをDBConfig.xmlの読み込み?変更(なので、?)? 482 * 483 * @param dbidVals 全てのDBIDの属???のリス?配?) 484 */ 485 // private static void dbidSort( final String[][] dbidVals ) { 486 // Arrays.sort( dbidVals, new Comparator<String[]>() { 487 // public int compare( String[] s1, String[] s2 ) { 488 // if( "DEFAULT".equals( s1[0] ) ) { 489 // return -1; 490 // } 491 // else if( "DEFAULT".equals( s2[0] ) ) { 492 // return 1; 493 // } 494 // return s1[0].compareTo( s2[0] ); 495 // } 496 // } 497 // ); 498 // } 499 500 /** 501 * DBドライバ?の属?キーを返します? 502 * 503 * @og.rev 5.1.9.0 (2010/08/01) 新規作? 504 * 505 * @return DBドライバ?の属?キー 506 */ 507 public static String getDriverKey() { 508 return DBDRIVER_CLASS_KEY; 509 } 510 511 /** 512 * DBドライバ?のリス?配?)を返します? 513 * 514 * @og.rev 5.1.9.0 (2010/08/01) 新規作? 515 * @og.rev 5.6.7.0 (2013/07/27) driverList を書き込??ではなく?作?します? 516 * 517 * @return DBドライバ?リス?配?) 518 */ 519 public synchronized String[] getDriverList() { 520 // Element ele = DomParser.read( new File(xmlFilename) ).getDocumentElement(); 521 // String [] rtn = getDriverList( ele ).toArray( new String[0] ); 522 523 String [] rtn = driverList.toArray( new String[driverList.size()] ); 524 // driverSort( rtn ); 525 return rtn; 526 } 527 528 /** 529 * DBドライバ?のリス?配?)をセ?します? 530 * 531 * こ?メソ?を呼び出すと、DBConfig.xmlで定義されて?class???削除?? 532 * そ?上で、引数のDBドライバ??をDBConfig.xmlに書き込みます? 533 * 534 * 書き込みの直前に、同じフォル?タイ?タンプを付加したバックア??ファイルを作?します? 535 * 536 * @og.rev 5.1.9.0 (2010/08/01) 新規作? 537 * @og.rev 5.6.7.0 (2013/07/27) DBドライバ?のリストを更新後?初期化します? 538 * 539 * @param drivers DBドライバ?のリス?配?) 540 */ 541 public void setDriverList( final String[] drivers ) { 542 FileUtil.copy( xmlFilename, xmlFilename + "_" + System.currentTimeMillis() ); 543 544 Document doc = DomParser.read( new File(xmlFilename) ); 545 Element firstRoot = doc.getDocumentElement(); 546 547 Element parent = (Element)firstRoot.getElementsByTagName( "dbDriver" ).item( 0 ); 548 deleteChildElements( parent, "class" ); 549 550 if( drivers != null && drivers.length > 0 ) { 551 // driverSort( drivers ); 552 for( int i=0; i<drivers.length; i++ ) { 553 Element newEle = doc.createElement( "class" ); 554 newEle.setTextContent( drivers[i] ); 555 parent.appendChild( newEle ); 556 } 557 } 558 559 DomParser.write( new File(xmlFilename), doc ); 560 561 reload(); // 5.6.7.0 (2013/07/27) DBドライバ?のリストを更新後?初期化します? 562 } 563 564 565 /** 566 * DBID??のキーとタイトルから、?ル?ンメニューを作?するための??を取得します? 567 * 568 * こ?メソ?を呼び出すと、DBConfig.xmlで定義されて? dbidKey と?title 属?から? 569 * 「key1:val1 key2:val2 ・・・」と???を作?します? 570 * これを利用すれば、?ル?ンメニューが簡単に作?できます? 571 * 572 * @og.rev 5.6.7.0 (2013/07/27) プル?ンメニュー用の??を作?します? 573 * @og.rev 5.6.7.1 (2013/08/09) 表?title)属?のスペ?ス対? 574 * 575 * @return プル?ンメニューを作?するための?? 576 */ 577 public synchronized String getCodeKeyVal() { 578 if( codeKeyVal == null ) { 579 StringBuilder buf = new StringBuilder(); 580 for( EDbid dbid : dbidMap.values() ) { 581 String key = dbid.getDbidKey(); 582 String lbl = StringUtil.nval( dbid.getTitle() , key ); 583 // buf.append( " " ).append( key ).append( ":" ).append( lbl ); 584 if( lbl.indexOf( ' ' ) >= 0 ) { // 5.6.7.1 (2013/08/09) スペ?ス対? 585 buf.append( " " ).append( key ).append( ":\"" ).append( lbl ).append( "\"" ); 586 } 587 else { 588 buf.append( " " ).append( key ).append( ":" ).append( lbl ); 589 } 590 } 591 592 codeKeyVal = buf.substring( 1 ); // 先?のスペ?スを削除 593 } 594 595 return codeKeyVal; 596 } 597 598 /** 599 * DBドライバ?の配?をソートします? 600 * ソート?方法としては? 601 * ?EFAULT_DRIVERのDBは?初め 602 * ②DEFAULT以外?、DBID? 603 * となります? 604 * 605 * @og.rev 5.6.7.0 (2013/07/27) ?ListをDBConfig.xmlの読み込み?変更(なので、?)? 606 * 607 * @param drivers 全てのDBIDの属???のリス?配?) 608 */ 609 // private static void driverSort( final String[] drivers ) { 610 // Arrays.sort( drivers, new Comparator<String>() { 611 // public int compare( String s1, String s2 ) { 612 // if( DEFAULT_DRIVER.equals( s1 ) ) { 613 // return -1; 614 // } 615 // else if( DEFAULT_DRIVER.equals( s2 ) ) { 616 // return 1; 617 // } 618 // return s1.compareTo( s2 ); 619 // } 620 // } 621 // ); 622 // } 623 624 /** 625 * 親要?基点として、引数で?されたタグ名を持つ子要?削除します? 626 * 627 * @og.rev 5.6.7.0 (2013/07/27) staticメソ? ?イスタンスメソ?に変更 628 * 629 * @param parent 親要? 630 * @param childTagName 削除する子要??タグ? 631 */ 632 // private static void deleteChildElements( final Element parent, final String childTagName ) { 633 private void deleteChildElements( final Element parent, final String childTagName ) { 634 Node child = parent.getFirstChild(); 635 boolean isDel = false; 636 while ( child != null ) { 637 // エレメント間の改行Cも削除するため、次の異なる要?来るまでは削除し続けます? 638 if( child.getNodeType() == Node.ELEMENT_NODE ) { 639 // if( ((Element)child).getTagName().equals( childTagName ) ) { 640 if( ((Element)child).getTagName().equalsIgnoreCase( childTagName ) ) { 641 isDel = true; 642 } 643 else { 644 isDel = false; 645 } 646 } 647 648 Node next = child.getNextSibling(); 649 if( isDel ) { 650 parent.removeChild( child ); 651 } 652 child = next; 653 } 654 } 655 }