Plugin.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.monitor.engine.dynamic;

  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.io.Serializable;
  24. import java.net.MalformedURLException;
  25. import java.net.URL;
  26. import java.util.ArrayList;
  27. import java.util.Date;
  28. import java.util.List;

  29. import org.openspcoop2.core.config.RegistroPlugin;
  30. import org.openspcoop2.core.config.RegistroPluginArchivio;
  31. import org.openspcoop2.core.plugins.constants.TipoPlugin;
  32. import org.openspcoop2.utils.UtilsException;
  33. import org.openspcoop2.utils.regexp.RegExpException;
  34. import org.openspcoop2.utils.regexp.RegExpNotFoundException;
  35. import org.openspcoop2.utils.regexp.RegExpNotValidException;
  36. import org.openspcoop2.utils.resources.DynamicClassLoader;

  37. /**
  38.  * Plugin
  39.  *
  40.  * @author Poli Andrea (apoli@link.it)
  41.  * @author $Author$
  42.  * @version $Rev$, $Date$
  43.  */
  44. public class Plugin implements Serializable {

  45.     /**
  46.      *
  47.      */
  48.     private static final long serialVersionUID = 1L;

  49.     private String nome;
  50.     public String getNome() {
  51.         return this.nome;
  52.     }

  53.     private Date date; // data di creazione del plugin
  54.     public Date getDate() {
  55.         return this.date;
  56.     }

  57.     private List<String> compatibilita;
  58.    
  59.     private List<PluginJar> archivePlugin = new ArrayList<>();
  60.    
  61.     private transient DynamicClassLoader classLoader;
  62.    
  63.     public Plugin(RegistroPlugin plugin) throws UtilsException, IOException, RegExpException, RegExpNotValidException, RegExpNotFoundException {
  64.         this.nome = plugin.getNome();
  65.         this.date = plugin.getData();
  66.         this.compatibilita = plugin.getCompatibilitaList();
  67.        
  68.         List<URL> listUrl = new ArrayList<>();
  69.        
  70.         for (RegistroPluginArchivio pluginJar : plugin.getArchivioList()) {
  71.             init(pluginJar, listUrl);
  72.         }

  73.         this.classLoader = new DynamicClassLoader(listUrl.toArray(new URL[1]));
  74.     }
  75.     private void init(RegistroPluginArchivio pluginJar, List<URL> listUrl) throws UtilsException, IOException, RegExpException, RegExpNotValidException, RegExpNotFoundException {
  76.         String archivioPrefix = "Archivio '"+pluginJar.getNome()+"' ";
  77.         PluginJar plug = null;
  78.         switch (pluginJar.getSorgente()) {
  79.         case JAR:
  80.             if(pluginJar.getContenuto()==null) {
  81.                 throw new UtilsException(archivioPrefix+"senza contenuto (sorgente: "+pluginJar.getSorgente()+")");
  82.             }
  83.             plug = new PluginJar(pluginJar.getNome(), pluginJar.getData(), pluginJar.getContenuto());
  84.             this.archivePlugin.add(plug);
  85.             listUrl.add(plug.getResourceURL());
  86.             break;
  87.         case URL:
  88.             if(pluginJar.getUrl()==null) {
  89.                 throw new UtilsException(archivioPrefix+"senza url (sorgente: "+pluginJar.getSorgente()+")");
  90.             }
  91.             plug = new PluginJar(pluginJar.getNome(), pluginJar.getData(), pluginJar.getUrl());
  92.             this.archivePlugin.add(plug);
  93.             listUrl.add(plug.getResourceURL());
  94.             break;
  95.         case DIR:
  96.             if(pluginJar.getDir()==null) {
  97.                 throw new UtilsException(archivioPrefix+"senza directory (sorgente: "+pluginJar.getSorgente()+")");
  98.             }
  99.             File fDir = new File(pluginJar.getDir());
  100.             if(!fDir.exists()) {
  101.                 throw new UtilsException(archivioPrefix+"indica una directory '"+fDir.getAbsolutePath()+"' non esistente (sorgente: "+pluginJar.getSorgente()+")");
  102.             }
  103.             if(fDir.isDirectory()) {
  104.                 loadJar(pluginJar.getNome(), pluginJar.getData(), fDir, listUrl);
  105.             }
  106.             else {
  107.                 plug = new PluginJar(pluginJar.getNome(), pluginJar.getData(), pluginJar.getDir());
  108.                 this.archivePlugin.add(plug);
  109.                 listUrl.add(plug.getResourceURL());
  110.             }
  111.             break;
  112.         }
  113.     }
  114.    
  115.     private void loadJar(String nomePlugin, Date data, File dir, List<URL> listUrl) throws UtilsException, MalformedURLException, RegExpException, RegExpNotValidException, RegExpNotFoundException {
  116.        
  117.         List<File> dirs = new ArrayList<>();
  118.        
  119.         File [] files = dir.listFiles();
  120.         if(files!=null && files.length>0) {
  121.             for (int i = 0; i < files.length; i++) {
  122.                 File f = files[i];
  123.                 if(f.isFile()) {
  124.                     if(f.getName().endsWith(".jar")) {
  125.                         PluginJar plug = new PluginJar(nomePlugin+"#"+f.getAbsolutePath(), data, f.getAbsolutePath());
  126.                         this.archivePlugin.add(plug);
  127.                         listUrl.add(plug.getResourceURL());
  128.                     }
  129.                 }
  130.                 else if(f.isDirectory()) {
  131.                     dirs.add(f);
  132.                 }
  133.             }
  134.         }
  135.        
  136.         if(!dirs.isEmpty()) {
  137.             for (File dirChild : dirs) {
  138.                 loadJar(nomePlugin, data, dirChild, listUrl);
  139.             }
  140.         }
  141.     }
  142.    
  143.    
  144.    
  145.     public ClassLoader getClassLoader(TipoPlugin tipoClasseDaRicercare) {
  146.         return this.getClassLoader(tipoClasseDaRicercare.getValue());
  147.     }
  148.     public ClassLoader getClassLoader(String tipoClasseCustomDaRicercare) {
  149.        
  150.         if(this.compatibilita!=null && !this.compatibilita.isEmpty() &&
  151.             !this.compatibilita.contains(tipoClasseCustomDaRicercare)) {
  152.             return null; // class loader non compatibile con il tipo cercato
  153.         }
  154.        
  155.         return this.classLoader;
  156.     }
  157.    
  158.    
  159.     public void close() {
  160.         if(!this.archivePlugin.isEmpty()) {
  161.             for (PluginJar pluginJar : this.archivePlugin) {
  162.                 pluginJar.close();
  163.             }
  164.         }
  165.     }
  166.    
  167.    
  168. }