Changeset 5617


Ignore:
Timestamp:
Apr 28, 2011, 8:09:11 AM (11 years ago)
Author:
Nicklas Nordborg
Message:

References #1592: Unified installation procedure for plug-ins, extensions and more...

Added support for installing plug-in configurations. The file we are looking for has changed to META-INF/plugin-configurations.xml

Moved duplicates of the installation code to the PluginConfiguration class (from the Install class and PluginConfigurationImporter).

Location:
trunk/src
Files:
1 added
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/clients/web/net/sf/basedb/clients/web/extensions/ExtensionsControl.java

    r5616 r5617  
    6868import net.sf.basedb.util.extensions.xml.ExtensionPointFilter;
    6969import net.sf.basedb.util.extensions.xml.PluginInfo;
    70 import net.sf.basedb.util.extensions.xml.XmlLoader;
    7170import net.sf.basedb.util.filter.Filter;
    7271
     
    255254
    256255      // 2b. Load plug-in definitions information
    257       PluginInstallationProcessor pluginInstaller = new PluginInstallationProcessor(dc, new XmlLoader(), results);
     256      PluginInstallationProcessor pluginInstaller = new PluginInstallationProcessor(dc, results);
    258257      manager.processFiles(pluginInstaller, installFilter);
    259258      numInstalledPlugins = pluginInstaller.getNumPlugins();
  • trunk/src/core/net/sf/basedb/core/Application.java

    r5616 r5617  
    553553 
    554554        // Initialize extensions system
    555         // TODO -- should this be moved to a utility class? Probably...
     555        boolean extensionsAreDisabled = Config.getBoolean("extensions.disabled");
    556556        Registry xtRegistry = new Registry();
    557557        java.io.File xtSettings = new java.io.File(userFilesDirectory, "extension-settings.xml");
     
    562562        // Add core plug-ins
    563563        xtManager.addURI(Application.class.getResource("/core-plugins.xml").toURI());
    564        
    565         if (!Config.getBoolean("extensions.disabled"))
    566         {
    567           // Add plugins directory to the manager (unless disabled)
    568           xtManager.addDirectory(pluginsDirectory);
    569         }
     564        // Add plugins directory to the manager (unless disabled)
     565        if (!extensionsAreDisabled) xtManager.addDirectory(pluginsDirectory);
    570566       
    571567        // Register and load core extensions only
  • trunk/src/core/net/sf/basedb/core/Install.java

    r5615 r5617  
    168168    try
    169169    {
     170      Config.setProperty("extensions.disabled", "true");
    170171      Application.start(false, false, false);
    171172      session = HibernateUtil.newSession();
     
    25032504    Create {@link PluginConfiguration}s from a XML file.
    25042505  */
     2506  @SuppressWarnings("unchecked")
    25052507  private static void createPluginConfigurations(String filePath, boolean update)
    25062508    throws BaseException
     
    25272529      }
    25282530     
    2529       URL dtdURL = Install.class.getResource("/net/sf/basedb/core/dtd/plugin-configuration-file.dtd");
    2530       URL fileURL = Install.class.getResource(filePath);
    2531       Document doc = XMLUtil.getValidatedXml(fileURL, dtdURL);
    2532       Element rootElement = doc.getRootElement();   
    2533      
    2534       List configurations = rootElement.getChildren();
    2535      
    2536       for (Object obj : configurations)
    2537       {
    2538         Element configuration = (Element)obj;
     2531      List<Element> configurations = PluginConfiguration.loadXmlFile(Install.class.getResource(filePath).openStream(), filePath);
     2532     
     2533      for (Element configuration : configurations)
     2534      {
    25392535        String name = configuration.getChildText("configname");
    25402536        String description = configuration.getChildText("description");
     
    25602556        try
    25612557        {
    2562           PluginDefinition pluginDefinition = PluginDefinition.getByClassName(dc, pluginClassName);
     2558          PluginDefinition plugin = PluginDefinition.getByClassName(dc, pluginClassName);
    25632559                             
    25642560          // Create a new plugin configurationj
    2565           PluginConfiguration pluginConfig = PluginConfiguration.getNew(dc, pluginDefinition); 
    2566           pluginConfig.setItemKey(pluginDefinition.getItemKey());
    2567           pluginConfig.setProjectKey(pluginDefinition.getProjectKey());
    2568           dc.saveItem(pluginConfig);
    2569           pluginConfig.setName(name);
    2570           pluginConfig.setDescription(description);
    2571          
    2572           // Get the confiuration parameters from file
    2573           List parameters = configuration.getChildren("parameter");
    2574           for (Object elementObj : parameters)
    2575           {
    2576             Element parameter = (Element)elementObj;
    2577             String parametername = parameter.getChildText("name");               
    2578             String cl = parameter.getChildText("class");
    2579             String label = parameter.getChildText("label").length() > 0 ? parameter.getChildText("label") : name;         
    2580             Class clazz = null;
    2581             List<Object> values = new ArrayList<Object>() ;
    2582            
    2583             if (cl.length() > 0)
    2584             {
    2585               clazz = Class.forName(cl);
    2586               List children = parameter.getChildren("value");
    2587               // Get the parameter values
    2588               for (Object ch : children)
    2589               {
    2590                 String text = ((Element)ch).getText();
    2591                 if (clazz.equals(Boolean.class)) values.add(Boolean.valueOf(text));
    2592                 else if (clazz.equals(Date.class)) values.add(DateUtil.parseString(text));
    2593                 else if (clazz.equals(Double.class)) values.add(new Double(text));
    2594                 else if (clazz.equals(Float.class)) values.add(new Float(text));
    2595                 else if (clazz.equals(Long.class)) values.add(new Long(text));
    2596                 else if (clazz.equals(String.class)) values.add(text);
    2597               }   
    2598               // Get the parametertype
    2599               ParameterType pType;
    2600               if (clazz.equals(Boolean.class)) pType = new BooleanParameterType();
    2601               else if (clazz.equals(Date.class)) pType =  new DateParameterType();
    2602               else if (clazz.equals(Double.class)) pType =  new DoubleParameterType();   
    2603               else if (clazz.equals(Float.class)) pType =  new FloatParameterType();
    2604               else if (clazz.equals(Integer.class)) pType =  new IntegerParameterType();   
    2605               else if (clazz.equals(Long.class)) pType =  new LongParameterType();
    2606               else if (clazz.equals(String.class)) pType =  new StringParameterType();
    2607               else pType = null;
    2608               pluginConfig.setParameterValues(parametername,label, "", pType, values);
    2609             }
    2610           }         
     2561          PluginConfiguration config = PluginConfiguration.getNew(dc, plugin); 
     2562          config.setItemKey(plugin.getItemKey());
     2563          config.setProjectKey(plugin.getProjectKey());
     2564          dc.saveItem(config);
     2565          config.setName(name);
     2566          config.setDescription(description);
     2567          config.setParameterValues((List<Element>)configuration.getChildren("parameter"));
    26112568        }       
    26122569        catch(Throwable ex)
  • trunk/src/core/net/sf/basedb/core/PluginConfiguration.java

    r5595 r5617  
    3535import net.sf.basedb.core.query.Hql;
    3636import net.sf.basedb.core.query.Expressions;
    37 
     37import net.sf.basedb.util.XMLUtil;
     38
     39import java.io.IOException;
     40import java.io.InputStream;
     41import java.net.URL;
     42import java.util.ArrayList;
    3843import java.util.Date;
    3944import java.util.HashSet;
     
    4247import java.util.Set;
    4348import java.util.Collections;
     49
     50import org.jdom.Document;
     51import org.jdom.Element;
     52import org.jdom.JDOMException;
    4453
    4554/**
     
    165174    }
    166175    return query;
     176  }
     177
     178  /**
     179    Load a configurations xml file from the given stream.
     180    @param in The input stream to read from
     181    @param fileName The name of the originating file (optional)
     182    @return A list with all &lt;configuration&gt; elements in the file
     183    @since 3.0
     184  */
     185  @SuppressWarnings("unchecked")
     186  public static List<Element> loadXmlFile(InputStream in, String fileName)
     187    throws IOException, JDOMException
     188  {
     189    URL dtdURL = PluginConfiguration.class.getResource("/net/sf/basedb/core/dtd/plugin-configuration-file.dtd");
     190    Document doc = XMLUtil.getValidatedXML(in, dtdURL, fileName);
     191    Element rootElement = doc.getRootElement();
     192    return (List<Element>)rootElement.getChildren();
    167193  }
    168194
     
    666692 
    667693
     694  /**
     695    Sets parameters for a PluginConfiguration from the elements in a xml
     696    configurations file.
     697   
     698      @param parameters A list of the parameters that shall be set to the configuration
     699      @throws ClassNotFoundException If there is an error while trying to find the type of the parameters.
     700      @since 3.0
     701  */
     702  @SuppressWarnings("unchecked")
     703  public void setParameterValues(List<Element> parameters)
     704    throws ClassNotFoundException
     705  {
     706    checkPermission(Permission.WRITE);
     707    // Set each configuration parameter for the plugin.
     708    for (Element parameter : parameters)
     709    {
     710      String name = parameter.getChildText("name");               
     711      String cl = parameter.getChildText("class");
     712      String label = parameter.getChildText("label");
     713      if (label == null || label.length() == 0) label = name;   
     714      String description = parameter.getChildText("description");
     715      Class clazz = null;
     716      List values = null;
     717     
     718      if (cl.length() > 0)
     719      {
     720        clazz = Class.forName(cl);
     721        values = getValueList(parameter, clazz);
     722        ParameterType pType = getParameterType(clazz, values);
     723        setParameterValues(name,label, description, pType, values);
     724      }
     725    }
     726  }
     727 
     728  /**
     729      Gets the list of values for a parameter from the XML-file.
     730      Returns a list with parameter values.
     731  */ 
     732  @SuppressWarnings("unchecked")
     733  private <T> List<T> getValueList(Element parameter, Class<T> classType)
     734  {
     735    List<Element> children = (List<Element>)parameter.getChildren("value");
     736    List<T> values = new ArrayList<T>();
     737    for (Object ch : children)
     738    {
     739      String text = ((Element)ch).getText();
     740      if (classType.equals(Boolean.class)) values.add((T)Boolean.valueOf(text));
     741      else if (classType.equals(Date.class)) values.add((T)DateUtil.parseString(text));
     742      else if (classType.equals(Double.class)) values.add((T)Double.valueOf(text));
     743      else if (classType.equals(Float.class)) values.add((T)Float.valueOf(text));
     744      else if (classType.equals(Long.class)) values.add((T)Long.valueOf(text));
     745      else if (classType.equals(String.class)) values.add((T)text);
     746    }
     747    return values;
     748  }
     749 
     750  /**
     751      Gets the ParameterType to use with a class.
     752      Returns null if this importer doesn't support the class.
     753  */
     754  @SuppressWarnings("unchecked")
     755  private <T> ParameterType<T> getParameterType(Class<T> parameterClass, List<?> values)
     756  {
     757    int multiplicity = 1;
     758    if (values != null && values.size() > 1) multiplicity = values.size();
     759    ParameterType pType = null;
     760   
     761    if (parameterClass.equals(Boolean.class))
     762    {
     763      pType = new BooleanParameterType(null, false, multiplicity, null);
     764    }
     765    else if (parameterClass.equals(Date.class))
     766    {
     767      pType = new DateParameterType(null, false, multiplicity, 0, 0, null);
     768    }
     769    else if (parameterClass.equals(Double.class))
     770    {
     771      pType = new DoubleParameterType(null, null, null, false, multiplicity, 0, 0, null);   
     772    }
     773    else if (parameterClass.equals(Float.class))
     774    {
     775      pType = new FloatParameterType(null, null, null, false, multiplicity, 0, 0, null);
     776    }
     777    else if (parameterClass.equals(Integer.class))
     778    {
     779      pType = new IntegerParameterType(null, null, null, false, multiplicity, 0, 0, null);   
     780    }
     781    else if (parameterClass.equals(Long.class))
     782    {
     783      pType = new LongParameterType(null, null, null, false, multiplicity, 0, 0, null);
     784    }
     785    else if (parameterClass.equals(String.class))
     786    {
     787      // Find longest string
     788      int maxLength = 255;
     789      if (values != null && !values.isEmpty())
     790      {
     791        for (Object obj : values)
     792        {
     793          if (obj instanceof String)
     794          {
     795            String str = obj.toString();
     796            if (str.length() > maxLength) maxLength = str.length();
     797          }
     798        }
     799      }
     800      pType = new StringParameterType(maxLength, null, false, multiplicity, 0, 0);
     801    }
     802    return pType;
     803  }
     804
     805 
    668806}
  • trunk/src/core/net/sf/basedb/core/Update.java

    r5592 r5617  
    7373    try
    7474    {
     75      Config.setProperty("extensions.disabled", "true");
    7576      Application.start(false, false, false);
    7677
     
    192193      try
    193194      {
     195        Config.setProperty("extensions.disabled", "true");
    194196        Application.start(false, false, false);
    195197 
  • trunk/src/core/net/sf/basedb/util/extensions/manager/ExtensionsFile.java

    r5616 r5617  
    385385
    386386  /**
     387    Get an input stream for reading the resource specified by the
     388    given path
     389    @param path The path of the resource
     390    @return An input stream (or null if this extensions file is not a JAR file or if the given path is not found)
     391    @throws IOException
     392  */
     393  public InputStream getStream(String path)
     394    throws IOException
     395  {
     396    InputStream in = null;
     397    if (isJar())
     398    {
     399      final ZipFile zipFile = new ZipFile(getFile());
     400      ZipEntry zipEntry = zipFile.getEntry(path);
     401      if (zipEntry != null)
     402      {
     403        in = new CloseResourceInputStream(zipFile.getInputStream(zipEntry),
     404          new Closeable()
     405          {
     406            // Make sure the zip file is closed when the returned stream is closed
     407            @Override
     408            public void close()
     409              throws IOException
     410            {
     411              zipFile.close();
     412            }
     413          }
     414        );
     415      }
     416    }
     417    return in;
     418  }
     419
     420 
     421  /**
    387422    Validate the XML file with the extension definitions.
    388423  */
  • trunk/src/core/net/sf/basedb/util/extensions/manager/ExtensionsManager.java

    r5616 r5617  
    9191    this.installedObjects = new HashMap<ObjectKey<?>, ExtensionsFile>();
    9292    addIgnore(settingsFile);
    93     // Add installed files
    94     for (File f : settings.getInstalledFiles())
    95     {
    96       ExtensionsFile xtFile = new ExtensionsFile(this, f);
    97       addExtensionsFile(xtFile);
    98     }
    9993  }
    10094 
     
    142136  /**
    143137    Add a directory to this manager. The directory will be monitored
    144     for extension files in the future.
     138    for extension files in the future. Files in the directory that
     139    are installed as per the settings {@link Settings#isInstalledFile(File)}
     140    are loaded into manager.
    145141    <p>
    146     This method call is ignored if the
    147     path is not pointing to an existing directory.
     142    This method call is ignored if the path is not pointing to an
     143    existing directory.
    148144   
    149145    @param dir An existing directory
    150146    @return The number of files in the directory that was
    151       not previously known to the manager
    152   */
    153   public synchronized void addDirectory(File dir)
     147      installed
     148  */
     149  public synchronized int addDirectory(File dir)
    154150  {
    155151    if (dir == null) throw new NullPointerException("dir");
    156     if (!dir.isDirectory()) return;
     152    if (!dir.isDirectory()) return 0;
    157153    directories.add(dir);
     154    return scanForInstalledFiles(dir, settings);
    158155  }
    159156 
     
    372369 
    373370  /**
     371    Scan the given directory and add files that are installed
     372    according to the settings.
     373    @param dir The directory to scan
     374    @return The number of installed files in the given directory
     375  */
     376  private int scanForInstalledFiles(File dir, Settings settings)
     377  {
     378    int numInstalled = 0;
     379   
     380    if (!dir.isDirectory())
     381    {
     382      log.warn("File '" + dir + "' is not a directory. Ignoring.");
     383      return numInstalled;
     384    }
     385 
     386    log.info("Scanning directory: " + dir);
     387    FileFilter filter = new RegexpFileFilter(".*\\.(xml|jar)", "");
     388    File[] files = dir.listFiles(filter);
     389   
     390    if (files != null && files.length > 0)
     391    {
     392      log.debug("Found " + files.length + " files");
     393      for (File file : files)
     394      {
     395        if (ignore.contains(file))
     396        {
     397          log.debug("File '" + file + "' is ignored.");
     398          continue; // with the next file
     399        }
     400       
     401        URI uri = file.toURI();
     402        ExtensionsFile xtFile = xtFiles.get(uri);
     403        if (xtFile != null)
     404        {
     405          log.debug("File '" + xtFile + "' is a known file.");
     406        }
     407        else if (settings.isInstalledFile(file))
     408        {
     409          log.debug("Adding installed file: " + file);
     410          xtFile = new ExtensionsFile(this, file);
     411          addExtensionsFile(xtFile);
     412          numInstalled++;
     413        }
     414        else
     415        {
     416          log.debug("File '" + file + "' is not installed.");
     417        }
     418      }
     419      log.info("Found " + numInstalled + " new *.xml and *.jar files");
     420    }
     421    else
     422    {
     423      log.info("Found no *.xml or *.jar files");
     424    }
     425    return numInstalled;
     426  }
     427
     428 
     429  /**
    374430    Get a list of all extension files managed by this manager.
    375431    @return A list
  • trunk/src/core/net/sf/basedb/util/extensions/manager/processor/PluginInstallationProcessor.java

    r5616 r5617  
    2323
    2424import java.io.InputStream;
     25import java.util.HashMap;
    2526import java.util.List;
     27import java.util.Map;
     28
     29import org.jdom.Element;
    2630
    2731import net.sf.basedb.core.DbControl;
    2832import net.sf.basedb.core.ItemKey;
     33import net.sf.basedb.core.ItemQuery;
    2934import net.sf.basedb.core.Permission;
     35import net.sf.basedb.core.PluginConfiguration;
    3036import net.sf.basedb.core.PluginDefinition;
     37import net.sf.basedb.core.Type;
     38import net.sf.basedb.core.query.Expressions;
     39import net.sf.basedb.core.query.Hql;
     40import net.sf.basedb.core.query.Restrictions;
    3141import net.sf.basedb.util.FileUtil;
    3242import net.sf.basedb.util.extensions.manager.ExtensionsFile.WriteableExtensionsFile;
     
    3646import net.sf.basedb.util.extensions.manager.PluginInfoKey;
    3747import net.sf.basedb.util.extensions.manager.ProcessResults;
     48import net.sf.basedb.util.extensions.xml.PluginDefinitionFilter;
    3849import net.sf.basedb.util.extensions.xml.PluginInfo;
    3950import net.sf.basedb.util.extensions.xml.XmlLoader;
     
    6778      (if not given, only the information from the XML files are loaded,
    6879      the plug-ins are not installed)
    69     @param loader The XML loader to use when parsing the metadata
    70   */
    71   public PluginInstallationProcessor(DbControl dc, XmlLoader loader, ProcessResults results)
     80  */
     81  public PluginInstallationProcessor(DbControl dc, ProcessResults results)
    7282  {
    7383    this.dc = dc;
    74     this.loader = loader;
     84    this.loader = new XmlLoader();
    7585    this.results = results;
    7686  }
     
    8696    this.numError = 0;
    8797    this.numTotalPlugins = 0;
    88   }
    89 
     98    loader.setFilter(new PluginDefinitionFilter());
     99  }
     100
     101  @SuppressWarnings("unchecked")
    90102  @Override
    91103  public void processFile(ExtensionsManager manager, WriteableExtensionsFile wFile)
     
    96108    try
    97109    {
    98       log.info("Loading extensions from file: " + xtFile);
     110      log.info("Loading plug-in definitions from file: " + xtFile);
    99111     
    100112      in = xtFile.getXmlStream();
    101113      loader.loadXmlFile(in, xtFile.getName(), xtFile.getClassLoader(), true);
     114      FileUtil.close(in);
    102115      List<PluginInfo> plugins = loader.getPluginDefinitions();
    103116     
     
    105118      int numUpdated = 0;
    106119      int numProcessed = 0;
     120      int numConfigurations = 0;
     121      Map<String, PluginDefinition> allPlugins = new HashMap<String, PluginDefinition>();
    107122      if (plugins.size() > 0)
    108123      {
     
    122137          {
    123138            PluginDefinition plugin = PluginDefinition.installOrUpdate(dc, info, jarFile, shareToEveryone);
     139            allPlugins.put(info.getClassName(), plugin);
    124140            if (plugin.isInDatabase())
    125141            {
     
    136152      }
    137153     
     154      // Load plug-in configurations
     155      if (dc != null)
     156      {
     157        in = xtFile.getStream("META-INF/plugin-configurations.xml");
     158        if (in != null)
     159        {
     160          List<Element> configurations = PluginConfiguration.loadXmlFile(in, "META-INF/plugin-configurations.xml");
     161          FileUtil.close(in);
     162          for (Element configuration : configurations)
     163          {
     164            String name = configuration.getChildText("configname");
     165            String description = configuration.getChildText("description");
     166            String pluginClassName = configuration.getAttributeValue("pluginClassName");
     167           
     168            PluginDefinition plugin = allPlugins.get(pluginClassName);
     169            if (plugin != null)
     170            {
     171              PluginConfiguration config = null;
     172              if (plugin.isInDatabase())
     173              {
     174                ItemQuery<PluginConfiguration> query = plugin.getPluginConfigurations();
     175                query.restrict(Restrictions.eq(Hql.property("name"), Expressions.parameter("name", name, Type.STRING)));
     176                List<PluginConfiguration> existing = query.list(dc);
     177                if (existing.size() == 0)
     178                {
     179                  config = plugin.newPluginConfiguration();
     180                }
     181              }
     182              else
     183              {
     184                config = plugin.newPluginConfiguration();
     185              }
     186              if (config != null && !config.isInDatabase())
     187              {
     188                dc.saveItem(config);
     189                config.setItemKey(plugin.getItemKey());
     190                config.setProjectKey(plugin.getProjectKey());
     191                config.setName(name);
     192                config.setDescription(description);
     193                config.setParameterValues((List<Element>)configuration.getChildren("parameter"));
     194                numConfigurations++;
     195              }
     196            }
     197 
     198          }
     199        }
     200      }
     201     
    138202      numFiles++;
    139203      numTotalPlugins += numInstalled + numUpdated;
     
    142206        if (numInstalled > 0) results.addMessage(xtFile, numInstalled + " plug-in(s) installed.");
    143207        if (numUpdated > 0) results.addMessage(xtFile, numUpdated + " plug-in(s) updated.");
     208        if (numConfigurations > 0) results.addMessage(xtFile, numConfigurations + " plug-in configuration(s) created");
    144209        if (numInstalled == 0 && numUpdated == 0 && numProcessed > 0)
    145210        {
  • trunk/src/install/net/sf/basedb/install/Webclient.java

    r5615 r5617  
    9090    throws BaseException
    9191  {
     92    Config.setProperty("extensions.disabled", "true");
    9293    Application.start(false);
    9394    SessionControl sc = Application.newSessionControl(null, null, null);
  • trunk/src/plugins/core/net/sf/basedb/plugins/PluginConfigurationImporter.java

    r5612 r5617  
    2626import net.sf.basedb.core.BaseException;
    2727import net.sf.basedb.core.BooleanParameterType;
    28 import net.sf.basedb.core.DateParameterType;
    29 import net.sf.basedb.core.DateUtil;
    3028import net.sf.basedb.core.DbControl;
    31 import net.sf.basedb.core.DoubleParameterType;
    3229import net.sf.basedb.core.File;
    3330import net.sf.basedb.core.FileParameterType;
    34 import net.sf.basedb.core.FloatParameterType;
    35 import net.sf.basedb.core.IntegerParameterType;
    3631import net.sf.basedb.core.InvalidDataException;
    3732import net.sf.basedb.core.Item;
    3833import net.sf.basedb.core.ItemContext;
    3934import net.sf.basedb.core.ItemNotFoundException;
    40 import net.sf.basedb.core.LongParameterType;
    41 import net.sf.basedb.core.ParameterType;
    4235import net.sf.basedb.core.PluginConfiguration;
    4336import net.sf.basedb.core.PluginDefinition;
     
    4538import net.sf.basedb.core.ProgressReporter;
    4639import net.sf.basedb.core.RequestInformation;
    47 import net.sf.basedb.core.StringParameterType;
    4840import net.sf.basedb.core.plugin.AbstractPlugin;
    4941import net.sf.basedb.core.plugin.AutoDetectingImporter;
     
    5648import net.sf.basedb.core.signal.SignalTarget;
    5749import net.sf.basedb.core.signal.ThreadSignalHandler;
    58 import net.sf.basedb.util.XMLUtil;
     50import net.sf.basedb.util.FileUtil;
    5951
    6052import java.io.BufferedReader;
     
    6254import java.io.InputStream;
    6355import java.io.InputStreamReader;
    64 import java.net.URL;
    6556import java.nio.charset.Charset;
    6657import java.util.ArrayList;
    6758import java.util.Arrays;
    68 import java.util.Date;
    6959import java.util.HashMap;
    7060import java.util.HashSet;
     
    7565import java.util.jar.JarFile;
    7666
    77 import org.jdom.Document;
    7867import org.jdom.Element;
    7968import org.jdom.JDOMException;
     
    340329  }
    341330
     331  @SuppressWarnings("unchecked")
    342332  public void doImport(InputStream in, ProgressReporter progress)
    343333    throws BaseException
     
    353343        filePath = f.getPath().toString();
    354344      }
    355       //Get the root element of the XML file
    356       URL dtdURL = PluginConfigurationImporter.class.getResource("/net/sf/basedb/core/dtd/"+dtdFileName);
    357       Document doc = XMLUtil.getValidatedXML(in, dtdURL, filePath);   
    358       Element rootElement = doc.getRootElement();   
    359345     
     346      List<Element> configurations = PluginConfiguration.loadXmlFile(in, filePath);
    360347      int parameternumber = 0;
    361       List configurations = rootElement.getChildren();
    362       for (Object obj : configurations)
     348      for (Element configuration : configurations)
    363349      {
    364350        ThreadSignalHandler.checkInterrupted();
    365         Element configuration = (Element)obj;
    366351        String name = configuration.getChildText("configname");
    367352        String description = configuration.getChildText("description");
    368353        String pluginClassName = configuration.getAttributeValue("pluginClassName");
    369         PluginDefinition pluginDefinition = null;
     354        PluginDefinition plugin = null;
    370355        try
    371356        {
    372           pluginDefinition = PluginDefinition.getByClassName(dc, pluginClassName);
     357          plugin = PluginDefinition.getByClassName(dc, pluginClassName);
    373358         
    374           boolean importConfiguration = !importAll && getConfigurationInstallOption(parameternumber);
    375           if ((importConfiguration || importAll))
     359          boolean importConfiguration = importAll || getConfigurationInstallOption(parameternumber);
     360          if (importConfiguration)
    376361          {
    377             PluginConfiguration pluginConfig = PluginConfiguration.getNew(dc, pluginDefinition);
     362            PluginConfiguration config = PluginConfiguration.getNew(dc, plugin);
    378363            if (setPermissions)
    379364            {
    380               pluginConfig.setItemKey(pluginDefinition.getItemKey());
    381               pluginConfig.setProjectKey(pluginDefinition.getProjectKey());
     365              config.setItemKey(plugin.getItemKey());
     366              config.setProjectKey(plugin.getProjectKey());
    382367            }
    383             pluginConfig.setName(name);
    384             pluginConfig.setDescription(description);
    385             dc.saveItem(pluginConfig);
    386            
    387             List parameters = configuration.getChildren("parameter");
    388             setPluginConf(parameters, pluginConfig);
     368            config.setName(name);
     369            config.setDescription(description);
     370            config.setParameterValues((List<Element>)configuration.getChildren("parameter"));
     371            dc.saveItem(config);
    389372          }
    390373        }
     
    486469     
    487470      DbControl dc = null;
    488      
     471      InputStream in = null;
    489472      try
    490473      {
    491474        dc = sc.newDbControl();
    492475        //Get the root element of the XML file
    493         String filePath = null;
    494476        File file = (File)job.getValue("xmlfile");
    495         {
    496           file = File.getById(dc, file.getId());
    497           filePath = file.getPath().toString();
    498         }
    499         URL dtdURL = PluginConfigurationImporter.class.getResource("/net/sf/basedb/core/dtd/"+dtdFileName);
    500         InputStream is = file.getDownloadStream(0);
    501         Document doc = XMLUtil.getValidatedXML(is, dtdURL, filePath);
    502         is.close();
    503         Element rootElement = doc.getRootElement();
     477        file = File.getById(dc, file.getId());
     478        String filePath = file.getPath().toString();
     479       
     480        in = file.getDownloadStream(0);
     481        List<Element> configurations = PluginConfiguration.loadXmlFile(in, filePath);
     482        FileUtil.close(in);
    504483       
    505484        List<PluginParameter<?>> typeList = null;
    506         List configurations = rootElement.getChildren();
    507485        int parameternumber = 0;
    508486        for (Object obj : configurations)
     
    564542      finally
    565543      {
    566         if (dc != null)
    567         {
    568           dc.close();
    569         }
     544        if (dc != null) dc.close();
     545        FileUtil.close(in);
    570546      }
    571547    }
    572548    return configureSelection;
    573   }
    574  
    575   /**
    576       Gets the list of values for a parameter from the XML-file.
    577       Returns a list with parameter values.
    578   */ 
    579   private List getValueList(Element parameter, Class classType)
    580   {
    581     List children = parameter.getChildren("value");
    582     List<Object> values = new ArrayList<Object>();
    583     for (Object ch : children)
    584     {
    585       String text = ((Element)ch).getText();
    586       if (classType.equals(Boolean.class)) values.add(Boolean.valueOf(text));
    587       else if (classType.equals(Date.class)) values.add(DateUtil.parseString(text));
    588       else if (classType.equals(Double.class)) values.add(Double.parseDouble(text));
    589       else if (classType.equals(Float.class)) values.add(Float.parseFloat(text));
    590       else if (classType.equals(Long.class)) values.add(Long.parseLong(text));
    591       else if (classType.equals(String.class)) values.add(text);
    592     }   
    593     return values;
    594   }
    595  
    596   /**
    597       Gets the ParameterType to use with a class.
    598       Returns null if this importer doesn't support the class.
    599   */
    600   private ParameterType getParameterType(Class parameterClass, List<?> values)
    601   {
    602     int multiplicity = 1;
    603     if (values != null && values.size() > 1) multiplicity = values.size();
    604     ParameterType pType = null;
    605    
    606     if (parameterClass.equals(Boolean.class))
    607     {
    608       pType = new BooleanParameterType(null, false, multiplicity, null);
    609     }
    610     else if (parameterClass.equals(Date.class))
    611     {
    612       pType = new DateParameterType(null, false, multiplicity, 0, 0, null);
    613     }
    614     else if (parameterClass.equals(Double.class))
    615     {
    616       pType = new DoubleParameterType(null, null, null, false, multiplicity, 0, 0, null);   
    617     }
    618     else if (parameterClass.equals(Float.class))
    619     {
    620       pType = new FloatParameterType(null, null, null, false, multiplicity, 0, 0, null);
    621     }
    622     else if (parameterClass.equals(Integer.class))
    623     {
    624       pType = new IntegerParameterType(null, null, null, false, multiplicity, 0, 0, null);   
    625     }
    626     else if (parameterClass.equals(Long.class))
    627     {
    628       pType = new LongParameterType(null, null, null, false, multiplicity, 0, 0, null);
    629     }
    630     else if (parameterClass.equals(String.class))
    631     {
    632       // Find longest string
    633       int maxLength = 255;
    634       if (values != null && !values.isEmpty())
    635       {
    636         for (Object obj : values)
    637         {
    638           if (obj instanceof String)
    639           {
    640             String str = obj.toString();
    641             if (str.length() > maxLength) maxLength = str.length();
    642           }
    643         }
    644       }
    645       pType = new StringParameterType(maxLength, null, false, multiplicity, 0, 0);
    646     }
    647     return pType;
    648   }
    649  
    650   /**
    651     Sets parameters for a PluginConfiguration
    652       @param parameters A list of the parameters that shall be set to the configuration
    653       @param plc The @link{PluginConfiguration} that the parameters shall be added to.
    654       @throws ClassNotFoundException If there is an error while trying to find the type of the parameters.
    655   */
    656   private void setPluginConf(List parameters, PluginConfiguration plc)
    657     throws ClassNotFoundException
    658   {
    659     // Set each configuration parameter for the plugin.
    660     for (Object elementObj : parameters)
    661     {
    662       Element parameter = (Element)elementObj;
    663       String name = parameter.getChildText("name");               
    664       String cl = parameter.getChildText("class");
    665       String label = parameter.getChildText("label");
    666       if (label == null || label.length() == 0) label = name;   
    667       String description = parameter.getChildText("description");
    668       Class clazz = null;
    669       List<?> values = null;
    670      
    671       if (cl.length() > 0)
    672       {
    673         clazz = Class.forName(cl);
    674         values = getValueList(parameter, clazz);
    675         ParameterType pType = getParameterType(clazz, values);
    676         plc.setParameterValues(name,label, description, pType, values);
    677       }
    678     }
    679549  }
    680550 
     
    689559    if (configurationMap != null)
    690560    {
    691       Boolean value = configurationMap.get(orderNumber);
    692       importIt = value != null ? value : false;
     561      importIt = Boolean.TRUE.equals(configurationMap.get(orderNumber));
    693562    }
    694563    else if (job != null)
    695564    {
    696       importIt = job.getValue("configuration"+orderNumber) != null ?
    697           Boolean.TRUE.equals(job.getValue("configuration" + orderNumber)) : false;
     565      importIt = Boolean.TRUE.equals(job.getValue("configuration" + orderNumber));
    698566    }
    699567   
Note: See TracChangeset for help on using the changeset viewer.