src/core/net/sf/basedb/util/extensions/manager/processor/PluginInstallationProcessor.java

Code
Comments
Other
Rev Date Author Line
5615 19 Apr 11 nicklas 1 /**
5615 19 Apr 11 nicklas 2   $Id$
5615 19 Apr 11 nicklas 3
5615 19 Apr 11 nicklas 4   Copyright (C) 2011 Nicklas Nordborg
5615 19 Apr 11 nicklas 5
5615 19 Apr 11 nicklas 6   This file is part of BASE - BioArray Software Environment.
5615 19 Apr 11 nicklas 7   Available at http://base.thep.lu.se/
5615 19 Apr 11 nicklas 8
5615 19 Apr 11 nicklas 9   BASE is free software; you can redistribute it and/or
5615 19 Apr 11 nicklas 10   modify it under the terms of the GNU General Public License
5615 19 Apr 11 nicklas 11   as published by the Free Software Foundation; either version 3
5615 19 Apr 11 nicklas 12   of the License, or (at your option) any later version.
5615 19 Apr 11 nicklas 13
5615 19 Apr 11 nicklas 14   BASE is distributed in the hope that it will be useful,
5615 19 Apr 11 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
5615 19 Apr 11 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5615 19 Apr 11 nicklas 17   GNU General Public License for more details.
5615 19 Apr 11 nicklas 18
5615 19 Apr 11 nicklas 19   You should have received a copy of the GNU General Public License
5615 19 Apr 11 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
5615 19 Apr 11 nicklas 21 */
5615 19 Apr 11 nicklas 22 package net.sf.basedb.util.extensions.manager.processor;
5615 19 Apr 11 nicklas 23
5615 19 Apr 11 nicklas 24 import java.io.InputStream;
5617 28 Apr 11 nicklas 25 import java.util.HashMap;
5615 19 Apr 11 nicklas 26 import java.util.List;
5617 28 Apr 11 nicklas 27 import java.util.Map;
5615 19 Apr 11 nicklas 28
6473 11 Jun 14 nicklas 29 import org.jdom2.Element;
5617 28 Apr 11 nicklas 30
5615 19 Apr 11 nicklas 31 import net.sf.basedb.core.DbControl;
6576 22 Oct 14 nicklas 32 import net.sf.basedb.core.Include;
5615 19 Apr 11 nicklas 33 import net.sf.basedb.core.ItemKey;
5617 28 Apr 11 nicklas 34 import net.sf.basedb.core.ItemQuery;
5615 19 Apr 11 nicklas 35 import net.sf.basedb.core.Permission;
5617 28 Apr 11 nicklas 36 import net.sf.basedb.core.PluginConfiguration;
5615 19 Apr 11 nicklas 37 import net.sf.basedb.core.PluginDefinition;
5617 28 Apr 11 nicklas 38 import net.sf.basedb.core.Type;
5617 28 Apr 11 nicklas 39 import net.sf.basedb.core.query.Expressions;
5617 28 Apr 11 nicklas 40 import net.sf.basedb.core.query.Hql;
5617 28 Apr 11 nicklas 41 import net.sf.basedb.core.query.Restrictions;
5615 19 Apr 11 nicklas 42 import net.sf.basedb.util.FileUtil;
5775 30 Sep 11 nicklas 43 import net.sf.basedb.util.Values;
5615 19 Apr 11 nicklas 44 import net.sf.basedb.util.extensions.manager.ExtensionsFile.WriteableExtensionsFile;
5615 19 Apr 11 nicklas 45 import net.sf.basedb.util.extensions.manager.ExtensionsFile;
5615 19 Apr 11 nicklas 46 import net.sf.basedb.util.extensions.manager.ExtensionsFileProcessor;
5615 19 Apr 11 nicklas 47 import net.sf.basedb.util.extensions.manager.ExtensionsManager;
5615 19 Apr 11 nicklas 48 import net.sf.basedb.util.extensions.manager.PluginInfoKey;
5615 19 Apr 11 nicklas 49 import net.sf.basedb.util.extensions.manager.ProcessResults;
5617 28 Apr 11 nicklas 50 import net.sf.basedb.util.extensions.xml.PluginDefinitionFilter;
5615 19 Apr 11 nicklas 51 import net.sf.basedb.util.extensions.xml.PluginInfo;
5615 19 Apr 11 nicklas 52 import net.sf.basedb.util.extensions.xml.XmlLoader;
5615 19 Apr 11 nicklas 53
5615 19 Apr 11 nicklas 54 /**
5615 19 Apr 11 nicklas 55   File processor implementation that load and install
5615 19 Apr 11 nicklas 56   plug-ins.
5615 19 Apr 11 nicklas 57   
5615 19 Apr 11 nicklas 58   @author Nicklas
5615 19 Apr 11 nicklas 59   @since 3.0
5615 19 Apr 11 nicklas 60   @base.modified $Date$
5615 19 Apr 11 nicklas 61 */
5615 19 Apr 11 nicklas 62 public class PluginInstallationProcessor
5615 19 Apr 11 nicklas 63   implements ExtensionsFileProcessor
5615 19 Apr 11 nicklas 64 {
5615 19 Apr 11 nicklas 65
6444 09 Apr 14 nicklas 66   private static final org.slf4j.Logger log = 
6444 09 Apr 14 nicklas 67     org.slf4j.LoggerFactory.getLogger(PluginInstallationProcessor.class);
5615 19 Apr 11 nicklas 68
5615 19 Apr 11 nicklas 69   private final DbControl dc;
5615 19 Apr 11 nicklas 70   private final XmlLoader loader;
5615 19 Apr 11 nicklas 71   private final ProcessResults results;
5615 19 Apr 11 nicklas 72   private int numFiles;
5615 19 Apr 11 nicklas 73   private int numError;
5615 19 Apr 11 nicklas 74   private int numTotalPlugins;
5615 19 Apr 11 nicklas 75
5615 19 Apr 11 nicklas 76   /**
5615 19 Apr 11 nicklas 77     Create a new processor. 
5615 19 Apr 11 nicklas 78     
5615 19 Apr 11 nicklas 79     @param dc Optional DbControl to use when installing plug-ins
5615 19 Apr 11 nicklas 80       (if not given, only the information from the XML files are loaded,
5615 19 Apr 11 nicklas 81       the plug-ins are not installed)
5615 19 Apr 11 nicklas 82   */
5617 28 Apr 11 nicklas 83   public PluginInstallationProcessor(DbControl dc, ProcessResults results)
5615 19 Apr 11 nicklas 84   {
5615 19 Apr 11 nicklas 85     this.dc = dc;
5617 28 Apr 11 nicklas 86     this.loader = new XmlLoader();
5615 19 Apr 11 nicklas 87     this.results = results;
5615 19 Apr 11 nicklas 88   }
5615 19 Apr 11 nicklas 89
5615 19 Apr 11 nicklas 90   /*
5615 19 Apr 11 nicklas 91     From the ExtensionsFileProcessor interface
5615 19 Apr 11 nicklas 92     ------------------------------------------
5615 19 Apr 11 nicklas 93   */
5615 19 Apr 11 nicklas 94   @Override
5615 19 Apr 11 nicklas 95   public void begin(ExtensionsManager manager, int numFiles) 
5615 19 Apr 11 nicklas 96   {
5615 19 Apr 11 nicklas 97     this.numFiles = 0;
5615 19 Apr 11 nicklas 98     this.numError = 0;
5615 19 Apr 11 nicklas 99     this.numTotalPlugins = 0;
5617 28 Apr 11 nicklas 100     loader.setFilter(new PluginDefinitionFilter());
5615 19 Apr 11 nicklas 101   }
5615 19 Apr 11 nicklas 102
5615 19 Apr 11 nicklas 103   @Override
5615 19 Apr 11 nicklas 104   public void processFile(ExtensionsManager manager, WriteableExtensionsFile wFile) 
5615 19 Apr 11 nicklas 105   {
5615 19 Apr 11 nicklas 106     InputStream in = null;
5615 19 Apr 11 nicklas 107     ExtensionsFile xtFile = wFile.getExtensionsFile();
5615 19 Apr 11 nicklas 108     String jarFile = xtFile.isJar() ? xtFile.getName() : null;
5615 19 Apr 11 nicklas 109     try
5615 19 Apr 11 nicklas 110     {
5617 28 Apr 11 nicklas 111       log.info("Loading plug-in definitions from file: " + xtFile);
5615 19 Apr 11 nicklas 112       
5615 19 Apr 11 nicklas 113       in = xtFile.getXmlStream();
5615 19 Apr 11 nicklas 114       loader.loadXmlFile(in, xtFile.getName(), xtFile.getClassLoader(), true);
5617 28 Apr 11 nicklas 115       FileUtil.close(in);
5615 19 Apr 11 nicklas 116       List<PluginInfo> plugins = loader.getPluginDefinitions();
5615 19 Apr 11 nicklas 117       
5615 19 Apr 11 nicklas 118       int numInstalled = 0;
5615 19 Apr 11 nicklas 119       int numUpdated = 0;
5616 27 Apr 11 nicklas 120       int numProcessed = 0;
5617 28 Apr 11 nicklas 121       int numConfigurations = 0;
5617 28 Apr 11 nicklas 122       Map<String, PluginDefinition> allPlugins = new HashMap<String, PluginDefinition>();
5615 19 Apr 11 nicklas 123       if (plugins.size() > 0)
5615 19 Apr 11 nicklas 124       {
5615 19 Apr 11 nicklas 125         PluginDefinition.checkInstallation(dc, plugins);
5615 19 Apr 11 nicklas 126         
5615 19 Apr 11 nicklas 127         ItemKey shareToEveryone = null;
5615 19 Apr 11 nicklas 128         if (dc != null && dc.getSessionControl().hasSystemPermission(Permission.SHARE_TO_EVERYONE))
5615 19 Apr 11 nicklas 129         {
5615 19 Apr 11 nicklas 130           shareToEveryone = ItemKey.getShareToEveryOneKey(dc, Permission.USE);
5615 19 Apr 11 nicklas 131         }
5615 19 Apr 11 nicklas 132         
5615 19 Apr 11 nicklas 133         wFile.open();
5615 19 Apr 11 nicklas 134         
5615 19 Apr 11 nicklas 135         for (PluginInfo info : plugins)
5615 19 Apr 11 nicklas 136         {
5775 30 Sep 11 nicklas 137           boolean deprecated = Values.getBoolean(info.getProperty("deprecated"));
5615 19 Apr 11 nicklas 138           if (dc != null)
5615 19 Apr 11 nicklas 139           {
5615 19 Apr 11 nicklas 140             PluginDefinition plugin = PluginDefinition.installOrUpdate(dc, info, jarFile, shareToEveryone);
5734 13 Sep 11 nicklas 141             if (plugin != null)
5615 19 Apr 11 nicklas 142             {
5734 13 Sep 11 nicklas 143               allPlugins.put(info.getClassName(), plugin);
5734 13 Sep 11 nicklas 144               if (plugin.isInDatabase())
5734 13 Sep 11 nicklas 145               {
5734 13 Sep 11 nicklas 146                 numUpdated++;
5734 13 Sep 11 nicklas 147               }
5734 13 Sep 11 nicklas 148               else
5734 13 Sep 11 nicklas 149               {
5734 13 Sep 11 nicklas 150                 numInstalled++;
5734 13 Sep 11 nicklas 151               }
5615 19 Apr 11 nicklas 152             }
5615 19 Apr 11 nicklas 153           }
5616 27 Apr 11 nicklas 154           numProcessed++;
5775 30 Sep 11 nicklas 155           if (!deprecated || !info.isNotFound())
5775 30 Sep 11 nicklas 156           {
5775 30 Sep 11 nicklas 157             wFile.registerObject(new PluginInfoKey(info), info);
5775 30 Sep 11 nicklas 158           }
5615 19 Apr 11 nicklas 159         }
5615 19 Apr 11 nicklas 160       }
5615 19 Apr 11 nicklas 161       
5617 28 Apr 11 nicklas 162       // Load plug-in configurations
5617 28 Apr 11 nicklas 163       if (dc != null)
5617 28 Apr 11 nicklas 164       {
5617 28 Apr 11 nicklas 165         in = xtFile.getStream("META-INF/plugin-configurations.xml");
5617 28 Apr 11 nicklas 166         if (in != null)
5617 28 Apr 11 nicklas 167         {
6473 11 Jun 14 nicklas 168           List<Element> configurations = PluginConfiguration.loadXmlFile2(in, "META-INF/plugin-configurations.xml");
5617 28 Apr 11 nicklas 169           FileUtil.close(in);
5617 28 Apr 11 nicklas 170           for (Element configuration : configurations)
5617 28 Apr 11 nicklas 171           {
5617 28 Apr 11 nicklas 172             String name = configuration.getChildText("configname");
5617 28 Apr 11 nicklas 173             String description = configuration.getChildText("description");
5617 28 Apr 11 nicklas 174             String pluginClassName = configuration.getAttributeValue("pluginClassName");
5617 28 Apr 11 nicklas 175             
5617 28 Apr 11 nicklas 176             PluginDefinition plugin = allPlugins.get(pluginClassName);
6576 22 Oct 14 nicklas 177             if (plugin == null)
6576 22 Oct 14 nicklas 178             {
6576 22 Oct 14 nicklas 179               try
6576 22 Oct 14 nicklas 180               {
6576 22 Oct 14 nicklas 181                 plugin = PluginDefinition.getByClassName(dc, pluginClassName);
6576 22 Oct 14 nicklas 182               }
6576 22 Oct 14 nicklas 183               catch (RuntimeException ex)
6576 22 Oct 14 nicklas 184               {}
6576 22 Oct 14 nicklas 185             }
5617 28 Apr 11 nicklas 186             if (plugin != null)
5617 28 Apr 11 nicklas 187             {
5617 28 Apr 11 nicklas 188               PluginConfiguration config = null;
5617 28 Apr 11 nicklas 189               if (plugin.isInDatabase())
5617 28 Apr 11 nicklas 190               {
5617 28 Apr 11 nicklas 191                 ItemQuery<PluginConfiguration> query = plugin.getPluginConfigurations();
6576 22 Oct 14 nicklas 192                 query.setIncludes(Include.ALL);
5617 28 Apr 11 nicklas 193                 query.restrict(Restrictions.eq(Hql.property("name"), Expressions.parameter("name", name, Type.STRING)));
5617 28 Apr 11 nicklas 194                 List<PluginConfiguration> existing = query.list(dc);
5617 28 Apr 11 nicklas 195                 if (existing.size() == 0)
5617 28 Apr 11 nicklas 196                 {
5617 28 Apr 11 nicklas 197                   config = plugin.newPluginConfiguration();
5617 28 Apr 11 nicklas 198                 }
5617 28 Apr 11 nicklas 199               }
5617 28 Apr 11 nicklas 200               else
5617 28 Apr 11 nicklas 201               {
5617 28 Apr 11 nicklas 202                 config = plugin.newPluginConfiguration();
5617 28 Apr 11 nicklas 203               }
5617 28 Apr 11 nicklas 204               if (config != null && !config.isInDatabase())
5617 28 Apr 11 nicklas 205               {
5617 28 Apr 11 nicklas 206                 dc.saveItem(config);
5617 28 Apr 11 nicklas 207                 config.setItemKey(plugin.getItemKey());
5617 28 Apr 11 nicklas 208                 config.setProjectKey(plugin.getProjectKey());
5617 28 Apr 11 nicklas 209                 config.setName(name);
5617 28 Apr 11 nicklas 210                 config.setDescription(description);
6870 16 Apr 15 nicklas 211                 config.setParameterValues2(configuration.getChildren("parameter"));
5617 28 Apr 11 nicklas 212                 numConfigurations++;
5617 28 Apr 11 nicklas 213               }
5617 28 Apr 11 nicklas 214             }
6576 22 Oct 14 nicklas 215             else 
6576 22 Oct 14 nicklas 216             {
6576 22 Oct 14 nicklas 217               numError++;
6576 22 Oct 14 nicklas 218               wFile.setError(true);
6576 22 Oct 14 nicklas 219               String msg = "Failed to install plug-in configuration '" + name + "', no plug-in: " + pluginClassName;
6576 22 Oct 14 nicklas 220               if (results != null) results.addErrorMessage(xtFile, msg);
6576 22 Oct 14 nicklas 221               log.error(msg);
6576 22 Oct 14 nicklas 222             }
5617 28 Apr 11 nicklas 223           }
5617 28 Apr 11 nicklas 224         }
5617 28 Apr 11 nicklas 225       }
5617 28 Apr 11 nicklas 226       
5615 19 Apr 11 nicklas 227       numFiles++;
5615 19 Apr 11 nicklas 228       numTotalPlugins += numInstalled + numUpdated;
5615 19 Apr 11 nicklas 229       if (results != null)
5615 19 Apr 11 nicklas 230       {
5616 27 Apr 11 nicklas 231         if (numInstalled > 0) results.addMessage(xtFile, numInstalled + " plug-in(s) installed.");
5616 27 Apr 11 nicklas 232         if (numUpdated > 0) results.addMessage(xtFile, numUpdated + " plug-in(s) updated.");
6576 22 Oct 14 nicklas 233         if (numConfigurations > 0) results.addMessage(xtFile, numConfigurations + " plug-in configuration(s) created.");
5616 27 Apr 11 nicklas 234         if (numInstalled == 0 && numUpdated == 0 && numProcessed > 0)
5616 27 Apr 11 nicklas 235         {
6576 22 Oct 14 nicklas 236           results.addMessage(xtFile, numProcessed + " plug-in(s) loaded.");
5616 27 Apr 11 nicklas 237         }
5615 19 Apr 11 nicklas 238       }
5616 27 Apr 11 nicklas 239       log.info("Installed/updated/processed " + numInstalled + "/" + numUpdated + "/" + numProcessed + " plug-ins from file: " + xtFile);
5615 19 Apr 11 nicklas 240     }
6379 13 Dec 13 nicklas 241     catch (Throwable ex)
5615 19 Apr 11 nicklas 242     {
5615 19 Apr 11 nicklas 243       wFile.setError(true);
5615 19 Apr 11 nicklas 244       numError++;
5615 19 Apr 11 nicklas 245       if (results != null)
5615 19 Apr 11 nicklas 246       {
6379 13 Dec 13 nicklas 247         results.addErrorMessage(xtFile, "Failed to install plug-in(s): " + ex.getClass().getName() + "; " + ex.getMessage());
5615 19 Apr 11 nicklas 248       }
5615 19 Apr 11 nicklas 249       log.error("Failed to install plug-ins from file: " + xtFile, ex);
5615 19 Apr 11 nicklas 250     }
5615 19 Apr 11 nicklas 251     finally
5615 19 Apr 11 nicklas 252     {
5615 19 Apr 11 nicklas 253       wFile.close();
5615 19 Apr 11 nicklas 254       FileUtil.close(in);
5615 19 Apr 11 nicklas 255     }
5615 19 Apr 11 nicklas 256
5615 19 Apr 11 nicklas 257   }
5615 19 Apr 11 nicklas 258
5615 19 Apr 11 nicklas 259   @Override
5615 19 Apr 11 nicklas 260   public void done(ExtensionsManager manager) 
5615 19 Apr 11 nicklas 261   {}
5615 19 Apr 11 nicklas 262
5615 19 Apr 11 nicklas 263   @Override
5615 19 Apr 11 nicklas 264   public void done(ExtensionsManager manager, Throwable t)
5615 19 Apr 11 nicklas 265   {}
5615 19 Apr 11 nicklas 266   // ------------------------------------------
5615 19 Apr 11 nicklas 267   
5615 19 Apr 11 nicklas 268   /**
5615 19 Apr 11 nicklas 269     Get the loader the processor is using for parsing xml files with
5615 19 Apr 11 nicklas 270     plug-in definitions.
5615 19 Apr 11 nicklas 271   */
5615 19 Apr 11 nicklas 272   public XmlLoader getXmlLoader()
5615 19 Apr 11 nicklas 273   {
5615 19 Apr 11 nicklas 274     return loader;
5615 19 Apr 11 nicklas 275   }
5615 19 Apr 11 nicklas 276   
5615 19 Apr 11 nicklas 277   public ProcessResults getProcessResults()
5615 19 Apr 11 nicklas 278   {
5615 19 Apr 11 nicklas 279     return results;
5615 19 Apr 11 nicklas 280   }
5615 19 Apr 11 nicklas 281
5615 19 Apr 11 nicklas 282   /**
5615 19 Apr 11 nicklas 283     Get the number of files that was successfully processed.
5615 19 Apr 11 nicklas 284   */
5615 19 Apr 11 nicklas 285   public int getNumFiles()
5615 19 Apr 11 nicklas 286   {
5615 19 Apr 11 nicklas 287     return numFiles;
5615 19 Apr 11 nicklas 288   }
5615 19 Apr 11 nicklas 289   
5615 19 Apr 11 nicklas 290   /**
5615 19 Apr 11 nicklas 291     Get the number of files that was had an error.
5615 19 Apr 11 nicklas 292   */
5615 19 Apr 11 nicklas 293   public int getNumError()
5615 19 Apr 11 nicklas 294   {
5615 19 Apr 11 nicklas 295     return numError;
5615 19 Apr 11 nicklas 296   }
5615 19 Apr 11 nicklas 297
5615 19 Apr 11 nicklas 298   /**
5615 19 Apr 11 nicklas 299     Get the total number of plug-in installed or updated
5615 19 Apr 11 nicklas 300     by the processor.
5615 19 Apr 11 nicklas 301   */
5615 19 Apr 11 nicklas 302   public int getNumPlugins()
5615 19 Apr 11 nicklas 303   {
5615 19 Apr 11 nicklas 304     return numTotalPlugins;
5615 19 Apr 11 nicklas 305   }
5615 19 Apr 11 nicklas 306   
5615 19 Apr 11 nicklas 307 }