<commit_before>package org.pentaho.di.ui.spoon;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.vfs.FileObject;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.annotations.RepositoryPlugin;
import org.pentaho.di.core.exception.KettlePluginException;
import org.pentaho.di.core.plugins.BasePluginType;
import org.pentaho.di.core.plugins.JarFileAnnotationPlugin;
import org.pentaho.di.core.plugins.Plugin;
import org.pentaho.di.core.plugins.PluginClassTypes;
import org.pentaho.di.core.plugins.PluginFolder;
import org.pentaho.di.core.plugins.PluginInterface;
import org.pentaho.di.core.plugins.PluginTypeInterface;
import org.pentaho.di.job.entry.JobEntryInterface;
import org.pentaho.di.repository.Repository;


public class SpoonPluginType extends BasePluginType implements PluginTypeInterface {

  private SpoonPluginType() {
    super("SPOONPLUGIN", "Spoon Plugin");
    
    pluginFolders.add( new PluginFolder("plugins/spoon", false, true) ); 
  }
  
  private static SpoonPluginType pluginType;
  public static SpoonPluginType getInstance() {
    if (pluginType==null) {
      pluginType = new SpoonPluginType();
    }
    return pluginType;
  }
    @Override
  protected List<JarFileAnnotationPlugin> findAnnotatedClassFiles(String annotationClassName) {
    // TODO Auto-generated method stub
    return super.findAnnotatedClassFiles(annotationClassName);
  }

  @Override
  protected List<FileObject> findPluginXmlFiles(String folder) {
    // TODO Auto-generated method stub
    return super.findPluginXmlFiles(folder);
  }

  /**
   * Scan & register internal repository type plugins
   */
  protected void registerAnnotations() throws KettlePluginException {

    List<Class<?>> classes = getAnnotatedClasses(RepositoryPlugin.class);
    for (Class<?> clazz : classes)
    {
      SpoonPlugin repositoryPlugin = clazz.getAnnotation(SpoonPlugin.class);
      handleAnnotation(clazz, repositoryPlugin, new ArrayList<String>(), true, null);
    }   
  }
  

  @Override
  protected void registerNatives() throws KettlePluginException {
    // TODO Auto-generated method stub
    
  }

