FileDocCategorySizeDatePackage
ConnectionDefinitionUtils.javaAPI DocGlassfish v2 API10555Mon Jul 16 02:17:24 BST 2007com.sun.enterprise.connectors.util

ConnectionDefinitionUtils

public class ConnectionDefinitionUtils extends Object
A simple class to get the properties of a ConnectionDefinition class , that could be overridden by the administrator during deployment.
author
Sivakumar Thyagarjan

Fields Summary
static Logger
_logger
Constructors Summary
Methods Summary
private static voidaddDefaultJDBCProperties(java.util.Map map)

    String[] defaultProperties = {
             "databaseName", "serverName", "portNumber", "networkProtocol",
             "user", "password", "roleName", "datasourceName" };

        //assuming that the provided map is not null
        for(int i=0; i<defaultProperties.length; i++){
            map.put(defaultProperties[i],null);
        }
    
public static java.util.SetgetConnectionDefinitionProperties(java.lang.String connectionDefinitionClassName)
Gets the properties of the Java bean connection definition class that have setter methods defined

param
connectionDefinitionClassName The Connection Definition Java bean class for which overrideable properties are required.
return
A Set of properties that have a setter method defined in the Connection Definition class


                                                                                                         	 
         
        TreeMap propertySet= new TreeMap();
        try {
            Method[] methods=
                Thread.currentThread().getContextClassLoader().loadClass(connectionDefinitionClassName).getMethods();
            for (int i= 0; i < methods.length; i++) {
                //Method starts with "set" and has only one parameter and has
                // a
                // valid argument
                if (isValidSetterMethod(methods[i])) {
                    String name= methods[i].getName();
                    String propertyName=
                        name.substring(
                            (name.indexOf("set") + "set".length()),
                            name.length());
                    propertySet.put(propertyName, propertyName);
                }
            }
        } catch (SecurityException e) {
            handleException(e);
        } catch (ClassNotFoundException e) {
            handleException(e);
        }
        ignoreOracle10gProperties(connectionDefinitionClassName, propertySet);
        return propertySet.keySet();
    
public static java.util.MapgetConnectionDefinitionPropertiesAndDefaults(java.lang.String connectionDefinitionClassName)
Gets the properties of the Java bean connection definition class that have setter methods defined and the default values as provided by the Connection Definition java bean developer.
This util method is used to get properties of jdbc-data-source
To get Connection definition properties for Connector Connection Pool, use ConnectorRuntime.getMCFConfigProperties()
When the connection definition class is not found, standard JDBC properties (of JDBC 3.0 Specification) will be returned.

param
connectionDefinitionClassName The Connection Definition Java bean class for which overrideable properties are required.
return
Map [property, defaultValue]

        Set s= getConnectionDefinitionProperties(connectionDefinitionClassName);
	TreeMap hm= new TreeMap();
        Class connectionDefinitionClass;
        try {
            connectionDefinitionClass=
                Thread.currentThread().getContextClassLoader().loadClass(connectionDefinitionClassName);
            Object obj= connectionDefinitionClass.newInstance();
            for (Iterator iter= s.iterator(); iter.hasNext();) {
                String property= (String) iter.next();
                Object defaultVal= null;
                try {
                    Method m=
                        connectionDefinitionClass.getMethod(
                            "get" + property,
                            new Class[] {});
                    defaultVal= m.invoke(obj, new Object[] {});
                    //ignore these exceptions. Some drivers have a setter but
                    // no getters for properties [example the password property
                    // in the OracleDataSource
                } catch (NoSuchMethodException e) {
                    //ignore
                } catch (IllegalArgumentException e) {
                    //ignore
                } catch (InvocationTargetException e) {
                    //ignore
                }
                //If the property does not have a corresponding getter method,
                // a null is placed as the default value.
                hm.put(property, defaultVal);
            }
        } catch (ClassNotFoundException e) {
            handleException(e);
            //since the specified connectionDefinitionClassName is not found, 
            //return the standard JDBC properties
            addDefaultJDBCProperties(hm);
        } catch (InstantiationException e) {
            handleException(e);
        } catch (IllegalAccessException e) {
            handleException(e);
        } catch (SecurityException e) {
            handleException(e);
        }
        return hm;
    
private static voidhandleException(java.lang.Exception ex)

        ex.printStackTrace();
        _logger.log(
            Level.SEVERE,
            "Exception while trying to find properties ",
            ex.getMessage());
    
private static voidignoreOracle10gProperties(java.lang.String className, java.util.Map map)
Oracle 10g (10.x) jdbc driver has two properties, connectionCachingEnabled, fastConnectionFailoverEnabled when set with their default values(false) throws exceptions. This workaround (IT 2525) will remove these properties during connection pool creation.

param
className DataSource Classname
param
map list of properties


           Set<String> oracleClasses = new HashSet<String>();
           oracleClasses.add("oracle.jdbc.pool.oracledatasource");
           oracleClasses.add("oracle.jdbc.pool.oracleconnectionpooldatasource");
           oracleClasses.add("oracle.jdbc.xa.client.oraclexadatasource");
           oracleClasses.add("oracle.jdbc.xa.oraclexadataSource");
           if(oracleClasses.contains(className.toLowerCase())){
               boolean property1Removed = removePropertyFromMap("connectionCachingEnabled", map);
               boolean property2Removed = removePropertyFromMap("fastConnectionFailoverEnabled",map);
               if(property1Removed || property2Removed){
                   _logger.log(Level.FINE, "Removing properties 'connectionCachingEnabled'," +
                       " 'fastConnectionFailoverEnabled' from Datasource : " + className );
               }
           }
       
private static booleanisValidArgumentType(java.lang.reflect.Method method)

        Class[] parameters= method.getParameterTypes();
        boolean isValid= true;
        for (int i= 0; i < parameters.length; i++) {
            Class param= parameters[i];
            if (!(param.isPrimitive() || param.equals(String.class)))
                return false;
        }
        return isValid;
    
private static booleanisValidSetterMethod(java.lang.reflect.Method method)

        return (
            (method.getName().startsWith("set"))
                && (method.getParameterTypes().length == 1)
                && (isValidArgumentType(method)));
    
public static voidmain(java.lang.String[] args)


        //oracle.jdbc.xa.client.OracleXADataSource
        //com.pointbase.jdbc.jdbcDataSource
        Map m=
            ConnectionDefinitionUtils
                .getConnectionDefinitionPropertiesAndDefaults(
                "sun.jdbc.odbc.ee.DataSource");

        for (Iterator iter= m.keySet().iterator(); iter.hasNext();) {
            String element= (String) iter.next();
            System.out.println(element + " : " + m.get(element));
        }
    
private static booleanremovePropertyFromMap(java.lang.String property, java.util.Map map)

        boolean  entryRemoved = false ;
        Iterator iterator = map.keySet().iterator();
        while(iterator.hasNext()){
            String key = (String)iterator.next();
            if(property.equalsIgnoreCase(key)){
                iterator.remove();
                entryRemoved = true;
            }
        }
        return entryRemoved;