Package com.ericsson.ssa.sip.timer

Source Code of com.ericsson.ssa.sip.timer.TimerServiceImpl

/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
* Copyright (c) Ericsson AB, 2004-2008. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License").  You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code.  If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license."  If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above.  However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package com.ericsson.ssa.sip.timer;

import com.ericsson.ssa.container.SipContainerThreadPool;
import com.ericsson.ssa.sip.SipApplicationSessionImpl;
import com.ericsson.ssa.sip.SipSessionManager;

import java.io.Serializable;

import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import java.util.logging.Level;
import org.jvnet.glassfish.comms.util.LogUtil;

import javax.servlet.sip.ServletTimer;
import javax.servlet.sip.SipApplicationSession;
import javax.servlet.sip.TimerListener;
import javax.servlet.sip.TimerService;


/**
* @reviewed ejoelbi 2007-jan-17
*/
public class TimerServiceImpl implements TimerService, GeneralTimerListener {
    private static TimerServiceImpl _instance = new TimerServiceImpl();
    private static Logger logger = LogUtil.SIP_LOGGER.getLogger();
    private SipContainerThreadPool _schedPool =
                                   SipContainerThreadPool.getInstance();

    private TimerServiceImpl() {
    }

    public static TimerServiceImpl getInstance() {
        return _instance;
    }

    /*
     * (non-Javadoc)
     * @see javax.servlet.sip.TimerService#createTimer(javax.servlet.sip.SipApplicationSession, long, boolean, java.io.Serializable)
     */
    public ServletTimer createTimer(SipApplicationSession appSession,
        long delay, boolean isPersistent, Serializable info) {

        SipApplicationSessionImpl as = (SipApplicationSessionImpl) appSession;
        SipSessionManager ssm = as.getSipSessionManager();

        if ((null == ssm) || (as.isValid() == false)) {
            throw new IllegalStateException("Invalid SipApplicationSession");
        }

        TimerListener tl = as.getTimerListener();

        if (tl == null) {
            throw new IllegalStateException("No TimerListener is configured ");
        }

        ServletTimerImpl st = createTimer(ssm, as, tl, delay, isPersistent, info);

        return st;
    }

    /*
     * (non-Javadoc)
     * @see javax.servlet.sip.TimerService#createTimer(javax.servlet.sip.SipApplicationSession, long, long, boolean, boolean, java.io.Serializable)
     */
    public ServletTimer createTimer(SipApplicationSession appSession,
        long delay, long period, boolean fixedDelay, boolean isPersistent,
        Serializable info) {
        if (period < 1) {
            throw new IllegalArgumentException(
                "Period should be greater than 0");
        }

        SipApplicationSessionImpl as = (SipApplicationSessionImpl) appSession;
        SipSessionManager ssm = as.getSipSessionManager();

        if ((null == ssm) || (as.isValid() == false)) {
            throw new IllegalStateException("Invalid SipApplicationSession");
        }

        TimerListener tl = as.getTimerListener();

        if (tl == null) {
            throw new IllegalStateException("No TimerListener is configured ");
        }

        ServletTimerImpl st = createTimer(ssm, as, tl, delay, period,
                fixedDelay, isPersistent, info);

        return st;
    }

    /**
     * Non SSA interface for creation of GeneralTimer to be used internally in the container.
     *
     * @param listener
     * @param delay
     * @param isPersistent
     * @param info
     * @return
     */
    public GeneralTimer createTimer(GeneralTimerListener listener, long delay, Object info) {
        // setupScheduller(); // TODO only for test

        GeneralTimerImpl st = new GeneralTimerImpl(info, delay, listener);

        ScheduledFuture<?> f = _schedPool.schedule(st, delay,
                TimeUnit.MILLISECONDS);
        st.setFuture(f);

        return st;
    }

    public GeneralTimer createTimer(GeneralTimerListener listener, long delay,
        long period, boolean fixedDelay, Object info) {
        // setupScheduller(); // TODO only for test

        GeneralTimerImpl st = null;
        st = new GeneralTimerImpl(info, delay, fixedDelay, period, listener);

        ScheduledFuture<?> f = null;

        if (fixedDelay) {
            f = _schedPool.scheduleWithFixedDelay(st, delay, period,
                    TimeUnit.MILLISECONDS);
        } else {
            f = _schedPool.scheduleAtFixedRate(st, delay, period,
                    TimeUnit.MILLISECONDS);
        }

        st.setFuture(f);

        return st;
    }

