Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/ftptest/FtpServer.java
41152 views
1
/*
2
* Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.net.*;
25
import java.io.*;
26
import java.util.ArrayList;
27
28
/**
29
* This class implements a simple FTP server that can handle multiple
30
* connections concurrently. This is mostly meant as a test environment.
31
* You have to provide 2 handlers for it to be effective, one to simulate
32
* (or access) a filesystem and one to deal with authentication.
33
* See {@link FtpFileSystemHandler} and {@link FtpAuthHandler}.
34
*
35
* Since it is a subclass of Thread, you have to call <code>start()</code>
36
* To get it running.
37
*
38
* Usage example:<p>
39
*
40
* <code>
41
* FtpServer server = new FtpServer(0);
42
* int port = server.getLocalPort();
43
* server.setFileSystemHandler(myFSHandler);
44
* server.setAuthHandler(myAuthHandler);
45
* server.start();
46
* </code>
47
*
48
*/
49
50
public class FtpServer extends Thread implements AutoCloseable {
51
private ServerSocket listener = null;
52
private FtpFileSystemHandler fsh = null;
53
private FtpAuthHandler auth = null;
54
private boolean done = false;
55
private ArrayList<FtpCommandHandler> clients = new ArrayList<FtpCommandHandler>();
56
57
/**
58
* Creates an instance of an FTP server which will listen for incoming
59
* connections on the specified port. If the port is set to 0, it will
60
* automatically select an available ephemeral port.
61
*/
62
public FtpServer(InetAddress addr, int port) throws IOException {
63
listener = new ServerSocket();
64
listener.bind(new InetSocketAddress(addr, port));
65
}
66
67
/**
68
* Creates an instance of an FTP server which will listen for incoming
69
* connections on the specified port. If the port is set to 0, it will
70
* automatically select an available ephemeral port.
71
*/
72
public FtpServer(int port) throws IOException {
73
listener = new ServerSocket(port);
74
}
75
76
/**
77
* Creates an instance of an FTP server that will listen on the default
78
* FTP port, usually 21.
79
*/
80
public FtpServer() throws IOException {
81
this(21);
82
}
83
84
public void setFileSystemHandler(FtpFileSystemHandler f) {
85
fsh = f;
86
}
87
88
public void setAuthHandler(FtpAuthHandler a) {
89
auth = a;
90
}
91
92
public void terminate() {
93
done = true;
94
interrupt();
95
}
96
97
public void killClients() {
98
synchronized (clients) {
99
int c = clients.size();
100
while (c > 0) {
101
c--;
102
FtpCommandHandler cl = clients.get(c);
103
cl.terminate();
104
cl.interrupt();
105
}
106
}
107
}
108
109
public int getLocalPort() {
110
return listener.getLocalPort();
111
}
112
113
public InetAddress getInetAddress() {
114
return listener.getInetAddress();
115
}
116
117
public String getAuthority() {
118
InetAddress address = getInetAddress();
119
String hostaddr = address.isAnyLocalAddress()
120
? "localhost" : address.getHostAddress();
121
if (hostaddr.indexOf(':') > -1) {
122
hostaddr = "[" + hostaddr + "]";
123
}
124
return hostaddr + ":" + getLocalPort();
125
}
126
127
128
void addClient(Socket client) {
129
FtpCommandHandler h = new FtpCommandHandler(client, this);
130
h.setHandlers(fsh, auth);
131
synchronized (clients) {
132
clients.add(h);
133
}
134
h.start();
135
}
136
137
void removeClient(FtpCommandHandler cl) {
138
synchronized (clients) {
139
clients.remove(cl);
140
}
141
}
142
143
public int activeClientsCount() {
144
synchronized (clients) {
145
return clients.size();
146
}
147
}
148
149
public void run() {
150
Socket client;
151
152
try {
153
while (!done) {
154
client = listener.accept();
155
addClient(client);
156
}
157
listener.close();
158
} catch (IOException e) {
159
160
}
161
}
162
163
@Override
164
public void close() throws Exception {
165
terminate();
166
listener.close();
167
if (activeClientsCount() > 0) {
168
killClients();
169
}
170
}
171
}
172
173