/*
 * 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 io.undertow.servlet.core;

import java.util.ArrayList;
import java.util.List;

import io.undertow.server.HttpServerExchange;
import io.undertow.servlet.api.ThreadSetupAction;

/**
 * @author Stuart Douglas
 */
public class CompositeThreadSetupAction implements ThreadSetupAction {

    private final List<ThreadSetupAction> actions;

    public CompositeThreadSetupAction(final List<ThreadSetupAction> actions) {
        this.actions = actions;
    }

    @Override
    public Handle setup(final HttpServerExchange exchange) {
        final List<Handle> handles = new ArrayList<Handle>(actions.size());
        try {
            for (ThreadSetupAction action : actions) {
                final Handle result = action.setup(exchange);
                if (result != null) {
                    handles.add(result);
                }
            }
            return new Handle() {
                @Override
                public void tearDown() {
                    Throwable problem = null;
                    for (final Handle handle : handles) {
                        try {
                            handle.tearDown();
                        } catch (Throwable e) {
                            problem = e;
                        }
                    }
                    if (problem != null) {
                        throw new RuntimeException(problem);
                    }
                }
            };
        } catch (RuntimeException e) {
            for (final Handle handle : handles) {
                try {
                    handle.tearDown();
                } catch (Throwable ignore) {

                }
            }
            throw e;
        } catch (Error e) {
            for (final Handle handle : handles) {
                try {
                    handle.tearDown();
                } catch (Throwable ignore) {

                }
            }
            throw e;
        }
    }
}

@@ -740,36 +740,8 @@
 e;%0A%0A
-import java.util.ArrayList;%0A
 impo
@@ -977,29 +977,24 @@
 ivate final 
-List%3C
 ThreadSetupA
@@ -990,33 +990,34 @@
 hreadSetupAction
-%3E
+%5B%5D
  actions;%0A%0A    p
@@ -1120,16 +1120,63 @@
  actions
+.toArray(new ThreadSetupAction%5Bactions.size()%5D)
 ;%0A    %7D%0A
@@ -1265,28 +1265,24 @@
   final 
-List%3C
 Handle
-%3E
+%5B%5D
  handles
@@ -1292,34 +1292,23 @@
 new 
-ArrayList%3C
 Handle
-%3E(
+%5B
 actions.
 size
@@ -1307,15 +1307,15 @@
 ons.
-size())
+length%5D
 ;%0A  
@@ -1347,42 +1347,42 @@
 or (
-ThreadSetupAction action : actions
+int i = 0; i %3C handles.length; ++i
 ) %7B%0A
@@ -1401,27 +1401,18 @@
     
-final Handle result
+handles%5Bi%5D
  = a
@@ -1416,16 +1416,20 @@
 = action
+s%5Bi%5D
 .setup(e
@@ -1442,105 +1442,8 @@
 e);%0A
-                if (result != null) %7B%0A                    handles.add(result);%0A                %7D%0A
     
@@ -1616,37 +1616,93 @@
            for (
-f
 in
-al
+t i = 0; i %3C handles.length; ++i) %7B%0A                       
  Handle handle :
@@ -1696,33 +1696,33 @@
 ndle handle 
-:
+=
  handles
 ) %7B%0A        
@@ -1701,32 +1701,79 @@
 handle = handles
+%5Bi%5D;%0A                        if (handle != null
 ) %7B%0A            
@@ -1776,38 +1776,46 @@
                 
+  
+  
 try %7B%0A
+    
                 
@@ -1861,32 +1861,36 @@
                 
+    
 %7D catch (Throwab
@@ -1925,16 +1925,20 @@
         
+ 
+   
 problem 
@@ -1938,24 +1938,54 @@
 roblem = e;%0A
+                            %7D%0A
             

Use arrays for iteration