-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CXF-7396: CachedOutputStream doesn't delete temp files
- Loading branch information
Showing
9 changed files
with
480 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
core/src/main/java/org/apache/cxf/io/CachedOutputStreamCleaner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.cxf.io; | ||
|
||
import java.io.Closeable; | ||
|
||
/** | ||
* The {@link Bus} extension to clean up unclosed {@link CachedOutputStream} instances (and alike) backed by | ||
* temporary files (leading to disk fill, see https://issues.apache.org/jira/browse/CXF-7396. | ||
*/ | ||
public interface CachedOutputStreamCleaner { | ||
/** | ||
* Run the clean up | ||
*/ | ||
void clean(); | ||
|
||
/** | ||
* Register the stream instance for the clean up | ||
*/ | ||
void unregister(Closeable closeable); | ||
|
||
/** | ||
* Unregister the stream instance from the clean up (closed properly) | ||
*/ | ||
void register(Closeable closeable); | ||
} |
194 changes: 194 additions & 0 deletions
194
core/src/main/java/org/apache/cxf/io/DelayedCachedOutputStreamCleaner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.cxf.io; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.Objects; | ||
import java.util.Timer; | ||
import java.util.TimerTask; | ||
import java.util.concurrent.DelayQueue; | ||
import java.util.concurrent.Delayed; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.logging.Logger; | ||
|
||
import org.apache.cxf.Bus; | ||
import org.apache.cxf.Bus.BusState; | ||
import org.apache.cxf.buslifecycle.BusLifeCycleListener; | ||
import org.apache.cxf.buslifecycle.BusLifeCycleManager; | ||
import org.apache.cxf.common.logging.LogUtils; | ||
|
||
public final class DelayedCachedOutputStreamCleaner implements CachedOutputStreamCleaner, BusLifeCycleListener { | ||
private static final Logger LOG = LogUtils.getL7dLogger(DelayedCachedOutputStreamCleaner.class); | ||
|
||
private final long delay; /* default is 30 minutes */ | ||
private final DelayQueue<DelayedCloseable> queue = new DelayQueue<>(); | ||
private final Timer timer; | ||
|
||
private static final class DelayedCloseable implements Delayed { | ||
private final Closeable closeable; | ||
private final long expiredAt; | ||
|
||
DelayedCloseable(final Closeable closeable, final long delay) { | ||
this.closeable = closeable; | ||
this.expiredAt = System.nanoTime() + delay; | ||
} | ||
|
||
@Override | ||
public int compareTo(Delayed o) { | ||
return Long.compare(getDelay(TimeUnit.NANOSECONDS), o.getDelay(TimeUnit.NANOSECONDS)); | ||
} | ||
|
||
@Override | ||
public long getDelay(TimeUnit unit) { | ||
return unit.convert(expiredAt - System.nanoTime(), TimeUnit.NANOSECONDS); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(closeable); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
|
||
if (obj == null) { | ||
return false; | ||
} | ||
|
||
if (getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
|
||
final DelayedCloseable other = (DelayedCloseable) obj; | ||
return Objects.equals(closeable, other.closeable); | ||
} | ||
} | ||
|
||
protected DelayedCachedOutputStreamCleaner(long delay, TimeUnit unit) { | ||
this.delay = TimeUnit.NANOSECONDS.convert(delay, unit); | ||
this.timer = new Timer("DelayedCachedOutputStreamCleaner", true); | ||
this.timer.scheduleAtFixedRate(new TimerTask() { | ||
@Override | ||
public void run() { | ||
clean(); | ||
} | ||
}, 0, TimeUnit.MILLISECONDS.convert(Math.max(1, delay >> 1), unit)); | ||
} | ||
|
||
@Override | ||
public void register(Closeable closeable) { | ||
queue.put(new DelayedCloseable(closeable, delay)); | ||
} | ||
|
||
@Override | ||
public void unregister(Closeable closeable) { | ||
queue.remove(new DelayedCloseable(closeable, delay)); | ||
} | ||
|
||
@Override | ||
public void clean() { | ||
final Collection<DelayedCloseable> closeables = new ArrayList<>(); | ||
queue.drainTo(closeables); | ||
clean(closeables); | ||
} | ||
|
||
@Override | ||
public void initComplete() { | ||
} | ||
|
||
@Override | ||
public void postShutdown() { | ||
} | ||
|
||
@Override | ||
public void preShutdown() { | ||
timer.cancel(); | ||
} | ||
|
||
public void forceClean() { | ||
clean(queue); | ||
} | ||
|
||
private void clean(Collection<DelayedCloseable> closeables) { | ||
final Iterator<DelayedCloseable> iterator = closeables.iterator(); | ||
while (iterator.hasNext()) { | ||
final DelayedCloseable next = iterator.next(); | ||
try { | ||
iterator.remove(); | ||
LOG.warning("Unclosed (leaked?) stream detected: " + next.closeable); | ||
next.closeable.close(); | ||
} catch (final IOException | RuntimeException ex) { | ||
LOG.warning("Unable to close (leaked?) stream: " + ex.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
public static CachedOutputStreamCleaner create(Bus bus) { | ||
Number delayValue = null; | ||
BusLifeCycleManager busLifeCycleManager = null; | ||
|
||
if (bus != null) { | ||
delayValue = (Number) bus.getProperty(CachedConstants.CLEANER_DELAY_BUS_PROP); | ||
if (bus.getState() == BusState.RUNNING) { | ||
busLifeCycleManager = bus.getExtension(BusLifeCycleManager.class); | ||
} | ||
} | ||
|
||
if (delayValue == null) { | ||
final DelayedCachedOutputStreamCleaner cleaner = | ||
new DelayedCachedOutputStreamCleaner(30, TimeUnit.MINUTES); | ||
if (busLifeCycleManager != null) { | ||
busLifeCycleManager.registerLifeCycleListener(cleaner); | ||
} | ||
return cleaner; | ||
} else { | ||
final long delay = delayValue.longValue(); | ||
if (delay > 0) { | ||
final DelayedCachedOutputStreamCleaner cleaner = | ||
new DelayedCachedOutputStreamCleaner(delay, TimeUnit.MILLISECONDS); | ||
if (busLifeCycleManager != null) { | ||
busLifeCycleManager.registerLifeCycleListener(cleaner); | ||
} | ||
return cleaner; | ||
} else { | ||
return new CachedOutputStreamCleaner() { | ||
@Override | ||
public void unregister(Closeable closeable) { | ||
} | ||
|
||
@Override | ||
public void register(Closeable closeable) { | ||
} | ||
|
||
@Override | ||
public void clean() { | ||
} | ||
}; /* noop */ | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.