Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2012 Red Hat, Inc., and individual contributors
   * as indicated by the @author tags.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.coyote.ajp;
 
 
 
Abstract the protocol implementation, including threading, etc. Processor is single threaded and specific to stream-based protocols, will not fit Jk protocols like JNI.

Author(s):
Remy Maucherat
Costin Manolache
 
 public class AjpProtocol 
     implements ProtocolHandlerMBeanRegistration {
     
     
     // ------------------------------------------------------------ Constructor
 
 
     public AjpProtocol() {
          = new AjpConnectionHandler(this);
         //setServerSoTimeout(Constants.DEFAULT_SERVER_SOCKET_TIMEOUT);
     }
 
     
     // ----------------------------------------------------- Instance Variables
 
 
     protected ObjectName tpOname;
     
     
     protected ObjectName rgOname;


    
Associated java.io endpoint.
 
     protected JIoEndpoint endpoint = new JIoEndpoint();


    
Configuration attributes.
 
     protected Hashtable attributes = new Hashtable();


    
Adapter which will process the requests recieved by this endpoint.
 
     private Adapter adapter;
    
    
    
Connection handler for AJP.
    // --------------------------------------------------------- Public Methods


    
Pass config info
    public void setAttribute(String nameObject value) {
        .put(namevalue);
    }
    public Object getAttribute(String key) {
        return .get(key);
    }
    public Iterator getAttributeNames() {
        return .keySet().iterator();
    }


    
The adapter, used to call the connector
    public void setAdapter(Adapter adapter) {
        this. = adapter;
    }
    public Adapter getAdapter() {
        return ;
    }
    public boolean hasIoEvents() {
        return false;
    }
        return .;
    }


    
Start the protocol
    public void init() throws Exception {
        .setName(getName());
        .setHandler();
        try {
            .init();
        } catch (Exception ex) {
            throw ex;
        }
    }
    public void start() throws Exception {
        if (.....) {
            if (this. != null ) {
                try {
                     = new ObjectName
                    ( + ":" + "type=ThreadPool,name=" + getJmxName());
                    Registry.getRegistry(nullnull)
                    .registerComponent(null );
                } catch (Exception e) {
                    ..errorRegisteringPool(e);
                }
                 = new ObjectName
                ( + ":type=GlobalRequestProcessor,name=" + getJmxName());
                Registry.getRegistry(nullnull).registerComponent
                (.null);
            }
        }
        try {
            .start();
        } catch (Exception ex) {
            ..errorStartingEndpoint(ex);
            throw ex;
        }
    }
    public void pause() throws Exception {
        try {
            .pause();
        } catch (Exception ex) {
            ..errorPausingEndpoint(ex);
            throw ex;
        }
        // Wait for a while until all the processors are idle
        RequestInfo[] states = ..getRequestProcessors();
        int retry = 0;
        boolean done = false;
        while (!done && retry < ....) {
            retry++;
            done = true;
            for (int i = 0; i < states.lengthi++) {
                if (states[i].getStage() == ....) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        ;
                    }
                    done = false;
                    break;
                }
            }
        }
    }
    public void resume() throws Exception {
        try {
            .resume();
        } catch (Exception ex) {
            ..errorResumingEndpoint(ex);
            throw ex;
        }
    }
    public void destroy() throws Exception {
        .destroy();
        if (.....) {
            if (!=null)
                Registry.getRegistry(nullnull).unregisterComponent();
            if ( != null)
                Registry.getRegistry(nullnull).unregisterComponent();
        }
    }
    public String getJmxName() {
        String encodedAddr = "";
        if (getAddress() != null) {
            encodedAddr = "" + getAddress();
            encodedAddr = URLEncoder.encode(encodedAddr.replace('/''-').replace(':''_').replace('%''-')) + "-";
        }
        return ("ajp-" + encodedAddr + .getPort());
    }
    public String getName() {
        String encodedAddr = "";
        if (getAddress() != null) {
            encodedAddr = getAddress() + ":";
        }
        return ("ajp-" + encodedAddr + .getPort());
    }

    
