IdleResetSjavac.java revision 2593:035b01d356ee
1/*
2 * Copyright (c) 2014, 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.File;
28import java.net.URI;
29import java.util.List;
30import java.util.Set;
31import java.util.Timer;
32import java.util.TimerTask;
33import java.util.concurrent.atomic.AtomicInteger;
34
35/**
36 * An sjavac implementation that keeps track of idleness and shuts down the
37 * given Terminable upon idleness timeout.
38 *
39 * An idleness timeout kicks in {@code idleTimeout} milliseconds after the last
40 * request is completed.
41 *
42 *  <p><b>This is NOT part of any supported API.
43 *  If you write code that depends on this, you do so at your own risk.
44 *  This code and its internal interfaces are subject to change or
45 *  deletion without notice.</b>
46 */
47public class IdleResetSjavac implements Sjavac {
48
49    private final Sjavac delegate;
50    private final AtomicInteger outstandingCalls = new AtomicInteger();
51    private final Terminable toShutdown;
52    private final Timer idlenessTimer = new Timer();
53    private final long idleTimeout;
54
55    // Class invariant: idlenessTimerTask != null <-> idlenessTimerTask is scheduled
56    private TimerTask idlenessTimerTask;
57
58    public IdleResetSjavac(Sjavac delegate,
59                            Terminable toShutdown,
60                            long idleTimeout) {
61        this.delegate = delegate;
62        this.toShutdown = toShutdown;
63        this.idleTimeout = idleTimeout;
64        scheduleTimeout();
65    }
66
67    @Override
68    public SysInfo getSysInfo() {
69        startCall();
70        try {
71            return delegate.getSysInfo();
72        } finally {
73            endCall();
74        }
75    }
76
77    @Override
78    public CompilationResult compile(String protocolId,
79                                     String invocationId,
80                                     String[] args,
81                                     List<File> explicitSources,
82                                     Set<URI> sourcesToCompile,
83                                     Set<URI> visibleSources) {
84        startCall();
85        try {
86            return delegate.compile(protocolId,
87                                    invocationId,
88                                    args,
89                                    explicitSources,
90                                    sourcesToCompile,
91                                    visibleSources);
92        } finally {
93            endCall();
94        }
95    }
96
97    private void startCall() {
98        // Was there no outstanding calls before this call?
99        if (outstandingCalls.incrementAndGet() == 1) {
100            // Then the timer task must have been scheduled
101            if (idlenessTimerTask == null)
102                throw new IllegalStateException("Idle timeout already cancelled");
103            // Cancel timeout task
104            idlenessTimerTask.cancel();
105            idlenessTimerTask = null;
106        }
107    }
108
109    private void endCall() {
110        if (outstandingCalls.decrementAndGet() == 0) {
111            // No more outstanding calls. Schedule timeout.
112            scheduleTimeout();
113        }
114    }
115
116    private void scheduleTimeout() {
117        if (idlenessTimerTask != null)
118            throw new IllegalStateException("Idle timeout already scheduled");
119        idlenessTimerTask = new TimerTask() {
120            public void run() {
121                toShutdown.shutdown("Server has been idle for " + (idleTimeout / 1000) + " seconds.");
122            }
123        };
124        idlenessTimer.schedule(idlenessTimerTask, idleTimeout);
125    }
126
127    @Override
128    public void shutdown() {
129        idlenessTimer.cancel();
130        delegate.shutdown();
131    }
132
133    @Override
134    public String serverSettings() {
135        return delegate.serverSettings();
136    }
137}
138