| Method from org.apache.tomcat.util.net.AprEndpoint Detail: |
protected AprEndpoint.Worker createWorkerThread() {
synchronized (workers) {
if (workers.size() > 0) {
curThreadsBusy++;
return ((Worker) workers.pop());
}
if ((maxThreads > 0) && (curThreads < maxThreads)) {
curThreadsBusy++;
return (newWorkerThread());
} else {
if (maxThreads < 0) {
curThreadsBusy++;
return (newWorkerThread());
} else {
return (null);
}
}
}
}
Create (or allocate) and return an available processor for use in
processing a specific HTTP request, if possible. If the maximum
allowed processors have already been created and are in use, return
null instead. |
public void destroy() throws Exception {
if (running) {
stop();
}
Pool.destroy(serverSockPool);
serverSockPool = 0;
// Close server socket
Socket.close(serverSock);
serverSock = 0;
sslContext = 0;
// Close all APR memory pools and resources
Pool.destroy(rootPool);
rootPool = 0;
initialized = false ;
}
Deallocate APR memory pools, and close server socket. |
public InetAddress getAddress() {
return address;
}
|
public int getBacklog() {
return backlog;
}
|
public int getCurrentThreadCount() {
return curThreads;
}
Return the amount of threads that are managed by the pool. |
public int getCurrentThreadsBusy() {
return curThreadsBusy;
}
Return the amount of threads currently busy. |
public boolean getDaemon() {
return daemon;
}
|
public int getFirstReadTimeout() {
return firstReadTimeout;
}
|
public AprEndpoint.Handler getHandler() {
return handler;
}
|
public int getKeepAliveCount() {
return keepAliveCount;
}
|
public int getMaxSpareThreads() {
return 0;
}
Dummy maxSpareThreads property. |
public int getMaxThreads() {
return maxThreads;
}
|
public int getMinSpareThreads() {
return 0;
}
Dummy minSpareThreads property. |
public String getName() {
return name;
}
|
public int getPollTime() {
return pollTime;
}
|
public AprEndpoint.Poller getPoller() {
return poller;
}
|
public int getPollerSize() {
return pollerSize;
}
|
public int getPort() {
return port;
}
|
public String getSSLCACertificateFile() {
return SSLCACertificateFile;
}
|
public String getSSLCACertificatePath() {
return SSLCACertificatePath;
}
|
public String getSSLCARevocationFile() {
return SSLCARevocationFile;
}
|
public String getSSLCARevocationPath() {
return SSLCARevocationPath;
}
|
public String getSSLCertificateChainFile() {
return SSLCertificateChainFile;
}
|
public String getSSLCertificateFile() {
return SSLCertificateFile;
}
|
public String getSSLCertificateKeyFile() {
return SSLCertificateKeyFile;
}
|
public String getSSLCipherSuite() {
return SSLCipherSuite;
}
|
public String getSSLEngine() {
return SSLEngine;
}
|
public String getSSLPassword() {
return SSLPassword;
}
|
public String getSSLProtocol() {
return SSLProtocol;
}
|
public String getSSLVerifyClient() {
return SSLVerifyClient;
}
|
public int getSSLVerifyDepth() {
return SSLVerifyDepth;
}
|
public AprEndpoint.Sendfile getSendfile() {
return sendfile;
}
|
public int getSendfileCount() {
return sendfileCount;
}
|
public int getSendfileSize() {
return sendfileSize;
}
|
protected int getSequence() {
return sequence++;
}
Get a sequence number used for thread naming. |
public int getSoLinger() {
return soLinger;
}
|
public int getSoTimeout() {
return soTimeout;
}
|
public boolean getTcpNoDelay() {
return tcpNoDelay;
}
|
public int getThreadPriority() {
return threadPriority;
}
|
public boolean getUseSendfile() {
return useSendfile;
}
|
protected AprEndpoint.Worker getWorkerThread() {
// Allocate a new worker thread
Worker workerThread = createWorkerThread();
while (workerThread == null) {
try {
synchronized (workers) {
workers.wait();
}
} catch (InterruptedException e) {
// Ignore
}
workerThread = createWorkerThread();
}
return workerThread;
}
Return a new worker thread, and block while to worker is available. |
public void init() throws Exception {
if (initialized)
return;
// Create the root APR memory pool
rootPool = Pool.create(0);
// Create the pool for the server socket
serverSockPool = Pool.create(rootPool);
// Create the APR address that will be bound
String addressStr = null;
if (address == null) {
addressStr = null;
} else {
addressStr = address.getHostAddress();
}
long inetAddress = Address.info(addressStr, Socket.APR_INET,
port, 0, rootPool);
// Create the APR server socket
serverSock = Socket.create(Socket.APR_INET, Socket.SOCK_STREAM,
Socket.APR_PROTO_TCP, rootPool);
if (OS.IS_UNIX) {
Socket.optSet(serverSock, Socket.APR_SO_REUSEADDR, 1);
}
// Deal with the firewalls that tend to drop the inactive sockets
Socket.optSet(serverSock, Socket.APR_SO_KEEPALIVE, 1);
// Bind the server socket
int ret = Socket.bind(serverSock, inetAddress);
if (ret != 0) {
throw new Exception(sm.getString("endpoint.init.bind", "" + ret));
}
// Start listening on the server socket
ret = Socket.listen(serverSock, backlog);
if (ret != 0) {
throw new Exception(sm.getString("endpoint.init.listen", "" + ret));
}
if (OS.IS_WIN32 || OS.IS_WIN64) {
// On Windows set the reuseaddr flag after the bind/listen
Socket.optSet(serverSock, Socket.APR_SO_REUSEADDR, 1);
}
// Sendfile usage on systems which don't support it cause major problems
if (useSendfile && !Library.APR_HAS_SENDFILE) {
log.warn(sm.getString("endpoint.sendfile.nosupport"));
useSendfile = false;
}
// Delay accepting of new connections until data is available
// Only Linux kernels 2.4 + have that implemented
// on other platforms this call is noop and will return APR_ENOTIMPL.
Socket.optSet(serverSock, Socket.APR_TCP_DEFER_ACCEPT, 1);
// Initialize SSL if needed
if (!"off".equalsIgnoreCase(SSLEngine)) {
// Initialize SSL
// FIXME: one per VM call ?
if ("on".equalsIgnoreCase(SSLEngine)) {
SSL.initialize(null);
} else {
SSL.initialize(SSLEngine);
}
// SSL protocol
int value = SSL.SSL_PROTOCOL_ALL;
if ("SSLv2".equalsIgnoreCase(SSLProtocol)) {
value = SSL.SSL_PROTOCOL_SSLV2;
} else if ("SSLv3".equalsIgnoreCase(SSLProtocol)) {
value = SSL.SSL_PROTOCOL_SSLV3;
} else if ("TLSv1".equalsIgnoreCase(SSLProtocol)) {
value = SSL.SSL_PROTOCOL_TLSV1;
} else if ("SSLv2+SSLv3".equalsIgnoreCase(SSLProtocol)) {
value = SSL.SSL_PROTOCOL_SSLV2 | SSL.SSL_PROTOCOL_SSLV3;
}
// Create SSL Context
sslContext = SSLContext.make(rootPool, value, SSL.SSL_MODE_SERVER);
// List the ciphers that the client is permitted to negotiate
SSLContext.setCipherSuite(sslContext, SSLCipherSuite);
// Load Server key and certificate
SSLContext.setCertificate(sslContext, SSLCertificateFile, SSLCertificateKeyFile, SSLPassword, SSL.SSL_AIDX_RSA);
// Support Client Certificates
if (SSLCACertificateFile != null) {
SSLContext.setCACertificate(sslContext, SSLCACertificateFile, null);
}
// Client certificate verification
value = SSL.SSL_CVERIFY_NONE;
if ("optional".equalsIgnoreCase(SSLVerifyClient)) {
value = SSL.SSL_CVERIFY_OPTIONAL;
} else if ("require".equalsIgnoreCase(SSLVerifyClient)) {
value = SSL.SSL_CVERIFY_REQUIRE;
} else if ("optionalNoCA".equalsIgnoreCase(SSLVerifyClient)) {
value = SSL.SSL_CVERIFY_OPTIONAL_NO_CA;
}
SSLContext.setVerify(sslContext, value, SSLVerifyDepth);
// For now, sendfile is not supported with SSL
useSendfile = false;
}
initialized = true;
}
|
public boolean isPaused() {
return paused;
}
Return the state of the endpoint. |
public boolean isRunning() {
return running;
}
Return the state of the endpoint. |
protected AprEndpoint.Worker newWorkerThread() {
Worker workerThread = new Worker();
workerThread.start();
return (workerThread);
}
Create and return a new processor suitable for processing HTTP
requests and returning the corresponding responses. |
public void pause() {
if (running && !paused) {
paused = true;
unlockAccept();
}
}
Pause the endpoint, which will make it stop accepting new sockets. |
protected void recycleWorkerThread(AprEndpoint.Worker workerThread) {
synchronized (workers) {
workers.push(workerThread);
curThreadsBusy--;
workers.notify();
}
}
Recycle the specified Processor so that it can be used again. |
public void resume() {
if (running) {
paused = false;
}
}
Resume the endpoint, which will make it start accepting new sockets
again. |
public void setAddress(InetAddress address) {
this.address = address;
}
|
public void setBacklog(int backlog) {
if (backlog > 0) this.backlog = backlog;
}
|
public void setDaemon(boolean b) {
daemon = b;
}
|
public void setFirstReadTimeout(int firstReadTimeout) {
this.firstReadTimeout = firstReadTimeout;
}
|
public void setHandler(AprEndpoint.Handler handler) {
this.handler = handler;
}
|
public void setMaxThreads(int maxThreads) {
this.maxThreads = maxThreads;
}
|
public void setName(String name) {
this.name = name;
}
|
public void setPollTime(int pollTime) {
this.pollTime = pollTime;
}
|
public void setPollerSize(int pollerSize) {
this.pollerSize = pollerSize;
}
|
public void setPort(int port) {
this.port=port;
}
|
public void setSSLCACertificateFile(String SSLCACertificateFile) {
this.SSLCACertificateFile = SSLCACertificateFile;
}
|
public void setSSLCACertificatePath(String SSLCACertificatePath) {
this.SSLCACertificatePath = SSLCACertificatePath;
}
|
public void setSSLCARevocationFile(String SSLCARevocationFile) {
this.SSLCARevocationFile = SSLCARevocationFile;
}
|
public void setSSLCARevocationPath(String SSLCARevocationPath) {
this.SSLCARevocationPath = SSLCARevocationPath;
}
|
public void setSSLCertificateChainFile(String SSLCertificateChainFile) {
this.SSLCertificateChainFile = SSLCertificateChainFile;
}
|
public void setSSLCertificateFile(String SSLCertificateFile) {
this.SSLCertificateFile = SSLCertificateFile;
}
|
public void setSSLCertificateKeyFile(String SSLCertificateKeyFile) {
this.SSLCertificateKeyFile = SSLCertificateKeyFile;
}
|
public void setSSLCipherSuite(String SSLCipherSuite) {
this.SSLCipherSuite = SSLCipherSuite;
}
|
public void setSSLEngine(String SSLEngine) {
this.SSLEngine = SSLEngine;
}
|
public void setSSLPassword(String SSLPassword) {
this.SSLPassword = SSLPassword;
}
|
public void setSSLProtocol(String SSLProtocol) {
this.SSLProtocol = SSLProtocol;
}
|
public void setSSLVerifyClient(String SSLVerifyClient) {
this.SSLVerifyClient = SSLVerifyClient;
}
|
public void setSSLVerifyDepth(int SSLVerifyDepth) {
this.SSLVerifyDepth = SSLVerifyDepth;
}
|
public void setSendfileSize(int sendfileSize) {
this.sendfileSize = sendfileSize;
}
|
public void setSoLinger(int soLinger) {
this.soLinger = soLinger;
}
|
public void setSoTimeout(int soTimeout) {
this.soTimeout = soTimeout;
}
|
protected boolean setSocketOptions(long socket) {
// Process the connection
int step = 1;
try {
// 1: Set socket options: timeout, linger, etc
if (soLinger >= 0)
Socket.optSet(socket, Socket.APR_SO_LINGER, soLinger);
if (tcpNoDelay)
Socket.optSet(socket, Socket.APR_TCP_NODELAY, (tcpNoDelay ? 1 : 0));
if (soTimeout > 0)
Socket.timeoutSet(socket, soTimeout * 1000);
// 2: SSL handshake
step = 2;
if (sslContext != 0) {
SSLSocket.attach(sslContext, socket);
if (SSLSocket.handshake(socket) != 0) {
if (log.isDebugEnabled()) {
log.debug(sm.getString("endpoint.err.handshake") + ": " + SSL.getLastError());
}
return false;
}
}
} catch (Throwable t) {
if (step == 2) {
if (log.isDebugEnabled()) {
log.debug(sm.getString("endpoint.err.handshake"), t);
}
} else {
log.error(sm.getString("endpoint.err.unexpected"), t);
}
// Tell to close the socket
return false;
}
return true;
}
Process the specified connection. |
public void setTcpNoDelay(boolean tcpNoDelay) {
this.tcpNoDelay = tcpNoDelay;
}
|
public void setThreadPriority(int threadPriority) {
this.threadPriority = threadPriority;
}
|
public void setUseSendfile(boolean useSendfile) {
this.useSendfile = useSendfile;
}
|
public void start() throws Exception {
// Initialize socket if not done before
if (!initialized) {
init();
}
if (!running) {
running = true;
paused = false;
// Start acceptor thread
acceptorThread = new Thread(new Acceptor(), getName() + "-Acceptor");
acceptorThread.setPriority(getThreadPriority());
acceptorThread.setDaemon(true);
acceptorThread.start();
// Start poller thread
poller = new Poller();
poller.init();
pollerThread = new Thread(poller, getName() + "-Poller");
pollerThread.setPriority(getThreadPriority());
pollerThread.setDaemon(true);
pollerThread.start();
// Start sendfile thread
if (useSendfile) {
sendfile = new Sendfile();
sendfile.init();
sendfileThread = new Thread(sendfile, getName() + "-Sendfile");
sendfileThread.setPriority(getThreadPriority());
sendfileThread.setDaemon(true);
sendfileThread.start();
}
}
}
Start the APR endpoint, creating acceptor, poller and sendfile threads. |
public void stop() {
if (running) {
running = false;
unlockAccept();
poller.destroy();
if (useSendfile) {
sendfile.destroy();
}
acceptorThread = null;
pollerThread = null;
sendfileThread = null;
}
}
Stop the endpoint. This will cause all processing threads to stop. |
protected void unlockAccept() {
java.net.Socket s = null;
try {
// Need to create a connection to unlock the accept();
if (address == null) {
s = new java.net.Socket("127.0.0.1", port);
} else {
s = new java.net.Socket(address, port);
// setting soLinger to a small value will help shutdown the
// connection quicker
s.setSoLinger(true, 0);
}
} catch(Exception e) {
if (log.isDebugEnabled()) {
log.debug(sm.getString("endpoint.debug.unlock", "" + port), e);
}
} finally {
if (s != null) {
try {
s.close();
} catch (Exception e) {
// Ignore
}
}
}
}
Unlock the server socket accept using a bugus connection. |