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