All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.javasimon.console.action.PluginsJsonAction Maven / Gradle / Ivy

There is a newer version: 4.2.0
Show newest version
package org.javasimon.console.action;

import org.javasimon.console.ActionContext;
import org.javasimon.console.ActionException;
import org.javasimon.console.SimonConsolePlugin;
import org.javasimon.console.json.ArrayJS;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import javax.servlet.ServletException;

/**
 * Action to get plugin list in JSON format.
 *
 * @author gquintana
 */
public class PluginsJsonAction extends AbstractJsonAction {

	public static final String PATH = "/data/plugins.json";

	/** Expected plugin type. */
	private Class type;
	private boolean invalidType = false;

	public PluginsJsonAction(ActionContext context) {
		super(context);
	}

	@Override
	public void readParameters() {
		super.readParameters();
		String sType = getContext().getParameterAsString("type", null);
		if (sType != null) {
			try {
				Class oType = Class.forName(sType);
				if (SimonConsolePlugin.class.isAssignableFrom(oType)) {
					type = (Class) oType;
				} else {
					invalidType = true;
				}
			} catch (Exception e) {
				invalidType = true;
			}
		}
	}

	@Override
	public void execute() throws ServletException, IOException, ActionException {
		super.execute();
		// Get plugin list
		List plugins;
		if (invalidType) {
			plugins = Collections.emptyList();
		} else if (type == null) {
			plugins = getContext().getPluginManager().getPlugins();
		} else {
			plugins = getContext().getPluginManager().getPluginsByType(type);
		}
		// Convert list to JSON
		ArrayJS pluginsJS = new ArrayJS();
		for (SimonConsolePlugin plugin : plugins) {
			pluginsJS.addElement(plugin.toJson(jsonStringifierFactory));
		}
		pluginsJS.write(getContext().getWriter());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy