PooledSjavac.java revision 2958:27da0c3ac83a
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.comp;
26
27import java.io.File;
28import java.net.URI;
29import java.util.List;
30import java.util.Objects;
31import java.util.Set;
32import java.util.concurrent.ExecutorService;
33import java.util.concurrent.Executors;
34import java.util.concurrent.TimeUnit;
35
36import com.sun.tools.sjavac.Log;
37import com.sun.tools.sjavac.server.CompilationResult;
38import com.sun.tools.sjavac.server.Sjavac;
39import com.sun.tools.sjavac.server.SysInfo;
40
41/**
42 * An sjavac implementation that limits the number of concurrent calls by
43 * wrapping invocations in Callables and delegating them to a FixedThreadPool.
44 *
45 *  <p><b>This is NOT part of any supported API.
46 *  If you write code that depends on this, you do so at your own risk.
47 *  This code and its internal interfaces are subject to change or
48 *  deletion without notice.</b>
49 */
50public class PooledSjavac implements Sjavac {
51
52    final Sjavac delegate;
53    final ExecutorService pool;
54
55    public PooledSjavac(Sjavac delegate, int poolsize) {
56        Objects.requireNonNull(delegate);
57        this.delegate = delegate;
58        pool = Executors.newFixedThreadPool(poolsize);
59    }
60
61    @Override
62    public SysInfo getSysInfo() {
63        try {
64            return pool.submit(() -> delegate.getSysInfo()).get();
65        } catch (Exception e) {
66            e.printStackTrace();
67            throw new RuntimeException("Error during getSysInfo", e);
68        }
69    }
70
71    @Override
72    public CompilationResult compile(final String protocolId,
73                                     final String invocationId,
74                                     final String[] args,
75                                     final List<File> explicitSources,
76                                     final Set<URI> sourcesToCompile,
77                                     final Set<URI> visibleSources) {
78        try {
79            return pool.submit(() -> {
80                return delegate.compile(protocolId,
81                                        invocationId,
82                                        args,
83                                        explicitSources,
84                                        sourcesToCompile,
85                                        visibleSources);
86            }).get();
87        } catch (Exception e) {
88            e.printStackTrace();
89            throw new RuntimeException("Error during compile", e);
90        }
91    }
92
93    @Override
94    public void shutdown() {
95        Log.debug("Shutting down PooledSjavac");
96        pool.shutdown(); // Disable new tasks from being submitted
97        try {
98            // Wait a while for existing tasks to terminate
99            if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
100                pool.shutdownNow(); // Cancel currently executing tasks
101                // Wait a while for tasks to respond to being cancelled
102                if (!pool.awaitTermination(60, TimeUnit.SECONDS))
103                    Log.error("ThreadPool did not terminate");
104            }
105        } catch (InterruptedException ie) {
106          // (Re-)Cancel if current thread also interrupted
107          pool.shutdownNow();
108          // Preserve interrupt status
109          Thread.currentThread().interrupt();
110        }
111
112        delegate.shutdown();
113    }
114
115    @Override
116    public String serverSettings() {
117        return delegate.serverSettings();
118    }
119}
120