  protected void registerPluginJars() throws KettlePluginException {
    
    List<JarFileAnnotationPlugin> jarFilePlugins = findAnnotatedClassFiles(SpoonPlugin.class.getName());
    for (JarFileAnnotationPlugin jarFilePlugin : jarFilePlugins) {
      
      System.out.println("found spoonPlugin in :"+jarFilePlugin.getPluginFolder());
      
      URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { jarFilePlugin.getJarFile(), }, getClass().getClassLoader());

      try {
        Class<?> clazz = urlClassLoader.loadClass(jarFilePlugin.getClassFile().getName());
        SpoonPlugin partitioner = clazz.getAnnotation(SpoonPlugin.class);
        List<String> libraries = new ArrayList<String>();
        
        File f = new File(jarFilePlugin.getJarFile().getFile());
        File parent = f.getParentFile();
        File libDir = new File(parent.toString()+File.separator+"lib");;
        if(libDir.exists()){
          for(File fil : libDir.listFiles()){
            if(fil.getName().indexOf(".jar") > 0){
              try {
                libraries.add(fil.toURI().toURL().getFile());
              } catch (MalformedURLException e) {
                e.printStackTrace();
              }
            }
          }
        }

        libraries.add(jarFilePlugin.getJarFile().getFile());
        handleAnnotation(clazz, partitioner, libraries, false, jarFilePlugin.getPluginFolder());
      } catch(ClassNotFoundException e) {
        // Ignore for now, don't know if it's even possible.
      }<commit_msg>[PDI-2840] Adding any number of plugin jars from plugin directory root<commit_after>package org.pentaho.di.ui.spoon;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.vfs.FileObject;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.annotations.RepositoryPlugin;
import org.pentaho.di.core.exception.KettlePluginException;
import org.pentaho.di.core.plugins.BasePluginType;
import org.pentaho.di.core.plugins.JarFileAnnotationPlugin;
import org.pentaho.di.core.plugins.Plugin;
import org.pentaho.di.core.plugins.PluginClassTypes;
import org.pentaho.di.core.plugins.PluginFolder;
import org.pentaho.di.core.plugins.PluginInterface;
import org.pentaho.di.core.plugins.PluginTypeInterface;
import org.pentaho.di.job.entry.JobEntryInterface;
import org.pentaho.di.repository.Repository;


public class SpoonPluginType extends BasePluginType implements PluginTypeInterface {

  private SpoonPluginType() {
    super("SPOONPLUGIN", "Spoon Plugin");
    
    pluginFolders.add( new PluginFolder("plugins/spoon", false, true) ); 
  }
  
  private static SpoonPluginType pluginType;
  public static SpoonPluginType getInstance() {
    if (pluginType==null) {
      pluginType = new SpoonPluginType();
    }
    return pluginType;
  }
    @Override
  protected List<JarFileAnnotationPlugin> findAnnotatedClassFiles(String annotationClassName) {
    // TODO Auto-generated method stub
    return super.findAnnotatedClassFiles(annotationClassName);
  }

  @Override
  protected List<FileObject> findPluginXmlFiles(String folder) {
    // TODO Auto-generated method stub
    return super.findPluginXmlFiles(folder);
  }

  /**
   * Scan & register internal repository type plugins
   */
  protected void registerAnnotations() throws KettlePluginException {

    List<Class<?>> classes = getAnnotatedClasses(SpoonPlugin.class);
    for (Class<?> clazz : classes)
    {
      SpoonPlugin repositoryPlugin = clazz.getAnnotation(SpoonPlugin.class);
      handleAnnotation(clazz, repositoryPlugin, new ArrayList<String>(), true, null);
    }   
  }
  

  @Override
  protected void registerNatives() throws KettlePluginException {
    // TODO Auto-generated method stub
    
  }

  protected void registerPluginJars() throws KettlePluginException {
    
    List<JarFileAnnotationPlugin> jarFilePlugins = findAnnotatedClassFiles(SpoonPlugin.class.getName());
    for (JarFileAnnotationPlugin jarFilePlugin : jarFilePlugins) {
      
      System.out.println("found spoonPlugin in :"+jarFilePlugin.getPluginFolder());
      
      URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { jarFilePlugin.getJarFile(), }, getClass().getClassLoader());

      try {
        Class<?> clazz = urlClassLoader.loadClass(jarFilePlugin.getClassFile().getName());
        SpoonPlugin partitioner = clazz.getAnnotation(SpoonPlugin.class);
        List<String> libraries = new ArrayList<String>();
        
        File f = new File(jarFilePlugin.getJarFile().getFile());
        File parent = f.getParentFile();
        for(File fil : parent.listFiles()){
          try {
            libraries.add(fil.toURI().toURL().getFile());
          } catch (MalformedURLException e) {
            e.printStackTrace();
          }
        }
        File libDir = new File(parent.toString()+File.separator+"lib");;
        if(libDir.exists()){
          for(File fil : libDir.listFiles()){
            if(fil.getName().indexOf(".jar") > 0){
              try {
                libraries.add(fil.toURI().toURL().getFile());
              } catch (MalformedURLException e) {
                e.printStackTrace();
              }
            }
          }
        }

//        libraries.add(jarFilePlugin.getJarFile().getFile());
        handleAnnotation(clazz, partitioner, libraries, false, jarFilePlugin.getPluginFolder());
      } catch(ClassNotFoundException e) {
        // Ignore for now, don't know if it's even possible.
      }
{'fraction_non_alphanumeric': 0.10955446169380285, 'fraction_numerical': 0.0007572889057175312, 'mean_word_length': 3.540974212034384, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, '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': '5038969', 'n_tokens_mistral': 2281, 'n_tokens_neox': 2240, 'n_words': 437}