All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fabric3.fabric.domain.LocalDeployer Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
/*
 * Fabric3
 * Copyright (c) 2009-2015 Metaform Systems
 *
 * 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.
 *
 * Portions originally based on Apache Tuscany 2007
 * licensed under the Apache 2.0 license.
 */
package org.fabric3.fabric.domain;

import java.util.List;
import java.util.ListIterator;

import org.fabric3.api.annotation.monitor.Monitor;
import org.fabric3.api.host.domain.DeploymentException;
import org.fabric3.api.model.type.component.Scope;
import org.fabric3.fabric.container.command.ExecutorNotFoundException;
import org.fabric3.spi.container.ContainerException;
import org.fabric3.spi.container.executor.CommandExecutorRegistry;
import org.fabric3.spi.container.component.InstanceLifecycleException;
import org.fabric3.spi.container.component.ScopeRegistry;
import org.fabric3.spi.domain.Deployer;
import org.fabric3.spi.domain.DeployerMonitor;
import org.fabric3.spi.domain.DeploymentPackage;
import org.fabric3.spi.container.command.Command;
import org.fabric3.spi.container.command.CompensatableCommand;
import org.fabric3.spi.domain.generator.DeploymentUnit;
import org.oasisopen.sca.annotation.Reference;

/**
 * A Deployer that sends DeploymentUnits to the local runtime instance.
 */
public class LocalDeployer implements Deployer {
    private CommandExecutorRegistry executorRegistry;
    private ScopeRegistry scopeRegistry;
    private DeployerMonitor monitor;

    public LocalDeployer(@Reference CommandExecutorRegistry executorRegistry, @Reference ScopeRegistry scopeRegistry, @Monitor DeployerMonitor monitor) {
        this.executorRegistry = executorRegistry;
        this.scopeRegistry = scopeRegistry;
        this.monitor = monitor;
    }

    public void deploy(DeploymentPackage deploymentPackage) throws DeploymentException {

        DeploymentUnit unit = deploymentPackage.getCurrentDeployment().getDeploymentUnit();
        List provisionCommands = unit.getProvisionCommands();
        execute(provisionCommands);

        // ignore extension commands since extensions will already be loaded locally
        List commands = unit.getCommands();
        execute(commands);
        try {
            if (scopeRegistry != null) {
                scopeRegistry.getScopeContainer(Scope.COMPOSITE).reinject();
            }
        } catch (InstanceLifecycleException e) {
            throw new DeploymentException(e);
        }
    }

    /**
     * Executes the commands, performing a rollback on error.
     *
     * @param commands the commands
     * @throws DeploymentException if a deployment error occurs
     */
    private void execute(List commands) throws DeploymentException {
        int marker = 0;
        for (Command command : commands) {
            try {
                executorRegistry.execute(command);
                ++marker;
            } catch (ContainerException e) {
                rollback(commands, marker);
                throw new DeploymentException(e);
            }
        }
    }

    /**
     * Rolls back the runtime state after a failed deployment by executing a collection of compensating commands.
     *
     * @param commands the deployment commands that failed
     * @param marker   the deployment command index where the failure occurred
     */
    private void rollback(List commands, int marker) {
        try {
            monitor.rollback("local");
            ListIterator iter = commands.listIterator(marker);
            while (iter.hasPrevious()) {
                CompensatableCommand command = iter.previous();
                Command compensating = command.getCompensatingCommand();
                executorRegistry.execute(compensating);
            }
            if (scopeRegistry != null) {
                scopeRegistry.getScopeContainer(Scope.COMPOSITE).reinject();
            }
        } catch (ExecutorNotFoundException ex) {
            // this is thrown when an error occurs during bootstrap: some of the command executors may not have be deployed at this point, which
            // results in an ExecutorNotFoundException. Log and ignore
            monitor.rollbackAborted();
        } catch (ContainerException ex) {
            monitor.rollbackError("local", ex);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy