jdk/src/share/classes/sun/net/httpserver/SelectorCache.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.net.httpserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Implements a cache of java.nio.channels.Selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * where Selectors are allocated on demand and placed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * in a temporary cache for a period of time, so they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * can be reused. If a period of between 2 and 4 minutes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * elapses without being used, then they are closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class SelectorCache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static SelectorCache cache = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private SelectorCache () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        freeSelectors = new LinkedList<SelectorWrapper>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        CacheCleaner c = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            new PrivilegedAction<CacheCleaner>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            public CacheCleaner run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                CacheCleaner cleaner = new CacheCleaner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                cleaner.setDaemon (true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                return cleaner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        c.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * factory method for creating single instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public static SelectorCache getSelectorCache () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        synchronized (SelectorCache.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            if (cache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                cache = new SelectorCache ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static class SelectorWrapper {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        private Selector sel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        private boolean deleteFlag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        private SelectorWrapper (Selector sel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            this.sel = sel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            this.deleteFlag = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        public Selector getSelector() { return sel;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        public boolean getDeleteFlag () {return deleteFlag;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        public void setDeleteFlag (boolean b) {deleteFlag = b;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /* list of free selectors. Can be re-allocated for a period
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * of time, after which if not allocated will be closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * and removed from the list (by CacheCleaner thread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    LinkedList<SelectorWrapper> freeSelectors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    synchronized Selector getSelector () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        SelectorWrapper wrapper = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        Selector selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (freeSelectors.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            wrapper = freeSelectors.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            selector = wrapper.getSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            selector = Selector.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    synchronized void freeSelector (Selector selector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        freeSelectors.add (new SelectorWrapper (selector));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /* Thread ensures that entries on freeSelector list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * remain there for at least 2 minutes and no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * than 4 minutes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    class CacheCleaner extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            long timeout = ServerConfig.getSelCacheTimeout() * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                try {Thread.sleep (timeout); } catch (Exception e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                synchronized (freeSelectors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    ListIterator<SelectorWrapper> l = freeSelectors.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    while (l.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        SelectorWrapper w = l.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        if (w.getDeleteFlag()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                            /* 2nd pass. Close the selector */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                w.getSelector().close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                            } catch (IOException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                            l.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                            /* 1st pass. Set the flag */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                            w.setDeleteFlag (true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
}