Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/ftptest/FtpServer.java
38855 views
1
/*
2
* Copyright (c) 2006, 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 {
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(int port) throws IOException {
63
listener = new ServerSocket(port);
64
}
65
66
/**
67
* Creates an instance of an FTP server that will listen on the default
68
* FTP port, usually 21.
69
*/
70
public FtpServer() throws IOException {
71
this(21);
72
}
73
74
public void setFileSystemHandler(FtpFileSystemHandler f) {
75
fsh = f;
76
}
77
78
public void setAuthHandler(FtpAuthHandler a) {
79
auth = a;
80
}
81
82
public void terminate() {
83
done = true;
84
interrupt();
85
}
86
87
public void killClients() {
88
synchronized (clients) {
89
int c = clients.size();
90
while (c > 0) {
91
c--;
92
FtpCommandHandler cl = clients.get(c);
93
cl.terminate();
94
cl.interrupt();
95
}
96
}
97
}
98
99
public int getLocalPort() {
100
return listener.getLocalPort();
101
}
102
103
void addClient(Socket client) {
104
FtpCommandHandler h = new FtpCommandHandler(client, this);
105
h.setHandlers(fsh, auth);
106
synchronized (clients) {
107
clients.add(h);
108
}
109
h.start();
110
}
111
112
void removeClient(FtpCommandHandler cl) {
113
synchronized (clients) {
114
clients.remove(cl);
115
}
116
}
117
118
public int activeClientsCount() {
119
synchronized (clients) {
120
return clients.size();
121
}
122
}
123
124
public void run() {
125
Socket client;
126
127
try {
128
while (!done) {
129
client = listener.accept();
130
addClient(client);
131
}
132
listener.close();
133
} catch (IOException e) {
134
135
}
136
}
137
}
138
139