/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.bsf.utils.http;

import java.util.LinkedList;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;

public class ScriptEnginePool {

    static private final int DEFAULT_SIZE = 10;
    private LinkedList pool = new LinkedList();
    private int engines = 0;
    private int capacity = 0;
    private ScriptEngineFactory factory;
    private boolean isMultithreaded = false;
    
    public ScriptEnginePool(ScriptEngineFactory factory, int capacity){
    	this.factory = factory;
        this.capacity = capacity;
        
        String param = (String)factory.getParameter("THREADING");
        if (param != null && param.equals("MULTITHREADED")) {
            this.isMultithreaded = true;
            pool.add(factory.getScriptEngine());
        }              	
    }
    
    public ScriptEnginePool(ScriptEngineFactory factory){
        this(factory,DEFAULT_SIZE);
     }
       
    public synchronized void free(ScriptEngine eng){
        pool.add(eng); // should I clear the engine namespaces .. 
        notifyAll();
    }
    
    public synchronized ScriptEngine get(){
        if (isMultithreadingSupported()) {
            return (ScriptEngine) pool.getFirst();
        } else {
            if (!pool.isEmpty()) {
                return (ScriptEngine)pool.removeFirst();
            } else {
                if (engines<capacity) {
                    engines++;
                    return factory.getScriptEngine();
                }
                while (!pool.isEmpty()) {
                    waiting();
                }
                return (ScriptEngine) pool.removeFirst();
            }
        }
    }
    
    public boolean isMultithreadingSupported(){
        return this.isMultithreaded;
    }
    
    public void waiting(){
        try{
            wait();            
        }catch(InterruptedException ie){
        }
    }
    
}

@@ -1043,16 +1043,22 @@
 private 
+final 
 LinkedLi
@@ -1081,24 +1081,48 @@
 nkedList();%0A
+    //GuardedBy(%22this%22)%0A
     private 
@@ -1146,24 +1146,30 @@
     private 
+final 
 int capacity
@@ -1168,20 +1168,16 @@
 capacity
- = 0
 ;%0A    pr
@@ -1182,16 +1182,22 @@
 private 
+final 
 ScriptEn
@@ -1229,16 +1229,22 @@
 private 
+final 
 boolean 
@@ -1262,16 +1262,8 @@
 aded
- = false
 ;%0A  
@@ -1637,17 +1637,23 @@
       %7D 
- 
+else %7B%0A
         
@@ -1656,17 +1656,55 @@
         
-%09
+this.isMultithreaded = false;%0A        %7D
 %0A    %7D%0A 
@@ -2445,18 +2445,131 @@
     
-waiting();
+try%7B%0A                        wait();            %0A                    %7Dcatch(InterruptedException ie)%7B%0A                    %7D
 %0A   
@@ -2776,140 +2776,6 @@
    %0A
-    public void waiting()%7B%0A        try%7B%0A            wait();            %0A        %7Dcatch(InterruptedException ie)%7B%0A        %7D%0A    %7D%0A    %0A
 %7D%0A

Make private variables final