<commit_before>package org.pentaho.hadoop.shim.common;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Driver;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;

public class DriverProxyInvocationChain {

  public static Driver getProxy(Class<? extends Driver> intf, 
    final Driver obj) {
      return (Driver) 
        Proxy.newProxyInstance(obj.getClass().getClassLoader(),
              new Class[] { intf }, new DriverInvocationHandler(obj));
  }
  
  private static class DriverInvocationHandler implements InvocationHandler {
    Driver driver;
    
    public DriverInvocationHandler(Driver obj) {
        driver = obj;
    }

      @Override
      public Object invoke(final Object proxy, Method method, 
        Object[] args) throws Throwable {
          
        try {
          Object o = method.invoke(driver, args);
          if(o instanceof Connection) {
            
            // Intercept the Connection object so we can proxy that too
            return (Connection)Proxy.newProxyInstance(o.getClass().getClassLoader(),
                new Class[] { Connection.class }, new ConnectionInvocationHandler((Connection)o));
          }
          else {
            return o;
          }
        }
        catch(Throwable t) {
          throw (t instanceof InvocationTargetException) ? t.getCause() : t;
        }
      }
  }
  
  private static class ConnectionInvocationHandler implements InvocationHandler {

    Connection connection;
    
    public ConnectionInvocationHandler(Connection obj) {
      connection = obj;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      Object o = null;
      try {
        o = method.invoke(connection, args);
      }
      catch(Throwable t) {

        if(t instanceof InvocationTargetException) {
          Throwable cause = t.getCause();
        
          if(cause instanceof SQLException) {
            if(cause.getMessage().equals("Method not supported")) {
              String methodName = method.getName();
              if("createStatement".equals(methodName)) {
                o = createStatement(connection,args);
              }
              else {
                StringBuffer sb = new StringBuffer("Intercepted ");
                sb.append(method.getDeclaringClass().getName());
                sb.append(".");
                sb.append(methodName);
                sb.append(" with MethodNotSupported");
                System.err.println(sb);
                throw cause;
              }
            }
            else throw cause;
          }
          else {
            throw cause;
          }
        }<commit_msg>PDI-9534: Fixed setReadOnly proxy for master<commit_after>package org.pentaho.hadoop.shim.common;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Driver;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;

public class DriverProxyInvocationChain {

  public static Driver getProxy(Class<? extends Driver> intf, 
    final Driver obj) {
      return (Driver) 
        Proxy.newProxyInstance(obj.getClass().getClassLoader(),
              new Class[] { intf }, new DriverInvocationHandler(obj));
  }
  
  private static class DriverInvocationHandler implements InvocationHandler {
    Driver driver;
    
    public DriverInvocationHandler(Driver obj) {
        driver = obj;
    }

      @Override
      public Object invoke(final Object proxy, Method method, 
        Object[] args) throws Throwable {
          
        try {
          Object o = method.invoke(driver, args);
          if(o instanceof Connection) {
            
            // Intercept the Connection object so we can proxy that too
            return (Connection)Proxy.newProxyInstance(o.getClass().getClassLoader(),
                new Class[] { Connection.class }, new ConnectionInvocationHandler((Connection)o));
          }
          else {
            return o;
          }
        }
        catch(Throwable t) {
          throw (t instanceof InvocationTargetException) ? t.getCause() : t;
        }
      }
  }
  
  private static class ConnectionInvocationHandler implements InvocationHandler {

    Connection connection;
    
    public ConnectionInvocationHandler(Connection obj) {
      connection = obj;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      Object o = null;
      try {
        o = method.invoke(connection, args);
      }
      catch(Throwable t) {

        if(t instanceof InvocationTargetException) {
          Throwable cause = t.getCause();
        
          if(cause instanceof SQLException) {
            if(cause.getMessage().equals("Method not supported")) {
              String methodName = method.getName();
              if("createStatement".equals(methodName)) {
                o = createStatement(connection,args);
              }
              else if("setReadOnly".equals(methodName)) {
                o = (Void)null;
              }
              else {
                StringBuffer sb = new StringBuffer("Intercepted ");
                sb.append(method.getDeclaringClass().getName());
                sb.append(".");
                sb.append(methodName);
                sb.append(" with MethodNotSupported");
                System.err.println(sb);
                throw cause;
              }
            }
            else throw cause;
          }
          else {
            throw cause;
          }
        }
{'fraction_non_alphanumeric': 0.0904019688269073, 'fraction_numerical': 0.0006562756357670221, 'mean_word_length': 2.099135739705135, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1568784', 'n_tokens_mistral': 1532, 'n_tokens_neox': 1503, 'n_words': 371}