Changeset 3704


Ignore:
Timestamp:
Aug 28, 2007, 4:42:30 PM (14 years ago)
Author:
Martin Svensson
Message:

References #364 Moved the PluginInfo? class to another package, added a class to handle information about
configurations for each plugin info object.
And some smaller adjustments.

Location:
trunk/src
Files:
1 added
2 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/core/dtd/base-plugins.dtd

    r3698 r3704  
    3434>
    3535
    36 <!ELEMENT pluginclass (minbaseversion,configurations)>
     36<!ELEMENT pluginclass (minbaseversion,hasconfigurations)>
    3737<!ATTLIST pluginclass
    3838  classname ID #REQUIRED
     
    4040
    4141<!ELEMENT minbaseversion (#PCDATA)>
    42 <!ELEMENT configurations (#PCDATA)>
     42<!ELEMENT hasconfigurations (#PCDATA)>
  • trunk/src/core/net/sf/basedb/util/PluginInfo.java

    r3700 r3704  
    2323*/
    2424
    25 package net.sf.basedb.core;
    26 
     25package net.sf.basedb.util;
     26
     27import net.sf.basedb.core.Application;
     28import net.sf.basedb.core.BaseException;
     29import net.sf.basedb.core.InvalidDataException;
    2730import net.sf.basedb.core.plugin.About;
    2831import net.sf.basedb.core.plugin.Plugin;
    29 import net.sf.basedb.util.XMLUtil;
    3032
    3133import java.io.IOException;
     
    4850    @base.modified $Date$
    4951 */
    50 public final class PluginInfo
     52public class PluginInfo
    5153{
    5254  /**
     
    6971   */
    7072  private About about;
     73 
     74  /**
     75    The list with belonging configurations
     76   */
     77  private List<PluginConfigInfo> configs;
    7178 
    7279  /**
     
    7683  private static final String xmlFile = "base-plugins.xml";
    7784 
    78   /**
    79     The name of the dtd file that xml file
    80     above must follow.
    81    */
    82   private static final String dtdFile = "base-plugins.dtd";
     85 
    8386 
    8487  /**
     
    9093      @param configurations A list with configurations for this plugin.
    9194   */
    92   public PluginInfo (String jarPath, String className, String minBaseVersion, About about)
     95  public PluginInfo (String jarPath, String className, String minBaseVersion, About about, List<PluginConfigInfo> configs)
    9396  {
    9497    this.jarPath = jarPath;
     
    9699    this.minBaseVersion = minBaseVersion;
    97100    this.about = about;
     101    this.configs = configs;
    98102  }
    99103 
     
    112116  @SuppressWarnings("unchecked")
    113117  public static List<PluginInfo> loadFromJar(java.io.File jarFile)
    114     throws IOException, JDOMException
    115   {
     118    //throws IOException, JDOMException
     119  {
     120    final String dtdFile = "base-plugins.dtd";
     121    final String configDtdFile ="plugin-configuration-file.dtd";
     122    final String hasConfigsValue = "yes";
     123    final String configXMLFile = "base-configurations.xml";
     124   
    116125    List<PluginInfo> pluginInfos = new ArrayList<PluginInfo>();
    117     JarFile jar = new JarFile(jarFile);
    118    
    119     //Get the info in base-plugins.xml file and throw exception if
    120     //the jar file doesn't have the name that base-plugins.xml file says.
    121     InputStream is = jar.getInputStream(new JarEntry("META-INF/" + xmlFile));
    122     URL dtdURL = PluginInfo.class.getResource("/net/sf/basedb/core/dtd/" + dtdFile);
    123     Document doc = XMLUtil.getValidatedXML(is , dtdURL, null);
    124     Element rootElement = doc.getRootElement();
    125     String jarname = rootElement.getAttributeValue("jarname");
    126     JarReader jarReader = JarReader.getNewInstance(jar);
    127     if (!jarname.equalsIgnoreCase(jarFile.getName()))
    128     {
    129       throw new InvalidDataException("Did not find the expected jar file: found '" +
    130           jarFile.getName() + "' but expected: '" + jarname + "'");
    131     }
    132    
    133     //Loop through each classelement in the xml file and create a PluginInfo-object
    134     //if the class implements Plugin-interface.
    135     List<Element> classElements = (List<Element>)rootElement.getChildren();
    136     for (Element classElement : classElements)
     126    InputStream is = null;
     127   
     128    try
    137129    {     
    138       String classname = classElement.getAttributeValue("classname");
    139       String baseVersion = classElement.getChildText("minbaseversion");
    140       try
     130      JarFile jar = new JarFile(jarFile);
     131         
     132      //Get the info in base-plugins.xml file.
     133      //An exception is thrown if the jar file's name is not the same as the xml-file suggest.
     134      is = jar.getInputStream(new JarEntry("META-INF/" + xmlFile));
     135      URL installDtdURL = PluginInfo.class.getResource("/net/sf/basedb/core/dtd/" + dtdFile);
     136      Element rootElement = XMLUtil.getValidatedXML(is , installDtdURL, null).getRootElement();
     137      String jarname = rootElement.getAttributeValue("jarname");
     138      JarReader jarReader = JarReader.getNewInstance(jar);
     139      if (!jarname.equalsIgnoreCase(jarFile.getName()))
    141140      {
     141        throw new InvalidDataException("Did not find the expected jar file: found '" +
     142            jarFile.getName() + "' but expected: '" + jarname + "'");
     143      }
     144     
     145      //Loop through each classelement in the xml file and create a PluginInfo-object
     146      //if the class implements Plugin-interface.
     147      List<Element> classElements = (List<Element>)rootElement.getChildren();
     148      for (Element classElement : classElements)
     149      {     
     150        String classname = classElement.getAttributeValue("classname");
     151        String baseVersion = classElement.getChildText("minbaseversion");
     152        String configElement = classElement.getChildText("hasconfigurations");
     153        boolean hasConfigs = (configElement != null) && configElement.equals(hasConfigsValue);
     154               
    142155        Class c = jarReader.loadClass(classname);       
    143156        if (versionCompatible(baseVersion) && Plugin.class.isAssignableFrom(c))
     
    145158          Plugin pl = (Plugin)c.newInstance();
    146159          About about = pl.getAbout();
    147           //TODO load configurations
    148           pluginInfos.add(new PluginInfo(jarFile.getPath(), classname, baseVersion, about));
     160          List<PluginConfigInfo> configs = null;
     161         
     162          //Get info about configurations if there are any.
     163          if (hasConfigs)
     164          {
     165            InputStream confStream = null;
     166            try
     167            {
     168              confStream = jar.getInputStream(new JarEntry("META-INF/" + configXMLFile));
     169              URL configDtdURL = PluginInfo.class.getResource("/net/sf/basedb/core/dtd/" + configDtdFile);
     170              Document configurationDoc = XMLUtil.getValidatedXML(confStream, configDtdURL, null);
     171              configs = PluginConfigInfo.loadConfigurationsForPlugin(configurationDoc, classname);
     172            }
     173            catch (IOException ex)
     174            {}
     175            finally
     176            {
     177              if (confStream != null) confStream.close();
     178            }
     179          }         
     180          pluginInfos.add(new PluginInfo(jarFile.getPath(), classname, baseVersion, about, configs));         
    149181        }       
     182      }
     183    }
     184    catch(Throwable ex)
     185    {
     186      throw new BaseException();
     187    }
     188    finally
     189    {
     190      try
     191      {
     192        if (is != null) is.close();
    150193      }
    151       catch(Throwable ex)
    152       {
    153         ex.printStackTrace();
    154       }
    155     }   
     194      catch(IOException ex)
     195      {}
     196    }
    156197    return pluginInfos;
    157198  }
     
    191232  {
    192233    return about;
     234  }
     235 
     236  public List<PluginConfigInfo> getConfigurations()
     237  {
     238    return configs;
    193239  }
    194240 
     
    209255    return majorCompatible && minorCompatible;
    210256  }
    211  
     257   
    212258  /**
    213259    A private class to get information about plugin classes
  • trunk/src/test/TestExternalPluginInstaller.java

    r3700 r3704  
    2424
    2525import net.sf.basedb.core.Application;
    26 import net.sf.basedb.core.PluginInfo;
    2726import net.sf.basedb.util.FileUtil;
     27import net.sf.basedb.util.PluginConfigInfo;
     28import net.sf.basedb.util.PluginInfo;
    2829
    2930import java.io.File;
     
    116117  static void test_installPlugins(List<PluginInfo> infos)
    117118  {
    118     List<Integer> pluginIds = new ArrayList<Integer>();
     119    List<Integer> pluginIds = new ArrayList<Integer>();   
    119120    List<PluginInfo> notInstalled = new ArrayList<PluginInfo>();
    120121    for (PluginInfo info : infos)
    121122    {
    122123      int pluginid = TestPluginDefinition.test_create(info.getClassName(), info.getJarPath());
    123       if (pluginid > 0)
     124      try
    124125      {
    125         pluginIds.add(pluginid);
     126        if (pluginid > 0)
     127        {         
     128          List<PluginConfigInfo> configurations = info.getConfigurations();
     129          write("--Configurations for this plugin that were found: " + configurations.size());
     130          pluginIds.add(pluginid);
     131        }
     132        else
     133        {
     134          notInstalled.add(info);
     135        }
    126136      }
    127       else
     137      catch(Throwable th)
    128138      {
    129         notInstalled.add(info);
     139        if (pluginid > 0)
     140        {
     141          TestPluginDefinition.test_delete(pluginid);
     142        }
    130143      }
    131144    }
Note: See TracChangeset for help on using the changeset viewer.