Thu, 31 Aug 2017 15:18:52 +0800
merge
1 /*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.xml.internal.ws.api.pipe;
28 import java.util.concurrent.Executor;
29 import java.util.concurrent.Executors;
30 import java.util.concurrent.ThreadFactory;
31 import java.util.concurrent.atomic.AtomicInteger;
33 import com.sun.xml.internal.ws.api.message.Packet;
34 import com.sun.xml.internal.ws.api.server.Container;
35 import com.sun.xml.internal.ws.api.server.ContainerResolver;
37 /**
38 * Collection of {@link Fiber}s.
39 * Owns an {@link Executor} to run them.
40 *
41 * @author Kohsuke Kawaguchi
42 * @author Jitendra Kotamraju
43 */
44 public class Engine {
45 private volatile Executor threadPool;
46 public final String id;
47 private final Container container;
49 String getId() { return id; }
50 Container getContainer() { return container; }
51 Executor getExecutor() { return threadPool; }
53 public Engine(String id, Executor threadPool) {
54 this(id, ContainerResolver.getDefault().getContainer(), threadPool);
55 }
57 public Engine(String id, Container container, Executor threadPool) {
58 this(id, container);
59 this.threadPool = threadPool != null ? wrap(threadPool) : null;
60 }
62 public Engine(String id) {
63 this(id, ContainerResolver.getDefault().getContainer());
64 }
66 public Engine(String id, Container container) {
67 this.id = id;
68 this.container = container;
69 }
71 public void setExecutor(Executor threadPool) {
72 this.threadPool = threadPool != null ? wrap(threadPool) : null;
73 }
75 void addRunnable(Fiber fiber) {
76 if(threadPool==null) {
77 synchronized(this) {
78 threadPool = wrap(Executors.newCachedThreadPool(new DaemonThreadFactory()));
79 }
80 }
81 threadPool.execute(fiber);
82 }
84 private Executor wrap(Executor ex) {
85 return ContainerResolver.getDefault().wrapExecutor(container, ex);
86 }
88 /**
89 * Creates a new fiber in a suspended state.
90 *
91 * <p>
92 * To start the returned fiber, call {@link Fiber#start(Tube,Packet,Fiber.CompletionCallback)}.
93 * It will start executing the given {@link Tube} with the given {@link Packet}.
94 *
95 * @return new Fiber
96 */
97 public Fiber createFiber() {
98 return new Fiber(this);
99 }
101 private static class DaemonThreadFactory implements ThreadFactory {
102 static final AtomicInteger poolNumber = new AtomicInteger(1);
103 final AtomicInteger threadNumber = new AtomicInteger(1);
104 final String namePrefix;
106 DaemonThreadFactory() {
107 namePrefix = "jaxws-engine-" + poolNumber.getAndIncrement() + "-thread-";
108 }
110 public Thread newThread(Runnable r) {
111 Thread t = new Thread(null, r, namePrefix + threadNumber.getAndIncrement(), 0);
112 if (!t.isDaemon()) {
113 t.setDaemon(true);
114 }
115 if (t.getPriority() != Thread.NORM_PRIORITY) {
116 t.setPriority(Thread.NORM_PRIORITY);
117 }
118 return t;
119 }
120 }
121 }