Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. package org.apache.lucene.mockfile.filterfs;
  2.  
  3. /*
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership.
  7. * The ASF licenses this file to You under the Apache License, Version 2.0
  8. * (the "License"); you may not use this file except in compliance with
  9. * the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19.  
  20. import java.io.FilterInputStream;
  21. import java.io.FilterOutputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.OutputStream;
  25. import java.nio.channels.AsynchronousFileChannel;
  26. import java.nio.channels.FileChannel;
  27. import java.nio.channels.SeekableByteChannel;
  28. import java.nio.file.FileSystem;
  29. import java.nio.file.OpenOption;
  30. import java.nio.file.Path;
  31. import java.nio.file.attribute.FileAttribute;
  32. import java.util.Collections;
  33. import java.util.IdentityHashMap;
  34. import java.util.Iterator;
  35. import java.util.Map;
  36. import java.util.Set;
  37. import java.util.concurrent.ExecutorService;
  38.  
  39. public class LeakFS extends FilterFileSystemProvider {
  40. private final Map<Object,Exception> openHandles = Collections.synchronizedMap(new IdentityHashMap<>());
  41.  
  42. public LeakFS(FileSystem delegate) {
  43. super("leakfs://", delegate);
  44. }
  45.  
  46. @Override
  47. public InputStream newInputStream(Path path, OpenOption... options) throws IOException {
  48. InputStream stream = new FilterInputStream(super.newInputStream(path, options)) {
  49. @Override
  50. public void close() throws IOException {
  51. openHandles.remove(this);
  52. super.close();
  53. }
  54.  
  55. @Override
  56. public String toString() {
  57. return path.toString();
  58. }
  59. };
  60. openHandles.put(stream, new Exception());
  61. return stream;
  62. }
  63.  
  64. @Override
  65. public OutputStream newOutputStream(Path path, OpenOption... options) throws IOException {
  66. OutputStream stream = new FilterOutputStream(super.newOutputStream(path, options)) {
  67. @Override
  68. public void close() throws IOException {
  69. openHandles.remove(this);
  70. super.close();
  71. }
  72.  
  73. @Override
  74. public String toString() {
  75. return path.toString();
  76. }
  77. };
  78. openHandles.put(stream, new Exception());
  79. return stream;
  80. }
  81.  
  82. @Override
  83. public FileChannel newFileChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
  84. return super.newFileChannel(path, options, attrs);
  85. }
  86.  
  87. @Override
  88. public AsynchronousFileChannel newAsynchronousFileChannel(Path path, Set<? extends OpenOption> options, ExecutorService executor, FileAttribute<?>... attrs) throws IOException {
  89. return super.newAsynchronousFileChannel(path, options, executor, attrs);
  90. }
  91.  
  92. @Override
  93. public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
  94. return super.newByteChannel(path, options, attrs);
  95. }
  96.  
  97. @Override
  98. public synchronized void onClose() {
  99. if (!openHandles.isEmpty()) {
  100. // print the first one as its very verbose otherwise
  101. Exception cause = null;
  102. Iterator<Exception> stacktraces = openHandles.values().iterator();
  103. if (stacktraces.hasNext()) {
  104. cause = stacktraces.next();
  105. }
  106. throw new RuntimeException("file handle leaks: " + openHandles.keySet(), cause);
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement