KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > c3p0 > cfg > DefaultC3P0ConfigFinder


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.c3p0.cfg;
25
26 import java.io.*;
27 import java.util.HashMap JavaDoc;
28 import java.util.Properties JavaDoc;
29 import com.mchange.v2.cfg.MultiPropertiesConfig;
30
31 public class DefaultC3P0ConfigFinder implements C3P0ConfigFinder
32 {
33     final static String JavaDoc XML_CFG_FILE_KEY = "com.mchange.v2.c3p0.cfg.xml";
34
35     public C3P0Config findConfig() throws Exception JavaDoc
36     {
37     C3P0Config out;
38
39     HashMap JavaDoc flatDefaults = C3P0ConfigUtils.extractHardcodedC3P0Defaults();
40
41     // this includes System properties, but we have to check for System properties
42
// again, since we want system properties to override unspecified user, default-config
43
// properties in the XML
44
flatDefaults.putAll( C3P0ConfigUtils.extractC3P0PropertiesResources() );
45
46     String JavaDoc cfgFile = MultiPropertiesConfig.readVmConfig().getProperty( XML_CFG_FILE_KEY );
47     if (cfgFile == null)
48         {
49         C3P0Config xmlConfig = C3P0ConfigXmlUtils.extractXmlConfigFromDefaultResource();
50         if (xmlConfig != null)
51             {
52             insertDefaultsUnderNascentConfig( flatDefaults, xmlConfig );
53             out = xmlConfig;
54             }
55         else
56             out = C3P0ConfigUtils.configFromFlatDefaults( flatDefaults );
57         }
58     else
59         {
60         InputStream is = new BufferedInputStream( new FileInputStream( cfgFile ) );
61         try
62             {
63             C3P0Config xmlConfig = C3P0ConfigXmlUtils.extractXmlConfigFromInputStream( is );
64             insertDefaultsUnderNascentConfig( flatDefaults, xmlConfig );
65             out = xmlConfig;
66             }
67         finally
68             {
69             try {is.close();}
70             catch (Exception JavaDoc e)
71                 { e.printStackTrace(); }
72             }
73         }
74
75     // overwrite default, unspecified user config with System properties
76
// defined values
77
Properties JavaDoc sysPropConfig = C3P0ConfigUtils.findAllC3P0SystemProperties();
78     out.defaultConfig.props.putAll( sysPropConfig );
79
80     return out;
81     }
82
83     private void insertDefaultsUnderNascentConfig(HashMap JavaDoc flatDefaults, C3P0Config config)
84     {
85     flatDefaults.putAll( config.defaultConfig.props );
86     config.defaultConfig.props = flatDefaults;
87     }
88 }
Popular Tags