KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.*;
28 import java.util.*;
29 import com.mchange.v2.cfg.*;
30 import com.mchange.v2.log.*;
31 import com.mchange.v2.c3p0.impl.*;
32
33 public final class C3P0ConfigUtils
34 {
35     public final static String JavaDoc PROPS_FILE_RSRC_PATH = "/c3p0.properties";
36     public final static String JavaDoc PROPS_FILE_PROP_PFX = "c3p0.";
37     public final static int PROPS_FILE_PROP_PFX_LEN = 5;
38
39     private final static String JavaDoc[] MISSPELL_PFXS = {"/c3pO", "/c3po", "/C3P0", "/C3PO"};
40     
41     final static MLogger logger = MLog.getLogger( C3P0ConfigUtils.class );
42     
43     static
44     {
45         if ( logger.isLoggable(MLevel.WARNING) && C3P0ConfigUtils.class.getResource( PROPS_FILE_RSRC_PATH ) == null )
46         {
47             // warn on a misspelling... its an ugly way to do this, but since resources are not listable...
48
for (int i = 0; i < MISSPELL_PFXS.length; ++i)
49             {
50                 String JavaDoc test = MISSPELL_PFXS[i] + ".properties";
51                 if (C3P0ConfigUtils.class.getResource( MISSPELL_PFXS[i] + ".properties" ) != null)
52                 {
53                     logger.warning("POSSIBLY MISSPELLED c3p0.properties CONFIG RESOURCE FOUND. " +
54                                    "Please ensure the file name is c3p0.properties, all lower case, " +
55                                    "with the digit 0 (NOT the letter O) in c3p0. It should be placed " +
56                                    " in the top level of c3p0's effective classpath.");
57                     break;
58                 }
59             }
60         }
61     }
62
63     public static HashMap extractHardcodedC3P0Defaults(boolean stringify)
64     {
65     HashMap out = new HashMap();
66
67     try
68         {
69         Method[] methods = C3P0Defaults.class.getMethods();
70         for (int i = 0, len = methods.length; i < len; ++i)
71             {
72             Method m = methods[i];
73             int mods = m.getModifiers();
74             if ((mods & Modifier.PUBLIC) != 0 && (mods & Modifier.STATIC) != 0 && m.getParameterTypes().length == 0)
75                 {
76                 if (stringify)
77                     {
78                     Object JavaDoc val = m.invoke( null, null );
79                     if ( val != null )
80                         out.put( m.getName(), String.valueOf( val ) );
81                     }
82                 else
83                     out.put( m.getName(), m.invoke( null, null ) );
84                 }
85             }
86         }
87     catch (Exception JavaDoc e)
88         {
89         logger.log( MLevel.WARNING, "Failed to extract hardcoded default config!?", e );
90         }
91
92     return out;
93     }
94
95     public static HashMap extractHardcodedC3P0Defaults()
96     { return extractHardcodedC3P0Defaults( true ); }
97
98     public static HashMap extractC3P0PropertiesResources()
99     {
100     HashMap out = new HashMap();
101
102 // Properties props = findResourceProperties();
103
// props.putAll( findAllC3P0Properties() );
104

105     Properties props = findAllC3P0Properties();
106     for (Iterator ii = props.keySet().iterator(); ii.hasNext(); )
107         {
108         String JavaDoc key = (String JavaDoc) ii.next();
109         String JavaDoc val = (String JavaDoc) props.get(key);
110         if ( key.startsWith(PROPS_FILE_PROP_PFX) )
111             out.put( key.substring(PROPS_FILE_PROP_PFX_LEN).trim(), val.trim() );
112         }
113
114     return out;
115     }
116
117     public static C3P0Config configFromFlatDefaults(HashMap flatDefaults)
118     {
119     NamedScope defaults = new NamedScope();
120     defaults.props.putAll( flatDefaults );
121     
122     HashMap configNamesToNamedScopes = new HashMap();
123     
124     return new C3P0Config( defaults, configNamesToNamedScopes );
125     }
126     
127     public static String JavaDoc getPropFileConfigProperty( String JavaDoc prop )
128     { return MultiPropertiesConfig.readVmConfig().getProperty( prop ); }
129
130     private static Properties findResourceProperties()
131     { return MultiPropertiesConfig.readVmConfig().getPropertiesByResourcePath(PROPS_FILE_RSRC_PATH); }
132
133     private static Properties findAllC3P0Properties()
134     { return MultiPropertiesConfig.readVmConfig().getPropertiesByPrefix("c3p0"); }
135
136     static Properties findAllC3P0SystemProperties()
137     {
138     Properties out = new Properties();
139
140     SecurityException JavaDoc sampleExc = null;
141     try
142         {
143         for (Iterator ii = C3P0Defaults.getKnownProperties().iterator(); ii.hasNext(); )
144             {
145             String JavaDoc key = (String JavaDoc) ii.next();
146             String JavaDoc prefixedKey = "c3p0." + key;
147             String JavaDoc value = System.getProperty( prefixedKey );
148             if (value != null && value.trim().length() > 0)
149                 out.put( key, value );
150             }
151         }
152     catch (SecurityException JavaDoc e)
153         { sampleExc = e; }
154
155     return out;
156     }
157
158     private C3P0ConfigUtils()
159     {}
160 }
Popular Tags