325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.ws.api.pipe;
325N/A
325N/Aimport java.util.concurrent.Executor;
325N/Aimport java.util.concurrent.Executors;
325N/Aimport java.util.concurrent.ThreadFactory;
325N/Aimport java.util.concurrent.atomic.AtomicInteger;
325N/A
325N/Aimport com.sun.xml.internal.ws.api.message.Packet;
325N/A
325N/A/**
325N/A * Collection of {@link Fiber}s.
325N/A * Owns an {@link Executor} to run them.
325N/A *
325N/A * @author Kohsuke Kawaguchi
325N/A * @author Jitendra Kotamraju
325N/A */
325N/Apublic class Engine {
325N/A private volatile Executor threadPool;
325N/A public final String id;
325N/A
325N/A public Engine(String id, Executor threadPool) {
325N/A this(id);
325N/A this.threadPool = threadPool;
325N/A }
325N/A
325N/A public Engine(String id) {
325N/A this.id = id;
325N/A }
325N/A
325N/A public void setExecutor(Executor threadPool) {
325N/A this.threadPool = threadPool;
325N/A }
325N/A
325N/A void addRunnable(Fiber fiber) {
325N/A if(threadPool==null) {
325N/A synchronized(this) {
325N/A threadPool = Executors.newCachedThreadPool(new DaemonThreadFactory());
325N/A }
325N/A }
325N/A threadPool.execute(fiber);
325N/A }
325N/A
325N/A /**
325N/A * Creates a new fiber in a suspended state.
325N/A *
325N/A * <p>
325N/A * To start the returned fiber, call {@link Fiber#start(Tube,Packet,Fiber.CompletionCallback)}.
325N/A * It will start executing the given {@link Tube} with the given {@link Packet}.
325N/A *
325N/A * @return new Fiber
325N/A */
325N/A public Fiber createFiber() {
325N/A return new Fiber(this);
325N/A }
325N/A
325N/A private static class DaemonThreadFactory implements ThreadFactory {
325N/A static final AtomicInteger poolNumber = new AtomicInteger(1);
325N/A final ThreadGroup group;
325N/A final AtomicInteger threadNumber = new AtomicInteger(1);
325N/A final String namePrefix;
325N/A
325N/A DaemonThreadFactory() {
325N/A SecurityManager s = System.getSecurityManager();
325N/A group = (s != null)? s.getThreadGroup() :
325N/A Thread.currentThread().getThreadGroup();
325N/A namePrefix = "jaxws-engine-" +
325N/A poolNumber.getAndIncrement() +
325N/A "-thread-";
325N/A }
325N/A
325N/A public Thread newThread(Runnable r) {
325N/A Thread t = new Thread(group, r,
325N/A namePrefix + threadNumber.getAndIncrement(),
325N/A 0);
325N/A if (!t.isDaemon())
325N/A t.setDaemon(true);
325N/A if (t.getPriority() != Thread.NORM_PRIORITY)
325N/A t.setPriority(Thread.NORM_PRIORITY);
325N/A return t;
325N/A }
325N/A }
325N/A}