/*
 * Copyright 2012 DTO Labs, Inc. (http://dtolabs.com)
 * 
 * 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.
 *
 */

/*
* NodeStepPluginAdapter.java
* 
* User: Greg Schueler <a href="mailto:greg@dtosolutions.com">greg@dtosolutions.com</a>
* Created: 11/12/12 5:39 PM
* 
*/
package com.dtolabs.rundeck.core.execution.workflow.steps.node;

import com.dtolabs.rundeck.core.common.INodeEntry;
import com.dtolabs.rundeck.core.dispatcher.DataContextUtils;
import com.dtolabs.rundeck.core.execution.ConfiguredStepExecutionItem;
import com.dtolabs.rundeck.core.execution.StepExecutionItem;
import com.dtolabs.rundeck.core.execution.workflow.StepExecutionContext;
import com.dtolabs.rundeck.core.plugins.configuration.PluginAdapterUtility;
import com.dtolabs.rundeck.core.execution.workflow.steps.PluginStepContextImpl;
import com.dtolabs.rundeck.core.plugins.configuration.PropertyResolver;
import com.dtolabs.rundeck.core.plugins.configuration.PropertyResolverFactory;
import com.dtolabs.rundeck.core.execution.workflow.steps.StepFailureReason;
import com.dtolabs.rundeck.core.plugins.configuration.Describable;
import com.dtolabs.rundeck.core.plugins.configuration.Description;
import com.dtolabs.rundeck.core.plugins.configuration.PropertyScope;
import com.dtolabs.rundeck.core.utils.Converter;
import com.dtolabs.rundeck.plugins.ServiceNameConstants;
import com.dtolabs.rundeck.plugins.step.NodeStepPlugin;
import com.dtolabs.rundeck.plugins.step.PluginStepContext;
import com.dtolabs.rundeck.plugins.util.DescriptionBuilder;

import java.util.Map;


/**
 * NodeStepPluginAdapter implements NodeStepExecutor, and makes use of a {@link NodeStepPlugin} instance to perform the
 * execution.
 *
 * @author Greg Schueler <a href="mailto:greg@dtosolutions.com">greg@dtosolutions.com</a>
 */
class NodeStepPluginAdapter implements NodeStepExecutor, Describable {


    @Override
    public Description getDescription() {
        if (plugin instanceof Describable) {
            final Describable desc = (Describable) plugin;
            return desc.getDescription();
        } else {
            return PluginAdapterUtility.buildDescription(plugin, DescriptionBuilder.builder());
        }
    }

    private NodeStepPlugin plugin;

    public NodeStepPluginAdapter(final NodeStepPlugin plugin) {
        this.plugin = plugin;
    }

    static class Convert implements Converter<NodeStepPlugin, NodeStepExecutor> {
        public NodeStepExecutor convert(final NodeStepPlugin plugin) {
            return new NodeStepPluginAdapter(plugin);
        }
    }

    public static final Convert CONVERTER = new Convert();

    @Override
    public NodeStepResult executeNodeStep(final StepExecutionContext context,
                                          final NodeStepExecutionItem item,
                                          final INodeEntry node)
        throws NodeStepException {
        Map<String, Object> instanceConfiguration = getStepConfiguration(item);
        if (null != instanceConfiguration) {
            instanceConfiguration = DataContextUtils.replaceDataReferences(instanceConfiguration,
                                                                           context.getDataContext());
        }
        final String providerName = item.getNodeStepType();

        final PropertyResolver resolver = PropertyResolverFactory.createStepPluginRuntimeResolver(context,
                                                                                                  instanceConfiguration,
                                                                                                  ServiceNameConstants.WorkflowNodeStep,
                                                                                                  providerName);
        final PluginStepContext pluginContext = PluginStepContextImpl.from(context);
        final Map<String, Object> config = PluginAdapterUtility.configureProperties(resolver, getDescription(), plugin, PropertyScope.InstanceOnly);
        try {
            plugin.executeNodeStep(pluginContext, config, node);
        } catch (RuntimeException e) {
            return new NodeStepResultImpl(e,
                                          StepFailureReason.PluginFailed,
                                          e.getMessage(),
                                          node);
        } catch (NodeStepException e){
            return new NodeStepResultImpl(e,
                                          e.getFailureReason(),
                                          e.getMessage(),
                                          node);
        }
        return new NodeStepResultImpl(node);
    }

    private Map<String, Object> getStepConfiguration(StepExecutionItem item) {
        if (item instanceof ConfiguredStepExecutionItem) {
            return ((ConfiguredStepExecutionItem) item).getStepConfiguration();
        } else {
            return null;
        }
    }
}

@@ -838,16 +838,72 @@
 .node;%0A%0A
+import java.util.Map;%0A%0Aimport org.apache.log4j.Logger;%0A%0A
 import c
@@ -941,24 +941,24 @@
 INodeEntry;%0A
-
 import com.d
@@ -1215,84 +1215,8 @@
 xt;%0A
-import com.dtolabs.rundeck.core.plugins.configuration.PluginAdapterUtility;%0A
 impo
@@ -1295,159 +1295,8 @@
 pl;%0A
-import com.dtolabs.rundeck.core.plugins.configuration.PropertyResolver;%0Aimport com.dtolabs.rundeck.core.plugins.configuration.PropertyResolverFactory;%0A
 impo
@@ -1497,24 +1497,251 @@
 escription;%0A
+import com.dtolabs.rundeck.core.plugins.configuration.PluginAdapterUtility;%0Aimport com.dtolabs.rundeck.core.plugins.configuration.PropertyResolver;%0Aimport com.dtolabs.rundeck.core.plugins.configuration.PropertyResolverFactory;%0A
 import com.d
@@ -2079,39 +2079,16 @@
 ilder;%0A%0A
-import java.util.Map;%0A%0A
 %0A/**%0A * 
@@ -2386,16 +2386,106 @@
 bable %7B%0A
+    protected static Logger log = Logger.getLogger(NodeStepPluginAdapter.class.getName());
 %0A%0A    @O
@@ -4721,24 +4721,101 @@
 eption e) %7B%0A
+            log.error(%22Uncaught runtime exception executing node step.%22, e);%0A
             
@@ -5067,16 +5067,72 @@
 ion e)%7B%0A
+            log.error(%22Error executing node step.%22, e);%0A
         

Add logging for plugin exceptions