001package org.javasimon.console.action; 002 003import org.javasimon.console.ActionContext; 004import org.javasimon.console.ActionException; 005import org.javasimon.console.SimonConsolePlugin; 006import org.javasimon.console.json.ArrayJS; 007 008import java.io.IOException; 009import java.util.Collections; 010import java.util.List; 011 012import javax.servlet.ServletException; 013 014/** 015 * Action to get plugin list in JSON format. 016 * 017 * @author gquintana 018 */ 019public class PluginsJsonAction extends AbstractJsonAction { 020 021 public static final String PATH = "/data/plugins.json"; 022 023 /** Expected plugin type. */ 024 private Class<? extends SimonConsolePlugin> type; 025 private boolean invalidType = false; 026 027 public PluginsJsonAction(ActionContext context) { 028 super(context); 029 } 030 031 @Override 032 public void readParameters() { 033 super.readParameters(); 034 String sType = getContext().getParameterAsString("type", null); 035 if (sType != null) { 036 try { 037 Class<?> oType = Class.forName(sType); 038 if (SimonConsolePlugin.class.isAssignableFrom(oType)) { 039 type = (Class<? extends SimonConsolePlugin>) oType; 040 } else { 041 invalidType = true; 042 } 043 } catch (Exception e) { 044 invalidType = true; 045 } 046 } 047 } 048 049 @Override 050 public void execute() throws ServletException, IOException, ActionException { 051 super.execute(); 052 // Get plugin list 053 List<? extends SimonConsolePlugin> plugins; 054 if (invalidType) { 055 plugins = Collections.emptyList(); 056 } else if (type == null) { 057 plugins = getContext().getPluginManager().getPlugins(); 058 } else { 059 plugins = getContext().getPluginManager().getPluginsByType(type); 060 } 061 // Convert list to JSON 062 ArrayJS pluginsJS = new ArrayJS(); 063 for (SimonConsolePlugin plugin : plugins) { 064 pluginsJS.addElement(plugin.toJson(jsonStringifierFactory)); 065 } 066 pluginsJS.write(getContext().getWriter()); 067 } 068}