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

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/aspectwerkz 
                        Tag: JBossProfiler_Expansion AopInterceptor.java
  Log:
  Moved to another directory
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.1   +187 -0    jboss-profiler/java/src/expansion/org/jboss/profiler/agent/interceptor/aspectwerkz/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,187 @@
  +/*
  + * 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.aspectwerkz;
  +
  +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.codehaus.aspectwerkz.AspectContext;
  +import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
  +import org.codehaus.aspectwerkz.joinpoint.Signature;
  +import org.codehaus.aspectwerkz.joinpoint.management.JoinPointType;
  +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.
  + * 
  + * @author Takuro Okada (Nomura Research Institute, Ltd.)
  + * Copyright 2007 Nomura Research Institute, Ltd. All Rights Reserved.
  + */
  +public class AopInterceptor {
  +
  +    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 boolean async = true;
  +    
  +    /*
  +     * metric factory
  +     */
  +    private MetricCollectorFactory factory = null;
  +    
  +    public AopInterceptor(AspectContext context) {
  +        if(initialized.get()) return;
  +        
  +        String async = context.getParameter("Async");
  +        if(async!=null) this.async = Boolean.valueOf(async);
  +        
  +        String category = context.getParameter("Category");
  +        if(category!=null) this.category = category;
  +        
  +        String collectorFactoryName = context.getParameter("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 = context.getParameter("CollectorPoolSize");
  +            if(collectorPoolSizeString!=null) collectorPoolSize = Integer.valueOf(collectorPoolSizeString);
  +            
  +            int collectorPoolMaxSize = Short.MAX_VALUE;
  +            String collectorPoolMaxSizeString = context.getParameter("CollectorPoolMaxSize");
  +            if(collectorPoolMaxSizeString!=null) collectorPoolMaxSize = Integer.valueOf(collectorPoolMaxSizeString);
  +            
  +            int collectorQueueSize = Short.MAX_VALUE;
  +            String collectorQueueSizeString = context.getParameter("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);
  +    }
  +    
  +    /**
  +     * Run profiling by the method invocation.
  +     * @see org.jboss.aop.advice.Interceptor#invoke(Invocation)
  +     */
  +    public Object invoke(JoinPoint invocation) throws Throwable {
  +        Object retVal = null;
  +        
  +        if(factory==null) {
  +            logger.error("is not specified collector factory.");
  +            return retVal;
  +        }
  +        
  +        MetricCollector collector = factory.createCollector(category, null);
  +        retVal = invocation.proceed();
  +        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, JoinPoint invocation, long threadId) {
  +        if(invocation.getTarget()==null) {
  +            // illegal access
  +            return;
  +        }
  +        
  +        Signature signature = invocation.getSignature();
  +        String operationName = signature.getDeclaringType().getName()+".";
  +        int invocationType = invocation.getType().hashCode();
  +        switch (invocationType) {
  +        case JoinPointType.CONSTRUCTOR_EXECUTION_INT:
  +        case JoinPointType.CONSTRUCTOR_CALL_INT:
  +            operationName = operationName.concat("<create>");
  +            break;
  +        default:
  +            operationName = operationName.concat(signature.getName());
  +            break;
  +        }
  +        
  +        collector.setOperationName(operationName);
  +        
  +        // AspectWerkz can not retrieve caller method and callee arguments...
  +        collector.setArguments(new Object[]{threadId});
  +        
  +        try {
  +            factory.submitCollector(collector);
  +        } catch (Exception e) {
  +            logger.error("failed to submit collector.");
  +        }
  +    }
  +    
  +    private class InvocationSubmissionWrapper extends Thread {
  +        private MetricCollector collector = null;
  +        private JoinPoint invocation = null;
  +        private long threadId = 0L;
  +        
  +        public InvocationSubmissionWrapper(MetricCollector collector, JoinPoint 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