/*
 * 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.camel.language.bean;

import java.util.Map;

import org.apache.camel.Expression;
import org.apache.camel.Predicate;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.StaticService;
import org.apache.camel.component.bean.ParameterMappingStrategy;
import org.apache.camel.component.bean.ParameterMappingStrategyHelper;
import org.apache.camel.spi.Language;
import org.apache.camel.support.ExpressionToPredicateAdapter;
import org.apache.camel.support.LanguageSupport;
import org.apache.camel.util.StringHelper;

/**
 * A <a href="http://camel.apache.org/bean-language.html">bean language</a> which uses a simple text notation to invoke
 * methods on beans to evaluate predicates or expressions
 * <p/>
 * The notation is essentially <code>beanName.methodName</code> which is then invoked using the beanName to lookup in
 * the <a href="http://camel.apache.org/registry.html>registry</a> then the method is invoked to evaluate the expression
 * using the <a href="http://camel.apache.org/bean-integration.html">bean integration</a> to bind the
 * {@link org.apache.camel.Exchange} to the method arguments.
 * <p/>
 * As of Camel 1.5 the bean language also supports invoking a provided bean by its classname or the bean itself.
 */
@org.apache.camel.spi.annotations.Language("bean")
public class BeanLanguage extends LanguageSupport implements StaticService {

    private volatile ParameterMappingStrategy parameterMappingStrategy;
    private volatile Language simple;

    private Object bean;
    private Class<?> beanType;
    private String ref;
    private String method;

    public BeanLanguage() {
    }

    public Object getBean() {
        return bean;
    }

    public void setBean(Object bean) {
        this.bean = bean;
    }

    public Class<?> getBeanType() {
        return beanType;
    }

    public void setBeanType(Class<?> beanType) {
        this.beanType = beanType;
    }

    public String getRef() {
        return ref;
    }

    public void setRef(String ref) {
        this.ref = ref;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    @Override
    public Predicate createPredicate(String expression) {
        return ExpressionToPredicateAdapter.toPredicate(createExpression(expression));
    }

    @Override
    public Predicate createPredicate(String expression, Map<String, Object> properties) {
        return ExpressionToPredicateAdapter.toPredicate(createExpression(expression, properties));
    }

    @Override
    public Expression createExpression(String expression, Map<String, Object> properties) {
        BeanExpression answer = null;

        String method = (String) properties.get("method");
        Object bean = properties.get("bean");
        if (bean != null) {
            answer = new BeanExpression(bean, method);
        }
        Class<?> beanType = (Class<?>) properties.get("beanType");
        if (beanType != null) {
            answer = new BeanExpression(beanType, method);
        }
        String ref = (String) properties.get("ref");
        if (ref != null) {
            answer = new BeanExpression(ref, method);
        }
        if (answer == null) {
            throw new IllegalArgumentException("Bean language requires bean, beanType, or ref argument");
        }

        answer.setParameterMappingStrategy(parameterMappingStrategy);
        answer.setSimple(simple);
        answer.init(getCamelContext());
        return answer;
    }

    @Override
    public Expression createExpression(String expression) {
        BeanExpression answer;

        // favour using the configured options
        if (bean != null) {
            answer = new BeanExpression(bean, method);
        } else if (beanType != null) {
            answer = new BeanExpression(beanType, method);
        } else if (ref != null) {
            answer = new BeanExpression(ref, method);
        } else {
            String beanName = expression;
            String method = null;

            // we support both the .method name and the ?method= syntax
            // as the ?method= syntax is very common for the bean component
            if (expression.contains("?method=")) {
                beanName = StringHelper.before(expression, "?");
                method = StringHelper.after(expression, "?method=");
            } else {
                //first check case :: because of my.own.Bean::method
                int doubleColonIndex = expression.indexOf("::");
                //need to check that not inside params
                int beginOfParameterDeclaration = expression.indexOf('(');
                if (doubleColonIndex > 0 && (!expression.contains("(") || doubleColonIndex < beginOfParameterDeclaration)) {
                    beanName = expression.substring(0, doubleColonIndex);
                    method = expression.substring(doubleColonIndex + 2);
                } else {
                    int idx = expression.indexOf('.');
                    if (idx > 0) {
                        beanName = expression.substring(0, idx);
                        method = expression.substring(idx + 1);
                    }
                }
            }

            if (beanName.startsWith("type:")) {
                try {
                    Class clazz = getCamelContext().getClassResolver().resolveMandatoryClass(beanName.substring(5));
                    answer = new BeanExpression(clazz, method);
                } catch (ClassNotFoundException e) {
                    throw RuntimeCamelException.wrapRuntimeException(e);
                }
            } else {
                answer = new BeanExpression(beanName, method);
            }
        }

        answer.setParameterMappingStrategy(parameterMappingStrategy);
        answer.setSimple(simple);
        answer.init(getCamelContext());
        return answer;
    }

    @Override
    public void start() {
        parameterMappingStrategy = ParameterMappingStrategyHelper.createParameterMappingStrategy(getCamelContext());
        simple = getCamelContext().resolveLanguage("simple");
    }

    @Override
    public void stop() {
        // noop
    }
}

@@ -1017,16 +1017,70 @@
 ervice;%0A
+import org.apache.camel.component.bean.BeanComponent;%0A
 import o
@@ -1136,16 +1136,16 @@
 rategy;%0A
-
 import o
@@ -2247,16 +2247,66 @@
 vice %7B%0A%0A
+    private volatile BeanComponent beanComponent;%0A
     priv
@@ -4275,32 +4275,80 @@
 t%22);%0A        %7D%0A%0A
+        answer.setBeanComponent(beanComponent);%0A
         answer.s
@@ -6706,32 +6706,80 @@
    %7D%0A        %7D%0A%0A
+        answer.setBeanComponent(beanComponent);%0A
         answer.s
@@ -6942,32 +6942,32 @@
 %0A%0A    @Override%0A
-
     public void 
@@ -6972,24 +6972,109 @@
 d start() %7B%0A
+        beanComponent = getCamelContext().getComponent(%22bean%22, BeanComponent.class);%0A
         para

optimize to not lookup bean component per message if we can lookup once only.