User: mulder  
  Date: 00/08/31 09:37:08

  Modified:    src/main/org/jboss/jdbc JDBCDataSourceLoader.java
                        JDBCDataSourceLoaderMBean.java
                        XADataSourceLoader.java
  Log:
  Bring non-transactional JDBC pool up to snuff.  Unlikely you'll ever use
  the jBoss MBean, but it least it works the same (parameters in the
  jboss.jcml file, etc.).  The JDBC pool itself will clear out cached
  PreparedStatements when the connections are closed.
  
  Minor bugfix in the transactional implementation - correctly return JDBC
  connection properties (also not generally used).
  
  Revision  Changes    Path
  1.3       +267 -159  jboss/src/main/org/jboss/jdbc/JDBCDataSourceLoader.java
  
  Index: JDBCDataSourceLoader.java
  ===================================================================
  RCS file: 
/products/cvs/ejboss/jboss/src/main/org/jboss/jdbc/JDBCDataSourceLoader.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- JDBCDataSourceLoader.java 2000/08/18 03:21:04     1.2
  +++ JDBCDataSourceLoader.java 2000/08/31 16:37:07     1.3
  @@ -1,160 +1,268 @@
  -/*
  - * jBoss, the OpenSource EJB server
  - *
  - * Distributable under GPL license.
  - * See terms of license at gnu.org.
  - */
  -package org.jboss.jdbc;
  -
  -import java.lang.reflect.*;
  -import java.util.*;
  -import javax.management.ObjectName;
  -import javax.management.MBeanServer;
  -import javax.naming.*;
  -import javax.sql.DataSource;
  -import org.jboss.logging.LogWriter;
  -import org.jboss.minerva.datasource.JDBCPoolDataSource;
  -import org.jboss.util.*;
  -import org.jboss.logging.Logger;
  -
  -
  -/**
  - * Service that loads a JDBC 1 connection pool.  The constructors are called by
  - * the JMX engine based on your MLET tags.
  - * @version $Revision: 1.2 $
  - * @author Aaron Mulder ([EMAIL PROTECTED])
  - */
  -public class JDBCDataSourceLoader extends ServiceMBeanSupport implements 
