1 /* 2 * Copyright (c) 2005, 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 sun.rmi.runtime; 27 28 import java.security.AccessController; 29 import java.security.Permission; 30 import java.security.PrivilegedAction; 31 import java.util.concurrent.ScheduledThreadPoolExecutor; 32 import java.util.concurrent.ThreadFactory; 33 import java.util.concurrent.TimeUnit; 34 import java.util.concurrent.atomic.AtomicInteger; 35 import java.util.logging.Level; 36 import sun.security.action.GetIntegerAction; 37 38 /** 39 * RMI runtime implementation utilities. 40 * 41 * There is a single instance of this class, which can be obtained 42 * with a GetInstanceAction. Getting the instance requires 43 * RuntimePermission("sun.rmi.runtime.RuntimeUtil.getInstance") 44 * because the public methods of this class expose security-sensitive 45 * capabilities. 46 * 47 * @author Peter Jones 48 **/ 49 public final class RuntimeUtil { 50 51 /** runtime package log */ 52 private static final Log runtimeLog = 53 Log.getLog("sun.rmi.runtime", null, false); 54 55 /** number of scheduler threads */ 56 private static final int schedulerThreads = // default 1 57 AccessController.doPrivileged( 58 new GetIntegerAction("sun.rmi.runtime.schedulerThreads", 1)); 59 60 /** permission required to get instance */ 61 private static final Permission GET_INSTANCE_PERMISSION = 62 new RuntimePermission("sun.rmi.runtime.RuntimeUtil.getInstance"); 63 64 /** the singleton instance of this class */ 65 private static final RuntimeUtil instance = new RuntimeUtil(); 66 67 /** thread pool for scheduling delayed tasks */ 68 private final ScheduledThreadPoolExecutor scheduler; 69 70 private RuntimeUtil() { 71 scheduler = new ScheduledThreadPoolExecutor( 72 schedulerThreads, 73 new ThreadFactory() { 74 private final AtomicInteger count = new AtomicInteger(0); 75 public Thread newThread(Runnable runnable) { 76 try { 77 return AccessController.doPrivileged( 78 new NewThreadAction(runnable, 79 "Scheduler(" + count.getAndIncrement() + ")", 80 true)); 81 } catch (Throwable t) { 82 runtimeLog.log(Level.WARNING, 83 "scheduler thread factory throws", t); 84 return null; 85 } 86 } 87 }); 88 /* 89 * We would like to allow the scheduler's threads to terminate 90 * if possible, but a bug in DelayQueue.poll can cause code 91 * like this to result in a busy loop: 92 */ 93 // stpe.setKeepAliveTime(10, TimeUnit.MINUTES); 94 // stpe.allowCoreThreadTimeOut(true); 95 } 96 97 /** 98 * A PrivilegedAction for getting the RuntimeUtil instance. 99 **/ 100 public static class GetInstanceAction 101 implements PrivilegedAction<RuntimeUtil> 102 { 103 /** 104 * Creates an action that returns the RuntimeUtil instance. 105 **/ 106 public GetInstanceAction() { 107 } 108 109 public RuntimeUtil run() { 110 return getInstance(); 111 } 112 } 113 114 private static RuntimeUtil getInstance() { 115 SecurityManager sm = System.getSecurityManager(); 116 if (sm != null) { 117 sm.checkPermission(GET_INSTANCE_PERMISSION); 118 } 119 return instance; 120 } 121 122 /** 123 * Returns the shared thread pool for scheduling delayed tasks. 124 * 125 * Note that the returned pool has limited concurrency, so 126 * submitted tasks should be short-lived and should not block. 127 **/ 128 public ScheduledThreadPoolExecutor getScheduler() { 129 return scheduler; 130 } 131 }