Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2014 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 io.undertow.servlet.core;
 
 
 import java.util.List;
Lister that handles a servlet exchange upgrade event.

Author(s):
Stuart Douglas
 
 public class ServletUpgradeListener<T extends HttpUpgradeHandlerimplements HttpUpgradeListener {
     private final InstanceHandle<T> instance;
     private final ThreadSetupAction threadSetupAction;
     private final HttpServerExchange exchange;
 
     public ServletUpgradeListener(final InstanceHandle<T> instanceThreadSetupAction threadSetupActionHttpServerExchange exchange) {
         this. = instance;
         this. = threadSetupAction;
         this. = exchange;
     }
 
     @Override
     public void handleUpgrade(final StreamConnection channelfinal HttpServerExchange exchange) {
         channel.getCloseSetter().set(new ChannelListener<StreamConnection>() {
             @Override
             public void handleEvent(StreamConnection channel) {
                 final ThreadSetupAction.Handle handle = .setup(ServletUpgradeListener.this.);
                 try {
                     .getInstance().destroy();
                 } finally {
                     try {
                         handle.tearDown();
                     } finally {
                         .release();
                     }
                 }
             }
         });
 
         this..getConnection().getWorker().execute(new Runnable() {
             @Override
             public void run() {
                 DelayedExecutor executor = new DelayedExecutor(exchange.getIoThread());
                 final ThreadSetupAction.Handle handle = .setup(ServletUpgradeListener.this.);
                 try {
                     //run the upgrade in the worker thread
                     .getInstance().init(new WebConnectionImpl(channelServletUpgradeListener.this..getConnection().getBufferPool(), executor));
                 } finally {
                     try {
                         handle.tearDown();
                     } finally {
                         executor.openGate();
                     }
                 }
             }
         });
     }

    
Executor that delays submitting tasks to the delegate until a condition is satisfied.
 
     private static final class DelayedExecutor implements Executor {
 
         private final Executor delegate;
         private volatile boolean queue = true;
         private final List<Runnabletasks = new ArrayList<>();
 
         private DelayedExecutor(Executor delegate) {
             this. = delegate;
         }
 
        @Override
        public void execute(Runnable command) {
            if (!) {
                .execute(command);
            } else {
                synchronized (this) {
                    if (!) {
                        .execute(command);
                    } else {
                        .add(command);
                    }
                }
            }
        }
        synchronized void openGate() {
             = false;
            for (Runnable task : ) {
                .execute(task);
            }
        }
    }
New to GrepCode? Check out our FAQ X