/*
 * Copyright 2009 the original author or authors.
 *
 * 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.gradle.api.internal;

import groovy.lang.GroovyObjectSupport;
import groovy.lang.MissingMethodException;
import groovy.lang.MissingPropertyException;

public class ConfigureDelegate extends GroovyObjectSupport {
    private static final Object[] EMPTY_PARAMS = new Object[0];

    protected final DynamicObject _owner;
    protected final DynamicObject _delegate;
    private final ThreadLocal<Boolean> _configuring = new ThreadLocal<Boolean>() {
        @Override
        protected Boolean initialValue() {
            return false;
        }
    };

    public ConfigureDelegate(Object owner, Object delegate) {
        _owner = DynamicObjectUtil.asDynamicObject(owner);
        _delegate = DynamicObjectUtil.asDynamicObject(delegate);
    }

    @Override
    public String toString() {
        return _delegate.toString();
    }

    protected boolean _isConfigureMethod(String name, Object[] params) {
        return false;
    }

    protected Object _configure(String name, Object[] params) {
        // do nothing
        return null;
    }

    public Object invokeMethod(String name, Object paramsObj) {
        Object[] params = (Object[])paramsObj;

        boolean isAlreadyConfiguring = _configuring.get();
        _configuring.set(true);
        try {
            MissingMethodException failure;
            try {
                return _delegate.invokeMethod(name, params);
            } catch (groovy.lang.MissingMethodException e) {
                // TODO - should check the type as well, and below too, however we have no idea what the final type is going to be.
                // Rework the DynamicObject contract to allow us to know if the method was found or not
                if (!name.equals(e.getMethod())) {
                    throw e;
                }
                failure = e;
            }

            if (!isAlreadyConfiguring && _isConfigureMethod(name, params)) {
                return _configure(name, params);
            }

            // try the owner
            try {
                return _owner.invokeMethod(name, params);
            } catch (groovy.lang.MissingMethodException e) {
                if (!name.equals(e.getMethod())) {
                    throw e;
                }
                // ignore
            }

            throw failure;

        } finally {
            _configuring.set(isAlreadyConfiguring);
        }
    }

    public Object get(String name) {
        boolean isAlreadyConfiguring = _configuring.get();
        _configuring.set(true);
        try {
            MissingPropertyException failure;
            try {
                return _delegate.getProperty(name);
            } catch (MissingPropertyException e) {
                if (!name.equals(e.getProperty())) {
                    throw e;
                }
                failure = e;
            }

            // try the owner
            try {
                return _owner.getProperty(name);
            } catch (MissingPropertyException e) {
                if (!name.equals(e.getProperty())) {
                    throw e;
                }
                // Ignore
            }

            if (isAlreadyConfiguring) {
                throw failure;
            }

            return _configure(name, EMPTY_PARAMS);
        } finally {
            _configuring.set(isAlreadyConfiguring);
        }
    }
}
@@ -1664,32 +1664,46 @@
 rn null;%0A    %7D%0A%0A
+    @Override%0A
     public Objec
@@ -1919,57 +1919,62 @@
     
-MissingMethodException failure;%0A            try %7B
+InvokeMethodResult result = new InvokeMethodResult();%0A
 %0A   
@@ -1978,35 +1978,24 @@
             
-    return 
 _delegate.in
@@ -2002,32 +2002,40 @@
 vokeMethod(name,
+ result,
  params);%0A      
@@ -2044,418 +2044,73 @@
     
-%7D catch (groovy.lang.MissingMethodException e) %7B%0A                // TODO - should check the type as well, and below too, however we have no idea what the final type is going to be.%0A                // Rework the DynamicObject contract to allow us to know if the method was found or not%0A                if (!name.equals(e.getMethod())) %7B%0A                    throw e;%0A                %7D%0A                failure = e
+if (result.isFound()) %7B%0A                return result.getResult()
 ;%0A  
@@ -2308,37 +2308,8 @@
     
-try %7B%0A                return 
 _own
@@ -2321,32 +2321,40 @@
 vokeMethod(name,
+ result,
  params);%0A      
@@ -2363,139 +2363,75 @@
     
-%7D catch (groovy.lang.MissingMethodException e) %7B%0A                if (!name.equals(e.getMethod())) %7B%0A                    throw e;%0A  
+if (result.isFound()) %7B%0A                return result.getResult();%0A
     
@@ -2430,35 +2430,34 @@
 ();%0A            
-  
 %7D
+%0A
 %0A               
@@ -2457,64 +2457,77 @@
     
-    // ignore%0A            %7D%0A%0A            throw failure;%0A
+throw new MissingMethodException(name, _delegate.getClass(), params);
 %0A   
@@ -2766,31 +2766,27 @@
         
-Missing
+Get
 Property
 Exceptio
@@ -2781,44 +2781,48 @@
 erty
-Exception failure;%0A            try %7B
+Result result = new GetPropertyResult();
 %0A   
@@ -2826,35 +2826,24 @@
             
-    return 
 _delegate.ge
@@ -2860,64 +2860,19 @@
 name
-);%0A            %7D catch (MissingPropertyException e) %7B%0A  
+, result);%0A
     
@@ -2883,207 +2883,100 @@
     
-  
 if (
-!name.equals(e.getProperty())) %7B%0A                    throw e;%0A                %7D%0A                failure = e;%0A            %7D%0A%0A            // try the owner%0A            try %7B%0A                return
+result.isFound()) %7B%0A                return result.getValue();%0A            %7D%0A%0A           
  _ow
@@ -2995,16 +2995,24 @@
 rty(name
+, result
 );%0A     
@@ -3022,172 +3022,73 @@
     
-%7D catch (MissingPropertyException e) %7B%0A                if (!name.equals(e.getProperty())) %7B%0A                    throw e;%0A                %7D%0A                // Ignore
+if (result.isFound()) %7B%0A                return result.getValue();
 %0A   
@@ -3165,15 +3165,64 @@
 row 
-failure
+new MissingPropertyException(name, _delegate.getClass())
 ;%0A  
@@ -3225,33 +3225,32 @@
 ;%0A            %7D%0A
-%0A
             retu
@@ -3373,8 +3373,9 @@
 %0A    %7D%0A%7D
+%0A

Use new DynamicObject methods in ConfigureDelegate