001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.rmi.runtime;
027:
028: import java.security.AccessController;
029: import java.security.Permission;
030: import java.security.PrivilegedAction;
031: import java.util.concurrent.ScheduledThreadPoolExecutor;
032: import java.util.concurrent.ThreadFactory;
033: import java.util.concurrent.TimeUnit;
034: import java.util.concurrent.atomic.AtomicInteger;
035: import java.util.logging.Level;
036: import sun.security.action.GetIntegerAction;
037:
038: /**
039: * RMI runtime implementation utilities.
040: *
041: * There is a single instance of this class, which can be obtained
042: * with a GetInstanceAction. Getting the instance requires
043: * RuntimePermission("sun.rmi.runtime.RuntimeUtil.getInstance")
044: * because the public methods of this class expose security-sensitive
045: * capabilities.
046: *
047: * @author Peter Jones
048: * @version 1.8, 07/05/05
049: **/
050: public final class RuntimeUtil {
051:
052: /** runtime package log */
053: private static final Log runtimeLog = Log.getLog("sun.rmi.runtime",
054: null, false);
055:
056: /** number of scheduler threads */
057: private static final int schedulerThreads = // default 1
058: AccessController.doPrivileged(new GetIntegerAction(
059: "sun.rmi.runtime.schedulerThreads", 1));
060:
061: /** permission required to get instance */
062: private static final Permission GET_INSTANCE_PERMISSION = new RuntimePermission(
063: "sun.rmi.runtime.RuntimeUtil.getInstance");
064:
065: /** the singleton instance of this class */
066: private static final RuntimeUtil instance = new RuntimeUtil();
067:
068: /** thread pool for scheduling delayed tasks */
069: private final ScheduledThreadPoolExecutor scheduler;
070:
071: private RuntimeUtil() {
072: scheduler = new ScheduledThreadPoolExecutor(schedulerThreads,
073: new ThreadFactory() {
074: private final AtomicInteger count = new AtomicInteger(
075: 0);
076:
077: public Thread newThread(Runnable runnable) {
078: try {
079: return AccessController
080: .doPrivileged(new NewThreadAction(
081: runnable,
082: "Scheduler("
083: + count
084: .getAndIncrement()
085: + ")", true));
086: } catch (Throwable t) {
087: runtimeLog.log(Level.WARNING,
088: "scheduler thread factory throws",
089: t);
090: return null;
091: }
092: }
093: });
094: /*
095: * We would like to allow the scheduler's threads to terminate
096: * if possible, but a bug in DelayQueue.poll can cause code
097: * like this to result in a busy loop:
098: */
099: // stpe.setKeepAliveTime(10, TimeUnit.MINUTES);
100: // stpe.allowCoreThreadTimeOut(true);
101: }
102:
103: /**
104: * A PrivilegedAction for getting the RuntimeUtil instance.
105: **/
106: public static class GetInstanceAction implements
107: PrivilegedAction<RuntimeUtil> {
108: /**
109: * Creates an action that returns the RuntimeUtil instance.
110: **/
111: public GetInstanceAction() {
112: }
113:
114: public RuntimeUtil run() {
115: return getInstance();
116: }
117: }
118:
119: private static RuntimeUtil getInstance() {
120: SecurityManager sm = System.getSecurityManager();
121: if (sm != null) {
122: sm.checkPermission(GET_INSTANCE_PERMISSION);
123: }
124: return instance;
125: }
126:
127: /**
128: * Returns the shared thread pool for scheduling delayed tasks.
129: *
130: * Note that the returned pool has limited concurrency, so
131: * submitted tasks should be short-lived and should not block.
132: **/
133: public ScheduledThreadPoolExecutor getScheduler() {
134: return scheduler;
135: }
136: }
|