Author: ay
Date: Wed Nov 28 18:27:47 2012
New Revision: 1414850
URL: http://svn.apache.org/viewvc?rev=1414850&view=rev
Log:
[CXF-4661] Make CachedOutputStream configurable using the Bus properties
Modified:
cxf/trunk/api/src/main/java/org/apache/cxf/io/CachedOutputStream.java
cxf/trunk/api/src/test/java/org/apache/cxf/io/CachedOutputStreamTest.java
Modified: cxf/trunk/api/src/main/java/org/apache/cxf/io/CachedOutputStream.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/api/src/main/java/org/apache/cxf/io/CachedOutputStream.java?rev=1414850&r1=1414849&r2=1414850&view=diff
==============================================================================
--- cxf/trunk/api/src/main/java/org/apache/cxf/io/CachedOutputStream.java
(original)
+++ cxf/trunk/api/src/main/java/org/apache/cxf/io/CachedOutputStream.java Wed
Nov 28 18:27:47 2012
@@ -45,6 +45,8 @@ import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;
+import org.apache.cxf.Bus;
+import org.apache.cxf.BusFactory;
import org.apache.cxf.common.util.SystemPropertyAction;
import org.apache.cxf.helpers.FileUtils;
import org.apache.cxf.helpers.IOUtils;
@@ -100,6 +102,7 @@ public class CachedOutputStream extends
public CachedOutputStream(PipedInputStream stream) throws IOException {
currentStream = new PipedOutputStream(stream);
inmem = true;
+ readBusProperties();
}
public CachedOutputStream() {
@@ -110,6 +113,30 @@ public class CachedOutputStream extends
this.threshold = threshold;
currentStream = new LoadingByteArrayOutputStream(2048);
inmem = true;
+ readBusProperties();
+ }
+
+ private void readBusProperties() {
+ Bus b = BusFactory.getDefaultBus(false);
+ if (b != null) {
+ String v = getBusProperty(b,
"bus.io.CachedOutputStream.Threshold", null);
+ if (v != null && threshold == defaultThreshold) {
+ threshold = Integer.parseInt(v);
+ }
+ v = getBusProperty(b, "bus.io.CachedOutputStream.MaxSize", null);
+ if (v != null) {
+ maxSize = Integer.parseInt(v);
+ }
+ v = getBusProperty(b,
"bus.io.CachedOutputStream.CipherTransformation", null);
+ if (v != null) {
+ cipherTransformation = v;
+ }
+ }
+ }
+
+ private static String getBusProperty(Bus b, String key, String dflt) {
+ String v = (String)b.getProperty(key);
+ return v != null ? v : dflt;
}
public void holdTempFile() {
Modified:
cxf/trunk/api/src/test/java/org/apache/cxf/io/CachedOutputStreamTest.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/api/src/test/java/org/apache/cxf/io/CachedOutputStreamTest.java?rev=1414850&r1=1414849&r2=1414850&view=diff
==============================================================================
--- cxf/trunk/api/src/test/java/org/apache/cxf/io/CachedOutputStreamTest.java
(original)
+++ cxf/trunk/api/src/test/java/org/apache/cxf/io/CachedOutputStreamTest.java
Wed Nov 28 18:27:47 2012
@@ -24,6 +24,11 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
+import org.apache.cxf.Bus;
+import org.apache.cxf.BusFactory;
+import org.easymock.EasyMock;
+import org.easymock.IMocksControl;
+
import org.junit.Assert;
import org.junit.Test;
@@ -114,6 +119,39 @@ public class CachedOutputStreamTest exte
assertFalse("file is not deleted", tmpfile.exists());
}
+ @Test
+ public void testUseBusProps() throws Exception {
+ Bus oldbus = BusFactory.getDefaultBus(false);
+ try {
+ CachedOutputStream cos = new CachedOutputStream();
+ cos.write("Hello World!".getBytes());
+ cos.flush();
+ assertNull("expects no tmp file", cos.getTempFile());
+ cos.close();
+
+ IMocksControl control = EasyMock.createControl();
+
+ Bus b = control.createMock(Bus.class);
+
EasyMock.expect(b.getProperty("bus.io.CachedOutputStream.Threshold")).andReturn("4");
+
EasyMock.expect(b.getProperty("bus.io.CachedOutputStream.MaxSize")).andReturn(null);
+
EasyMock.expect(b.getProperty("bus.io.CachedOutputStream.CipherTransformation")).andReturn(null);
+
+ BusFactory.setDefaultBus(b);
+
+ control.replay();
+
+ cos = new CachedOutputStream();
+ cos.write("Hello World!".getBytes());
+ cos.flush();
+ assertNotNull("expects a tmp file", cos.getTempFile());
+ cos.close();
+
+ control.verify();
+ } finally {
+ BusFactory.setDefaultBus(oldbus);
+ }
+ }
+
private static String readFromStream(InputStream is) throws IOException {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
try {