OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / main / java / java / nio / channels / spi / SelectorProvider.java
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17
18 package java.nio.channels.spi;
19
20 import java.io.IOException;
21 import java.nio.channels.Channel;
22 import java.nio.channels.DatagramChannel;
23 import java.nio.channels.Pipe;
24 import java.nio.channels.ServerSocketChannel;
25 import java.nio.channels.SocketChannel;
26 import java.security.AccessController;
27 import java.security.PrivilegedAction;
28 import java.util.ServiceLoader;
29 import org.apache.harmony.nio.internal.SelectorProviderImpl;
30
31 /**
32  * {@code SelectorProvider} is an abstract base class that declares methods for
33  * providing instances of {@link DatagramChannel}, {@link Pipe},
34  * {@link java.nio.channels.Selector} , {@link ServerSocketChannel}, and
35  * {@link SocketChannel}. All the methods of this class are thread-safe.
36  *
37  * <p>A provider instance can be retrieved through a system property or the
38  * configuration file in a jar file; if no provider is available that way then
39  * the system default provider is returned.
40  */
41 public abstract class SelectorProvider {
42     private static SelectorProvider provider = null;
43
44     /**
45      * Constructs a new {@code SelectorProvider}.
46      *
47      * @throws SecurityException
48      *             if there is a security manager installed that does not permit
49      *             the runtime permission labeled "selectorProvider".
50      */
51     protected SelectorProvider() {
52         super();
53         if (null != System.getSecurityManager()) {
54             System.getSecurityManager().checkPermission(
55                     new RuntimePermission("selectorProvider"));
56         }
57     }
58
59     /**
60      * Gets a provider instance by executing the following steps when called for
61      * the first time:
62      * <ul>
63      * <li> if the system property "java.nio.channels.spi.SelectorProvider" is
64      * set, the value of this property is the class name of the provider
65      * returned; </li>
66      * <li>if there is a provider-configuration file named
67      * "java.nio.channels.spi.SelectorProvider" in META-INF/services of a jar
68      * file valid in the system class loader, the first class name is the
69      * provider's class name; </li>
70      * <li> otherwise, a system default provider will be returned.</li>
71      * </ul>
72      *
73      * @return the provider.
74      */
75     synchronized public static SelectorProvider provider() {
76         if (provider == null) {
77             provider = ServiceLoader.loadFromSystemProperty(SelectorProvider.class);
78             if (provider == null) {
79                 provider = loadProviderByJar();
80             }
81             if (provider == null) {
82                 provider = AccessController.doPrivileged(new PrivilegedAction<SelectorProvider>() {
83                     public SelectorProvider run() {
84                         return new SelectorProviderImpl();
85                     }
86                 });
87             }
88         }
89         return provider;
90     }
91
92     private static SelectorProvider loadProviderByJar() {
93         for (SelectorProvider provider : ServiceLoader.load(SelectorProvider.class, null)) {
94             return provider;
95         }
96         return null;
97     }
98
99     /**
100      * Creates a new open {@code DatagramChannel}.
101      *
102      * @return the new channel.
103      * @throws IOException
104      *             if an I/O error occurs.
105      */
106     public abstract DatagramChannel openDatagramChannel() throws IOException;
107
108     /**
109      * Creates a new {@code Pipe}.
110      *
111      * @return the new pipe.
112      * @throws IOException
113      *             if an I/O error occurs.
114      */
115     public abstract Pipe openPipe() throws IOException;
116
117     /**
118      * Creates a new selector.
119      *
120      * @return the new selector.
121      * @throws IOException
122      *             if an I/O error occurs.
123      */
124     public abstract AbstractSelector openSelector() throws IOException;
125
126     /**
127      * Creates a new open {@code ServerSocketChannel}.
128      *
129      * @return the new channel.
130      * @throws IOException
131      *             if an I/O error occurs.
132      */
133     public abstract ServerSocketChannel openServerSocketChannel()
134             throws IOException;
135
136     /**
137      * Create a new open {@code SocketChannel}.
138      *
139      * @return the new channel.
140      * @throws IOException
141      *             if an I/O error occurs.
142      */
143     public abstract SocketChannel openSocketChannel() throws IOException;
144
145     /**
146      * Returns the channel inherited from the instance that created this
147      * virtual machine.
148      *
149      * @return the channel.
150      * @throws IOException
151      *             if an I/O error occurs.
152      * @throws SecurityException
153      *             if there is a security manager installed that does not permit
154      *             the runtime permission labeled "selectorProvider".
155      */
156     public Channel inheritedChannel() throws IOException {
157         SecurityManager sm = System.getSecurityManager();
158         if (sm != null) {
159             sm.checkPermission(new RuntimePermission("inheritedChannel"));
160         }
161         // Android never has stdin/stdout connected to a socket.
162         return null;
163     }
164 }