1 /*
2
3    Licensed to the Apache Software Foundation (ASF) under one or more
4    contributor license agreements.  See the NOTICE file distributed with
5    this work for additional information regarding copyright ownership.
6    The ASF licenses this file to You under the Apache License, Version 2.0
7    (the "License"); you may not use this file except in compliance with
8    the License.  You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17
18  */

19 package org.apache.batik.util;
20
21 import java.lang.ref.Reference;
22 import java.lang.ref.ReferenceQueue;
23 import java.lang.ref.SoftReference;
24 import java.lang.ref.WeakReference;
25 import java.lang.ref.PhantomReference;
26
27 /**
28  * One line Class Desc
29  *
30  * Complete Class Desc
31  *
32  * @author <a href="mailto:deweese@apache.org">l449433</a>
33  * @version $Id: CleanerThread.java 1808023 2017-09-11 12:43:22Z ssteiner $
34  */

35 public class CleanerThread extends Thread {
36
37     static volatile ReferenceQueue queue = null;
38     static CleanerThread  thread = null;
39
40     public static ReferenceQueue getReferenceQueue() {
41
42         if ( queue == null ) {
43             synchronized (CleanerThread.class) {
44                 queue = new ReferenceQueue();
45                 thread = new CleanerThread();
46             }
47         }
48         return queue;
49     }
50
51     /**
52      * If objects registered with the reference queue associated with
53      * this class implement this interface then the 'cleared' method
54      * will be called when the reference is queued.
55      */

56     public interface ReferenceCleared {
57         /* Called when the reference is cleared */
58         void cleared();
59     }
60
61     /**
62      * A SoftReference subclass that automatically registers with
63      * the cleaner ReferenceQueue.
64      */

65     public abstract static class SoftReferenceCleared extends SoftReference
66       implements ReferenceCleared {
67         public SoftReferenceCleared(Object o) {
68             super (o, CleanerThread.getReferenceQueue());
69         }
70     }
71
72     /**
73      * A WeakReference subclass that automatically registers with
74      * the cleaner ReferenceQueue.
75      */

76     public abstract static class WeakReferenceCleared extends WeakReference
77       implements ReferenceCleared {
78         public WeakReferenceCleared(Object o) {
79             super (o, CleanerThread.getReferenceQueue());
80         }
81     }
82
83     /**
84      * A PhantomReference subclass that automatically registers with
85      * the cleaner ReferenceQueue.
86      */

87     public abstract static class PhantomReferenceCleared
88         extends PhantomReference
89         implements ReferenceCleared {
90         public PhantomReferenceCleared(Object o) {
91             super (o, CleanerThread.getReferenceQueue());
92         }
93     }
94
95     protected CleanerThread() {
96         super("Batik CleanerThread");
97         setDaemon(true);
98         start();
99     }
100
101     public void run() {
102         while(true) {
103             try {
104                 Reference ref;
105                 try {
106                     ref = queue.remove();
107                     // System.err.println("Cleaned: " + ref);
108                 } catch (InterruptedException ie) {
109                     continue;
110                 }
111
112                 if (ref instanceof ReferenceCleared) {
113                     ReferenceCleared rc = (ReferenceCleared)ref;
114                     rc.cleared();
115                 }
116             } catch (ThreadDeath td) {
117                 throw td;
118             } catch (Throwable t) {
119                 t.printStackTrace();
120             }
121         }
122     }
123 }
124