View Javadoc

1   /*
2    * Copyright 2006 the original author or 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  package org.springframework.ws.transport.context;
17  
18  /**
19   * Simple holder class that associates a <code>TransportContext</code> instance with the current thread. The
20   * <code>TransportContext</code> will be inherited by any child threads spawned by the current thread.
21   *
22   * @author Arjen Poutsma
23   * @see TransportContext
24   * @since 1.0.0
25   */
26  public abstract class TransportContextHolder {
27  
28      private static final ThreadLocal transportContextHolder = new TransportThreadLocal();
29  
30      /**
31       * Associate the given <code>TransportContext</code> with the current thread.
32       *
33       * @param transportContext the current transport context, or <code>null</code> to reset the thread-bound context
34       */
35      public static void setTransportContext(TransportContext transportContext) {
36          transportContextHolder.set(transportContext);
37      }
38  
39      /**
40       * Return the <code>TransportContext</code> associated with the current thread, if any.
41       *
42       * @return the current transport context, or <code>null</code> if none
43       */
44      public static TransportContext getTransportContext() {
45          return (TransportContext) transportContextHolder.get();
46      }
47  
48      private static class TransportThreadLocal extends ThreadLocal {
49  
50          public String toString() {
51              return "Transport State";
52          }
53      }
54  }