Skip to content

Commit 2759de3

Browse files
authored
Merge pull request #395 from rpmoore/io_debug_logging
Adding a ObjectChannelBuilder and a SeekableByteChannel to add extra …
2 parents aa6bb40 + d51d9f1 commit 2759de3

File tree

3 files changed

+134
-1
lines changed

3 files changed

+134
-1
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
allprojects {
1717
group = 'com.spectralogic.ds3'
18-
version = '3.2.9-RC1'
18+
version = '3.2.9-RC2'
1919
}
2020

2121
subprojects {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* ****************************************************************************
3+
* Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the "license" file accompanying this file.
10+
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
* ****************************************************************************
14+
*/
15+
16+
package com.spectralogic.ds3client.helpers.channelbuilders;
17+
18+
import com.spectralogic.ds3client.helpers.Ds3ClientHelpers;
19+
import com.spectralogic.ds3client.utils.LoggingSeekableByteChannel;
20+
21+
import java.io.IOException;
22+
import java.nio.channels.SeekableByteChannel;
23+
24+
25+
/**
26+
* This ObjectChannelBuilder is used to wrap an existing ObjectChannelBuilder and wrap all the channels it creates
27+
* in a logging channel to log how many bytes were read or written on each call. This should only be used
28+
* for debugging and should not be used in a production deployment.
29+
*/
30+
public class IoLoggingChannelBuilder implements Ds3ClientHelpers.ObjectChannelBuilder {
31+
32+
public static final String IO_CHANNEL_DEFAULT_NAME = "IoChannel";
33+
34+
private final Ds3ClientHelpers.ObjectChannelBuilder wrappedObjectChannelBuilder;
35+
private final String streamName;
36+
37+
public IoLoggingChannelBuilder(final Ds3ClientHelpers.ObjectChannelBuilder wrappedObjectChannelBuilder, final String streamName) {
38+
this.wrappedObjectChannelBuilder = wrappedObjectChannelBuilder;
39+
this.streamName = streamName;
40+
}
41+
42+
public IoLoggingChannelBuilder(final Ds3ClientHelpers.ObjectChannelBuilder wrappedObjectChannelBuilder) {
43+
this(wrappedObjectChannelBuilder, IO_CHANNEL_DEFAULT_NAME);
44+
}
45+
46+
@Override
47+
public SeekableByteChannel buildChannel(final String key) throws IOException {
48+
49+
final SeekableByteChannel seekableByteChannel = wrappedObjectChannelBuilder.buildChannel(key);
50+
51+
return new LoggingSeekableByteChannel(seekableByteChannel, streamName);
52+
}
53+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* ****************************************************************************
3+
* Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the "license" file accompanying this file.
10+
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
* ****************************************************************************
14+
*/
15+
16+
package com.spectralogic.ds3client.utils;
17+
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
21+
import java.io.IOException;
22+
import java.nio.ByteBuffer;
23+
import java.nio.channels.SeekableByteChannel;
24+
25+
public class LoggingSeekableByteChannel implements SeekableByteChannel {
26+
27+
private static final Logger LOG = LoggerFactory.getLogger(LoggingSeekableByteChannel.class);
28+
29+
private final SeekableByteChannel wrappedChannel;
30+
private final String channelName;
31+
32+
public LoggingSeekableByteChannel(final SeekableByteChannel wrappedChannel, final String channelName) {
33+
this.wrappedChannel = wrappedChannel;
34+
this.channelName = channelName;
35+
}
36+
37+
@Override
38+
public int read(final ByteBuffer dst) throws IOException {
39+
final int readCount = wrappedChannel.read(dst);
40+
LOG.info("Read {} bytes from {}", readCount, channelName);
41+
return readCount;
42+
}
43+
44+
@Override
45+
public int write(final ByteBuffer src) throws IOException {
46+
final int writeCount = wrappedChannel.write(src);
47+
LOG.info("Wrote {} bytes to {}", writeCount, channelName);
48+
return writeCount;
49+
}
50+
51+
@Override
52+
public long position() throws IOException {
53+
return wrappedChannel.position();
54+
}
55+
56+
@Override
57+
public SeekableByteChannel position(final long newPosition) throws IOException {
58+
return wrappedChannel.position(newPosition);
59+
}
60+
61+
@Override
62+
public long size() throws IOException {
63+
return wrappedChannel.size();
64+
}
65+
66+
@Override
67+
public SeekableByteChannel truncate(final long size) throws IOException {
68+
return wrappedChannel.truncate(size);
69+
}
70+
71+
@Override
72+
public boolean isOpen() {
73+
return wrappedChannel.isOpen();
74+
}
75+
76+
@Override
77+
public void close() throws IOException {
78+
wrappedChannel.close();
79+
}
80+
}

0 commit comments

Comments
 (0)