/** * Constructs a Database object from a set of properties * * @param props the properties by which this Database is configured * @throws ClassNotFoundException if there is a class in props that cannot be found */ protected Database(Properties props) throws ClassNotFoundException { settings = props; configure(props); try { LOG.info( "Creating new Database " + getURL() + "(" + toString() + ") with ClassLoader " + getClass().getClassLoader() + " and parallelism " + parallel); } catch (Exception e) { LOG.info("Creating new invalid Database with ClassLoader " + getClass().getClassLoader(), e); } ShutdownHook.registerObject(new WeakReference<Database>(this)); }
/** * Configures a datasource from a Properties object * * @param props the properties for configuring the Database * @throws ClassNotFoundException if the class given in the properties file cannot be found * @throws IllegalArgumentException if the configuration properties are empty * @throws NullPointerException if props is null */ protected void configure(Properties props) throws ClassNotFoundException { if (props == null) { throw new NullPointerException("Props cannot be null"); } if (props.size() == 0) { throw new IllegalArgumentException("No configuration details"); } Properties subProps = new Properties(); for (Map.Entry<Object, Object> entry : props.entrySet()) { String propertyName = (String) entry.getKey(); String propertyValue = (String) entry.getValue(); Field field = null; // Get the first part of the string - this is the attribute we are taking about String attribute = propertyName; String subAttribute = ""; int index = propertyName.indexOf("."); if (index != -1) { attribute = propertyName.substring(0, index); subAttribute = propertyName.substring(index + 1); } try { field = Database.class.getDeclaredField(attribute); } catch (Exception e) { LOG.warn("Ignoring field for Database: " + attribute); // Ignore this property - no such field continue; } if ("class".equals(subAttribute)) { // make a new instance of this class for this attribute Class<?> clazz = Class.forName(propertyValue.toString()); Object obj; try { obj = clazz.newInstance(); } catch (Exception e) { throw new ClassNotFoundException( "Cannot instantiate class " + clazz.getName() + " " + e.getMessage()); } // Set the field to this newly instantiated class try { field.set(this, obj); } catch (Exception e) { continue; } } else if ("".equals(subAttribute)) { // Set this attribute directly try { field.set(this, propertyValue); } catch (Exception e) { continue; } } else { // Set parameters on the attribute Method m = null; // Set this configuration parameter on the DataSource; try { // Strings first Object o = field.get(this); // Sometimes the class will not have been instantiated yet if (o == null) { subProps.put(propertyName, propertyValue); continue; } Class<?> clazz = o.getClass(); m = clazz.getMethod( "set" + StringUtil.capitalise(subAttribute), new Class[] {String.class}); if (m != null) { m.invoke(field.get(this), new Object[] {propertyValue}); } // now integers } catch (Exception e) { // Don't do anything - either the method not found or cannot be invoked } try { if (m == null) { m = field .get(this) .getClass() .getMethod( "set" + StringUtil.capitalise(subAttribute), new Class[] {int.class}); if (m != null) { m.invoke(field.get(this), new Object[] {Integer.valueOf(propertyValue.toString())}); } } } catch (Exception e) { // Don't do anything - either the method not found or cannot be invoked } } if (subProps.size() > 0) { configure(subProps); } } }