/*
 * Copyright 2004-2014 ICEsoft Technologies Canada Corp.
 *
 * 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.icepush.servlet;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletRequest;

import org.icepush.Configuration;
import org.icepush.http.ResponseHandler;
import org.icepush.http.Server;
import org.icepush.util.Slot;

public class ThreadBlockingAdaptingServlet implements PseudoServlet {
    private static final Logger LOGGER = Logger.getLogger(ThreadBlockingAdaptingServlet.class.getName());

    private final Configuration configuration;
    private final Slot heartbeatInterval;
    private final Server server;

    public ThreadBlockingAdaptingServlet(
        final Server server, final Slot heartbeatInterval, final Configuration configuration) {

        this.server = server;
        this.heartbeatInterval = heartbeatInterval;
        this.configuration = configuration;
    }

    public void service(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
        ThreadBlockingRequestResponse requestResponse =
            new ThreadBlockingRequestResponse(request, response, configuration);
        server.service(requestResponse);
        requestResponse.blockUntilRespond();
    }

    public void shutdown() {
        server.shutdown();
    }

    private class ThreadBlockingRequestResponse extends ServletRequestResponse {
        private final Semaphore semaphore;

        public ThreadBlockingRequestResponse(final HttpServletRequest request, final HttpServletResponse response, final Configuration configuration) throws Exception {
            super(request, response, configuration);
            semaphore = new Semaphore(1);
            //Acquire semaphore hoping to have it released by a call to respondWith() method.
            semaphore.acquire();
        }

        public void respondWith(final ResponseHandler handler) throws Exception {
            try {
                super.respondWith(handler);
            } finally {
                semaphore.release();
            }
        }

        public void blockUntilRespond() throws InterruptedException {
            long timeout = heartbeatInterval.getLongValue() * 3;
            //Block thread by trying to acquire the semaphore a second time.
            boolean acquired = semaphore.tryAcquire(timeout, TimeUnit.MILLISECONDS);
            if (acquired) {
                //Release the semaphore previously acquired.
                semaphore.release();
            } else {
                String remoteAddress = "unknown";
                if( request instanceof ServletRequest ){
                    remoteAddress = ((ServletRequest)request).getRemoteAddr();
                }
                LOGGER.warning("No response sent to " +
                        "request '" + request.getRequestURI() + "' " +
                        "with ICEfaces ID '" +
                        request.getParameter("ice.session") + "' " +
                        "from " + remoteAddress + " " +
                        "in " + timeout + " milliseconds.  " +
                        "Unblocking " +
                        "thread '" + Thread.currentThread().getName() + "'.");
                //Release the semaphore; most probably respondWith() method was not invoked.
                semaphore.release();
            }
        }
    }
}
