// ========================================================================
// 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.plus.sipapp;

import java.util.EventListener;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
import javax.servlet.Filter;
import javax.servlet.Servlet;
import javax.servlet.ServletException;

import org.cipango.sipapp.SipAppContext;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler.Decorator;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.log.Log;

public class SipResourceDecorator implements Decorator
{

	public static final String JNDI_SIP_PREFIX = "sip/";
	public static final String JNDI_SIP_FACTORY = "SipFactory";
	public static final String JNDI_TIMER_SERVICE = "TimerService";
	public static final String JNDI_SIP_SESSIONS_UTIL = "SipSessionsUtil";
	public static final String JNDI_SIP_FACTORY_POSTFIX = "/" + JNDI_SIP_FACTORY;
	public static final String JNDI_TIMER_SERVICE_POSTFIX = "/" + JNDI_TIMER_SERVICE;
	public static final String JNDI_SIP_SESSIONS_UTIL_POSTFIX = "/" + JNDI_SIP_SESSIONS_UTIL;
	
	protected String _name;
	protected SipAppContext _context;
	
	public SipResourceDecorator(SipAppContext context)
	{
		_context = context;
	}
	
	public void bindSipResources() throws Exception
	{
		ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
		Thread.currentThread().setContextClassLoader(_context.getClassLoader());
		Context context = new InitialContext();
		Context compCtx;

		try
		{
			compCtx = (Context) context.lookup("java:comp/env");
		}
		catch (NameNotFoundException e)
		{
			compCtx = ((Context) context.lookup("java:comp")).createSubcontext("env");
		}

		Context sipCtx;
		try
		{
			sipCtx = (Context) compCtx.lookup("sip");
		}
		catch (NameNotFoundException e)
		{
			sipCtx = (Context) compCtx.createSubcontext("sip");
		}

		sipCtx.createSubcontext(_name);
		compCtx.bind(JNDI_SIP_PREFIX + _name + JNDI_SIP_FACTORY_POSTFIX, _context.getSipFactory());
		compCtx.bind(JNDI_SIP_PREFIX + _name + JNDI_TIMER_SERVICE_POSTFIX, _context.getTimerService());
		compCtx.bind(JNDI_SIP_PREFIX + _name + JNDI_SIP_SESSIONS_UTIL_POSTFIX, _context.getSipSessionsUtil());
		Log.debug("Bind SIP Resources on app " + _name);
		Thread.currentThread().setContextClassLoader(oldLoader);
	}
	
	protected void init()
	{
		try
		{
			String name = _context.getName();
			if (name == null || "".equals(name.trim()))
				name = _context.getDefaultName();
				
			if (_name == null || !_name.endsWith(name))
			{
				_name = name;
				bindSipResources();
			}
		}
		catch (Exception e) 
		{
			Log.warn("Failed to bind SIP resources", e);
		}
	}


	public <T extends Filter> T decorateFilterInstance(T filter) throws ServletException
	{
		return filter;
	}

	public <T extends Servlet> T decorateServletInstance(T servlet) throws ServletException
	{
		init();
		return servlet;
	}

	public <T extends EventListener> T decorateListenerInstance(T listener) throws ServletException
	{
		init();
		return listener;
	}

	public void decorateFilterHolder(FilterHolder filter) throws ServletException
	{		
	}

	public void decorateServletHolder(ServletHolder servlet) throws ServletException
	{
		init();
	}

	public void destroyServletInstance(Servlet s)
	{
	}

	public void destroyFilterInstance(Filter f)
	{		
	}

	public void destroyListenerInstance(EventListener f)
	{		
	}
}

@@ -2729,16 +2729,70 @@
 %0A%09%09%7D%0D%0A%0D%0A
+%09%09if (!%22/%22.equals(_name) && !%22%22.equals(_name))%0D%0A%09%09%7B%0D%0A%09
 %09%09sipCtx
@@ -2816,24 +2816,25 @@
 (_name);%0D%0A%09%09
+%09
 compCtx.bind
@@ -2910,24 +2910,25 @@
 actory());%0D%0A
+%09
 %09%09compCtx.bi
@@ -3014,16 +3014,17 @@
 ce());%0D%0A
+%09
 %09%09compCt
@@ -3121,16 +3121,359 @@
 il());%0D%0A
+%09%09%7D%0D%0A%09%09else%0D%0A%09%09%7B%0D%0A%09%09%09compCtx.bind(JNDI_SIP_PREFIX + JNDI_SIP_FACTORY_POSTFIX.substring(1), _context.getSipFactory());%0D%0A%09%09%09compCtx.bind(JNDI_SIP_PREFIX + JNDI_TIMER_SERVICE_POSTFIX.substring(1), _context.getTimerService());%0D%0A%09%09%09compCtx.bind(JNDI_SIP_PREFIX + JNDI_SIP_SESSIONS_UTIL_POSTFIX.substring(1), _context.getSipSessionsUtil());%0D%0A%0D%0A%09%09%7D%0D%0A
 %09%09Log.de

Fix CIPANGO-141: Got javax.naming.NamingException when running maven plugin with a SIP Servlet 1.0 application