server
Clone or download
Modified Files
/*
/*
Stream-Pi - Free & Open-Source Modular Cross-Platform Programmable Macropad
Stream-Pi - Free & Open-Source Modular Cross-Platform Programmable Macropad
Copyright (C) 2019-2021 Debayan Sutradhar (rnayabed), Samuel Quiñones (SamuelQuinones)
Copyright (C) 2019-2021 Debayan Sutradhar (rnayabed), Samuel Quiñones (SamuelQuinones)
This program is free software: you can redistribute it and/or modify
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
(at your option) any later version.
This program is distributed in the hope that it will be useful,
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
GNU General Public License for more details.
Written by : Debayan Sutradhar (rnayabed)
Written by : Debayan Sutradhar (rnayabed)
*/
*/
package com.stream_pi.server.action;
package com.stream_pi.server.action;
import com.stream_pi.action_api.action.Action;
import com.stream_pi.action_api.action.Action;
import com.stream_pi.action_api.action.ActionType;
import com.stream_pi.action_api.action.ActionType;
import com.stream_pi.action_api.action.PropertySaver;
import com.stream_pi.action_api.action.PropertySaver;
import com.stream_pi.action_api.action.ServerConnection;
import com.stream_pi.action_api.action.ServerConnection;
import com.stream_pi.action_api.actionproperty.ServerProperties;
import com.stream_pi.action_api.actionproperty.ServerProperties;
import com.stream_pi.action_api.actionproperty.property.Property;
import com.stream_pi.action_api.actionproperty.property.Property;
import com.stream_pi.action_api.actionproperty.property.Type;
import com.stream_pi.action_api.actionproperty.property.Type;
import com.stream_pi.action_api.externalplugin.ExternalPlugin;
import com.stream_pi.action_api.externalplugin.ExternalPlugin;
import com.stream_pi.action_api.externalplugin.ToggleAction;
import com.stream_pi.action_api.externalplugin.ToggleAction;
import com.stream_pi.action_api.externalplugin.ToggleExtras;
import com.stream_pi.action_api.externalplugin.ToggleExtras;
import com.stream_pi.util.exception.MinorException;
import com.stream_pi.util.exception.MinorException;
import com.stream_pi.util.exception.SevereException;
import com.stream_pi.util.exception.SevereException;
import com.stream_pi.util.exception.StreamPiException;
import com.stream_pi.util.exception.StreamPiException;
import com.stream_pi.util.version.Version;
import com.stream_pi.util.version.Version;
import com.stream_pi.util.xmlconfighelper.XMLConfigHelper;
import com.stream_pi.util.xmlconfighelper.XMLConfigHelper;
import org.w3c.dom.Document;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.File;
import java.lang.module.Configuration;
import java.lang.module.Configuration;
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleFinder;
import java.lang.module.ModuleFinder;
import java.lang.module.ModuleReference;
import java.lang.module.ModuleReference;
import java.nio.file.Path;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashMap;
import java.util.List;
import java.util.List;
import java.util.ServiceLoader;
import java.util.ServiceLoader;
import java.util.logging.Level;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Collectors;
public class ExternalPlugins
public class ExternalPlugins
{
{
private static ExternalPlugins instance = null;
private static ExternalPlugins instance = null;
private final Logger logger;
private final Logger logger;
private File configFile;
private File configFile;
private Document document;
private Document document;
private static String pluginsLocation = null;
private static String pluginsLocation = null;
/**
/**
* Singleton class instance getter. Creates new one, when asked for the first time
* Singleton class instance getter. Creates new one, when asked for the first time
*
*
* @return returns instance of NormalActionPlugins (one and only, always)
* @return returns instance of NormalActionPlugins (one and only, always)
*/
*/
public static synchronized ExternalPlugins getInstance()
public static synchronized ExternalPlugins getInstance()
{
{
if(instance == null)
if(instance == null)
{
{
instance = new ExternalPlugins();
instance = new ExternalPlugins();
}
}
return instance;
return instance;
}
}
/**
/**
* Sets the folder location where the plugin JARs and their dependencies are stored
* Sets the folder location where the plugin JARs and their dependencies are stored
*
*
* @param location Folder location
* @param location Folder location
*/
*/
public static void setPluginsLocation(String location)
public static void setPluginsLocation(String location)
{
{
pluginsLocation = location;
pluginsLocation = location;
}
}
/**
/**
* Private constructor
* Private constructor
*/
*/
private ExternalPlugins()
private ExternalPlugins()
{
{
logger = Logger.getLogger(ExternalPlugins.class.getName());
logger = Logger.getLogger(ExternalPlugins.class.getName());
externalPluginsHashmap = new HashMap<>();
externalPluginsHashmap = new HashMap<>();
}
}
/**
/**
* init Method
* init Method
*/
*/
public void init() throws SevereException, MinorException
public void init() throws SevereException, MinorException
{
{
registerPlugins();
registerPlugins();
initPlugins();
initPlugins();
}
}
/**
/**
* Used to fetch list of all external Plugins
* Used to fetch list of all external Plugins
*
*
* @return List of plugins
* @return List of plugins
*/
*/
public List<ExternalPlugin> getPlugins()
public List<ExternalPlugin> getPlugins()
{
{
return externalPlugins;
return externalPlugins;
}
}
/**
/**
* Returns a plugin by its module name
* Returns a plugin by its module name
*
*
* @param name Module Name
* @param name Module Name
* @return The plugin. If not found, then null is returned
* @return The plugin. If not found, then null is returned
*/
*/
public ExternalPlugin getPluginByModuleName(String name)
public ExternalPlugin getPluginByModuleName(String name)
{
{
logger.info("Plugin being requested : "+name);
logger.info("Plugin being requested : "+name);
Integer index = externalPluginsHashmap.getOrDefault(name, -1);
Integer index = externalPluginsHashmap.getOrDefault(name, -1);
if(index != -1)
if(index != -1)
{
{
return externalPlugins.get(index);
return externalPlugins.get(index);
}
}
return null;
return null;
}
}
private List<ExternalPlugin> externalPlugins = null;
private List<ExternalPlugin> externalPlugins = null;
HashMap<String, Integer> externalPluginsHashmap;
HashMap<String, Integer> externalPluginsHashmap;
/**
/**
* Used to register plugins from plugin location
* Used to register plugins from plugin location
*/
*/
public void registerPlugins() throws SevereException, MinorException
public void registerPlugins() throws SevereException, MinorException
{
{
logger.info("Registering external plugins from "+pluginsLocation+" ...");
logger.info("Registering external plugins from "+pluginsLocation+" ...");
try
try
{
{
configFile = new File(pluginsLocation+"/config.xml");
configFile = new File(pluginsLocation+"/config.xml");
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
document = docBuilder.parse(configFile);
document = docBuilder.parse(configFile);
}
}
catch (Exception e)
catch (Exception e)
{
{
e.printStackTrace();
e.printStackTrace();
throw new SevereException("Plugins","Error reading plugins config.xml. Cannot continue.");
throw new SevereException("Plugins","Error reading plugins config.xml. Cannot continue.");
}
}
ArrayList<ExternalPlugin> errorModules = new ArrayList<>();
ArrayList<ExternalPlugin> errorModules = new ArrayList<>();
ArrayList<String> errorModuleError = new ArrayList<>();
ArrayList<String> errorModuleError = new ArrayList<>();
ArrayList<Action> pluginsConfigs = new ArrayList<>();
ArrayList<Action> pluginsConfigs = new ArrayList<>();
NodeList actionsNode = document.getElementsByTagName("actions").item(0).getChildNodes();
NodeList actionsNode = document.getElementsByTagName("actions").item(0).getChildNodes();
for(int i=0; i<actionsNode.getLength(); i++)
for(int i=0; i<actionsNode.getLength(); i++)
{
{
Node eachActionNode = actionsNode.item(i);
Node eachActionNode = actionsNode.item(i);
if(eachActionNode.getNodeType() != Node.ELEMENT_NODE ||
if(eachActionNode.getNodeType() != Node.ELEMENT_NODE ||
!eachActionNode.getNodeName().equals("action"))
!eachActionNode.getNodeName().equals("action"))
continue;
continue;
Element eachActionElement = (Element) eachActionNode;
Element eachActionElement = (Element) eachActionNode;
String name;
String name;
Version version;
Version version;
ActionType actionType;
ActionType actionType;
try
try
{
{
name = XMLConfigHelper.getStringProperty(eachActionElement, "module-name");
name = XMLConfigHelper.getStringProperty(eachActionElement, "module-name");
actionType = ActionType.valueOf(XMLConfigHelper.getStringProperty(eachActionElement, "type"));
actionType = ActionType.valueOf(XMLConfigHelper.getStringProperty(eachActionElement, "type"));
version = new Version(XMLConfigHelper.getStringProperty(eachActionElement, "version"));
version = new Version(XMLConfigHelper.getStringProperty(eachActionElement, "version"));
}
}
catch (Exception e)
catch (Exception e)
{
{
logger.log(Level.WARNING, "Skipping configuration because invalid ...");
logger.log(Level.WARNING, "Skipping configuration because invalid ...");
e.printStackTrace();
e.printStackTrace();
continue;
continue;
}
}
ServerProperties serverProperties = new ServerProperties();
ServerProperties serverProperties = new ServerProperties();
NodeList serverPropertiesNodeList = eachActionElement.getElementsByTagName("properties").item(0).getChildNodes();
NodeList serverPropertiesNodeList = eachActionElement.getElementsByTagName("properties").item(0).getChildNodes();
for(int j = 0;j<serverPropertiesNodeList.getLength();j++)
for(int j = 0;j<serverPropertiesNodeList.getLength();j++)
{
{
Node eachPropertyNode = serverPropertiesNodeList.item(j);
Node eachPropertyNode = serverPropertiesNodeList.item(j);
if(eachPropertyNode.getNodeType() != Node.ELEMENT_NODE)
if(eachPropertyNode.getNodeType() != Node.ELEMENT_NODE)
continue;
continue;
if(!eachPropertyNode.getNodeName().equals("property"))
if(!eachPropertyNode.getNodeName().equals("property"))
continue;
continue;
Element eachPropertyElement = (Element) eachPropertyNode;
Element eachPropertyElement = (Element) eachPropertyNode;
try
try
{
{
Property property = new Property(XMLConfigHelper.getStringProperty(eachPropertyElement, "name"), Type.STRING);
Property property = new Property(XMLConfigHelper.getStringProperty(eachPropertyElement, "name"), Type.STRING);
property.setRawValue(XMLConfigHelper.getStringProperty(eachPropertyElement, "value"));
property.setRawValue(XMLConfigHelper.getStringProperty(eachPropertyElement, "value"));
serverProperties.addProperty(property);
serverProperties.addProperty(property);
}
}
catch (Exception e)
catch (Exception e)
{
{
logger.log(Level.WARNING, "Skipping property because invalid ...");
logger.log(Level.WARNING, "Skipping property because invalid ...");
e.printStackTrace();
e.printStackTrace();
}
}
}
}
Action action = new Action(actionType);
Action action = new Action(actionType);
action.setModuleName(name);
action.setModuleName(name);
action.setVersion(version);
action.setVersion(version);
action.getServerProperties().set(serverProperties);
action.getServerProperties().set(serverProperties);
pluginsConfigs.add(action);
pluginsConfigs.add(action);
}
}
logger.info("Size : "+pluginsConfigs.size());
logger.info("Size : "+pluginsConfigs.size());
Path pluginsDir = Paths.get(pluginsLocation); // Directory with plugins JARs
Path pluginsDir = Paths.get(pluginsLocation);
try
try
{
{
// Search for plugins in the plugins directory
ModuleFinder pluginsFinder = ModuleFinder.of(pluginsDir);
ModuleFinder pluginsFinder = ModuleFinder.of(pluginsDir);
// Find all names of all found plugin modules
List<String> p = pluginsFinder
List<String> p = pluginsFinder
.findAll()
.findAll()
.stream()
.stream()
.map(ModuleReference::descriptor)
.map(ModuleReference::descriptor)
.map(ModuleDescriptor::name)
.map(ModuleDescriptor::name)
.collect(Collectors.toList());
.collect(Collectors.toList());
// Create configuration that will resolve plugin modules
// (verify that the graph of modules is correct)
Configuration pluginsConfiguration = ModuleLayer
Configuration pluginsConfiguration = ModuleLayer
.boot()
.boot()
.configuration()
.configuration()
.resolve(pluginsFinder, ModuleFinder.of(), p);
.resolve(pluginsFinder, ModuleFinder.of(), p);
// Create a module layer for plugins
ModuleLayer layer = ModuleLayer
ModuleLayer layer = ModuleLayer
.boot()
.boot()
.defineModulesWithOneLoader(pluginsConfiguration, ClassLoader.getSystemClassLoader());
.defineModulesWithOneLoader(pluginsConfiguration, ClassLoader.getSystemClassLoader());
logger.info("Loading plugins from jar ...");
logger.info("Loading plugins from jar ...");
// Now you can use the new module layer to find service implementations in it
externalPlugins = ServiceLoader
externalPlugins = ServiceLoader
.load(layer, ExternalPlugin.class).stream()
.load(layer, ExternalPlugin.class).stream()
.map(ServiceLoader.Provider::get)
.map(ServiceLoader.Provider::get)
.collect(Collectors.toList());
.collect(Collectors.toList());
logger.info("...Done!");
logger.info("...Done!");
}
}
catch (Exception e)
catch (Exception e)
{
{
e.printStackTrace();
e.printStackTrace();
throw new MinorException("Error", "Error loading modules\n"+e.getMessage()+"\nPlease fix the errors. Other plugins wont be loaded.");
throw new MinorException("Error", "Error loading modules\n"+e.getMessage()+"\nPlease fix the errors. Other plugins wont be loaded.");
}
}
sortedPlugins = new HashMap<>();
sortedPlugins = new HashMap<>();
for (ExternalPlugin eachPlugin : externalPlugins)
for (ExternalPlugin eachPlugin : externalPlugins)
{
{
try
try
{
{
eachPlugin.setPropertySaver(getPropertySaver());
eachPlugin.setPropertySaver(getPropertySaver());
eachPlugin.setServerConnection(serverConnection);
eachPlugin.setServerConnection(serverConnection);
if(eachPlugin instanceof ToggleAction)
if(eachPlugin instanceof ToggleAction)
((ToggleAction) eachPlugin).setToggleExtras(getToggleExtras());
((ToggleAction) eachPlugin).setToggleExtras(getToggleExtras());
eachPlugin.initProperties();
eachPlugin.initProperties();
logger.info("MODULE : "+eachPlugin.getModuleName());
logger.info("MODULE : "+eachPlugin.getModuleName());
Action foundAction = null;
Action foundAction = null;
for (Action action : pluginsConfigs)
for (Action action : pluginsConfigs)
{
{
if (action.getModuleName().equals(eachPlugin.getModuleName())
if (action.getModuleName().equals(eachPlugin.getModuleName())
&& action.getVersion().isEqual(eachPlugin.getVersion()))
&& action.getVersion().isEqual(eachPlugin.getVersion()))
{
{
foundAction = action;
foundAction = action;
List<Property> eachPluginStoredProperties = action.getServerProperties().get();
List<Property> eachPluginStoredProperties = action.getServerProperties().get();
List<Property> eachPluginCodeProperties = eachPlugin.getServerProperties().get();
List<Property> eachPluginCodeProperties = eachPlugin.getServerProperties().get();
for (int i =0;i< eachPluginCodeProperties.size(); i++)
for (int i =0;i< eachPluginCodeProperties.size(); i++)
{
{
Property eachPluginCodeProperty = eachPluginCodeProperties.get(i);
Property eachPluginCodeProperty = eachPluginCodeProperties.get(i);
Property foundProp = null;
Property foundProp = null;
for (Property eachPluginStoredProperty : eachPluginStoredProperties) {
for (Property eachPluginStoredProperty : eachPluginStoredProperties) {
if (eachPluginCodeProperty.getName().equals(eachPluginStoredProperty.getName())) {
if (eachPluginCodeProperty.getName().equals(eachPluginStoredProperty.getName())) {
eachPluginCodeProperty.setRawValue(eachPluginStoredProperty.getRawValue());
eachPluginCodeProperty.setRawValue(eachPluginStoredProperty.getRawValue());
foundProp = eachPluginStoredProperty;
foundProp = eachPluginStoredProperty;
}
}
}
}
eachPluginCodeProperties.set(i, eachPluginCodeProperty);
eachPluginCodeProperties.set(i, eachPluginCodeProperty);
if (foundProp != null) {
if (foundProp != null) {
eachPluginStoredProperties.remove(foundProp);
eachPluginStoredProperties.remove(foundProp);
}
}
}
}
eachPlugin.getServerProperties().set(eachPluginCodeProperties);
eachPlugin.getServerProperties().set(eachPluginCodeProperties);
break;
break;
}
}
}
}
if (foundAction != null)
if (foundAction != null)
pluginsConfigs.remove(foundAction);
pluginsConfigs.remove(foundAction);
else
else
{
{
List<Property> eachPluginStoredProperties = eachPlugin.getServerProperties().get();
List<Property> eachPluginStoredProperties = eachPlugin.getServerProperties().get();
for(Property property :eachPluginStoredProperties)
for(Property property :eachPluginStoredProperties)
{
{
if(property.getType() == Type.STRING || property.getType() == Type.INTEGER || property.getType() == Type.DOUBLE)
if(property.getType() == Type.STRING || property.getType() == Type.INTEGER || property.getType() == Type.DOUBLE)
property.setRawValue(property.getDefaultRawValue());
property.setRawValue(property.getDefaultRawValue());
}
}
}
}
if (!sortedPlugins.containsKey(eachPlugin.getCategory())) {
if (!sortedPlugins.containsKey(eachPlugin.getCategory())) {
sortedPlugins.put(eachPlugin.getCategory(), new ArrayList<>());
sortedPlugins.put(eachPlugin.getCategory(), new ArrayList<>());
}
}
sortedPlugins.get(eachPlugin.getCategory()).add(eachPlugin);
sortedPlugins.get(eachPlugin.getCategory()).add(eachPlugin);
}
}
catch (MinorException e)
catch (MinorException e)
{
{
e.printStackTrace();
e.printStackTrace();
errorModules.add(eachPlugin);
errorModules.add(eachPlugin);
errorModuleError.add(e.getShortMessage());
errorModuleError.add(e.getShortMessage());
}
}
}
}
try {
try {
saveServerSettings();
saveServerSettings();
} catch (MinorException e) {
} catch (MinorException e) {
e.printStackTrace();
e.printStackTrace();
}
}
logger.log(Level.INFO, "All plugins registered!");
logger.log(Level.INFO, "All plugins registered!");
if(errorModules.size() > 0)
if(errorModules.size() > 0)
{
{
StringBuilder errors = new StringBuilder("The following action modules could not be loaded:");
StringBuilder errors = new StringBuilder("The following action modules could not be loaded:");
for(int i = 0; i<errorModules.size(); i++)
for(int i = 0; i<errorModules.size(); i++)
{
{
externalPlugins.remove(errorModules.get(i));
externalPlugins.remove(errorModules.get(i));
errors.append("\n * ").append(errorModules.get(i).getModuleName()).append("\n(")
errors.append("\n * ").append(errorModules.get(i).getModuleName()).append("\n(")
.append(errorModuleError.get(i)).append(")");
.append(errorModuleError.get(i)).append(")");
}
}
throw new MinorException("Plugins", errors.toString());
throw new MinorException("Plugins", errors.toString());
}
}
for(int i=0; i<externalPlugins.size(); i++)
for(int i=0; i<externalPlugins.size(); i++)
{
{
externalPluginsHashmap.put(externalPlugins.get(i).getModuleName(), i);
externalPluginsHashmap.put(externalPlugins.get(i).getModuleName(), i);
}
}
}
}
/**
/**
* Used to init plugins
* Used to init plugins
*/
*/
public void initPlugins() throws MinorException
public void initPlugins() throws MinorException
{
{
StringBuilder errors = new StringBuilder("There were errors registering the following plugins. As a result, they have been omitted : ");
StringBuilder errors = new StringBuilder("There were errors registering the following plugins. As a result, they have been omitted : ");
boolean isError = false;
boolean isError = false;
for(ExternalPlugin eachPlugin : externalPlugins)
for(ExternalPlugin eachPlugin : externalPlugins)
{
{
try
try
{
{
eachPlugin.initAction();
eachPlugin.initAction();
}
}
catch (MinorException e)
catch (MinorException e)
{
{
e.printStackTrace();
e.printStackTrace();
isError = true;
isError = true;
errors.append("\n* ")
errors.append("\n* ")
.append(eachPlugin.getName())
.append(eachPlugin.getName())
.append(" - ")
.append(" - ")
.append(eachPlugin.getModuleName())
.append(eachPlugin.getModuleName())
.append("\n");
.append("\n");
errors.append(e.getShortMessage());
errors.append(e.getShortMessage());
errors.append("\n");
errors.append("\n");
}
}
}
}
if(isError)
if(isError)
{
{
throw new MinorException("Plugin init error", errors.toString());
throw new MinorException("Plugin init error", errors.toString());
}
}
}
}
HashMap<String, ArrayList<ExternalPlugin>> sortedPlugins;
HashMap<String, ArrayList<ExternalPlugin>> sortedPlugins;
/**
/**
* Gets list of sorted plugins
* Gets list of sorted plugins
*
*
* @return Hashmap with category key, and list of plugins of each category
* @return Hashmap with category key, and list of plugins of each category
*/
*/
public HashMap<String, ArrayList<ExternalPlugin>> getSortedPlugins()
public HashMap<String, ArrayList<ExternalPlugin>> getSortedPlugins()
{
{
return sortedPlugins;
return sortedPlugins;
}
}
/**
/**
* @return Gets actions element from the config.xml in plugins folder
* @return Gets actions element from the config.xml in plugins folder
*/
*/
private Element getActionsElement()
private Element getActionsElement()
{
{
return (Element) document.getElementsByTagName("actions").item(0);
return (Element) document.getElementsByTagName("actions").item(0);
}
}
/**
/**
* Saves ServerProperties of every plugin in config.xml in plugins folder
* Saves ServerProperties of every plugin in config.xml in plugins folder
*
*
* @throws MinorException Thrown when failed to save settings
* @throws MinorException Thrown when failed to save settings
*/
*/
public void saveServerSettings() throws MinorException
public void saveServerSettings() throws MinorException
{
{
XMLConfigHelper.removeChilds(getActionsElement());
XMLConfigHelper.removeChilds(getActionsElement());
for(ExternalPlugin externalPlugin : externalPlugins)
for(ExternalPlugin externalPlugin : externalPlugins)
{
{
Element actionElement = document.createElement("action");
Element actionElement = document.createElement("action");
getActionsElement().appendChild(actionElement);
getActionsElement().appendChild(actionElement);
Element moduleNameElement = document.createElement("module-name");
Element moduleNameElement = document.createElement("module-name");
moduleNameElement.setTextContent(externalPlugin.getModuleName());
moduleNameElement.setTextContent(externalPlugin.getModuleName());
actionElement.appendChild(moduleNameElement);
actionElement.appendChild(moduleNameElement);
Element versionElement = document.createElement("version");
Element versionElement = document.createElement("version");
versionElement.setTextContent(externalPlugin.getVersion().getText());
versionElement.setTextContent(externalPlugin.getVersion().getText());
actionElement.appendChild(versionElement);
actionElement.appendChild(versionElement);
Element actionTypeElement = document.createElement("type");
Element actionTypeElement = document.createElement("type");
actionTypeElement.setTextContent(externalPlugin.getActionType().toString());
actionTypeElement.setTextContent(externalPlugin.getActionType().toString());
actionElement.appendChild(actionTypeElement);
actionElement.appendChild(actionTypeElement);
Element propertiesElement = document.createElement("properties");
Element propertiesElement = document.createElement("properties");
actionElement.appendChild(propertiesElement);
actionElement.appendChild(propertiesElement);
for(String key : externalPlugin.getServerProperties().getNames())
for(String key : externalPlugin.getServerProperties().getNames())
{
{
for(Property eachProperty : externalPlugin.getServerProperties().getMultipleProperties(key))
for(Property eachProperty : externalPlugin.getServerProperties().getMultipleProperties(key))
{
{
Element propertyElement = document.createElement("property");
Element propertyElement = document.createElement("property");
propertiesElement.appendChild(propertyElement);
propertiesElement.appendChild(propertyElement);
Element nameElement = document.createElement("name");
Element nameElement = document.createElement("name");
nameElement.setTextContent(eachProperty.getName());
nameElement.setTextContent(eachProperty.getName());
propertyElement.appendChild(nameElement);
propertyElement.appendChild(nameElement);
Element valueElement = document.createElement("value");
Element valueElement = document.createElement("value");
valueElement.setTextContent(eachProperty.getRawValue());
valueElement.setTextContent(eachProperty.getRawValue());
propertyElement.appendChild(valueElement);
propertyElement.appendChild(valueElement);
}
}
}
}
}
}
save();
save();
}
}
private PropertySaver propertySaver = null;
private PropertySaver propertySaver = null;
/**
/**
* Set PropertySaver class
* Set PropertySaver class
* @param propertySaver instance of PropertySaver
* @param propertySaver instance of PropertySaver
*/
*/
public void setPropertySaver(PropertySaver propertySaver)
public void setPropertySaver(PropertySaver propertySaver)
{
{
this.propertySaver = propertySaver;
this.propertySaver = propertySaver;
}
}
public PropertySaver getPropertySaver()
public PropertySaver getPropertySaver()
{
{
return propertySaver;
return propertySaver;
}
}
private ToggleExtras toggleExtras = null;
private ToggleExtras toggleExtras = null;
/**
/**
* Set PropertySaver class
* Set PropertySaver class
* @param toggleExtras instance of PropertySaver
* @param toggleExtras instance of PropertySaver
*/
*/
public void setToggleExtras(ToggleExtras toggleExtras)
public void setToggleExtras(ToggleExtras toggleExtras)
{
{
this.toggleExtras = toggleExtras;
this.toggleExtras = toggleExtras;
}
}
public ToggleExtras getToggleExtras() {
public ToggleExtras getToggleExtras() {
return toggleExtras;
return toggleExtras;
}
}
private ServerConnection serverConnection = null;
private ServerConnection serverConnection = null;
/**
/**
* Set setServerConnection class
* Set setServerConnection class
* @param serverConnection instance of ServerConnection
* @param serverConnection instance of ServerConnection
*/
*/
public void setServerConnection(ServerConnection serverConnection)
public void setServerConnection(ServerConnection serverConnection)
{
{
this.serverConnection = serverConnection;
this.serverConnection = serverConnection;
}
}
/**
/**
* Get plugin from index from list
* Get plugin from index from list
*
*
* @param index of plugin
* @param index of plugin
* @return found plugin
* @return found plugin
*/
*/
public ExternalPlugin getActionFromIndex(int index)
public ExternalPlugin getActionFromIndex(int index)
{
{
return externalPlugins.get(index);
return externalPlugins.get(index);
}
}
/**
/**
* Calls onShutDown method in every plugin
* Calls onShutDown method in every plugin
*/
*/
public void shutDownActions()
public void shutDownActions()
{
{
if(externalPlugins != null)
if(externalPlugins != null)
{
{
for(ExternalPlugin eachPlugin : externalPlugins)
for(ExternalPlugin eachPlugin : externalPlugins)
{
{
try
try
{
{
eachPlugin.onShutDown();
eachPlugin.onShutDown();
}
}
catch (MinorException e)
catch (MinorException e)
{
{
e.printStackTrace();
e.printStackTrace();
}
}
}
}
externalPlugins.clear();
externalPlugins.clear();
}
}
}
}
/**
/**
* Saves all Server Properties of each Plugin in config.xml in Plugins folder
* Saves all Server Properties of each Plugin in config.xml in Plugins folder
* @throws MinorException thrown when failed to save
* @throws MinorException thrown when failed to save
*/
*/
public void save() throws MinorException
public void save() throws MinorException
{
{
try
try
{
{
Transformer transformer = TransformerFactory.newInstance().newTransformer();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
Result output = new StreamResult(configFile);
Result output = new StreamResult(configFile);
Source input = new DOMSource(document);
Source input = new DOMSource(document);
transformer.transform(input, output);
transformer.transform(input, output);
}
}
catch (Exception e)
catch (Exception e)
{
{
throw new MinorException("Config", "unable to save server plugins settings");
throw new MinorException("Config", "unable to save server plugins settings");
}
}
}
}
}
}