Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit d2fc3d5

Browse files
authored
Add Wrapper for WriteListener. (#1984)
* Add Wrapper for WriteListener. * add try block after attach context and detach context in finally block.
1 parent f77bba1 commit d2fc3d5

File tree

3 files changed

+143
-1
lines changed

3 files changed

+143
-1
lines changed

contrib/http_servlet/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ description = 'OpenCensus Http Servlet Plugin'
1010
}
1111

1212
dependencies {
13+
compile libraries.grpc_context
1314
compile project(':opencensus-api')
1415
compile project(':opencensus-contrib-http-util')
1516

16-
compile "javax.servlet:javax.servlet-api:3.0.1"
17+
compile "javax.servlet:javax.servlet-api:3.1.0"
1718

1819
signature "org.codehaus.mojo.signature:java17:1.0@signature"
1920
signature "net.sf.androidscents.signature:android-api-level-14:4.0_r4@signature"
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2019, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opencensus.contrib.http.servlet;
18+
19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
21+
import io.grpc.Context;
22+
import io.opencensus.common.ExperimentalApi;
23+
import java.io.IOException;
24+
import javax.servlet.WriteListener;
25+
26+
/**
27+
* This class is a wrapper class for {@link WriteListener}. It facilitates executing asynchronous
28+
* onWritePossible method in the original context at the time of creating the listener.
29+
*
30+
* @since 0.25.0
31+
*/
32+
@ExperimentalApi
33+
public class WriteListenerWrapper implements WriteListener {
34+
private final io.grpc.Context context;
35+
private final WriteListener writeListener;
36+
37+
/**
38+
* Creates an instance of {@code WriteListenerWrapper}. It saves current {@link Context} at the
39+
* time of creation.
40+
*
41+
* @param writeListener {@link WriteListener} object being wrapped.
42+
* @since 0.25.0
43+
*/
44+
public WriteListenerWrapper(WriteListener writeListener) {
45+
checkNotNull(writeListener, "WriteListener is null");
46+
context = io.grpc.Context.current();
47+
this.writeListener = writeListener;
48+
}
49+
50+
/**
51+
* It executes onWritePossible() method of the object being wrapped in the saved context. It saves
52+
* current context before executing the method and restores it after it is finished executing.
53+
*
54+
* @since 0.25.0
55+
*/
56+
@Override
57+
public void onWritePossible() throws IOException {
58+
Context previousContext = context.attach();
59+
try {
60+
writeListener.onWritePossible();
61+
} finally {
62+
context.detach(previousContext);
63+
}
64+
}
65+
66+
@Override
67+
public void onError(final Throwable t) {
68+
writeListener.onError(t);
69+
}
70+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2019, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opencensus.contrib.http.servlet;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import io.grpc.Context;
22+
import io.grpc.Context.Key;
23+
import java.io.IOException;
24+
import javax.servlet.ServletException;
25+
import javax.servlet.WriteListener;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.junit.runners.JUnit4;
29+
30+
/** Unit tests for {@link WriteListenerWrapper}. */
31+
@RunWith(JUnit4.class)
32+
public class WriteListenerWrapperTest {
33+
34+
@Test
35+
public void testWriteOnPossibleWithContext() throws IOException, ServletException {
36+
37+
Key<String> key = Context.<String>key("test-key");
38+
Context curr = Context.current();
39+
assertThat(curr).isNotNull();
40+
final Context parentContext = curr.withValue(key, "parent");
41+
assertThat(parentContext).isNotNull();
42+
43+
Context prev = parentContext.attach();
44+
try {
45+
WriteListenerWrapper writeListener =
46+
new WriteListenerWrapper(
47+
new WriteListener() {
48+
@Override
49+
public void onWritePossible() throws IOException {
50+
Context curr = Context.current();
51+
assertThat(curr).isNotNull();
52+
assertThat(curr).isEqualTo(parentContext);
53+
}
54+
55+
@Override
56+
public void onError(Throwable t) {}
57+
});
58+
59+
Context childContext = parentContext.withValue(key, "child");
60+
assertThat(childContext).isNotNull();
61+
assertThat(childContext.attach()).isNotNull();
62+
try {
63+
writeListener.onWritePossible();
64+
} finally {
65+
childContext.detach(parentContext);
66+
}
67+
} finally {
68+
parentContext.detach(prev);
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)