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