[jboss-cvs] jboss-profiler/java/src/expansion/org/jboss/profiler/agent/interceptor ...

Takuro Okada t2-okada at nri.co.jp
Wed Apr 11 07:13:09 EDT 2007


  User: tokada  
  Date: 07/04/11 07:13:09

  Added:       java/src/expansion/org/jboss/profiler/agent/interceptor    
                        Tag: JBossProfiler_Expansion RequestSequence.java
                        ServletInterceptor.java DriverWrapper.java
                        JdbcInterceptor.java
  Log:
  Moved to another directory
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.1   +53 -0     jboss-profiler/java/src/expansion/org/jboss/profiler/agent/interceptor/Attic/RequestSequence.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: RequestSequence.java
  ===================================================================
  RCS file: RequestSequence.java
  diff -N RequestSequence.java
  --- /dev/null	1 Jan 1970 00:00:00 -0000
  +++ RequestSequence.java	11 Apr 2007 11:13:09 -0000	1.1.2.1
  @@ -0,0 +1,53 @@
  +/*
  + * JBoss, Home of Professional Open Source
  + * Copyright 2006, JBoss Inc., and individual contributors as indicated
  + * by the @authors tag. See the copyright.txt in the distribution for a
  + * full listing of individual contributors.
  + *
  + * This is free software; you can redistribute it and/or modify it
  + * under the terms of the GNU Lesser General Public License as
  + * published by the Free Software Foundation; either version 2.1 of
  + * the License, or (at your option) any later version.
  + *
  + * This software is distributed in the hope that it will be useful,
  + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  + * Lesser General Public License for more details.
  + *
  + * You should have received a copy of the GNU Lesser General Public
  + * License along with this software; if not, write to the Free
  + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  + * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  + */
  +
  +package org.jboss.profiler.agent.interceptor;
  +
  +import java.util.concurrent.atomic.AtomicLong;
  +
  +/**
  + * 
  + * @author Takuro Okada (Nomura Research Institute, Ltd.)
  + * Copyright 2006 Nomura Research Institute, Ltd. All Rights Reserved.
  + * Copyright(c) Information-technology Promotion Agency, Japan. All rights reserved 2006.
  + * Result of Open Source Software Development Activities of Information-technology Promotion Agency, Japan.
  + */
  +public class RequestSequence {
  +    private static AtomicLong currentValue = new AtomicLong();
  +
  +    private static ThreadLocal<Long> sequence = new ThreadLocal<Long>(){
  +        @Override
  +        protected Long initialValue() {
  +            return currentValue.incrementAndGet();
  +        }
  +        
  +    };
  +
  +    public static long get() {
  +        return sequence.get();
  +    }
  +    
  +    public static long getNext() {
  +        sequence.set(currentValue.incrementAndGet());
  +        return sequence.get();
  +    }
  +}
  
  
  
  1.1.2.1   +213 -0    jboss-profiler/java/src/expansion/org/jboss/profiler/agent/interceptor/Attic/ServletInterceptor.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ServletInterceptor.java
  ===================================================================
  RCS file: ServletInterceptor.java
  diff -N ServletInterceptor.java
  --- /dev/null	1 Jan 1970 00:00:00 -0000
  +++ ServletInterceptor.java	11 Apr 2007 11:13:09 -0000	1.1.2.1
  @@ -0,0 +1,213 @@
  +/*
  + * JBoss, Home of Professional Open Source
  + * Copyright 2006, JBoss Inc., and individual contributors as indicated
  + * by the @authors tag. See the copyright.txt in the distribution for a
  + * full listing of individual contributors.
  + *
  + * This is free software; you can redistribute it and/or modify it
  + * under the terms of the GNU Lesser General Public License as
  + * published by the Free Software Foundation; either version 2.1 of
  + * the License, or (at your option) any later version.
  + *
  + * This software is distributed in the hope that it will be useful,
  + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  + * Lesser General Public License for more details.
  + *
  + * You should have received a copy of the GNU Lesser General Public
  + * License along with this software; if not, write to the Free
  + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  + * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  + */
  +
  +package org.jboss.profiler.agent.interceptor;
  +
  +import java.io.IOException;
  +import java.util.Map;
  +import java.util.concurrent.ArrayBlockingQueue;
  +import java.util.concurrent.RejectedExecutionException;
  +import java.util.concurrent.ThreadPoolExecutor;
  +import java.util.concurrent.TimeUnit;
  +import java.util.concurrent.atomic.AtomicBoolean;
  +
  +import javax.servlet.Filter;
  +import javax.servlet.FilterChain;
  +import javax.servlet.FilterConfig;
  +import javax.servlet.ServletException;
  +import javax.servlet.ServletRequest;
  +import javax.servlet.ServletResponse;
  +import javax.servlet.http.HttpServletRequest;
  +
  +import org.apache.log4j.Logger;
  +import org.jboss.profiler.agent.collector.MetricCollector;
  +import org.jboss.profiler.agent.collector.MetricCollectorFactory;
  +
  +/**
  + * The interceptor to profile a method of servlet filter.
  + * 
  + * @see org.jboss.profiler.production.interceptors.http.ServletFilterInterceptor
  + * @author Takuro Okada (Nomura Research Institute, Ltd.)
  + * Copyright 2006 Nomura Research Institute, Ltd. All Rights Reserved.
  + * Copyright(c) Information-technology Promotion Agency, Japan. All rights reserved 2006.
  + * Result of Open Source Software Development Activities of Information-technology Promotion Agency, Japan.
  + */
  +public class ServletInterceptor implements Filter {
  +
  +    private static final String DEFAULT_CATEGORY = "Http Requests";
  +    
  +    private static Logger logger = Logger.getLogger(ServletInterceptor.class);
  +    
  +    private AtomicBoolean initialized = new AtomicBoolean(false);
  +    
  +    /*
  +     * asynchronous executor
  +     */
  +    private ThreadPoolExecutor asyncExecutor = null;
  +    
  +    /*
  +     * parameters
  +     */
  +    private String category = DEFAULT_CATEGORY;
  +    private boolean async = true;
  +    
  +    /*
  +     * metric factory
  +     */
  +    private MetricCollectorFactory factory = null;
  +    
  +    /**
  +     * Create collector factory object.
  +     */
  +	public void init(FilterConfig filterConfig) throws ServletException {
  +        if(initialized.get()) return;
  +        
  +        String async = filterConfig.getInitParameter("Async");
  +        if(async!=null) this.async = Boolean.valueOf(async);
  +        
  +        String category = filterConfig.getInitParameter("Category");
  +        if(category!=null) this.category = category;
  +        
  +		String collectorFactoryName = filterConfig.getInitParameter("CollectorFactoryName");
  +        if(collectorFactoryName!=null) {
  +            try {
  +                factory = (MetricCollectorFactory)Class.forName(collectorFactoryName).newInstance();
  +            } catch (Exception e) {
  +                logger.error("Collector factory name is illegal.");
  +            }
  +        }else {
  +            logger.error("Profiler service name or Collector factory name is illegal.");
  +        }
  +        
  +        if(this.async) {
  +            int collectorPoolSize = 1;
  +            String collectorPoolSizeString = filterConfig.getInitParameter("CollectorPoolSize");
  +            if(collectorPoolSizeString!=null) collectorPoolSize = Integer.valueOf(collectorPoolSizeString);
  +            
  +            int collectorPoolMaxSize = Short.MAX_VALUE;
  +            String collectorPoolMaxSizeString = filterConfig.getInitParameter("CollectorPoolMaxSize");
  +            if(collectorPoolMaxSizeString!=null) collectorPoolMaxSize = Integer.valueOf(collectorPoolMaxSizeString);
  +            
  +            int collectorQueueSize = Short.MAX_VALUE;
  +            String collectorQueueSizeString = filterConfig.getInitParameter("CollectorQueueSize");
  +            if(collectorQueueSizeString!=null) collectorQueueSize = Integer.valueOf(collectorQueueSizeString);
  +            
  +            // In default, the executor gives priority to queuing.
  +            asyncExecutor = new ThreadPoolExecutor(collectorPoolSize, collectorPoolMaxSize,
  +                                                   1000L, TimeUnit.MILLISECONDS,
  +                                                   new ArrayBlockingQueue<Runnable>(collectorQueueSize));
  +        }
  +        
  +        initialized.set(true);
  +    }
  +    
  +    public void destroy() {
  +        asyncExecutor.shutdown();
  +    }
  +    
  +    /**
  +     * Run profiling by the method invocation.
  +     */
  +	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
  +        throws IOException, ServletException {
  +        
  +        long threadId = RequestSequence.getNext();
  +        
  +	    String operationName = null;
  +	    if(servletRequest instanceof HttpServletRequest) {
  +	        HttpServletRequest request = (HttpServletRequest)servletRequest;
  +	        operationName = request.getContextPath() + request.getServletPath();	      
  +	    }else {
  +	        operationName = "<unknown>";
  +        }
  +        
  +        MetricCollector collector = null;
  +        if(operationName!=null) {
  +            collector = factory.createCollector(category, operationName);
  +        }
  +        
  +	    filterChain.doFilter(servletRequest, servletResponse);
  +        
  +	    if(operationName!=null) {
  +            factory.updateCollector(collector);
  +            if(async) {
  +                try {
  +                    asyncExecutor.execute(new InvocationSubmissionWrapper(collector, retrieveParameter(servletRequest), threadId));
  +                } catch (RejectedExecutionException e) {
  +                    logger.error("A collector was rejected to insert into the collector pool.");
  +                }
  +            } else {
  +                processCollection(collector, retrieveParameter(servletRequest), threadId);
  +            }
  +        }
  +	}
  +    
  +    private String retrieveParameter(ServletRequest servletRequest) {
  +        String parameterExpression = null;
  +        if(servletRequest instanceof HttpServletRequest) {
  +            HttpServletRequest request = (HttpServletRequest)servletRequest;
  +            Map parameterMap = request.getParameterMap();
  +            if(parameterMap!=null && parameterMap.size()>0) {
  +                StringBuilder sb = new StringBuilder();
  +                for(Object me : parameterMap.entrySet()) {
  +                    Map.Entry entry = (Map.Entry)me;
  +                    sb.append(entry.getKey());
  +                    sb.append("=");
  +                    String[] values = (String[])entry.getValue();
  +                    for(String value : values) {
  +                        sb.append(value);
  +                        sb.append(",");
  +                    }
  +                    sb.replace(sb.length()-1, sb.length(), " ");
  +                }
  +                parameterExpression = sb.toString();
  +            }
  +        }
  +        return parameterExpression;
  +    }
  +    
  +    private void processCollection(MetricCollector collector, String parameters, long threadId) {
  +        collector.setArguments(new Object[]{threadId, null, parameters});
  +        try {
  +            factory.submitCollector(collector);
  +        } catch (Exception e) {
  +            logger.error("failed to submit collector.");
  +        }
  +    }
  +    
  +    private class InvocationSubmissionWrapper extends Thread {
  +        private MetricCollector collector = null;
  +        String parameters = null;
  +        private long threadId = 0L;
  +        
  +        public InvocationSubmissionWrapper(MetricCollector collector, String parameters, long threadId) {
  +            this.collector = collector;
  +            this.parameters = parameters;
  +            this.threadId = threadId;
  +        }
  +        
  +        public void run() {
  +            processCollection(collector, parameters, threadId);
  +        }
  +    }
  +
  +}
  
  
  
  1.1.2.1   +252 -0    jboss-profiler/java/src/expansion/org/jboss/profiler/agent/interceptor/Attic/DriverWrapper.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: DriverWrapper.java
  ===================================================================
  RCS file: DriverWrapper.java
  diff -N DriverWrapper.java
  --- /dev/null	1 Jan 1970 00:00:00 -0000
  +++ DriverWrapper.java	11 Apr 2007 11:13:09 -0000	1.1.2.1
  @@ -0,0 +1,252 @@
  +/*
  + * JBoss, Home of Professional Open Source
  + * Copyright 2006, JBoss Inc., and individual contributors as indicated
  + * by the @authors tag. See the copyright.txt in the distribution for a
  + * full listing of individual contributors.
  + *
  + * This is free software; you can redistribute it and/or modify it
  + * under the terms of the GNU Lesser General Public License as
  + * published by the Free Software Foundation; either version 2.1 of
  + * the License, or (at your option) any later version.
  + *
  + * This software is distributed in the hope that it will be useful,
  + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  + * Lesser General Public License for more details.
  + *
  + * You should have received a copy of the GNU Lesser General Public
  + * License along with this software; if not, write to the Free
  + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  + * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  + */
  +
  +package org.jboss.profiler.agent.interceptor;
  +
  +import java.lang.reflect.Method;
  +import java.sql.Connection;
  +import java.sql.Driver;
  +import java.sql.DriverPropertyInfo;
  +import java.sql.SQLException;
  +import java.util.Arrays;
  +import java.util.Properties;
  +import java.util.concurrent.ArrayBlockingQueue;
  +import java.util.concurrent.RejectedExecutionException;
  +import java.util.concurrent.ThreadPoolExecutor;
  +import java.util.concurrent.TimeUnit;
  +import java.util.concurrent.atomic.AtomicBoolean;
  +
  +import org.apache.log4j.Logger;
  +import org.jboss.profiler.agent.collector.MetricCollector;
  +import org.jboss.profiler.agent.collector.MetricCollectorFactory;
  +
  +/**
  + * The wrapper class for JDBC driver.
  + * 
  + * @see org.jboss.profiler.production.interceptors.jdbc.DriverWrapper
  + * @author Takuro Okada (Nomura Research Institute, Ltd.)
  + * Copyright 2006 Nomura Research Institute, Ltd. All Rights Reserved.
  + * Copyright(c) Information-technology Promotion Agency, Japan. All rights reserved 2006.
  + * Result of Open Source Software Development Activities of Information-technology Promotion Agency, Japan.
  + */
  +public class DriverWrapper implements Driver {
  +
  +    private static final String DEFAULT_CATEGORY = "Data Access";
  +    
  +    private static Logger logger = Logger.getLogger(DriverWrapper.class);
  +    
  +    private AtomicBoolean initialized = new AtomicBoolean(false);
  +    
  +    /*
  +     * asynchronous executor
  +     */
  +    private ThreadPoolExecutor asyncExecutor = null;
  +    
  +    /*
  +     * parameters
  +     */
  +    private String category = null;
  +    private boolean async = true;
  +    private String[] filters = null;
  +    
  +    /*
  +     * metric factory
  +     */
  +    private MetricCollectorFactory factory = null;
  +    
  +    /*
  +     * JDBC driver
  +     */
  +    private Driver driver = null;
  +    
  +    public Connection connect(String url, Properties info) throws SQLException {
  +        Connection retVal = null;
  +        
  +        async = Boolean.valueOf(info.getProperty("Async", "true"));
  +        
  +        if(!initialized.get()) {
  +            if(async) {
  +                int collectorPoolSize = 1;
  +                String collectorPoolSizeString = info.getProperty("CollectorPoolSize");
  +                if(collectorPoolSizeString!=null) collectorPoolSize = Integer.valueOf(collectorPoolSizeString);
  +                
  +                int collectorPoolMaxSize = Short.MAX_VALUE;
  +                String collectorPoolMaxSizeString = info.getProperty("CollectorPoolMaxSize");
  +                if(collectorPoolMaxSizeString!=null) collectorPoolMaxSize = Integer.valueOf(collectorPoolMaxSizeString);
  +                
  +                int collectorQueueSize = Short.MAX_VALUE;
  +                String collectorQueueSizeString = info.getProperty("CollectorQueueSize");
  +                if(collectorQueueSizeString!=null) collectorQueueSize = Integer.valueOf(collectorQueueSizeString);
  +                
  +                // In default, the executor gives priority to queuing.
  +                asyncExecutor = new ThreadPoolExecutor(collectorPoolSize, collectorPoolMaxSize,
  +                                                       1000L, TimeUnit.MILLISECONDS,
  +                                                       new ArrayBlockingQueue<Runnable>(collectorQueueSize));
  +            }
  +            
  +            category = info.getProperty("jboss.profiler.Category", DEFAULT_CATEGORY);
  +            
  +            String collectorFactoryName = info.getProperty("CollectorFactoryName");
  +            if(collectorFactoryName!=null) {
  +                try {
  +                    factory = (MetricCollectorFactory)Class.forName(collectorFactoryName).newInstance();
  +                } catch (Exception e) {
  +                    logger.error("Collector factory name is illegal.");
  +                }
  +            }else {
  +                logger.error("Profiler service name or Collector factory name is illegal.");
  +            }
  +            
  +            String filterString = info.getProperty("Includes");
  +            if(filterString!=null) {
  +                filters = filterString.split(",");
  +                for(int i=0; i<filters.length; i++) {
  +                    filters[i] = filters[i].trim();
  +                }
  +            }
  +            
  +            String wrappedDriverName = info.getProperty("WrappedDriverName");
  +            if(wrappedDriverName!=null) {
  +                try {
  +                    driver = (Driver)Class.forName(wrappedDriverName).newInstance();
  +                } catch (Exception e) {
  +                    logger.error("Wrapped driver name is illegal.");
  +                }
  +            }else {
  +                logger.error("Wrapped driver name is illegal.");
  +            }
  +        }
  +        
  +        Connection connection = driver.connect(url, info);      
  +        retVal = (Connection)JdbcInterceptor.getInstance(Connection.class, connection, this);
  +        
  +        return retVal;
  +    }
  +    
  +    public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
  +        return driver.getPropertyInfo(url, info);
  +    }
  +    
  +    public boolean acceptsURL(String url) throws SQLException {
  +        if(driver==null) {
  +            return true;
  +        }
  +        return driver.acceptsURL(url);
  +    }
  +    
  +    public int getMajorVersion() {
  +        return driver.getMajorVersion();
  +    }
  +
  +    public int getMinorVersion() {
  +        return driver.getMinorVersion();
  +    }
  +    
  +    public boolean jdbcCompliant() {
  +        return driver.jdbcCompliant();
  +    }
  +
  +    public String[] getFilters() {
  +        return filters;
  +    }
  +
  +    /**
  +     * Creates collector before the child JDBC method is invoked.
  +     * @param method - invoked method
  +     * @param args - arguments of method
  +     * @return
  +     */
  +    MetricCollector beforeInvoke() {
  +        return factory.createCollector(category, null);
  +    }
  +    
  +    /**
  +     * Updates and submits collector after the child JDBC method was invoked.
  +     * @param collector - collector object
  +     */
  +    void afterInvoke(MetricCollector collector, Method method, Object[] args) {
  +        factory.updateCollector(collector);
  +        long threadId = RequestSequence.get();
  +        if(async) {
  +            try {
  +                asyncExecutor.execute(new InvocationSubmissionWrapper(collector, threadId, method, args));
  +            } catch (RejectedExecutionException e) {
  +                logger.error("A collector was rejected to insert into the collector pool.");
  +            }
  +        } else {
  +            processCollection(collector, threadId, method, args);
  +        }
  +    }
  +    
  +    private void processCollection(MetricCollector collector, long threadId, Method method, Object[] args) {
  +        String className = method.getDeclaringClass().getCanonicalName();
  +        String methodName = method.getName();
  +        
  +        StringBuilder sb = new StringBuilder();
  +        sb.append(className);
  +        sb.append(".");
  +        sb.append(methodName);
  +        collector.setOperationName(sb.toString());
  +        
  +        String argumentsExpression = null;
  +        if(args!=null && args.length>0) {
  +            StringBuilder arguments = new StringBuilder();
  +            for(Object arg : args) {
  +                if(!arg.getClass().isArray()) {
  +                    arguments.append(String.valueOf(arg));
  +                }else {
  +                    arguments.append(Arrays.toString((Object[])arg));
  +                }
  +                arguments.append(", ");
  +            }
  +            arguments.delete(arguments.length()-2, arguments.length());
  +            argumentsExpression = arguments.toString();
  +        }
  +        
  +        collector.setArguments(new Object[]{threadId, null, argumentsExpression});
  +        
  +        try {
  +            factory.submitCollector(collector);
  +        } catch (Exception e) {
  +            logger.error("failed to submit collector.");
  +        }
  +    }
  +    
  +    private class InvocationSubmissionWrapper extends Thread {
  +        private MetricCollector collector = null;
  +        private long threadId = 0L;
  +        private Method method = null;
  +        private Object[] args = null;
  +        
  +        public InvocationSubmissionWrapper(MetricCollector collector, long threadId, Method method, Object[] args) {
  +            this.collector = collector;
  +            this.threadId = threadId;
  +            this.method = method;
  +            this.args = args;
  +        }
  +        
  +        public void run() {
  +            processCollection(collector, threadId, method, args);
  +        }
  +    }
  +
  +}
  
  
  
  1.1.2.1   +133 -0    jboss-profiler/java/src/expansion/org/jboss/profiler/agent/interceptor/Attic/JdbcInterceptor.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: JdbcInterceptor.java
  ===================================================================
  RCS file: JdbcInterceptor.java
  diff -N JdbcInterceptor.java
  --- /dev/null	1 Jan 1970 00:00:00 -0000
  +++ JdbcInterceptor.java	11 Apr 2007 11:13:09 -0000	1.1.2.1
  @@ -0,0 +1,133 @@
  +/*
  + * JBoss, Home of Professional Open Source
  + * Copyright 2006, JBoss Inc., and individual contributors as indicated
  + * by the @authors tag. See the copyright.txt in the distribution for a
  + * full listing of individual contributors.
  + *
  + * This is free software; you can redistribute it and/or modify it
  + * under the terms of the GNU Lesser General Public License as
  + * published by the Free Software Foundation; either version 2.1 of
  + * the License, or (at your option) any later version.
  + *
  + * This software is distributed in the hope that it will be useful,
  + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  + * Lesser General Public License for more details.
  + *
  + * You should have received a copy of the GNU Lesser General Public
  + * License along with this software; if not, write to the Free
  + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  + * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  + */
  +
  +package org.jboss.profiler.agent.interceptor;
  +
  +import java.lang.reflect.InvocationHandler;
  +import java.lang.reflect.Method;
  +import java.lang.reflect.Proxy;
  +
  +import org.jboss.profiler.agent.collector.MetricCollector;
  +
  +/**
  + * The interceptor to profile a method of JDBC object.
  + * 
  + * @see org.jboss.profiler.production.interceptors.jdbc.GenericJDBCProxy
  + * @author Takuro Okada (Nomura Research Institute, Ltd.)
  + * Copyright 2006 Nomura Research Institute, Ltd. All Rights Reserved.
  + * Copyright(c) Information-technology Promotion Agency, Japan. All rights reserved 2006.
  + * Result of Open Source Software Development Activities of Information-technology Promotion Agency, Japan.
  + */
  +public class JdbcInterceptor implements InvocationHandler {
  +
  +    Object jdbcObject = null;
  +    DriverWrapper parent = null;
  +    
  +    private JdbcInterceptor(Object jdbcObject, DriverWrapper parent) {
  +        this.jdbcObject = jdbcObject;
  +        this.parent = parent;
  +    }
  +    
  +    /**
  +     * Gets proxy instance of JDBC object
  +     * @param jdbcObject
  +     * @param parent
  +     * @return
  +     */
  +    public static Object getInstance(Class jdbcInterface, Object jdbcObject, DriverWrapper parent) {
  +        return Proxy.newProxyInstance(jdbcObject.getClass().getClassLoader(), 
  +                                       new Class[]{jdbcInterface}, 
  +                                       new JdbcInterceptor(jdbcObject, parent));
  +    }
  +    
  +    /**
  +     * Run profiling by the method invocation.
  +     */
  +    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  +        boolean profilable = filter(method);
  +        
  +        MetricCollector collector = null;
  +        if(profilable) collector = parent.beforeInvoke();
  +        
  +        Object retVal = method.invoke(jdbcObject, args);
  +        
  +        // Creates new proxy for the return value if the return value is JDBC classes.
  +        if(retVal!=null) {
  +            Class retValInterface = detectImmediateInterface(retVal.getClass());
  +            if(retValInterface!=null && filter(retValInterface)) {
  +                retVal = getInstance(retValInterface, retVal, parent);
  +            }
  +        }
  +        
  +        if(collector!=null) parent.afterInvoke(collector, method, args);
  +        
  +        return retVal;
  +    }
  +    
  +    private Class detectImmediateInterface(Class clazz) {
  +        Class detectedInterface = null;
  +        Class[] interfaces = clazz.getInterfaces();
  +        if(interfaces.length>0) {
  +            for(Class i : interfaces) {
  +                if(i.getName().contains("java.sql")) detectedInterface = i;
  +            }
  +        }
  +        if(detectedInterface==null) {
  +            Class sc = clazz.getSuperclass();
  +            if(sc!=null) detectedInterface = detectImmediateInterface(sc);
  +        }
  +        return detectedInterface;
  +    }
  +    
  +    private boolean filter(Class clazz) {
  +        boolean isTarget = false;
  +        for(String filter : parent.getFilters()) {
  +            if(filter.length()>9) {
  +                int mi = filter.indexOf('.', 9);
  +                if(mi>0) {
  +                    filter = filter.substring(0, mi);
  +                }
  +            }
  +            if(clazz.getName().contains(filter)) {
  +                isTarget = true;
  +                break;
  +            }
  +        }
  +        return isTarget;
  +    }
  +    
  +    private boolean filter(Method method) {
  +        boolean isTarget = false;
  +        StringBuilder expression = new StringBuilder();
  +        expression.append(method.getDeclaringClass());
  +        expression.append(".");
  +        expression.append(method.getName());
  +        for(String filter : parent.getFilters()) {
  +            if(expression.toString().contains(filter)) {
  +                isTarget = true;
  +                break;
  +            }
  +        }
  +        return isTarget;
  +    }
  +
  +}
  
  
  



More information about the jboss-cvs-commits mailing list