001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.util;
021:
022: import org.apache.axis2.AxisFault;
023: import org.apache.axis2.context.ConfigurationContext;
024: import org.apache.axis2.context.MessageContext;
025:
026: import java.util.LinkedList;
027: import java.util.List;
028: import java.util.ListIterator;
029:
030: /**
031: * This is a utility class to make it easier/cleaner for user programming
032: * model-level implementations (e.g. the Axis2 JAX-WS code) to invoke the
033: * ThreadContextMigrators.
034: */
035: public class ThreadContextMigratorUtil {
036: /**
037: * Register a new ThreadContextMigrator.
038: *
039: * @param configurationContext
040: * @param threadContextMigratorListID The name of the property in the
041: * ConfigurationContext that contains
042: * the list of migrators.
043: * @param migrator
044: */
045: public static void addThreadContextMigrator(
046: ConfigurationContext configurationContext,
047: String threadContextMigratorListID,
048: ThreadContextMigrator migrator) {
049: List migratorList = (List) configurationContext
050: .getProperty(threadContextMigratorListID);
051:
052: if (migratorList == null) {
053: migratorList = new LinkedList();
054: configurationContext.setProperty(
055: threadContextMigratorListID, migratorList);
056: }
057:
058: migratorList.add(migrator);
059: }
060:
061: /**
062: * Activate any registered ThreadContextMigrators to move context info
063: * to the thread of execution.
064: *
065: * @param threadContextMigratorListID The name of the property in the
066: * ConfigurationContext that contains
067: * the list of migrators.
068: * @param msgContext
069: * @throws AxisFault
070: */
071: public static void performMigrationToThread(
072: String threadContextMigratorListID,
073: MessageContext msgContext) throws AxisFault {
074: if (msgContext == null) {
075: return;
076: }
077:
078: List migratorList = (List) msgContext.getConfigurationContext()
079: .getProperty(threadContextMigratorListID);
080:
081: if (migratorList != null) {
082: ListIterator threadContextMigrators = migratorList
083: .listIterator();
084: while (threadContextMigrators.hasNext()) {
085: ((ThreadContextMigrator) threadContextMigrators.next())
086: .migrateContextToThread(msgContext);
087: }
088: }
089: }
090:
091: /**
092: * Activate any registered ThreadContextMigrators to remove information
093: * from the thread of execution if necessary.
094: *
095: * @param threadContextMigratorListID The name of the property in the
096: * ConfigurationContext that contains
097: * the list of migrators.
098: * @param msgContext
099: */
100: public static void performThreadCleanup(
101: String threadContextMigratorListID,
102: MessageContext msgContext) {
103: if (msgContext == null) {
104: return;
105: }
106:
107: List migratorList = (List) msgContext.getConfigurationContext()
108: .getProperty(threadContextMigratorListID);
109:
110: if (migratorList != null) {
111: ListIterator threadContextMigrators = migratorList
112: .listIterator();
113: while (threadContextMigrators.hasNext()) {
114: ((ThreadContextMigrator) threadContextMigrators.next())
115: .cleanupThread(msgContext);
116: }
117: }
118: }
119:
120: /**
121: * Activate any registered ThreadContextMigrators to move info from the
122: * thread of execution into the context.
123: *
124: * @param threadContextMigratorListID The name of the property in the
125: * ConfigurationContext that contains
126: * the list of migrators.
127: * @param msgContext
128: * @throws AxisFault
129: */
130: public static void performMigrationToContext(
131: String threadContextMigratorListID,
132: MessageContext msgContext) throws AxisFault {
133: if (msgContext == null) {
134: return;
135: }
136:
137: List migratorList = (List) msgContext.getConfigurationContext()
138: .getProperty(threadContextMigratorListID);
139:
140: if (migratorList != null) {
141: ListIterator threadContextMigrators = migratorList
142: .listIterator();
143: while (threadContextMigrators.hasNext()) {
144: ((ThreadContextMigrator) threadContextMigrators.next())
145: .migrateThreadToContext(msgContext);
146: }
147: }
148: }
149:
150: /**
151: * Activate any registered ThreadContextMigrators to remove information from
152: * the context if necessary.
153: *
154: * @param threadContextMigratorListID The name of the property in the
155: * ConfigurationContext that contains
156: * the list of migrators.
157: * @param msgContext
158: */
159: public static void performContextCleanup(
160: String threadContextMigratorListID,
161: MessageContext msgContext) {
162: if (msgContext == null) {
163: return;
164: }
165:
166: List migratorList = (List) msgContext.getConfigurationContext()
167: .getProperty(threadContextMigratorListID);
168:
169: if (migratorList != null) {
170: ListIterator threadContextMigrators = migratorList
171: .listIterator();
172: while (threadContextMigrators.hasNext()) {
173: ((ThreadContextMigrator) threadContextMigrators.next())
174: .cleanupContext(msgContext);
175: }
176: }
177: }
178: }
|