1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package dalvik.system;
18 
19 import java.io.FileDescriptor;
20 import java.net.DatagramSocket;
21 import java.net.Socket;
22 import java.net.SocketException;
23 
24 /**
25  * Callbacks for socket assignment and reassignment.
26  *
27  * @hide
28  */
29 public abstract class SocketTagger {
30 
31     private static SocketTagger tagger = new SocketTagger() {
32         @Override public void tag(FileDescriptor socketDescriptor) throws SocketException {}
33         @Override public void untag(FileDescriptor socketDescriptor) throws SocketException {}
34     };
35 
36     /**
37      * Notified when {@code socketDescriptor} is either assigned to the current
38      * thread. The socket is either newly connected or reused from a connection
39      * pool. Implementations of this method should be thread-safe.
40      */
tag(FileDescriptor socketDescriptor)41     public abstract void tag(FileDescriptor socketDescriptor) throws SocketException;
42 
43     /**
44      * Notified when {@code socketDescriptor} is released from the current
45      * thread to a connection pool. Implementations of this method should be
46      * thread-safe.
47      *
48      * <p><strong>Note:</strong> this method will not be invoked when the socket
49      * is closed.
50      */
untag(FileDescriptor socketDescriptor)51     public abstract void untag(FileDescriptor socketDescriptor) throws SocketException;
52 
tag(Socket socket)53     public final void tag(Socket socket) throws SocketException {
54         if (!socket.isClosed()) {
55             tag(socket.getFileDescriptor$());
56         }
57     }
58 
untag(Socket socket)59     public final void untag(Socket socket) throws SocketException {
60         if (!socket.isClosed()) {
61             untag(socket.getFileDescriptor$());
62         }
63     }
64 
tag(DatagramSocket socket)65     public final void tag(DatagramSocket socket) throws SocketException {
66         if (!socket.isClosed()) {
67             tag(socket.getFileDescriptor$());
68         }
69     }
70 
untag(DatagramSocket socket)71     public final void untag(DatagramSocket socket) throws SocketException {
72         if (!socket.isClosed()) {
73             untag(socket.getFileDescriptor$());
74         }
75     }
76 
77     /**
78      * Sets this process' socket tagger to {@code tagger}.
79      */
set(SocketTagger tagger)80     public static synchronized void set(SocketTagger tagger) {
81         if (tagger == null) {
82             throw new NullPointerException("tagger == null");
83         }
84         SocketTagger.tagger = tagger;
85     }
86 
87     /**
88      * Returns this process socket tagger.
89      */
get()90     public static synchronized SocketTagger get() {
91         return tagger;
92     }
93 }
94