]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/io/LeakTrackingByteBufferPool.java
Update notes about password security
[gigi.git] / lib / jetty / org / eclipse / jetty / io / LeakTrackingByteBufferPool.java
1 //
2 //  ========================================================================
3 //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 //  ------------------------------------------------------------------------
5 //  All rights reserved. This program and the accompanying materials
6 //  are made available under the terms of the Eclipse Public License v1.0
7 //  and Apache License v2.0 which accompanies this distribution.
8 //
9 //      The Eclipse Public License is available at
10 //      http://www.eclipse.org/legal/epl-v10.html
11 //
12 //      The Apache License v2.0 is available at
13 //      http://www.opensource.org/licenses/apache2.0.php
14 //
15 //  You may elect to redistribute this code under either of these licenses.
16 //  ========================================================================
17 //
18
19 package org.eclipse.jetty.io;
20
21 import java.nio.ByteBuffer;
22
23 import org.eclipse.jetty.util.LeakDetector;
24 import org.eclipse.jetty.util.component.ContainerLifeCycle;
25 import org.eclipse.jetty.util.log.Log;
26 import org.eclipse.jetty.util.log.Logger;
27
28 public class LeakTrackingByteBufferPool extends ContainerLifeCycle implements ByteBufferPool
29 {
30     private static final Logger LOG = Log.getLogger(LeakTrackingByteBufferPool.class);
31
32     private final LeakDetector<ByteBuffer> leakDetector = new LeakDetector<ByteBuffer>()
33     {
34         @Override
35         protected void leaked(LeakInfo leakInfo)
36         {
37             LeakTrackingByteBufferPool.this.leaked(leakInfo);
38         }
39     };
40     
41     private final ByteBufferPool delegate;
42
43     public LeakTrackingByteBufferPool(ByteBufferPool delegate)
44     {
45         this.delegate = delegate;
46         addBean(leakDetector);
47         addBean(delegate);
48     }
49
50     @Override
51     public ByteBuffer acquire(int size, boolean direct)
52     {
53         ByteBuffer buffer = delegate.acquire(size, direct);
54         if (!leakDetector.acquired(buffer))
55             LOG.warn("ByteBuffer {}@{} not tracked", buffer, System.identityHashCode(buffer));
56         return buffer;
57     }
58
59     @Override
60     public void release(ByteBuffer buffer)
61     {
62         if (buffer == null)
63             return;
64         if (!leakDetector.released(buffer))
65             LOG.warn("ByteBuffer {}@{} released but not acquired", buffer, System.identityHashCode(buffer));
66         delegate.release(buffer);
67     }
68
69     protected void leaked(LeakDetector<ByteBuffer>.LeakInfo leakInfo)
70     {
71         LOG.warn("ByteBuffer " + leakInfo.getResourceDescription() + " leaked at:", leakInfo.getStackFrames());
72     }
73 }