/*
  * 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.contrib;

import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.LoaderClassPath;

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

/**
 * @author Marshall
 *         <p/>
 *         This is a class pool factory used by JBossIDE to create an AOP Class Pool
 */
public class IDEClassPoolFactory implements ClassPoolFactory
{

   private ArrayList classPaths;

   public IDEClassPoolFactory()
   {
      classPaths = new ArrayList();
   }

   public AOPClassPool create(ClassLoader loader, ClassPool pool, AspectManager manager)
   {
      SimpleClassPool classPool = new SimpleClassPool(loader, pool, manager);
      return classPool;
   }

   public AOPClassPool create(ClassPool pool, AspectManager manager)
   {
      SimpleClassPool classPool = new SimpleClassPool(pool, manager);
      return classPool;
   }

   public void insertClasspath(String path)
   {
      try
      {
         classPaths.add(new URL(path));
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
   }

   private class SimpleClassPool extends AOPClassPool
   {
      private Loader loader;

      public SimpleClassPool(ClassLoader loader, ClassPool pool, AspectManager manager)
      {
         super(loader, pool, manager);
         loadClasspath();
      }

      public SimpleClassPool(ClassPool pool, AspectManager manager)
      {
         super(pool, manager);
         loadClasspath();

      }

      private void loadClasspath()
      {
         childFirstLookup = true;

         URL urlPaths[] = (URL[]) classPaths.toArray(new URL[classPaths.size()]);
         loader = new Loader(urlPaths, Thread.currentThread().getContextClassLoader());
         setClassLoader(loader);

         Thread.currentThread().setContextClassLoader(loader);

         classPath = new LoaderClassPath(loader);
         insertClassPath(classPath);
      }

      public Class toClass(CtClass cc)
              throws CannotCompileException
      {
         try
         {
            return loader.loadClass(cc.getName());
         }
         catch (Exception e)
         {
            try
            {
               return loader.loadClass(cc.getName(), cc.toBytecode());
            }
            catch (IOException e2)
            {
               throw new CannotCompileException(e2);
            }
         }
      }
   }

   private class Loader extends URLClassLoader
   {
      public Loader(URL urls[], ClassLoader src)
      {
         super(urls, src);
      }

      public Class loadClass(String name, byte[] classfile)
              throws ClassFormatError
      {
         Class c = defineClass(name, classfile, 0, classfile.length);

         resolveClass(c);
         return c;
      }
   }

   public ClassLoader getTCLIfScoped()
   {
      return null;
   }
}