Processor cache.
    protected int processorCache = -1;
    public int getProcessorCache() { return this.; }
    public void setProcessorCache(int processorCache) { this. = processorCache; }
    public Executor getExecutor() { return .getExecutor(); }
    public void setExecutor(Executor executor) { .setExecutor(executor); }
    
    public int getMaxThreads() { return .getMaxThreads(); }
    public void setMaxThreads(int maxThreads) { .setMaxThreads(maxThreads); }
    public int getThreadPriority() { return .getThreadPriority(); }
    public void setThreadPriority(int threadPriority) { .setThreadPriority(threadPriority); }
    public int getBacklog() { return .getBacklog(); }
    public void setBacklog(int backlog) { .setBacklog(backlog); }
    public int getPort() { return .getPort(); }
    public void setPort(int port) { .setPort(port); }
    public InetAddress getAddress() { return .getAddress(); }
    public void setAddress(InetAddress ia) { .setAddress(ia); }
    public boolean getTcpNoDelay() { return .getTcpNoDelay(); }
    public void setTcpNoDelay(boolean tcpNoDelay) { .setTcpNoDelay(tcpNoDelay); }
    public int getSoLinger() { return .getSoLinger(); }
    public void setSoLinger(int soLinger) { .setSoLinger(soLinger); }
    public int getSoTimeout() { return .getSoTimeout(); }
    public void setSoTimeout(int soTimeout) { .setSoTimeout(soTimeout); }
    public void setPollerSize(int pollerSize) { .setPollerSize(pollerSize); }
    public int getPollerSize() { return .getPollerSize(); }

    
Should authentication be done in the native webserver layer, or in the Servlet container ?
    public boolean getTomcatAuthentication() { return ; }
    public void setTomcatAuthentication(boolean tomcatAuthentication) { this. = tomcatAuthentication; }

    
Required secret.
    public void setRequiredSecret(String requiredSecret) { this. = requiredSecret; }
    
    
AJP packet size.
    protected int packetSize = .;
    public int getPacketSize() { return ; }
    public void setPacketSize(int packetSize) { this. = packetSize; }

    
    
