|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.cxf.io; |
| 21 | + |
| 22 | +import java.io.Closeable; |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.Iterator; |
| 27 | +import java.util.Objects; |
| 28 | +import java.util.Timer; |
| 29 | +import java.util.TimerTask; |
| 30 | +import java.util.concurrent.DelayQueue; |
| 31 | +import java.util.concurrent.Delayed; |
| 32 | +import java.util.concurrent.TimeUnit; |
| 33 | +import java.util.logging.Logger; |
| 34 | + |
| 35 | +import jakarta.annotation.Resource; |
| 36 | +import org.apache.cxf.Bus; |
| 37 | +import org.apache.cxf.buslifecycle.BusLifeCycleListener; |
| 38 | +import org.apache.cxf.buslifecycle.BusLifeCycleManager; |
| 39 | +import org.apache.cxf.common.logging.LogUtils; |
| 40 | + |
| 41 | +public final class DelayedCachedOutputStreamCleaner implements CachedOutputStreamCleaner, BusLifeCycleListener { |
| 42 | + private static final Logger LOG = LogUtils.getL7dLogger(DelayedCachedOutputStreamCleaner.class); |
| 43 | + private static final long MIN_DELAY = 2000; /* 2 seconds */ |
| 44 | + private static final DelayedCleaner NOOP_CLEANER = new DelayedCleaner() { |
| 45 | + // NOOP |
| 46 | + }; |
| 47 | + |
| 48 | + private DelayedCleaner cleaner = NOOP_CLEANER; |
| 49 | + |
| 50 | + private interface DelayedCleaner extends CachedOutputStreamCleaner, Closeable { |
| 51 | + @Override |
| 52 | + default void register(Closeable closeable) { |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + default void unregister(Closeable closeable) { |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + default void close() { |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + default void clean() { |
| 65 | + } |
| 66 | + |
| 67 | + default void forceClean() { |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private static final class DelayedCleanerImpl implements DelayedCleaner { |
| 72 | + private final long delay; /* default is 30 minutes, in milliseconds */ |
| 73 | + private final DelayQueue<DelayedCloseable> queue = new DelayQueue<>(); |
| 74 | + private final Timer timer; |
| 75 | + |
| 76 | + DelayedCleanerImpl(final long delay) { |
| 77 | + this.delay = delay; |
| 78 | + this.timer = new Timer("DelayedCachedOutputStreamCleaner", true); |
| 79 | + this.timer.scheduleAtFixedRate(new TimerTask() { |
| 80 | + @Override |
| 81 | + public void run() { |
| 82 | + clean(); |
| 83 | + } |
| 84 | + }, 0, Math.max(MIN_DELAY, delay >> 1)); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void register(Closeable closeable) { |
| 89 | + queue.put(new DelayedCloseable(closeable, delay)); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void unregister(Closeable closeable) { |
| 94 | + queue.remove(new DelayedCloseable(closeable, delay)); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void clean() { |
| 99 | + final Collection<DelayedCloseable> closeables = new ArrayList<>(); |
| 100 | + queue.drainTo(closeables); |
| 101 | + clean(closeables); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void forceClean() { |
| 106 | + clean(queue); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void close() { |
| 111 | + timer.cancel(); |
| 112 | + queue.clear(); |
| 113 | + } |
| 114 | + |
| 115 | + private void clean(Collection<DelayedCloseable> closeables) { |
| 116 | + final Iterator<DelayedCloseable> iterator = closeables.iterator(); |
| 117 | + while (iterator.hasNext()) { |
| 118 | + final DelayedCloseable next = iterator.next(); |
| 119 | + try { |
| 120 | + iterator.remove(); |
| 121 | + LOG.warning("Unclosed (leaked?) stream detected: " + next.closeable); |
| 122 | + next.closeable.close(); |
| 123 | + } catch (final IOException | RuntimeException ex) { |
| 124 | + LOG.warning("Unable to close (leaked?) stream: " + ex.getMessage()); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private static final class DelayedCloseable implements Delayed { |
| 131 | + private final Closeable closeable; |
| 132 | + private final long expireAt; |
| 133 | + |
| 134 | + DelayedCloseable(final Closeable closeable, final long delay) { |
| 135 | + this.closeable = closeable; |
| 136 | + this.expireAt = System.nanoTime() + delay; |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public int compareTo(Delayed o) { |
| 141 | + return Long.compare(expireAt, ((DelayedCloseable) o).expireAt); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public long getDelay(TimeUnit unit) { |
| 146 | + return unit.convert(expireAt - System.nanoTime(), TimeUnit.NANOSECONDS); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public int hashCode() { |
| 151 | + return Objects.hash(closeable); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public boolean equals(Object obj) { |
| 156 | + if (this == obj) { |
| 157 | + return true; |
| 158 | + } |
| 159 | + |
| 160 | + if (obj == null) { |
| 161 | + return false; |
| 162 | + } |
| 163 | + |
| 164 | + if (getClass() != obj.getClass()) { |
| 165 | + return false; |
| 166 | + } |
| 167 | + |
| 168 | + final DelayedCloseable other = (DelayedCloseable) obj; |
| 169 | + return Objects.equals(closeable, other.closeable); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + @Resource |
| 174 | + public void setBus(Bus bus) { |
| 175 | + Number delayValue = null; |
| 176 | + BusLifeCycleManager busLifeCycleManager = null; |
| 177 | + |
| 178 | + if (bus != null) { |
| 179 | + delayValue = (Number) bus.getProperty(CachedConstants.CLEANER_DELAY_BUS_PROP); |
| 180 | + busLifeCycleManager = bus.getExtension(BusLifeCycleManager.class); |
| 181 | + } |
| 182 | + |
| 183 | + if (cleaner != null) { |
| 184 | + cleaner.close(); |
| 185 | + } |
| 186 | + |
| 187 | + if (delayValue == null) { |
| 188 | + // Default delay is set to 30 mins |
| 189 | + cleaner = new DelayedCleanerImpl(TimeUnit.MILLISECONDS.convert(30, TimeUnit.MINUTES)); |
| 190 | + } else { |
| 191 | + final long value = delayValue.longValue(); |
| 192 | + if (value > 0 && value >= MIN_DELAY) { |
| 193 | + cleaner = new DelayedCleanerImpl(value); /* already in milliseconds */ |
| 194 | + } else { |
| 195 | + cleaner = NOOP_CLEANER; |
| 196 | + if (value != 0) { |
| 197 | + throw new IllegalArgumentException("The value of " + CachedConstants.CLEANER_DELAY_BUS_PROP |
| 198 | + + " property is invalid: " + value + " (should be >= " + MIN_DELAY + ", 0 to deactivate)"); |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + if (busLifeCycleManager != null) { |
| 204 | + busLifeCycleManager.registerLifeCycleListener(this); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + @Override |
| 209 | + public void register(Closeable closeable) { |
| 210 | + cleaner.register(closeable); |
| 211 | + } |
| 212 | + |
| 213 | + @Override |
| 214 | + public void unregister(Closeable closeable) { |
| 215 | + cleaner.unregister(closeable); |
| 216 | + } |
| 217 | + |
| 218 | + @Override |
| 219 | + public void clean() { |
| 220 | + cleaner.clean(); |
| 221 | + } |
| 222 | + |
| 223 | + @Override |
| 224 | + public void initComplete() { |
| 225 | + } |
| 226 | + |
| 227 | + @Override |
| 228 | + public void postShutdown() { |
| 229 | + } |
| 230 | + |
| 231 | + @Override |
| 232 | + public void preShutdown() { |
| 233 | + cleaner.close(); |
| 234 | + } |
| 235 | + |
| 236 | + public void forceClean() { |
| 237 | + cleaner.forceClean(); |
| 238 | + } |
| 239 | +} |
0 commit comments