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    }