1 /*
   2  * Copyright (c) 1997, 2010, 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  */
  25 
  26 package com.sun.xml.internal.ws.api.pipe;
  27 
  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;
  32 
  33 import com.sun.xml.internal.ws.api.message.Packet;
  34 
  35 /**
  36  * Collection of {@link Fiber}s.
  37  * Owns an {@link Executor} to run them.
  38  *
  39  * @author Kohsuke Kawaguchi
  40  * @author Jitendra Kotamraju
  41  */
  42 public class Engine {
  43     private volatile Executor threadPool;
  44     public final String id;
  45 
  46     public Engine(String id, Executor threadPool) {
  47         this(id);
  48         this.threadPool = threadPool;
  49     }
  50 
  51     public Engine(String id) {
  52         this.id = id;
  53     }
  54 
  55     public void setExecutor(Executor threadPool) {
  56         this.threadPool = threadPool;
  57     }
  58 
  59     void addRunnable(Fiber fiber) {
  60         if(threadPool==null) {
  61             synchronized(this) {
  62                 threadPool = Executors.newCachedThreadPool(new DaemonThreadFactory());
  63             }
  64         }
  65         threadPool.execute(fiber);
  66     }
  67 
  68     /**
  69      * Creates a new fiber in a suspended state.
  70      *
  71      * <p>
  72      * To start the returned fiber, call {@link Fiber#start(Tube,Packet,Fiber.CompletionCallback)}.
  73      * It will start executing the given {@link Tube} with the given {@link Packet}.
  74      *
  75      * @return new Fiber
  76      */
  77     public Fiber createFiber() {
  78         return new Fiber(this);
  79     }
  80 
  81     private static class DaemonThreadFactory implements ThreadFactory {
  82         static final AtomicInteger poolNumber = new AtomicInteger(1);
  83         final ThreadGroup group;
  84         final AtomicInteger threadNumber = new AtomicInteger(1);
  85         final String namePrefix;
  86 
  87         DaemonThreadFactory() {
  88             SecurityManager s = System.getSecurityManager();
  89             group = (s != null)? s.getThreadGroup() :
  90                                  Thread.currentThread().getThreadGroup();
  91             namePrefix = "jaxws-engine-" +
  92                           poolNumber.getAndIncrement() +
  93                          "-thread-";
  94         }
  95 
  96         public Thread newThread(Runnable r) {
  97             Thread t = new Thread(group, r,
  98                                   namePrefix + threadNumber.getAndIncrement(),
  99                                   0);
 100             if (!t.isDaemon())
 101                 t.setDaemon(true);
 102             if (t.getPriority() != Thread.NORM_PRIORITY)
 103                 t.setPriority(Thread.NORM_PRIORITY);
 104             return t;
 105         }
 106     }
 107 }