The number of seconds Tomcat will wait for a subsequent request before closing the connection.
    protected int keepAliveTimeout = -1;
    public int getKeepAliveTimeout() { return ; }
    public void setKeepAliveTimeout(int timeout) {  = timeout; }
    // --------------------------------------  AjpConnectionHandler Inner Class
    protected static class AjpConnectionHandler implements Handler {
        protected AjpProtocol proto;
        protected AtomicLong registerCount = new AtomicLong(0);
        protected RequestGroupInfo global = new RequestGroupInfo();
        protected ConcurrentHashMap<SocketAjpProcessorconnections =
            new ConcurrentHashMap<SocketAjpProcessor>();
        protected ConcurrentLinkedQueue<AjpProcessorrecycledProcessors = 
            new ConcurrentLinkedQueue<AjpProcessor>() {
            protected AtomicInteger size = new AtomicInteger(0);
            public boolean offer(AjpProcessor processor) {
                boolean offer = (. == -1) ? true : (.get() < .);
                //avoid over growing our cache or add after we have stopped
                boolean result = false;
                if ( offer ) {
                    result = super.offer(processor);
                    if ( result ) {
                        .incrementAndGet();
                    }
                }
                if (!resultunregister(processor);
                return result;
            }
            
            public AjpProcessor poll() {
                AjpProcessor result = super.poll();
                if ( result != null ) {
                    .decrementAndGet();
                }
                return result;
            }
            
            public void clear() {
                AjpProcessor next = poll();
                while ( next != null ) {
                    unregister(next);
                    next = poll();
                }
                super.clear();
                .set(0);
            }
        };
        public AjpConnectionHandler(AjpProtocol proto) {
            this. = proto;
        }
        public SocketState event(Socket socketSocketStatus status) {
            AjpProcessor result = .get(socket);
            SocketState state = .
            if (result != null) {
                result.startProcessing();
                // Call the appropriate event
                try {
                    state = result.event(status);
                } catch (java.net.SocketException e) {
                    // SocketExceptions are normal
                    ..socketException(e);
                } catch (java.io.IOException e) {
                    // IOExceptions are normal
                    ..socketException(e);
                }
                // Future developers: if you discover any other
                // rare-but-nonfatal exceptions, catch them here, and log as
                // above.
                catch (Throwable e) {
                    // any other exception or error is odd. Here we log it
                    // with "ERROR" level, so it will show up even on
                    // less-than-verbose logs.
                    ..socketError(e);
                } finally {
                    if (state != .) {
                        .remove(socket);
                        .offer(result);
                    } else {
                        if (..isRunning()) {
                            ..getEventPoller().add(socketresult.getTimeout(), 
                                    result.getResumeNotification(), false);
                        }
                    }
                    result.endProcessing();
                }
            }
            return state;
        }
        
        public SocketState process(Socket socket) {
            AjpProcessor processor = .poll();
            try {
                if (processor == null) {
                    processor = createProcessor();
                }
                SocketState state = processor.process(socket);
                if (state == .) {
                    // Associate the connection with the processor. The next request 
                    // processed by this thread will use either a new or a recycled
                    // processor.
                    .put(socketprocessor);
                    ..getEventPoller().add(socketprocessor.getTimeout(), 
                            processor.getResumeNotification(), false);
                } else {
                    .offer(processor);
                }
                return state;
            } catch(java.net.SocketException e) {
                // SocketExceptions are normal
                ..socketException(e);
            } catch (java.io.IOException e) {
                // IOExceptions are normal
                ..socketException(e);
            }
            // Future developers: if you discover any other
            // rare-but-nonfatal exceptions, catch them here, and log as
            // above.
            catch (Throwable e) {
                // any other exception or error is odd. Here we log it
                // with "ERROR" level, so it will show up even on
                // less-than-verbose logs.
                ..socketError(e);
            }
            .offer(processor);
            return .;
        }
        protected AjpProcessor createProcessor() {
            AjpProcessor processor = new AjpProcessor(..);
            processor.setAdapter(.);
            processor.setTomcatAuthentication(.);
            processor.setRequiredSecret(.);
            processor.setKeepAliveTimeout(.);
            register(processor);
            return processor;
        }
        
        protected void register(AjpProcessor processor) {
            RequestInfo rp = processor.getRequest().getRequestProcessor();
            rp.setGlobalProcessor();
            if (..... && .getDomain() != null) {
                synchronized (this) {
                    try {
                        long count = .incrementAndGet();
                        ObjectName rpName = new ObjectName
                            (.getDomain() + ":type=RequestProcessor,worker="
                                + .getJmxName() + ",name=AjpRequest" + count);
                        Registry.getRegistry(nullnull).registerComponent(rprpNamenull);
                        rp.setRpName(rpName);
                    } catch (Exception e) {
                        ..errorRegisteringRequest(e);
                    }
                }
            }
        }
        protected void unregister(AjpProcessor processor) {
            RequestInfo rp = processor.getRequest().getRequestProcessor();
            rp.setGlobalProcessor(null);
            if (..... && .getDomain() != null) {
                synchronized (this) {
                    try {
                        ObjectName rpName = rp.getRpName();
                        Registry.getRegistry(nullnull).unregisterComponent(rpName);
                        rp.setRpName(null);
                    } catch (Exception e) {
                        ..errorUnregisteringRequest(e);
                    }
                }
            }
        }
    }
    // -------------------- Various implementation classes --------------------
    protected String domain;
    protected ObjectName oname;
    protected MBeanServer mserver;
    public ObjectName getObjectName() {
        return ;
    }
    public String getDomain() {
        return ;
    }
    public ObjectName preRegister(MBeanServer server,
                                  ObjectName namethrows Exception {
        =name;
        =server;
        =name.getDomain();
        return name;
    }
    public void postRegister(Boolean registrationDone) {
    }
    public void preDeregister() throws Exception {
    }
    public void postDeregister() {
    }
    
 
New to GrepCode? Check out our FAQ X