IdleResetSjavac.java revision 3012:adba44f6b471
1/*
2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25package com.sun.tools.sjavac.server;
26
27import java.util.Timer;
28import java.util.TimerTask;
29
30/**
31 * An sjavac implementation that keeps track of idleness and shuts down the
32 * given Terminable upon idleness timeout.
33 *
34 * An idleness timeout kicks in {@code idleTimeout} milliseconds after the last
35 * request is completed.
36 *
37 *  <p><b>This is NOT part of any supported API.
38 *  If you write code that depends on this, you do so at your own risk.
39 *  This code and its internal interfaces are subject to change or
40 *  deletion without notice.</b>
41 */
42public class IdleResetSjavac implements Sjavac {
43
44    private final Sjavac delegate;
45    private final Terminable toShutdown;
46    private final Timer idlenessTimer = new Timer();
47    private final long idleTimeout;
48    private int outstandingCalls = 0;
49
50    // Class invariant: idlenessTimerTask != null <-> idlenessTimerTask is scheduled
51    private TimerTask idlenessTimerTask;
52
53    public IdleResetSjavac(Sjavac delegate,
54                            Terminable toShutdown,
55                            long idleTimeout) {
56        this.delegate = delegate;
57        this.toShutdown = toShutdown;
58        this.idleTimeout = idleTimeout;
59        scheduleTimeout();
60    }
61
62    @Override
63    public CompilationResult compile(String[] args) {
64        startCall();
65        try {
66            return delegate.compile(args);
67        } finally {
68            endCall();
69        }
70    }
71
72    private synchronized void startCall() {
73        // Was there no outstanding calls before this call?
74        if (++outstandingCalls == 1) {
75            // Then the timer task must have been scheduled
76            if (idlenessTimerTask == null)
77                throw new IllegalStateException("Idle timeout already cancelled");
78            // Cancel timeout task
79            idlenessTimerTask.cancel();
80            idlenessTimerTask = null;
81        }
82    }
83
84    private synchronized void endCall() {
85        if (--outstandingCalls == 0) {
86            // No more outstanding calls. Schedule timeout.
87            scheduleTimeout();
88        }
89    }
90
91    private void scheduleTimeout() {
92        if (idlenessTimerTask != null)
93            throw new IllegalStateException("Idle timeout already scheduled");
94        idlenessTimerTask = new TimerTask() {
95            public void run() {
96                toShutdown.shutdown("Server has been idle for " + (idleTimeout / 1000) + " seconds.");
97            }
98        };
99        idlenessTimer.schedule(idlenessTimerTask, idleTimeout);
100    }
101
102    @Override
103    public void shutdown() {
104        idlenessTimer.cancel();
105        delegate.shutdown();
106    }
107
108}
109