Search in sources :

Example 1 with CustomAction

use of co.cask.cdap.api.customaction.CustomAction in project cdap by caskdata.

the class CustomActionExecutor method createCustomAction.

@SuppressWarnings("unchecked")
private CustomAction createCustomAction(BasicCustomActionContext context, InstantiatorFactory instantiator, ClassLoader classLoader) throws Exception {
    Class<?> clz = Class.forName(context.getSpecification().getClassName(), true, classLoader);
    Preconditions.checkArgument(CustomAction.class.isAssignableFrom(clz), "%s is not a CustomAction.", clz);
    CustomAction action = instantiator.get(TypeToken.of((Class<? extends CustomAction>) clz)).create();
    Reflections.visit(action, action.getClass(), new PropertyFieldSetter(context.getSpecification().getProperties()), new DataSetFieldSetter(context), new MetricsFieldSetter(context.getMetrics()));
    return action;
}
Also used : PropertyFieldSetter(co.cask.cdap.common.lang.PropertyFieldSetter) MetricsFieldSetter(co.cask.cdap.internal.app.runtime.MetricsFieldSetter) CustomAction(co.cask.cdap.api.customaction.CustomAction) AbstractCustomAction(co.cask.cdap.api.customaction.AbstractCustomAction) DataSetFieldSetter(co.cask.cdap.internal.app.runtime.DataSetFieldSetter)

Example 2 with CustomAction

use of co.cask.cdap.api.customaction.CustomAction in project cdap by caskdata.

the class CustomActionExecutor method executeCustomAction.

private void executeCustomAction() throws Exception {
    try {
        customActionContext.setState(new ProgramState(ProgramStatus.INITIALIZING, null));
        // AbstractCustomAction implements final initialize(context) and requires subclass to
        // implement initialize(), whereas programs that directly implement CustomAction can
        // override initialize(context)
        TransactionControl txControl = customAction instanceof AbstractCustomAction ? Transactions.getTransactionControl(TransactionControl.IMPLICIT, AbstractCustomAction.class, customAction, "initialize") : Transactions.getTransactionControl(TransactionControl.IMPLICIT, CustomAction.class, customAction, "initialize", CustomActionContext.class);
        customActionContext.initializeProgram(customAction, customActionContext, txControl, false);
        customActionContext.setState(new ProgramState(ProgramStatus.RUNNING, null));
        customActionContext.executeChecked(new AbstractContext.ThrowingRunnable() {

            @Override
            public void run() throws Exception {
                customAction.run();
            }
        });
        customActionContext.setState(new ProgramState(ProgramStatus.COMPLETED, null));
    } catch (Throwable t) {
        customActionContext.setState(new ProgramState(ProgramStatus.FAILED, Throwables.getRootCause(t).getMessage()));
        Throwables.propagateIfPossible(t, Exception.class);
        throw Throwables.propagate(t);
    } finally {
        TransactionControl txControl = Transactions.getTransactionControl(TransactionControl.IMPLICIT, CustomAction.class, customAction, "destroy");
        customActionContext.destroyProgram(customAction, customActionContext, txControl, false);
    }
}
Also used : CustomAction(co.cask.cdap.api.customaction.CustomAction) AbstractCustomAction(co.cask.cdap.api.customaction.AbstractCustomAction) TransactionControl(co.cask.cdap.api.annotation.TransactionControl) AbstractCustomAction(co.cask.cdap.api.customaction.AbstractCustomAction) CustomActionContext(co.cask.cdap.api.customaction.CustomActionContext) BasicCustomActionContext(co.cask.cdap.internal.app.runtime.customaction.BasicCustomActionContext) ProgramState(co.cask.cdap.api.ProgramState) AbstractContext(co.cask.cdap.internal.app.runtime.AbstractContext)

Aggregations

AbstractCustomAction (co.cask.cdap.api.customaction.AbstractCustomAction)2 CustomAction (co.cask.cdap.api.customaction.CustomAction)2 ProgramState (co.cask.cdap.api.ProgramState)1 TransactionControl (co.cask.cdap.api.annotation.TransactionControl)1 CustomActionContext (co.cask.cdap.api.customaction.CustomActionContext)1 PropertyFieldSetter (co.cask.cdap.common.lang.PropertyFieldSetter)1 AbstractContext (co.cask.cdap.internal.app.runtime.AbstractContext)1 DataSetFieldSetter (co.cask.cdap.internal.app.runtime.DataSetFieldSetter)1 MetricsFieldSetter (co.cask.cdap.internal.app.runtime.MetricsFieldSetter)1 BasicCustomActionContext (co.cask.cdap.internal.app.runtime.customaction.BasicCustomActionContext)1