IdleResetSjavac.java revision 3022:5ba1a29a0eb0
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.io.Writer;
28import java.util.Timer;
29import java.util.TimerTask;
30
31/**
32 * An sjavac implementation that keeps track of idleness and shuts down the
33 * given Terminable upon idleness timeout.
34 *
35 * An idleness timeout kicks in {@code idleTimeout} milliseconds after the last
36 * request is completed.
37 *
38 *  <p><b>This is NOT part of any supported API.
39 *  If you write code that depends on this, you do so at your own risk.
40 *  This code and its internal interfaces are subject to change or
41 *  deletion without notice.</b>
42 */
43public class IdleResetSjavac implements Sjavac {
44
45    private final Sjavac delegate;
46    private final Terminable toShutdown;
47    private final Timer idlenessTimer = new Timer();
48    private final long idleTimeout;
49    private int outstandingCalls = 0;
50
51    // Class invariant: idlenessTimerTask != null <-> idlenessTimerTask is scheduled
52    private TimerTask idlenessTimerTask;
53
54    public IdleResetSjavac(Sjavac delegate,
55                            Terminable toShutdown,
56                            long idleTimeout) {
57        this.delegate = delegate;
58        this.toShutdown = toShutdown;
59        this.idleTimeout = idleTimeout;
60        scheduleTimeout();
61    }
62
63    @Override
64    public int compile(String[] args, Writer out, Writer err) {
65        startCall();
66        try {
67            return delegate.compile(args, out, err);
68        } finally {
69            endCall();
70        }
71    }
72
73    private synchronized void startCall() {
74        // Was there no outstanding calls before this call?
75        if (++outstandingCalls == 1) {
76            // Then the timer task must have been scheduled
77            if (idlenessTimerTask == null)
78                throw new IllegalStateException("Idle timeout already cancelled");
79            // Cancel timeout task
80            idlenessTimerTask.cancel();
81            idlenessTimerTask = null;
82        }
83    }
84
85    private synchronized void endCall() {
86        if (--outstandingCalls == 0) {
87            // No more outstanding calls. Schedule timeout.
88            scheduleTimeout();
89        }
90    }
91
92    private void scheduleTimeout() {
93        if (idlenessTimerTask != null)
94            throw new IllegalStateException("Idle timeout already scheduled");
95        idlenessTimerTask = new TimerTask() {
96            public void run() {
97                toShutdown.shutdown("Server has been idle for " + (idleTimeout / 1000) + " seconds.");
98            }
99        };
100        idlenessTimer.schedule(idlenessTimerTask, idleTimeout);
101    }
102
103    @Override
104    public void shutdown() {
105        idlenessTimer.cancel();
106        delegate.shutdown();
107    }
108
109}
110