/*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, 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.aop.instrument;

import java.lang.reflect.Constructor;

import org.jboss.aop.AOPClassPool;
import org.jboss.aop.AspectManager;

/**
 * Comment
 *
 * @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
 * @version $Revision$
 */
public class InstrumentorFactory 
{
   protected static final int CLASSIC = 1;
   protected static final int GENERATED_ADVISOR = 2;
   protected static final int OTHER_INSTRUMENTOR = 1000; //The jrockit aop instrumentor is in another jar which we should not depend on
   protected static int instrumentor = 0; 
   
   protected static Constructor otherInstrumentorConstructor;
   
   private static final Class[] CONSTRUCTOR_SIG = new Class[] {AOPClassPool.class, AspectManager.class, JoinpointClassifier.class, DynamicTransformationObserver.class};
   
   public static void initialise(String property)
   {
      if (AspectManager.verbose)
      {
         System.out.println("[debug] Passed in instrumentor: " + property);
      }
      
      if (property != null)
      {
         if (property.equals(ClassicInstrumentor.class.getName()))
         {
            instrumentor = CLASSIC;
         }
         else if (property.equals(GeneratedAdvisorInstrumentor.class.getName()))
         {
            instrumentor = GENERATED_ADVISOR;
         }
         else
         {
            
            try
            {
               Class otherInstrumentorClass = Thread.currentThread().getContextClassLoader().loadClass(property);
               otherInstrumentorConstructor = otherInstrumentorClass.getConstructor(CONSTRUCTOR_SIG);
            }
            catch (ClassNotFoundException e)
            {
               throw new RuntimeException("Invalid instrumentor " + property + " was used");
            }
            catch(NoSuchMethodException e)
            {
               throw new RuntimeException(property + " does not have a constructor with the expected signature");
            }
         }
      }
      else
      {
         if (AspectManager.verbose)
         {
            System.out.println("[debug] Defaulting instrumentor to: " + ClassicInstrumentor.class.getName());
         }
         instrumentor = CLASSIC;
      }
   }
   
   public static Instrumentor getInstrumentor(AOPClassPool pool, AspectManager manager, JoinpointClassifier joinpointClassifier, DynamicTransformationObserver observer)
   {
      if (instrumentor == CLASSIC)
      {
         return new ClassicInstrumentor(pool, manager, joinpointClassifier, observer);         
      }
      else if (instrumentor == GENERATED_ADVISOR)
      {
         return new GeneratedAdvisorInstrumentor(pool, manager, joinpointClassifier, observer);
      }
      else if (otherInstrumentorConstructor != null)
      {
         try
         {
            return (Instrumentor) otherInstrumentorConstructor.newInstance(new Object[] {pool, manager, joinpointClassifier, observer});
         }
         catch (Exception e)
         {
            throw new RuntimeException(e);
         }
      }
      else
      {
         throw new RuntimeException("Instrumentor is not set");
      }
   }
   

   public static Instrumentor getInstrumentor(AspectManager manager, JoinpointClassifier joinpointClassifier)
   {
      switch(instrumentor)
      {
         case CLASSIC:
            return new ClassicInstrumentor(manager, joinpointClassifier);
         case GENERATED_ADVISOR:
            return new GeneratedAdvisorInstrumentor(manager, joinpointClassifier);
         default:
            throw new RuntimeException("Instrumentor is not set");
               
      }
   }
   
}
