|
1 /* |
|
2 * Copyright (c) 2016, 2018, 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 |
|
26 package jdk.management.jfr; |
|
27 |
|
28 import java.io.IOException; |
|
29 import java.io.InputStream; |
|
30 import java.util.Date; |
|
31 import java.util.HashMap; |
|
32 import java.util.Map; |
|
33 import java.util.Timer; |
|
34 import java.util.concurrent.TimeUnit; |
|
35 |
|
36 final class StreamManager { |
|
37 |
|
38 public static final long TIME_OUT = TimeUnit.MINUTES.toMillis(2); |
|
39 public static final int DEFAULT_BLOCK_SIZE = 50000; |
|
40 |
|
41 private static long idCounter = 0; |
|
42 |
|
43 private final Map<Long, Stream> streams = new HashMap<>(); |
|
44 private Timer timer; |
|
45 |
|
46 public synchronized Stream getStream(long streamIdentifer) { |
|
47 Stream stream = streams.get(streamIdentifer); |
|
48 if (stream == null) { |
|
49 throw new IllegalArgumentException("Unknown stream identifier " + streamIdentifer); |
|
50 } |
|
51 return stream; |
|
52 } |
|
53 |
|
54 public synchronized Stream create(InputStream is, int blockSize) { |
|
55 idCounter++; |
|
56 Stream stream = new Stream(is, idCounter, blockSize); |
|
57 streams.put(stream.getId(), stream); |
|
58 |
|
59 scheduleAbort(stream, System.currentTimeMillis() + TIME_OUT); |
|
60 return stream; |
|
61 } |
|
62 |
|
63 public synchronized void destroy(Stream stream) { |
|
64 try { |
|
65 stream.close(); |
|
66 } catch (IOException e) { |
|
67 // OK |
|
68 } |
|
69 streams.remove(stream.getId()); |
|
70 if (streams.isEmpty()) { |
|
71 timer.cancel(); |
|
72 timer = null; |
|
73 } |
|
74 } |
|
75 |
|
76 public synchronized void scheduleAbort(Stream s, long when) { |
|
77 if (timer == null) { |
|
78 timer = new Timer(true); |
|
79 } |
|
80 timer.schedule(new StreamCleanupTask(this, s), new Date(when + TIME_OUT)); |
|
81 } |
|
82 } |