01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19:
20: package org.apache.axis2.jaxws.utility;
21:
22: import java.security.PrivilegedActionException;
23: import java.security.PrivilegedExceptionAction;
24: import java.util.concurrent.ThreadFactory;
25:
26: import org.apache.axis2.java.security.AccessController;
27: import org.apache.axis2.jaxws.ExceptionFactory;
28: import org.apache.commons.logging.Log;
29: import org.apache.commons.logging.LogFactory;
30:
31: /**
32: * Factory to create threads in the ThreadPool Executor. We provide a factory so
33: * the threads can be set as daemon threads so that they do not prevent the JVM from
34: * exiting normally when the main client thread is finished.
35: *
36: */
37: public class JAXWSThreadFactory implements ThreadFactory {
38: private static final Log log = LogFactory
39: .getLog(JAXWSThreadFactory.class);
40: private static int groupNumber = 0;
41: private int threadNumber = 0;
42: // We put the threads into a unique thread group only for ease of identifying them
43: private ThreadGroup threadGroup = null;
44:
45: public Thread newThread(final Runnable r) {
46: if (threadGroup == null) {
47: try {
48: threadGroup = (ThreadGroup) AccessController
49: .doPrivileged(new PrivilegedExceptionAction() {
50: public Object run() {
51: return new ThreadGroup(
52: "JAX-WS Default Executor Group "
53: + groupNumber++);
54: }
55: });
56: } catch (PrivilegedActionException e) {
57: if (log.isDebugEnabled()) {
58: log
59: .debug("Exception thrown from AccessController: "
60: + e);
61: }
62: throw ExceptionFactory.makeWebServiceException(e
63: .getException());
64: }
65: }
66:
67: threadNumber++;
68: Thread returnThread = null;
69: try {
70: returnThread = (Thread) AccessController
71: .doPrivileged(new PrivilegedExceptionAction() {
72: public Object run() {
73: Thread newThread = new Thread(threadGroup,
74: r);
75: newThread.setDaemon(true);
76: return newThread;
77: }
78: });
79: } catch (PrivilegedActionException e) {
80: if (log.isDebugEnabled()) {
81: log.debug("Exception thrown from AccessController: "
82: + e);
83: }
84: throw ExceptionFactory.makeWebServiceException(e
85: .getException());
86: }
87:
88: return returnThread;
89: }
90: }
|