package org.javasimon.console;

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.javasimon.Manager;
import org.javasimon.utils.SimonUtils;

/**
 * Front controller servlet of Simon Web console.
 *
 * @author gquintana
 */
@SuppressWarnings("UnusedDeclaration")
public class SimonConsoleServlet extends HttpServlet {
	/**
	 * Serial version UID since class is Serializable.
	 */
	public static final long serialVersionUID = 1L;

	/**
	 * URL Prefix init parameter name.
	 */
	public static final String URL_PREFIX_INIT_PARAMETER = "url-prefix";
	/**
	 * Plugin classes init parameter name.
	 */
	public static final String PLUGIN_CLASSES_INIT_PARAMETER = "plugin-classes";
	private SimonConsoleRequestProcessor requestProcessor;

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);

		pickUpSharedManagerIfExists(config);

		String urlPrefix = config.getInitParameter(URL_PREFIX_INIT_PARAMETER);
		initRequestProcessor(urlPrefix);

		String pluginClasses = config.getInitParameter(PLUGIN_CLASSES_INIT_PARAMETER);
		if (pluginClasses != null) {
			requestProcessor.getPluginManager().addPlugins(pluginClasses);
		}
	}

	public void initRequestProcessor(String urlPrefix) {
		if (urlPrefix == null) {
			urlPrefix = "";
		} else {
			urlPrefix = urlPrefix.trim();
		}
		requestProcessor = new SimonConsoleRequestProcessor(urlPrefix);
		requestProcessor.initActionBindings();
	}

	public SimonConsoleRequestProcessor getRequestProcessor() {
		return requestProcessor;
	}

	private void pickUpSharedManagerIfExists(ServletConfig config) {
		Object managerObject = config.getServletContext().getAttribute(SimonUtils.MANAGER_SERVLET_CTX_ATTRIBUTE);
		if (managerObject != null && managerObject instanceof Manager) {
			requestProcessor.setManager((Manager) managerObject);
		}
	}

	/**
	 * Handles the HTTP
	 * <code>GET</code> method.
	 *
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException
	{
		requestProcessor.processRequest(request, response);
	}

	/**
	 * Handles the HTTP
	 * <code>POST</code> method.
	 *
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException
	{
		requestProcessor.processRequest(request, response);
	}

	/**
	 * Returns a short description of the servlet.
	 *
	 * @return a String containing servlet description
	 */
	@Override
	public String getServletInfo() {
		return "Short description";
	}
}

@@ -1178,46 +1178,8 @@
 );%0D%0A
-%09%09initRequestProcessor(urlPrefix);%0D%0A%0D%0A
 %09%09St
@@ -1263,87 +1263,39 @@
 %0A%09%09i
-f (pluginClasses != null) %7B%0D%0A%09%09%09requestProcessor.getPluginManager().addPlugins(
+nitRequestProcessor(urlPrefix, 
 plug
@@ -1303,29 +1303,24 @@
 nClasses);%0D%0A
-%09%09%7D%0D%0A
 %09%7D%0D%0A%0D%0A%09publi
@@ -1363,16 +1363,38 @@
 rlPrefix
+, String pluginClasses
 ) %7B%0D%0A%09%09i
@@ -1556,16 +1556,120 @@
 efix);%0D%0A
+%09%09if (pluginClasses != null) %7B%0D%0A%09%09%09requestProcessor.getPluginManager().addPlugins(pluginClasses);%0D%0A%09%09%7D%0D%0A
 %09%09reques

Fix Issue #5 Console plugins are not loaded by Servlet init