// ========================================================================
// Copyright 2010 NEXCOM Systems
// ------------------------------------------------------------------------
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at 
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========================================================================
package org.cipango.console.printer;

import java.io.Writer;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;

public class ServletMappingPrinter implements HtmlPrinter
{

	private ObjectName[] _appContexts;
	private MBeanServerConnection _connection;

	public ServletMappingPrinter(ObjectName[] appContexts,
			MBeanServerConnection connection)
	{
		_appContexts = appContexts;
		_connection = connection;
	}

	public void print(Writer out) throws Exception
	{
		out.write("<h2>Servlets</h2>");
		for (int i = 0; i < _appContexts.length; i++)
		{
			String contextPath = (String) _connection.getAttribute(
					_appContexts[i], "name");
			ObjectName servletHandler = (ObjectName) _connection.getAttribute(_appContexts[i], "servletHandler");
			printAppContext(contextPath, servletHandler, out);
		}
	}

	private void printAppContext(String name, ObjectName servletHandler,
			Writer out) throws Exception
	{
		out.write("<h3>" + name + "</h3>");
		ObjectName[] sipServletMappings = (ObjectName[]) _connection
				.getAttribute(servletHandler, "sipServletMappings");

		if (sipServletMappings != null && sipServletMappings.length != 0)
		{
			out.write("<div class=\"data\"><table class=\"table_hover\">");
			out.write("<tr><th>SIP Servlet name</th><th>Mapping</th></tr>");
			for (int i = 0; i < sipServletMappings.length; i++)
			{
				String matchingRuleExpression = (String) _connection
						.getAttribute(sipServletMappings[i],
								"matchingRuleExpression");
				String servletName = (String) _connection
						.getAttribute(sipServletMappings[i], "servletName");
				out.write("<tr class=\"" + (i % 2 == 0 ? "even" : "odd")
						+ "\">");
				out.write("<td>" + servletName + "</td><td>"
						+ matchingRuleExpression + "</td>");
				out.write("</tr>");
			}
			out.write("</table></div>");
		}
		else
		{
			ObjectName mainServlet = (ObjectName) _connection.getAttribute(servletHandler, "mainServlet");
			if (mainServlet != null)
			{
				ObjectName[] sipServlets = 
					(ObjectName[]) _connection.getAttribute(servletHandler, "sipServlets");
				out.write("<div class=\"data\"><table class=\"table_hover\">\n");
				out.write("<tr><th>SIP Servlet name</th><th>Main servlet</th></tr>");
				for (int i = 0; i < sipServlets.length; i++)
				{
					String servletName = (String) _connection
							.getAttribute(sipServlets[i], "name");
					out.write("<tr class=\"" + (i % 2 == 0 ? "even" : "odd")
							+ "\">");
					out.write("<td>" + servletName + "</td><td>"
							+ sipServlets[i].equals(mainServlet) + "</td>");
					out.write("</tr>");
				}
				out.write("</table></div>");
			}
			else
				out.write("No SIP servlets for this application");
		}
	}

}

@@ -871,32 +871,85 @@
 a.io.Writer;%0D%0A%0D%0A
+import javax.management.AttributeNotFoundException;%0D%0A
 import javax.man
@@ -976,24 +976,24 @@
 onnection;%0D%0A
-
 import javax
@@ -1474,16 +1474,21 @@
 )%0D%0A%09%09%7B%0D%0A
+%09%09%09%0D%0A
 %09%09%09Strin
@@ -1489,16 +1489,49 @@
 %09String 
+contextPath;%0D%0A%09%09%09try %0D%0A%09%09%09%7B%0D%0A%09%09%09%09
 contextP
@@ -1570,23 +1570,16 @@
 tribute(
-%0D%0A%09%09%09%09%09
 _appCont
@@ -1597,19 +1597,22 @@
 ame%22);%0D%0A
+%0D%0A
 %09%09%09
+%09
 ObjectNa
@@ -1709,16 +1709,17 @@
 %22);%0D%0A%09%09%09
+%09
 printApp
@@ -1758,24 +1758,271 @@
 ler, out);%0D%0A
+%09%09%09%7D%0D%0A%09%09%09catch (AttributeNotFoundException e) %7B%0D%0A%09%09%09%09contextPath = (String) _connection.getAttribute(_appContexts%5Bi%5D, %22contextPath%22);%0D%0A%09%09%09%09out.write(%22%3Ch3%3E%22 + contextPath + %22%3C/h3%3E%5Cn%22);%0D%0A%09%09%09%09out.write(%22No SIP servlets for this application%22);%0D%0A%09%09%09%7D%0D%0A
 %09%09%7D%0D%0A%09%7D%0D%0A%0D%0A%09
@@ -2120,32 +2120,32 @@
  Exception%0D%0A%09%7B%0D%0A
-
 %09%09out.write(%22%3Ch3
@@ -2162,16 +2162,18 @@
 + %22%3C/h3%3E
+%5Cn
 %22);%0D%0A%09%09O

Fix CIPANGO-105: Cipango-console: "Applications" Tab