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