001: /**
002: * Library name : Primrose - A Java Database Connection Pool.
003: * Published by Ben Keeping, http://primrose.org.uk .
004: * Copyright (C) 2004 Ben Keeping, primrose.org.uk
005: * Email: Use "Contact Us Form" on website
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */package uk.org.primrose;
021:
022: import java.io.*;
023: import java.util.*;
024: import java.sql.*;
025: import java.lang.reflect.*;
026:
027: import uk.org.primrose.pool.CannotConnectException;
028:
029: public class Util {
030:
031: /**
032: * Is the given value a valid pool config parameter ?
033: */
034: public static boolean isConfigParameterValid(String name) {
035: if (name == null)
036: return false;
037: for (String s : Constants.POOL_CONFIG_ITEM_NAMES) {
038: if (name.equals(s))
039: return true;
040: }
041: return false;
042: }
043:
044: /**
045: * Using reflection, invoke a method on a given object
046: * Returns whether it found the method to run or not.
047: */
048: public static boolean callClassMethod(Class targetClass,
049: Object targetObj, String methodNameToRun, Object[] args)
050: throws GeneralException {
051: if (targetObj == null)
052: throw new GeneralException("Target Object is null !");
053: Method[] publicMethods = targetClass.getMethods();
054: boolean foundMethodToRun = false;
055:
056: int argsLength = 0;
057: if (args != null) {
058: argsLength = args.length;
059: }
060:
061: for (int j = 0; j < publicMethods.length; j++) {
062: String methodName = publicMethods[j].getName();
063: int numOfArgs = publicMethods[j].getParameterTypes().length;
064:
065: if (methodNameToRun.equalsIgnoreCase(methodName)
066: && numOfArgs == argsLength) {
067: try {
068: publicMethods[j].invoke(targetObj, args);
069: foundMethodToRun = true;
070: break;
071: } catch (Exception e) {
072: throw new GeneralException("Error invoking "
073: + targetClass.getName() + "."
074: + methodNameToRun, e);
075: }
076: }
077: }
078:
079: return foundMethodToRun;
080: }
081:
082: /**
083: * Using reflection, print out the values of all 'get' methods for an object
084: */
085: public static void printGetMethodValues(String prefixData,
086: Logger logger, Class targetClass, Object targetObj) {
087: if (targetObj == null)
088: return;
089: Method[] publicMethods = targetClass.getMethods();
090:
091: for (int j = 0; j < publicMethods.length; j++) {
092: String methodName = publicMethods[j].getName();
093: int numOfArgs = publicMethods[j].getParameterTypes().length;
094:
095: if (methodName.startsWith("get") && numOfArgs == 0) {
096: try {
097: // Don't print user or password info in the logs
098: if ((methodName.toLowerCase().indexOf(
099: Constants.PASSWORD.toLowerCase()) == -1)
100: && (methodName.toLowerCase().indexOf(
101: Constants.USER.toLowerCase()) == -1)) {
102: String value = (String) publicMethods[j]
103: .invoke(targetObj, new Object[] {});
104: methodName = methodName.substring(3, methodName
105: .length());
106: String camelName = (methodName.charAt(0) + "")
107: .toLowerCase()
108: + methodName.substring(1, methodName
109: .length());
110:
111: logger.info(prefixData + camelName + "='"
112: + value + "'");
113: }
114: } catch (Exception e) {
115: // don't care
116: }
117: }
118: }
119: }
120:
121: /**
122: * Get connection to the database
123: */
124: public static Connection getConnection(String driver, String url,
125: String username, String password)
126: throws CannotConnectException {
127:
128: try {
129: DebugLogger
130: .log("About to load driver into JVM : " + driver);
131: Class.forName(driver);
132: DebugLogger.log("About to get connect from db using url("
133: + driver + "), user(" + username + "), password("
134: + password + ")");
135: Connection c = DriverManager.getConnection(url, username,
136: password);
137: if (c == null) {
138: throw new CannotConnectException(
139: "When getting a new connection from the db, no error was thrown, but the connection was null");
140: }
141:
142: return c;
143: } catch (Exception e) {
144: throw new CannotConnectException(
145: "Cannot connect to db using driver (" + driver
146: + "), url(" + url + "), username("
147: + username + ")", e);
148: }
149:
150: }
151:
152: /**
153: * Load multiple pools from a primrose config file
154: */
155: public static Properties generatePropertiesForPoolName(
156: String configFile, String poolNameToFind)
157: throws IOException {
158: Properties p = null;
159: BufferedReader br = new BufferedReader(new InputStreamReader(
160: new FileInputStream(configFile)));
161: String line;
162: while ((line = br.readLine()) != null) {
163: line = line.trim();
164: if (line.startsWith("#") || line.length() == 0)
165: continue;
166: String[] parts = line.split("=");
167: String key = parts[0];
168: String value = "";
169: if (parts.length > 1) {
170: // handle cases where line is like "name=value&soemthing=value2" - like on driver URLs
171: for (int i = 1; i < parts.length; i++) {
172: if (i == (parts.length - 1)) {
173: value += parts[i];
174: } else {
175: value += (parts[i] + "=");
176: }
177: }
178: }
179: key = key.trim();
180: value = value.trim();
181: if (key.equals("poolName")) {
182:
183: if (p != null) {
184: // more than one pool defined - break out
185: // and return data
186: break;
187: }
188:
189: if (value.equals(poolNameToFind)) {
190: p = new Properties();
191: }
192: }
193:
194: if (p != null)
195: p.setProperty(key, value);
196:
197: }
198:
199: br.close();
200:
201: return p;
202: }
203:
204: }
|