|
Packit |
5e46da |
/*
|
|
Packit |
5e46da |
* This file is part of libbluray
|
|
Packit |
5e46da |
* Copyright (C) 2014 Petri Hintukainen <phintuka@users.sourceforge.net>
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* This library is free software; you can redistribute it and/or
|
|
Packit |
5e46da |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit |
5e46da |
* License as published by the Free Software Foundation; either
|
|
Packit |
5e46da |
* version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* This library is distributed in the hope that it will be useful,
|
|
Packit |
5e46da |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
5e46da |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
5e46da |
* Lesser General Public License for more details.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit |
5e46da |
* License along with this library. If not, see
|
|
Packit |
5e46da |
* <http://www.gnu.org/licenses/>.
|
|
Packit |
5e46da |
*/
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
package org.videolan;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
import java.lang.reflect.Method;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
import java.util.Iterator;
|
|
Packit |
5e46da |
import java.util.LinkedList;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
import java.security.AccessController;
|
|
Packit |
5e46da |
import java.security.PrivilegedActionException;
|
|
Packit |
5e46da |
import java.security.PrivilegedExceptionAction;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
import java.net.Socket;
|
|
Packit |
5e46da |
import java.net.SocketImpl;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
class BDJSockets {
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
protected BDJSockets() {
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
protected synchronized void add(Object obj) {
|
|
Packit |
5e46da |
if (!(obj instanceof SocketImpl)) {
|
|
Packit |
5e46da |
logger.error("expected SocketImpl, got " + obj);
|
|
Packit |
5e46da |
throw new Error("expected SocketImpl");
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
if (closed) {
|
|
Packit |
5e46da |
logger.error("Terminated Xlet tried to create socket at " + Logger.dumpStack());
|
|
Packit |
5e46da |
throw new Error("Terminated Xlet can not create sockets");
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
/* drop closed sockets */
|
|
Packit |
5e46da |
for (Iterator it = sockets.iterator(); it.hasNext(); ) {
|
|
Packit |
5e46da |
SocketImpl socketImpl = (SocketImpl)it.next();
|
|
Packit |
5e46da |
Socket socket = getSocket(socketImpl);
|
|
Packit |
5e46da |
if (socket != null && socket.isClosed()) {
|
|
Packit |
5e46da |
it.remove();
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
sockets.addLast(obj);
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
protected synchronized void closeAll() {
|
|
Packit |
5e46da |
closed = true;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
while (!sockets.isEmpty()) {
|
|
Packit |
5e46da |
SocketImpl socketImpl = (SocketImpl)sockets.removeFirst();
|
|
Packit |
5e46da |
Socket socket = getSocket(socketImpl);
|
|
Packit |
5e46da |
if (socket != null && !socket.isClosed()) {
|
|
Packit |
5e46da |
logger.warning("Closing " + socket);
|
|
Packit |
5e46da |
try {
|
|
Packit |
5e46da |
socket.close();
|
|
Packit |
5e46da |
} catch (Exception e) {
|
|
Packit |
5e46da |
logger.error("Failed to close socket: " + e);
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
private Socket getSocket(SocketImpl socketImpl) {
|
|
Packit |
5e46da |
try {
|
|
Packit |
5e46da |
final SocketImpl si = socketImpl;
|
|
Packit |
5e46da |
return (Socket)AccessController.doPrivileged(
|
|
Packit |
5e46da |
new PrivilegedExceptionAction() {
|
|
Packit |
5e46da |
public Object run() throws Exception {
|
|
Packit |
5e46da |
Method getSocket = SocketImpl.class.getDeclaredMethod("getSocket", new Class[0]);
|
|
Packit |
5e46da |
getSocket.setAccessible(true);
|
|
Packit |
5e46da |
return getSocket.invoke(si, new Object[0]);
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
});
|
|
Packit |
5e46da |
} catch (PrivilegedActionException e) {
|
|
Packit |
5e46da |
logger.error("Failed to get Socket: " + e.getException() + " at " + Logger.dumpStack());
|
|
Packit |
5e46da |
return null;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
private LinkedList sockets = new LinkedList();
|
|
Packit |
5e46da |
private boolean closed = false;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
private static final Logger logger = Logger.getLogger(BDJSockets.class.getName());
|
|
Packit |
5e46da |
}
|