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

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


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

  Added:       java/src/expansion/org/jboss/profiler/agent/interceptor/jbossaop 
                        Tag: JBossProfiler_Expansion AopInterceptor.java
  Log:
  Moved to another directory
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.1   +295 -0    jboss-profiler/java/src/expansion/org/jboss/profiler/agent/interceptor/jbossaop/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	11 Apr 2007 11:13:11 -0000	1.1.2.1
  @@ -0,0 +1,295 @@
  +/*
  + * 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.jbossaop;
  +
  +import java.lang.reflect.Method;
  +import java.util.Arrays;
  +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.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.agent.collector.MetricCollector;
  +import org.jboss.profiler.agent.collector.MetricCollectorFactory;
  +import org.jboss.profiler.agent.interceptor.RequestSequence;
  +
  +/**
  + * 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 ThreadPoolExecutor asyncExecutor = null;
  +    
  +    /*
  +     * parameters
  +     */
  +    private String category = DEFAULT_CATEGORY;
  +    private String collectorFactoryName = null;
  +    private boolean async = true;
  +    private int collectorPoolSize = 1;
  +    private int collectorPoolMaxSize = Short.MAX_VALUE;
  +    private int collectorQueueSize = Short.MAX_VALUE;
  +    private boolean collectArgs = false;
  +    
  +    /*
  +     * metric factory
  +     */
  +    private MetricCollectorFactory factory = null;
  +    
  +    /**
  +     * Create collector factory object and setup asynchronous executor.
  +     */
  +    private void init() {
  +        if(initialized.get()) return;
  +        
  +        try {
  +            factory = (MetricCollectorFactory)Class.forName(collectorFactoryName).newInstance();
  +        } catch (Exception e) {
  +            logger.error("Collector factory name is illegal.");
  +        }
  +        
  +        if(async) {
  +            // 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 setCategory(String category) {
  +        this.category = trimParameter(category);
  +    }
  +
  +    public void setAsync(boolean async) {
  +        this.async = async;
  +    }
  +    
  +    public void setCollectorFactoryName(String collectorFactoryName) {
  +        this.collectorFactoryName = trimParameter(collectorFactoryName);
  +    }
  +    
  +    public void setCollectorPoolSize(int collectorPoolSize) {
  +        this.collectorPoolSize = collectorPoolSize;
  +    }
  +    
  +    public void setCollectorPoolMaxSize(int collectorPoolMaxSize) {
  +        this.collectorPoolMaxSize = collectorPoolMaxSize;
  +    }
  +
  +    public void setCollectorQueueSize(int collectorQueueSize) {
  +        this.collectorQueueSize = collectorQueueSize;
  +    }
  +    
  +    public void setCollectArgs(boolean collectArgs) {
  +        this.collectArgs = collectArgs;
  +    }
  +
  +    private String trimParameter(String parameter) {
  +        String result = null;
  +        if(parameter==null) return result;
  +        if(parameter.contains("\n")) {
  +            StringBuffer sb = new StringBuffer();
  +            for(String p : parameter.split("\n")) {
  +                sb.append(p.trim());
  +            }
  +            result = sb.toString();
  +        }else {
  +            result = parameter.trim();
  +        }
  +        return result;
  +    }
  +
  +    /**
  +     * @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 = RequestSequence.get();
  +        if(async) {
  +            try {
  +                asyncExecutor.execute(new InvocationSubmissionWrapper(collector, invocation, threadId));
  +            } catch (RejectedExecutionException e) {
  +                logger.error("A collector was rejected to insert into the collector pool.");
  +            }
  +        } 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];
  +            if(collectArgs) arguments = dInvocation.getArguments();
  +        }
  +        else if(invocation instanceof ConstructorInvocation) {
  +            ConstructorInvocation dInvocation = (ConstructorInvocation)invocation;
  +            operationName = dInvocation.getTargetObject() + ".<create>";
  +            if(collectArgs) arguments = dInvocation.getArguments();
  +        }
  +        else if(invocation instanceof FieldInvocation) {
  +            FieldInvocation dInvocation = (FieldInvocation)invocation;
  +            operationName = dInvocation.getTargetObject().getClass().getName() + "." + dInvocation.getField().getName();
  +        }
  +        else if(invocation instanceof CallerInvocation) {
  +            CallerInvocation callerInvocation = (CallerInvocation)invocation;
  +            operationName = callerInvocation.getTargetObject().getClass().getName() + ".";
  +            
  +            if(callerInvocation instanceof MethodCalledByMethodInvocation) {
  +                MethodCalledByMethodInvocation dInvocation = (MethodCalledByMethodInvocation)callerInvocation;
  +                operationName = operationName.concat(dInvocation.getCalledMethod().getName());
  +                if(collectArgs) 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());
  +                if(collectArgs) arguments = dInvocation.getArguments();
  +                // This invocation cannot get caller object.
  +                caller = dInvocation.getCalling().getDeclaringClass().getName();
  +            }
  +            else if(callerInvocation instanceof ConstructorCalledByMethodInvocation) {
  +                ConstructorCalledByMethodInvocation dInvocation = (ConstructorCalledByMethodInvocation)callerInvocation;
  +                operationName = operationName.concat(dInvocation.getCalledConstructor().getName());
  +                if(collectArgs) arguments = dInvocation.getArguments();
  +                if(dInvocation.getCallingObject() instanceof Servlet) {
  +                    caller = "<servlet>";
  +                }else {
  +                    caller = dInvocation.getCallingClass().getName();
  +                }
  +            }
  +            else if(callerInvocation instanceof ConstructorCalledByConstructorInvocation) {
  +                ConstructorCalledByConstructorInvocation dInvocation = (ConstructorCalledByConstructorInvocation)callerInvocation;
  +                operationName = operationName.concat(dInvocation.getCalledConstructor().getName());
  +                if(collectArgs) arguments = dInvocation.getArguments();
  +                // This invocation cannot get caller object.
  +                caller = dInvocation.getCallingConstructor().getDeclaringClass().getName();
  +            }
  +            else {
  +                operationName = 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);
  +        }
  +    }
  +
  +}
  
  
  



More information about the jboss-cvs-commits mailing list