author | alundblad |
Fri, 04 Sep 2015 13:24:15 +0200 | |
changeset 32542 | f4e4f4c4f9f4 |
parent 32335 | 7df616378cf3 |
child 34752 | 9c262a013456 |
permissions | -rw-r--r-- |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
1 |
/* |
31115
8d8e98052d5d
8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
26107
diff
changeset
|
2 |
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
4 |
* |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
10 |
* |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
15 |
* accompanied this code). |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
16 |
* |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
20 |
* |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
23 |
* questions. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
24 |
*/ |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
25 |
package com.sun.tools.sjavac.comp; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
26 |
|
32542
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
27 |
import java.io.Writer; |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
28 |
import java.util.Objects; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
29 |
import java.util.concurrent.ExecutorService; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
30 |
import java.util.concurrent.Executors; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
31 |
import java.util.concurrent.TimeUnit; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
32 |
|
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
33 |
import com.sun.tools.sjavac.Log; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
34 |
import com.sun.tools.sjavac.server.Sjavac; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
35 |
|
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
36 |
/** |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
37 |
* An sjavac implementation that limits the number of concurrent calls by |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
38 |
* wrapping invocations in Callables and delegating them to a FixedThreadPool. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
39 |
* |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
40 |
* <p><b>This is NOT part of any supported API. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
41 |
* If you write code that depends on this, you do so at your own risk. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
42 |
* This code and its internal interfaces are subject to change or |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
43 |
* deletion without notice.</b> |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
44 |
*/ |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
45 |
public class PooledSjavac implements Sjavac { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
46 |
|
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
47 |
final Sjavac delegate; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
48 |
final ExecutorService pool; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
49 |
|
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
50 |
public PooledSjavac(Sjavac delegate, int poolsize) { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
51 |
Objects.requireNonNull(delegate); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
52 |
this.delegate = delegate; |
31115
8d8e98052d5d
8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
26107
diff
changeset
|
53 |
pool = Executors.newFixedThreadPool(poolsize); |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
54 |
} |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
55 |
|
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
56 |
@Override |
32542
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
57 |
public int compile(String[] args, Writer out, Writer err) { |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
58 |
try { |
31115
8d8e98052d5d
8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
26107
diff
changeset
|
59 |
return pool.submit(() -> { |
32542
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
60 |
return delegate.compile(args, out, err); |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
61 |
}).get(); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
62 |
} catch (Exception e) { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
63 |
e.printStackTrace(); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
64 |
throw new RuntimeException("Error during compile", e); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
65 |
} |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
66 |
} |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
67 |
|
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
68 |
@Override |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
69 |
public void shutdown() { |
31115
8d8e98052d5d
8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
26107
diff
changeset
|
70 |
Log.debug("Shutting down PooledSjavac"); |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
71 |
pool.shutdown(); // Disable new tasks from being submitted |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
72 |
try { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
73 |
// Wait a while for existing tasks to terminate |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
74 |
if (!pool.awaitTermination(60, TimeUnit.SECONDS)) { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
75 |
pool.shutdownNow(); // Cancel currently executing tasks |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
76 |
// Wait a while for tasks to respond to being cancelled |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
77 |
if (!pool.awaitTermination(60, TimeUnit.SECONDS)) |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
78 |
Log.error("ThreadPool did not terminate"); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
79 |
} |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
80 |
} catch (InterruptedException ie) { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
81 |
// (Re-)Cancel if current thread also interrupted |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
82 |
pool.shutdownNow(); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
83 |
// Preserve interrupt status |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
84 |
Thread.currentThread().interrupt(); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
85 |
} |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
86 |
|
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
87 |
delegate.shutdown(); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
88 |
} |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
89 |
|
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
90 |
} |