Changeset 3732


Ignore:
Timestamp:
Sep 14, 2007, 2:03:09 PM (14 years ago)
Author:
Martin Svensson
Message:

References #733 The PluginConfigurationImporter? class can be used outside the plugin system to
import selected configurations in an XML-file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/plugins/core/net/sf/basedb/plugins/PluginConfigurationImporter.java

    r3720 r3732  
    7474import java.util.Map;
    7575import java.util.Set;
     76import java.util.jar.JarEntry;
     77import java.util.jar.JarFile;
    7678
    7779import org.jdom.Document;
    7880import org.jdom.Element;
     81import org.jdom.JDOMException;
    7982
    8083/**
     
    143146  private boolean setPermissions;
    144147  private boolean importAll;
     148  private HashMap<Integer, Boolean> configurationMap;
    145149
    146150  /*
     
    347351    throws BaseException
    348352  {           
    349     DbControl dc = null;
    350    
     353    DbControl dc = null;   
    351354    try
    352355    {
     
    358361        filePath = f.getPath().toString();
    359362      }
    360      
    361363      //Get the root element of the XML file
    362364      URL dtdURL = PluginConfigurationImporter.class.getResource("/net/sf/basedb/core/dtd/"+dtdFileName);
    363       Document doc = XMLUtil.getValidatedXML(in, dtdURL, filePath);    
     365      Document doc = XMLUtil.getValidatedXML(in, dtdURL, filePath);   
    364366      Element rootElement = doc.getRootElement();   
    365367     
    366368      int parameternumber = 0;
    367       List configurations = rootElement.getChildren();     
     369      List configurations = rootElement.getChildren();
    368370      for (Object obj : configurations)
    369371      {
     
    373375        String pluginClassName = configuration.getAttributeValue("pluginClassName");
    374376       
    375         boolean importConfiguration = !importAll &&
    376           job.getValue("configuration"+parameternumber) != null ? Boolean.TRUE.equals(job.getValue("configuration"+parameternumber)) : false;
     377        boolean importConfiguration = !importAll && getConfigurationInstallOption(parameternumber);
    377378        if (importConfiguration || importAll)
    378379        {
     
    410411  /*
    411412      -----------------------------------------------------
    412   */ 
     413  */
     414 
     415  /**
     416    Imports plugin configurations direct from an xml file, located in a jar.
     417    @since 2.5
     418    @param jarPath Path to the jar-file
     419    @param xmlPath Where in the jar-file the XML-file is located
     420    @param configurations A hasmap with a boolean value for each configuration in the xml-file.
     421      The value should be TRUE if the configuration should be imported, FALSE otherwise.
     422    @param importAll If all configurations in the xml-file should be imported or not.
     423      @param setPermissions If the configurations should be shared to the same users as the plugins they belong to.
     424      @throws IOException When something goes wrong accessing a file
     425      @throws JDOMException When failed to read the XML-file
     426      @throws BaseException When the hashmap with import options has different
     427        size compared to number of configurations in XML-file
     428   
     429   */
     430  public void importPluginConfigurationsFromJar(String jarPath, String xmlPath,
     431        HashMap<Integer, Boolean> configurations,  boolean importAll, boolean setPermissions)
     432    throws IOException, JDOMException, BaseException
     433  {
     434    this.importAll = importAll;
     435    this.setPermissions = setPermissions;
     436    this.configurationMap = configurations;
     437   
     438    URL dtdURL = PluginConfigurationImporter.class.getResource("/net/sf/basedb/core/dtd/"+dtdFileName);
     439   
     440    JarFile jar = new JarFile(jarPath);
     441    InputStream is = jar.getInputStream(new JarEntry(xmlPath));
     442    Document doc = XMLUtil.getValidatedXML(is, dtdURL, null);
     443    is.close();
     444    Element rootElement = doc.getRootElement();
     445    List children = rootElement.getChildren();   
     446   
     447    if (children.size() != configurations.size())
     448    {
     449      throw new BaseException("The list of configuration to import or not does'n " +
     450          "have the same size as the list of available plugins");
     451    }
     452   
     453    is = jar.getInputStream(new JarEntry(xmlPath));
     454    doImport(is, null);
     455    is.close();
     456  }
     457 
    413458 
    414459  private RequestInformation getConfiguredJobParameters()
     
    433478  }
    434479 
     480  /**
     481    Gets request information to set which configuration to import
     482      @return request information with parameter to use.
     483  */
    435484  private RequestInformation getConfiguredSelectionParameters()
    436485  {
     
    519568        }
    520569      }
    521      
    522570    }
    523571    return configureSelection;
     
    613661    }
    614662  }
     663 
     664  /**
     665    Returns if a configuration in a certain place/order in the XML-file should be imported
     666      @param orderNumber The number of order the configuration has in the XML-file
     667      @return TRUE if the configuration should be imported, FALSE otherwise.
     668   */
     669  private boolean getConfigurationInstallOption(int orderNumber)
     670  {
     671    boolean importIt = false;
     672    if (configurationMap != null)
     673    {
     674      Boolean value = configurationMap.get(orderNumber);
     675      importIt = value != null ? value : false;
     676    }
     677    else if (job != null)
     678    {
     679      importIt = job.getValue("configuration"+orderNumber) != null ?
     680          Boolean.TRUE.equals(job.getValue("configuration" + orderNumber)) : false;
     681    }
     682   
     683    return importIt;
     684  }
    615685}
Note: See TracChangeset for help on using the changeset viewer.