/*
* 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.resource.adapter.jdbc;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.Modifier;
import java.security.PrivilegedExceptionAction;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.util.HashSet;

/** A PrivilegedExceptionAction that proxies whatever interfaces the target
 * object supports. When an invocation through an interface method is done,
 * the is executed via reflection in a PrivilegedExceptionAction to run
 * at the privilege level of this codebase.
 * 
 * @author Scott.Stark@jboss.org
 * @version $Revision: 1.1.2.6 $
 */
public class PrivilegedProxy
   implements InvocationHandler, PrivilegedExceptionAction
{
   private Object target;
   private Method method;
   private Object[] args;

   public Object run() throws Exception
   {
      Object value = null;
      try
      {
         value = method.invoke(target, args);
      }
      catch(InvocationTargetException e)
      {
         throw (Exception) e.getTargetException();
      }
      return value;
   }

   PrivilegedProxy(Object target)
   {
      this.target = target;
   }

   Object getProxy() throws Exception
   {
      BuildProxyAction action = new BuildProxyAction(target, this);
      Object proxy = null;
      try
      {
         proxy = AccessController.doPrivileged(action);
      }
      catch(PrivilegedActionException e)
      {
         throw e.getException();
      }
      return proxy;
   }

   public Object invoke(Object proxy, Method method, Object[] args)
      throws Throwable
   {
      this.method = method;
      this.args = args;
      Object value = null;
      try
      {
         value = AccessController.doPrivileged(this);
      }
      catch(PrivilegedActionException e)
      {
         throw e.getException();
      }
      return value;
   }

   private static class BuildProxyAction implements PrivilegedExceptionAction
   {
      Object target;
      InvocationHandler handler;
      BuildProxyAction(Object target, InvocationHandler handler)
      {
         this.target = target;
         this.handler = handler;
      }
      public Object run() throws Exception
      {
         HashSet tmp = new HashSet();
         // Get all interfaces
         Class c = target.getClass();
         do
         {
            Class[] ifaces = c.getInterfaces();
            for(int i = 0; i < ifaces.length; i ++)
            {
               Class iface = ifaces[i];
               int modifiers = iface.getModifiers();
               if( Modifier.isPublic(modifiers) )
                  tmp.add(iface);
            }
            c = c.getSuperclass();
         } while( c != null );

         Class[] ifaces = new Class[tmp.size()];
         tmp.toArray(ifaces);
         ClassLoader loader = Thread.currentThread().getContextClassLoader();
         return Proxy.newProxyInstance(loader, ifaces, handler);
      }
   }
}