    /**
     * Non SSA interface for creation of GeneralTimer to be used internally in the container.
     * @param ssm
     * @param sas
     * @param listener
     * @param delay
     * @param isPersistent
     * @param info
     * @return
     */
    private ServletTimerImpl createTimer(SipSessionManager ssm,
        SipApplicationSessionImpl sas, TimerListener listener, long delay,
        boolean isPersistent, Serializable info) {
        // setupScheduller(); // TODO only for test

        ServletTimerImpl st = null;

        if ((ssm != null) && (sas != null)) {
            st = ssm.createServletTimer(sas, info, delay, listener,
                                        isPersistent);
        } else {
            throw new IllegalStateException("SipApplicationSession is null");
        }

        ScheduledFuture<?> f = _schedPool.schedule(st, delay,
                TimeUnit.MILLISECONDS);
        st.setFuture(f);

        return st;
    }

    private ServletTimerImpl createTimer(SipSessionManager ssm,
        SipApplicationSessionImpl sas, TimerListener listener, long delay,
        long period, boolean fixedDelay, boolean isPersistent,
        Serializable info) {

        // setupScheduller(); // TODO only for test

        ServletTimerImpl st = null;

        if ((ssm != null) && (sas != null)) {
            st = ssm.createServletTimer(sas, info, delay, fixedDelay, period,
                    listener, isPersistent);
        } else {
            throw new IllegalStateException("SipApplicationSession is null");
        }

        ScheduledFuture<?> f = null;

        if (fixedDelay) {
            f = _schedPool.scheduleWithFixedDelay(st, delay, period,
                    TimeUnit.MILLISECONDS);
        } else {
            f = _schedPool.scheduleAtFixedRate(st, delay, period,
                    TimeUnit.MILLISECONDS);
        }

        st.setFuture(f);

        return st;
    }

    public void rescheduleTimer(ServletTimerImpl timer) {
        final ServletTimerImpl st = timer;
        // setupScheduller();

        long now = System.currentTimeMillis();
        RescheduleAlgorithm alg = new RescheduleAlgorithm(timer, now);

        // Fetch what the RescheduleAlgorithm believe it should do.
        // NOTE!!! Important to remeber those values before doint anything
        // with the timer as the rescedul algorithm might answer something
        // else if servlet fires.
        boolean fireAtOnceSeparately = alg.fireAtOnceSeparately();
        boolean needRescheduling = alg.needReschedule(); // Next time to do a periodic reschedule
        long initDelay = alg.delayUntilNextExecution();

        if (fireAtOnceSeparately) {
            _schedPool.execute(st);
        }

        ScheduledFuture<?> f = null;

        if (needRescheduling) {
            if (!st.isRepeatingTimer()) {
                f = _schedPool.schedule(st, initDelay, TimeUnit.MILLISECONDS);
            } else if (st.isFixedDelay()) {
                f = _schedPool.scheduleWithFixedDelay(st, initDelay,
                        st.getPeriod(), TimeUnit.MILLISECONDS);
            } else {
                f = _schedPool.scheduleAtFixedRate(st, initDelay,
                        st.getPeriod(), TimeUnit.MILLISECONDS);
            }
        }

        st.setFuture(f);
    }

    private void setupScheduller() {
        // Could be configured in dispatcher.xml so that TM is unaware of NM
        if (_schedPool == null) {
            _schedPool = SipContainerThreadPool.getInstance();

            if (_schedPool == null) {
                throw new IllegalStateException(
                    "Failed to get access to the ThreadPool");
            }

           // createTimer((GeneralTimerListener) this, 10000, 10000,
           //      false, null);
        }
    }

    /*
     * // Only for unit test public void
     * setScheduledExecutorService(ScheduledExecutorService schedPool) {
     * _schedPool = schedPool; }
     */
    public void timeout(GeneralTimer timer) {
        // logger.log(Level.FINE, "Purging Thread pool");
        _schedPool.purge();
    }
}
TOP

Related Classes of com.ericsson.ssa.sip.timer.TimerServiceImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.