JDBCDataSourceLoaderMBean {
  -    private JDBCPoolDataSource source;
  -
  -    public JDBCDataSourceLoader() {
  -    }
  -    public JDBCDataSourceLoader(String poolName, String url, String username, 
String password, Integer minSize, Integer maxSize) {
  -        this(poolName, url, username, password, minSize, maxSize, "");
  -    }
  -    public JDBCDataSourceLoader(String poolName, String url, String username, 
String password, Integer minSize, Integer maxSize, String poolParameters) {
  -        source = new JDBCPoolDataSource();
  -        source.setPoolName(poolName);
  -        source.setJDBCURL(url);
  -        source.setJDBCUser(username);
  -        source.setJDBCPassword(password);
  -        source.setMinSize(minSize.intValue());
  -        source.setMaxSize(maxSize.intValue());
  -        setAdditionalProperties(parseProperties(poolParameters));
  -        try {
  -            source.setLogWriter(new LogWriter(log));
  -        }catch(java.sql.SQLException e) {
  -            Logger.exception(e);
  -        }
  -        source.initialize();
  -    }
  -
  -    public ObjectName getObjectName(MBeanServer parm1, ObjectName parm2) throws 
javax.management.MalformedObjectNameException {
  -        return new ObjectName(OBJECT_NAME+",name="+source.getJNDIName());
  -    }
  -
  -    public String getName() {
  -        return "JDBCDataSource";
  -    }
  -
  -    public void startService() throws Exception {
  -        // Bind in JNDI
  -        bind(new InitialContext(), "jdbc."+source.getPoolName(), source);
  -
  -        log.log("JDBC Connection pool "+source.getPoolName()+" bound to 
jdbc."+source.getPoolName());
  -
  -        // Test database
  -        source.getConnection().close();
  -    }
  -
  -    public void stopService() {
  -        // Unbind from JNDI
  -        try {
  -            new InitialContext().unbind("jdbc."+source.getPoolName());
  -            log.log("JDBC Connection pool "+source.getPoolName()+" removed from 
JNDI");
  -        } catch (NamingException e) {
  -            // Ignore
  -        }
  -    }
  -
  -     // Private -------------------------------------------------------
  -
  -    private void bind(Context ctx, String name, Object val) throws NamingException {
  -        // Bind val to name in ctx, and make sure that all intermediate contexts 
exist
  -        Name n = ctx.getNameParser("").parse(name);
  -        while (n.size() > 1) {
  -            String ctxName = n.get(0);
  -            try {
  -                ctx = (Context)ctx.lookup(ctxName);
  -            } catch (NameNotFoundException e) {
  -                ctx = ctx.createSubcontext(ctxName);
  -            }
  -            n = n.getSuffix(1);
  -        }
  -
  -        ctx.bind(n.get(0), val);
  -    }
  -
  -    private static Properties parseProperties(String string) {
  -        Properties props = new Properties();
  -        if(string == null || string.length() == 0) return props;
  -        int lastPos = -1;
  -        int pos = string.indexOf(";");
  -        while(pos > -1) {
  -            addProperty(props, string.substring(lastPos+1, pos));
  -            lastPos = pos;
  -            pos = string.indexOf(";", lastPos+1);
  -        }
  -        addProperty(props, string.substring(lastPos+1));
  -        return props;
  -    }
  -
  -    private static void addProperty(Properties props, String property) {
  -        int pos = property.indexOf("=");
  -        if(pos < 0) {
  -            System.err.println("Unable to parse property '"+property+"' - please 
use 'name=value'");
  -            return;
  -        }
  -        props.setProperty(property.substring(0, pos), property.substring(pos+1));
  -    }
  -
  -    private void setAdditionalProperties(Properties props) {
  -        Iterator it = props.keySet().iterator();
  -        while(it.hasNext()) {
  -            String property = (String)it.next();
  -            String value = props.getProperty(property);
  -            try {
  -                Class cls = source.getClass();
  -                Method list[] = cls.getMethods();
  -                Method setter = null;
  -                for(int i=0; i<list.length; i++)
  -                    if(list[i].getName().equals("set"+property) && 
list[i].getParameterTypes().length == 1) {
  -                        setter = list[i];
  -                        break;
  -                    }
  -                if(setter == null) throw new NoSuchMethodException("Unable to find 
1-arg setter for property '"+property+"'");
  -                Class argClass = setter.getParameterTypes()[0];
  -                if(argClass.isPrimitive())
  -                    argClass = getPrimitiveClass(argClass);
  -                Constructor con = argClass.getDeclaredConstructor(new 
Class[]{String.class});
  -                Object arg = con.newInstance(new Object[]{value});
  -                setter.invoke(source, new Object[]{arg});
  -            } catch(Exception e) {
  -                log.error("Unable to set pool property '"+property+"' to 
'"+value+"': "+e);
  -                Logger.exception(e);
  -            }
  -        }
  -    }
  -
  -    private static Class getPrimitiveClass(Class source) {
  -        if(source.equals(Boolean.TYPE)) return Boolean.class;
  -        if(source.equals(Integer.TYPE)) return Integer.class;
  -        if(source.equals(Float.TYPE)) return Float.class;
  -        if(source.equals(Long.TYPE)) return Long.class;
  -        if(source.equals(Double.TYPE)) return Double.class;
  -        if(source.equals(Character.TYPE)) return Character.class;
  -        if(source.equals(Short.TYPE)) return Short.class;
  -        if(source.equals(Byte.TYPE)) return Byte.class;
  -        return null;
  -    }
  +/*
  + * jBoss, the OpenSource EJB server
  + *
  + * Distributable under GPL license.
  + * See terms of license at gnu.org.
  + */
  +package org.jboss.jdbc;
  +import java.io.PrintWriter;
  +import java.sql.SQLException;
  +import java.util.Iterator;
  +import java.util.Properties;
  +import javax.management.ObjectName;
  +import javax.management.MBeanServer;
  +import javax.naming.Context;
  +import javax.naming.InitialContext;
  +import javax.naming.Name;
  +import javax.naming.NameNotFoundException;
  +import javax.naming.NamingException;
  +import javax.sql.DataSource;
  +import org.jboss.logging.LogWriter;
  +import org.jboss.minerva.datasource.JDBCPoolDataSource;
  +import org.jboss.util.ServiceMBeanSupport;
  +import org.jboss.logging.Logger;
  +
  +/**
  + * Service that loads a JDBC 1 connection pool.  The constructors are called by
  + * the JMX engine based on your MLET tags.
  + * @version $Revision: 1.3 $
  + * @author Aaron Mulder ([EMAIL PROTECTED])
  + */
  +public class JDBCDataSourceLoader extends ServiceMBeanSupport implements 
JDBCDataSourceLoaderMBean {
  +    private JDBCPoolDataSource source;
  +
  +    public JDBCDataSourceLoader() {
  +    }
  +    public JDBCDataSourceLoader(String poolName) {
  +        source = new JDBCPoolDataSource();
  +        source.setPoolName(poolName);
  +    }
  +
  +    public void setURL(String jdbcURL) {
  +        source.setJDBCURL(jdbcURL);
  +    }
  +
  +    public String getURL() {
  +        return source.getJDBCURL();
  +    }
  +
  +    public void setProperties(String properties) {
  +        Properties props = parseProperties(properties);
  +        source.setJDBCProperties(props);
  +    }
  +
  +    public String getProperties() {
  +        return buildProperties(source.getJDBCProperties());
  +    }
  +
  +    public void setJDBCUser(String userName) {
  +        if(userName != null && userName.length() > 0)
  +            source.setJDBCUser(userName);
  +    }
  +
  +    public String getJDBCUser() {
  +        return source.getJDBCUser();
  +    }
  +
  +    public void setPassword(String password) {
  +        if(password != null && password.length() > 0)
  +            source.setJDBCPassword(password);
  +    }
  +
  +    public String getPassword() {
  +        return source.getJDBCPassword();
  +    }
  +
  +    public void setLoggingEnabled(boolean enabled) {
  +        PrintWriter writer = enabled ? new LogWriter(log) : null;
  +        try {
  +            source.setLogWriter(writer);
  +        } catch(Exception e) {
  +            System.out.println("Unable to set logger for Minerva JDBC Pool!");
  +        }
  +    }
  +
  +    public boolean isLoggingEnabled() {
  +        try {
  +            return source.getLogWriter() != null;
  +        } catch(Exception e) {
  +            return false;
  +        }
  +    }
  +
  +    public void setMinSize(int minSize) {
  +        source.setMinSize(minSize);
  +    }
  +
  +    public int getMinSize() {
  +        return source.getMinSize();
  +    }
  +
  +    public void setMaxSize(int maxSize) {
  +        source.setMaxSize(maxSize);
  +    }
  +
  +    public int getMaxSize() {
  +        return source.getMaxSize();
  +    }
  +
  +    public void setBlocking(boolean blocking) {
  +        source.setBlocking(blocking);
  +    }
  +
  +    public boolean isBlocking() {
  +        return source.isBlocking();
  +    }
  +
  +    public void setGCEnabled(boolean gcEnabled) {
  +        source.setGCEnabled(gcEnabled);
  +    }
  +
  +    public boolean isGCEnabled() {
  +        return source.isGCEnabled();
  +    }
  +
  +    public void setGCInterval(long interval) {
  +        source.setGCInterval(interval);
  +    }
  +
  +    public long getGCInterval() {
  +        return source.getGCInterval();
  +    }
  +
  +    public void setGCMinIdleTime(long idleMillis) {
  +        source.setGCMinIdleTime(idleMillis);
  +    }
  +
  +    public long getGCMinIdleTime() {
  +        return source.getGCMinIdleTime();
  +    }
  +
  +    public void setShrinkingEnabled(boolean enabled) {
  +        source.setShrinkingEnabled(enabled);
  +    }
  +
  +    public boolean isShrinkingEnabled() {
  +        return source.isShrinkingEnabled();
  +    }
  +
  +    public void setShrinkMinIdleTime(long idleMillis) {
  +        source.setShrinkMinIdleTime(idleMillis);
  +    }
  +
  +    public long getShrinkMinIdleTime() {
  +        return source.getShrinkMinIdleTime();
  +    }
  +
  +    public void setShrinkPercent(float percent) {
  +        source.setShrinkPercent(percent);
  +    }
  +
  +    public float getShrinkPercent() {
  +        return source.getShrinkPercent();
  +    }
  +
  +    public void setInvalidateOnError(boolean invalidate) {
  +        source.setInvalidateOnError(invalidate);
  +    }
  +
  +    public boolean isInvalidateOnError() {
  +        return source.isInvalidateOnError();
  +    }
  +
  +    public void setTimestampUsed(boolean timestamp) {
  +        source.setTimestampUsed(timestamp);
  +    }
  +
  +    public boolean isTimestampUsed() {
  +        return source.isTimestampUsed();
  +    }
  +
  +    public ObjectName getObjectName(MBeanServer parm1, ObjectName parm2) throws 
javax.management.MalformedObjectNameException {
  +        return new ObjectName(OBJECT_NAME+",name="+source.getJNDIName());
  +    }
  +
  +    public String getName() {
  +        return "JDBCDataSource";
  +    }
  +
  +    public void startService() throws Exception {
  +        initializePool();
  +    }
  +
  +    public void stopService() {
  +        // Unbind from JNDI
  +        try {
  +            new InitialContext().unbind("jdbc."+source.getPoolName());
  +            log.log("JDBC Connection pool "+source.getPoolName()+" removed from 
JNDI");
  +        } catch (NamingException e) {
  +            // Ignore
  +        }
  +    }
  +
  +     // Private -------------------------------------------------------
  +
  +    private void initializePool() throws NamingException, SQLException {
  +        source.initialize();
  +
  +        // Bind in JNDI
  +        bind(new InitialContext(), "jdbc."+source.getPoolName(), source);
  +
  +        log.log("JDBC Connection pool "+source.getPoolName()+" bound to 
jdbc."+source.getPoolName());
  +
  +        // Test database
  +        source.getConnection().close();
  +    }
  +
  +    private void bind(Context ctx, String name, Object val) throws NamingException {
  +        // Bind val to name in ctx, and make sure that all intermediate contexts 
exist
  +        Name n = ctx.getNameParser("").parse(name);
  +        while (n.size() > 1) {
  +            String ctxName = n.get(0);
  +            try {
  +                ctx = (Context)ctx.lookup(ctxName);
  +            } catch (NameNotFoundException e) {
  +                ctx = ctx.createSubcontext(ctxName);
  +            }
  +            n = n.getSuffix(1);
  +        }
  +
  +        ctx.bind(n.get(0), val);
  +    }
  +
  +    private static Properties parseProperties(String string) {
  +        Properties props = new Properties();
  +        if(string == null || string.length() == 0) return props;
  +        int lastPos = -1;
  +        int pos = string.indexOf(";");
  +        while(pos > -1) {
  +            addProperty(props, string.substring(lastPos+1, pos));
  +            lastPos = pos;
  +            pos = string.indexOf(";", lastPos+1);
  +        }
  +        addProperty(props, string.substring(lastPos+1));
  +        return props;
  +    }
  +
  +    private static void addProperty(Properties props, String property) {
  +        int pos = property.indexOf("=");
  +        if(pos < 0) {
  +            System.err.println("Unable to parse property '"+property+"' - please 
use 'name=value'");
  +            return;
  +        }
  +        props.setProperty(property.substring(0, pos), property.substring(pos+1));
  +    }
  +
  +    private static String buildProperties(Properties props) {
  +        StringBuffer buf = new StringBuffer();
  +        Iterator it = props.keySet().iterator();
  +        Object key;
  +        while(it.hasNext()) {
  +            key = it.next();
  +            if(buf.length() > 0)
  +                buf.append(';');
  +            buf.append(key).append('=').append(props.get(key));
  +        }
  +        return buf.toString();
  +    }
   }
  
  
  
  1.2       +32 -0     jboss/src/main/org/jboss/jdbc/JDBCDataSourceLoaderMBean.java
  
  Index: JDBCDataSourceLoaderMBean.java
  ===================================================================
  RCS file: 
/products/cvs/ejboss/jboss/src/main/org/jboss/jdbc/JDBCDataSourceLoaderMBean.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- JDBCDataSourceLoaderMBean.java    2000/06/02 13:48:38     1.1
  +++ JDBCDataSourceLoaderMBean.java    2000/08/31 16:37:08     1.2
  @@ -13,4 +13,36 @@
      public static final String OBJECT_NAME = ":service=JDBCDataSource";
   
      // Public --------------------------------------------------------
  +    public void setURL(String url);
  +    public String getURL();
  +    public void setJDBCUser(String userName);
  +    public String getJDBCUser();
  +    public void setPassword(String password);
  +    public String getPassword();
  +    public void setProperties(String properties);
  +    public String getProperties();
  +    public void setLoggingEnabled(boolean enabled);
  +    public boolean isLoggingEnabled();
  +    public void setMinSize(int minSize);
  +    public int getMinSize();
  +    public void setMaxSize(int maxSize);
  +    public int getMaxSize();
  +    public void setBlocking(boolean blocking);
  +    public boolean isBlocking();
  +    public void setGCEnabled(boolean gcEnabled);
  +    public boolean isGCEnabled();
  +    public void setGCInterval(long interval);
  +    public long getGCInterval();
  +    public void setGCMinIdleTime(long idleMillis);
  +    public long getGCMinIdleTime();
  +    public void setShrinkingEnabled(boolean enabled);
  +    public boolean isShrinkingEnabled();
  +    public void setShrinkMinIdleTime(long idleMillis);
  +    public long getShrinkMinIdleTime();
  +    public void setShrinkPercent(float percent);
  +    public float getShrinkPercent();
  +    public void setInvalidateOnError(boolean invalidate);
  +    public boolean isInvalidateOnError();
  +    public void setTimestampUsed(boolean timestamp);
  +    public boolean isTimestampUsed();
   }
  
  
  
  1.7       +27 -2     jboss/src/main/org/jboss/jdbc/XADataSourceLoader.java
  
  Index: XADataSourceLoader.java
  ===================================================================
  RCS file: 
/products/cvs/ejboss/jboss/src/main/org/jboss/jdbc/XADataSourceLoader.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- XADataSourceLoader.java   2000/08/30 12:41:53     1.6
  +++ XADataSourceLoader.java   2000/08/31 16:37:08     1.7
  @@ -9,6 +9,7 @@
   import java.io.PrintWriter;
   import java.lang.reflect.Method;
   import java.sql.SQLException;
  +import java.util.Iterator;
   import java.util.Properties;
   import javax.management.ObjectName;
   import javax.management.MBeanServer;
  @@ -28,7 +29,7 @@
    * pool generates connections that are registered with the current Transaction
    * and support two-phase commit.  The constructors are called by the JMX engine
    * based on your MLET tags.
  - * @version $Revision: 1.6 $
  + * @version $Revision: 1.7 $
    * @author Aaron Mulder ([EMAIL PROTECTED])
    */
   public class XADataSourceLoader extends ServiceMBeanSupport
  @@ -90,7 +91,18 @@
       }
   
       public String getProperties() {
  -        return "";
  +        XADataSource vendorSource = (XADataSource)source.getDataSource();
  +        try {
  +            Class cls = vendorSource.getClass();
  +            Method getProperties = cls.getMethod("getProperties", new Class[0]);
  +            Properties result = (Properties) getProperties.invoke(vendorSource, new 
Object[0]);
  +            if(result == null)
  +                return "";
  +            else
  +                return buildProperties(result);
  +        } catch(Exception e) {
  +            return "";
  +        }
       }
   
       public void setJDBCUser(String userName) {
  @@ -303,5 +315,18 @@
               return;
           }
           props.setProperty(property.substring(0, pos), property.substring(pos+1));
  +    }
  +
  +    private static String buildProperties(Properties props) {
  +        StringBuffer buf = new StringBuffer();
  +        Iterator it = props.keySet().iterator();
  +        Object key;
  +        while(it.hasNext()) {
  +            key = it.next();
  +            if(buf.length() > 0)
  +                buf.append(';');
  +            buf.append(key).append('=').append(props.get(key));
  +        }
  +        return buf.toString();
       }
   }
  
  
  

Reply via email to