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

Takuro Okada t2-okada at nri.co.jp
Thu Oct 26 04:58:57 EDT 2006


  User: tokada  
  Date: 06/10/26 04:58:57

  Added:       java/src/expansion/org/jboss/profiler/exp/agent/interceptor     
                        Tag: JBossProfiler_Expansion AopInterceptor.java
                        ServletInterceptor.java DriverWrapper.java
                        RequestSequence.java JdbcInterceptor.java
  Log:
  
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.1   +265 -0    jboss-profiler/java/src/expansion/org/jboss/profiler/exp/agent/interceptor/Attic/AopInterceptor.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: AopInterceptor.java
  ===================================================================
  RCS file: AopInterceptor.java
  diff -N AopInterceptor.java
  --- /dev/null	1 Jan 1970 00:00:00 -0000
  +++ AopInterceptor.java	26 Oct 2006 08:58:57 -0000	1.1.2.1
  @@ -0,0 +1,265 @@
  +/*
  + * 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.exp.agent.interceptor;
  +
  +import java.lang.reflect.Constructor;
  +import java.lang.reflect.Method;
  +import java.util.Arrays;
  +import java.util.concurrent.ExecutorService;
  +import java.util.concurrent.LinkedBlockingQueue;
  +import java.util.concurrent.ThreadPoolExecutor;
  +import java.util.concurrent.TimeUnit;
  +import java.util.concurrent.atomic.AtomicBoolean;
  +
  +import javax.servlet.Servlet;
  +
  +import org.apache.log4j.Logger;
  +import org.jboss.aop.advice.Interceptor;
  +import org.jboss.aop.joinpoint.CallerInvocation;
  +import org.jboss.aop.joinpoint.ConstructorCalledByConstructorInvocation;
  +import org.jboss.aop.joinpoint.ConstructorCalledByMethodInvocation;
  +import org.jboss.aop.joinpoint.ConstructorInvocation;
  +import org.jboss.aop.joinpoint.FieldInvocation;
  +import org.jboss.aop.joinpoint.Invocation;
  +import org.jboss.aop.joinpoint.MethodCalledByConstructorInvocation;
  +import org.jboss.aop.joinpoint.MethodCalledByMethodInvocation;
  +import org.jboss.aop.joinpoint.MethodInvocation;
  +import org.jboss.profiler.exp.agent.collector.MetricCollector;
  +import org.jboss.profiler.exp.agent.collector.MetricCollectorFactory;
  +
  +/**
  + * The interceptor to profile any method.
  + * 
  + * @see org.jboss.profiler.production.interceptors.aop.AOPInterceptor
  + * @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 AopInterceptor implements Interceptor {
  +
  +    private static final String DEFAULT_CATEGORY = "Beans";
  +    
  +    private static Logger logger = Logger.getLogger(AopInterceptor.class);
  +    
  +    private AtomicBoolean initialized = new AtomicBoolean(false);
  +    
  +    /*
  +     * asynchronous executor
  +     */
  +    private ExecutorService asyncExecutor = null;
  +    
  +    /*
  +     * parameters
  +     */
  +    private String category = DEFAULT_CATEGORY;
  +    private String profilerServiceName = null;
  +    private String profilerServiceAddress = null;
  +    private String collectorFactoryName = null;
  +    private boolean async = true;
  +    private int collectorPoolSize = 10;
  +
  +    /*
  +     * metric factory
  +     */
  +    private MetricCollectorFactory factory = null;
  +    
  +    /**
  +     * Create collector factory object and setup asynchronous executor.
  +     */
  +    private void init() {
  +        if(initialized.get()) return;
  +        
  +        try {
  +            Class clazz = Class.forName(collectorFactoryName);
  +            if(profilerServiceAddress==null) {
  +                Constructor constructor = clazz.getConstructor(String.class);
  +                factory = (MetricCollectorFactory)constructor.newInstance(new Object[]{profilerServiceName});
  +            }else {
  +                Constructor constructor = clazz.getConstructor(String.class, String.class);
  +                factory = (MetricCollectorFactory)constructor.newInstance(new Object[]{profilerServiceName, profilerServiceAddress});
  +            }
  +        } catch (Exception e) {
  +            logger.error("Collector factory name is illegal.");
  +        }
  +        
  +        if(async) {
  +            asyncExecutor = new ThreadPoolExecutor(1, collectorPoolSize,
  +                                                   1000L, TimeUnit.MILLISECONDS,
  +                                                   new LinkedBlockingQueue<Runnable>());
  +        }
  +        
  +        initialized.set(true);
  +    }
  +    
  +    public void setCategory(String category) {
  +        this.category = category;
  +    }
  +    
  +    public void setProfilerServiceName(String profilerServiceName) {
  +        this.profilerServiceName = profilerServiceName;
  +    }
  +
  +    public void setProfilerServiceAddress(String profilerServiceAddress) {
  +        this.profilerServiceAddress = profilerServiceAddress;
  +    }
  +
  +    public void setAsync(boolean async) {
  +        this.async = async;
  +    }
  +    
  +    public void setCollectorFactoryName(String collectorFactoryName) {
  +        this.collectorFactoryName = collectorFactoryName;
  +    }
  +    
  +    public void setCollectorPoolSize(int collectorPoolSize) {
  +        this.collectorPoolSize = collectorPoolSize;
  +    }
  +    
  +    /**
  +     * @see org.jboss.aop.advice.Interceptor#getName()
  +     */
  +    public String getName() {
  +        return category;
  +    }
  +    
  +    /**
  +     * Run profiling by the method invocation.
  +     * @see org.jboss.aop.advice.Interceptor#invoke(Invocation)
  +     */
  +    public Object invoke(Invocation invocation) throws Throwable {
  +        Object retVal = null;
  +        
  +        if(!initialized.get()) {
  +            init();
  +        }
  +        
  +        if(factory==null) {
  +            logger.error("is not specified collector factory.");
  +            return retVal;
  +        }
  +        
  +        MetricCollector collector = factory.createCollector(category, null);
  +        retVal = invocation.invokeNext();
  +        factory.updateCollector(collector);
  +//        long threadId = Thread.currentThread().getId();
  +        long threadId = RequestSequence.get();
  +        if(async) {
  +            asyncExecutor.execute(new InvocationSubmissionWrapper(collector, invocation, threadId));
  +        } else {
  +            processCollection(collector, invocation, threadId);
  +        }
  +        return retVal;
  +    }
  +    
  +    private void processCollection(MetricCollector collector, Invocation invocation, long threadId) {
  +        if(invocation.getTargetObject()==null) {
  +            // illegal access
  +            return;
  +        }
  +        String operationName = null;
  +        String caller = null;
  +        Object[] arguments = null;
  +        if(invocation instanceof MethodInvocation) {
  +            MethodInvocation dInvocation = (MethodInvocation) invocation;
  +            Method method = dInvocation.getActualMethod();
  +            String[] splitMethod = method.getName().split("\\$");
  +            operationName = method.getDeclaringClass().getName() + "." + splitMethod[splitMethod.length-2];
  +            arguments = dInvocation.getArguments();
  +        } else if(invocation instanceof FieldInvocation) {
  +            FieldInvocation dInvocation = (FieldInvocation)invocation;
  +            operationName = dInvocation.getTargetObject().getClass().getName() + "." + dInvocation.getField().getName();
  +        } else if(invocation instanceof ConstructorInvocation) {
  +            ConstructorInvocation dInvocation = (ConstructorInvocation)invocation;
  +            operationName = dInvocation.getTargetObject() + "." + dInvocation.getConstructor().getName();
  +            arguments = dInvocation.getArguments();
  +        } else if(invocation instanceof CallerInvocation) {
  +            CallerInvocation callerInvocation = (CallerInvocation)invocation;
  +            operationName = callerInvocation.getTargetObject().getClass().getName() + ".";
  +            if(callerInvocation instanceof ConstructorCalledByConstructorInvocation) {
  +                ConstructorCalledByConstructorInvocation dInvocation = (ConstructorCalledByConstructorInvocation)callerInvocation;
  +                operationName = operationName.concat(dInvocation.getCalledConstructor().getName());
  +                arguments = dInvocation.getArguments();
  +                // This invocation cannot get caller object.
  +                caller = dInvocation.getCallingConstructor().getDeclaringClass().getName();
  +            } else if(callerInvocation instanceof ConstructorCalledByMethodInvocation) {
  +                ConstructorCalledByMethodInvocation dInvocation = (ConstructorCalledByMethodInvocation)callerInvocation;
  +                operationName = operationName.concat(dInvocation.getCalledConstructor().getName());
  +                arguments = dInvocation.getArguments();
  +                if(dInvocation.getCallingObject() instanceof Servlet) {
  +                    caller = "<servlet>";
  +                }else {
  +                    caller = dInvocation.getCallingClass().getName();
  +                }
  +            } else if(callerInvocation instanceof MethodCalledByConstructorInvocation) {
  +                MethodCalledByConstructorInvocation dInvocation = (MethodCalledByConstructorInvocation)callerInvocation;
  +                operationName = operationName.concat(dInvocation.getCalledMethod().getName());
  +                arguments = dInvocation.getArguments();
  +                // This invocation cannot get caller object.
  +                caller = dInvocation.getCalling().getDeclaringClass().getName();
  +            } else if(callerInvocation instanceof MethodCalledByMethodInvocation) {
  +                MethodCalledByMethodInvocation dInvocation = (MethodCalledByMethodInvocation)callerInvocation;
  +                operationName = operationName.concat(dInvocation.getCalledMethod().getName());
  +                arguments = dInvocation.getArguments();
  +                if(dInvocation.getCallingObject() instanceof Servlet) {
  +                    caller = "<servlet>";
  +                }else {
  +                    caller = dInvocation.getCallingClass().getName();
  +                }
  +            } else {
  +                operationName.concat("<unknown>");
  +            }
  +        }else {
  +            operationName = "<Unknown>";
  +        }
  +        collector.setOperationName(operationName);
  +        String argumentsExpression = null;
  +        if(arguments!=null && arguments.length>0) {
  +            argumentsExpression = Arrays.toString(arguments);
  +            argumentsExpression = argumentsExpression.substring(1, argumentsExpression.length()-1);
  +        }
  +        collector.setArguments(new Object[]{threadId, caller, argumentsExpression});
  +        try {
  +            factory.submitCollector(collector);
  +        } catch (Exception e) {
  +            logger.error("failed to submit collector.");
  +        }
  +    }
  +    
  +    private class InvocationSubmissionWrapper extends Thread {
  +        private MetricCollector collector = null;
  +        private Invocation invocation = null;
  +        private long threadId = 0L;
  +        
  +        public InvocationSubmissionWrapper(MetricCollector collector, Invocation invocation, long threadId) {
  +            this.collector = collector;
  +            this.invocation = invocation;
  +            this.threadId = threadId;
  +        }
  +        
  +        public void run() {
  +            processCollection(collector, invocation, threadId);
  +        }
  +    }
  +
  +}
  
  
  
  1.1.2.1   +189 -0    jboss-profiler/java/src/expansion/org/jboss/profiler/exp/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	26 Oct 2006 08:58:57 -0000	1.1.2.1
  @@ -0,0 +1,189 @@
  +/*
  + * 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.exp.agent.interceptor;
  +
  +import java.io.IOException;
  +import java.lang.reflect.Constructor;
  +import java.util.Map;
  +import java.util.concurrent.ExecutorService;
  +import java.util.concurrent.LinkedBlockingQueue;
  +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.exp.agent.collector.MetricCollector;
  +import org.jboss.profiler.exp.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 ExecutorService asyncExecutor = null;
  +    
  +    /*
  +     * parameters
  +     */
  +    private String category = DEFAULT_CATEGORY;
  +    private MetricCollectorFactory factory = null;
  +    private boolean async = true;
  +    
  +    /**
  +     * Create collector factory object.
  +     */
  +	public void init(FilterConfig filterConfig) throws ServletException {
  +        if(initialized.get()) return;
  +        
  +        String category = filterConfig.getInitParameter("Category");
  +        if(category!=null) this.category = category;
  +        String async = filterConfig.getInitParameter("Async");
  +        if(async!=null) this.async = Boolean.valueOf(async);
  +        String collectorPoolSize = filterConfig.getInitParameter("CollectorPoolSize");
  +        if(collectorPoolSize!=null) collectorPoolSize = "10";
  +		String profilerServiceName = filterConfig.getInitParameter("ProfilerServiceName");
  +        String profilerServiceAddress = filterConfig.getInitParameter("ProfilerServiceAddress");
  +		String collectorFactoryName = filterConfig.getInitParameter("CollectorFactoryName");
  +		
  +        if(profilerServiceName!=null && collectorFactoryName!=null) {
  +            try {
  +                Class clazz = Class.forName(collectorFactoryName);
  +                if(profilerServiceAddress==null) {
  +                    Constructor constructor = clazz.getConstructor(String.class);
  +                    factory = (MetricCollectorFactory)constructor.newInstance(new Object[]{profilerServiceName});
  +                }else {
  +                    Constructor constructor = clazz.getConstructor(String.class, String.class);
  +                    factory = (MetricCollectorFactory)constructor.newInstance(new Object[]{profilerServiceName, profilerServiceAddress});
  +                }
  +            } 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) {
  +            asyncExecutor = new ThreadPoolExecutor(1, Integer.valueOf(collectorPoolSize),
  +                                                   1000L, TimeUnit.MILLISECONDS,
  +                                                   new LinkedBlockingQueue<Runnable>());
  +        }
  +        
  +        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);
  +//            long threadId = Thread.currentThread().getId();
  +            if(async) {
  +                asyncExecutor.execute(new InvocationSubmissionWrapper(collector, servletRequest, threadId));
  +            } else {
  +                processCollection(collector, servletRequest, threadId);
  +            }
  +        }
  +	}
  +    
  +    private void processCollection(MetricCollector collector, ServletRequest servletRequest, long threadId) {
  +        String parameterExpression = null;
  +        if(servletRequest instanceof HttpServletRequest) {
  +            HttpServletRequest request = (HttpServletRequest)servletRequest;
  +            Map parameterMap = null;
  +            // Cope with tomcat implementation.
  +            if(request.getMethod()!=null) parameterMap = request.getParameterMap();
  +            if(parameterMap!=null && parameterMap.size()>0) {
  +                parameterExpression = parameterMap.toString();
  +                parameterExpression = parameterExpression.substring(1, parameterExpression.length()-1);
  +            }
  +        }
  +        collector.setArguments(new Object[]{threadId, null, parameterExpression});
  +        try {
  +            factory.submitCollector(collector);
  +        } catch (Exception e) {
  +            logger.error("failed to submit collector.");
  +        }
  +    }
  +    
  +    private class InvocationSubmissionWrapper extends Thread {
  +        private MetricCollector collector = null;
  +        private ServletRequest servletRequest = null;
  +        private long threadId = 0L;
  +        
  +        public InvocationSubmissionWrapper(MetricCollector collector, ServletRequest servletRequest, long threadId) {
  +            this.collector = collector;
  +            this.servletRequest = servletRequest;
  +            this.threadId = threadId;
  +        }
  +        
  +        public void run() {
  +            processCollection(collector, servletRequest, threadId);
  +        }
  +    }
  +
  +}
  
  
  
  1.1.2.1   +231 -0    jboss-profiler/java/src/expansion/org/jboss/profiler/exp/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	26 Oct 2006 08:58:57 -0000	1.1.2.1
  @@ -0,0 +1,231 @@
  +/*
  + * 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.exp.agent.interceptor;
  +
  +import java.lang.reflect.Constructor;
  +import java.lang.reflect.Method;
  +import java.sql.Connection;
  +import java.sql.Driver;
  +import java.sql.DriverPropertyInfo;
  +import java.sql.SQLException;
  +import java.util.Properties;
  +import java.util.concurrent.ExecutorService;
  +import java.util.concurrent.LinkedBlockingQueue;
  +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.exp.agent.collector.MetricCollector;
  +import org.jboss.profiler.exp.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(AopInterceptor.class);
  +    
  +    private AtomicBoolean initialized = new AtomicBoolean(false);
  +    
  +    /*
  +     * asynchronous executor
  +     */
  +    private ExecutorService asyncExecutor = null;
  +    
  +    /*
  +     * parameters
  +     */
  +    private String category = null;
  +    private MetricCollectorFactory factory = null;
  +    private boolean async = true;
  +    private String[] filters = null;
  +    
  +    /*
  +     * JDBC driver
  +     */
  +    private Driver driver = null;
  +    
  +    public Connection connect(String url, Properties info) throws SQLException {
  +        Connection retVal = null;
  +        
  +        async = Boolean.valueOf(info.getProperty("jboss.profiler.Async", "true"));
  +        
  +        if(!initialized.get()) {
  +            if(async) {
  +                String collectorPoolSize = info.getProperty("jboss.profiler.CollectorPoolSize", "10");
  +                asyncExecutor = new ThreadPoolExecutor(1, Integer.valueOf(collectorPoolSize),
  +                                                       1000L, TimeUnit.MILLISECONDS,
  +                                                       new LinkedBlockingQueue<Runnable>());
  +            }
  +            
  +            category = info.getProperty("jboss.profiler.Category", DEFAULT_CATEGORY);
  +            
  +            String profilerServiceName = info.getProperty("jboss.profiler.ProfilerServiceName");
  +            String profilerServiceAddress = info.getProperty("jboss.profiler.ProfilerServiceAddress");
  +            String collectorFactoryName = info.getProperty("jboss.profiler.CollectorFactoryName");
  +            if(profilerServiceName!=null && collectorFactoryName!=null) {
  +                try {
  +                    Class clazz = Class.forName(collectorFactoryName);
  +                    if(profilerServiceAddress==null) {
  +                        Constructor constructor = clazz.getConstructor(String.class);
  +                        factory = (MetricCollectorFactory)constructor.newInstance(new Object[]{profilerServiceName});
  +                    }else {
  +                        Constructor constructor = clazz.getConstructor(String.class, String.class);
  +                        factory = (MetricCollectorFactory)constructor.newInstance(new Object[]{profilerServiceName, profilerServiceAddress});
  +                    }
  +                } 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("jboss.profiler.Includes");
  +            if(filterString!=null) {
  +                filters = filterString.split(",");
  +            }
  +            
  +            String wrappedDriverName = info.getProperty("jboss.profiler.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, 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 = Thread.currentThread().getId();
  +        long threadId = RequestSequence.get();
  +        if(async) {
  +            asyncExecutor.execute(new InvocationSubmissionWrapper(collector, threadId, method, args));
  +        } else {
  +            processCollection(collector, threadId, method, args);
  +        }
  +    }
  +    
  +    private void processCollection(MetricCollector collector, long threadId, Method method, Object[] args) {
  +        StringBuilder sb = new StringBuilder();
  +        sb.append(method.getDeclaringClass().getCanonicalName());
  +        sb.append(".");
  +        sb.append(method.getName());
  +        String methodExpression = sb.toString();
  +        collector.setOperationName(methodExpression);
  +        
  +        String query = null;
  +        if(method.getName().equals("execute") ||
  +           method.getName().equals("executeQuery") ||
  +           method.getName().equals("executeUpdate") ||
  +           method.getName().equals("prepareStatement") ||
  +           method.getName().equals("prepareCall")) {
  +            if(args.length>0) {
  +                query = (String)args[0];
  +            }
  +        }
  +        collector.setArguments(new Object[]{threadId, null, query});
  +        
  +        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   +53 -0     jboss-profiler/java/src/expansion/org/jboss/profiler/exp/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	26 Oct 2006 08:58:57 -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.exp.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   +129 -0    jboss-profiler/java/src/expansion/org/jboss/profiler/exp/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	26 Oct 2006 08:58:57 -0000	1.1.2.1
  @@ -0,0 +1,129 @@
  +/*
  + * 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.exp.agent.interceptor;
  +
  +import java.lang.reflect.InvocationHandler;
  +import java.lang.reflect.Method;
  +import java.lang.reflect.Proxy;
  +import java.sql.Array;
  +import java.sql.Blob;
  +import java.sql.CallableStatement;
  +import java.sql.Clob;
  +import java.sql.Connection;
  +import java.sql.DatabaseMetaData;
  +import java.sql.ParameterMetaData;
  +import java.sql.PreparedStatement;
  +import java.sql.Ref;
  +import java.sql.ResultSet;
  +import java.sql.ResultSetMetaData;
  +import java.sql.SQLData;
  +import java.sql.SQLInput;
  +import java.sql.SQLOutput;
  +import java.sql.Savepoint;
  +import java.sql.Statement;
  +import java.sql.Struct;
  +
  +import org.jboss.profiler.exp.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(Object jdbcObject, DriverWrapper parent) {
  +        return Proxy.newProxyInstance(jdbcObject.getClass().getClassLoader(), 
  +                                       jdbcObject.getClass().getInterfaces(), 
  +                                       new JdbcInterceptor(jdbcObject, parent));
  +    }
  +    
  +    /**
  +     * Run profiling by the method invocation.
  +     */
  +    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  +        boolean profilable = false;
  +        
  +        StringBuilder sb = new StringBuilder();
  +        sb.append(method.getDeclaringClass().getCanonicalName());
  +        sb.append(".");
  +        sb.append(method.getName());
  +        String methodExpression = sb.toString();
  +        
  +        for(String filter : parent.getFilters()) {
  +            if(methodExpression.contains(filter)) {
  +                profilable = true;
  +                break;
  +            }
  +        }
  +        
  +        MetricCollector collector = null;
  +        if(profilable) collector = parent.beforeInvoke();
  +        
  +        Object retVal = method.invoke(jdbcObject, args);
  +        
  +        if( retVal instanceof ResultSet ||
  +            retVal instanceof Statement ||
  +            retVal instanceof PreparedStatement ||
  +            retVal instanceof CallableStatement ||
  +            retVal instanceof Connection ||
  +            retVal instanceof Array || 
  +            retVal instanceof Blob ||
  +            retVal instanceof Clob ||
  +            retVal instanceof Ref ||
  +            retVal instanceof DatabaseMetaData ||
  +            retVal instanceof ParameterMetaData ||
  +            retVal instanceof ResultSetMetaData ||
  +            retVal instanceof Savepoint ||
  +            retVal instanceof SQLData ||
  +            retVal instanceof SQLInput ||
  +            retVal instanceof SQLOutput ||
  +            retVal instanceof Struct
  +        ) {
  +            retVal = getInstance(retVal, parent);
  +        }
  +        
  +        if(collector!=null) parent.afterInvoke(collector, method, args);
  +        
  +        return retVal;
  +    }
  +
  +}
  
  
  



More information about the jboss-cvs-commits mailing list