Package org.wso2.carbon.deployment.synchronizer

Source Code of org.wso2.carbon.deployment.synchronizer.DeploymentSynchronizer$AutoSyncTask

/*
*  Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  WSO2 Inc. 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.wso2.carbon.deployment.synchronizer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.registry.core.Collection;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.session.UserRegistry;
import org.wso2.carbon.registry.synchronization.RegistrySynchronizer;
import org.wso2.carbon.registry.synchronization.SynchronizationException;

/**
* This class is responsible for maintaining a repository in the file system (the 'deployment') in
* sync with a set of resources stored in the registry. The deployment could be an artifact
* repository used by Carbon, Axis2 or Synapse. Alternatively it could be any directory hierarchy
* stored in the local file system. This implementation can commit the changes in the local file
* system to the registry, and checkout any updated artifacts from the registry to the local
* file system. Commit operations and checkout operations are synchronized so that both operations
* will never run at the same time.
*/
public class DeploymentSynchronizer {

    private static final Log log = LogFactory.getLog(DeploymentSynchronizer.class);

    private UserRegistry registry;

    /**
     * Location in the registry where the artifacts are stored
     */
    private String registryPath;

    /**
     * Location in the file system where the repository is located
     */
    private String filePath;

    private long lastCommitTime = -1L;
    private long lastCheckoutTime = -1L;

    private boolean autoCommit;
    private boolean autoCheckout;

    private AutoCheckoutController autoCheckoutController = new DefaultAutoCheckoutController();

    private long period = DeploymentSynchronizerConstants.DEFAULT_AUTO_SYNC_PERIOD;

    public DeploymentSynchronizer(UserRegistry registry, String registryPath, String filePath) {
        this.registry = registry;
        this.registryPath = registryPath;
        this.filePath = filePath;

        try {
            if (!registry.resourceExists(registryPath)) {
                Collection collection = registry.newCollection();
                registry.put(registryPath, collection);
                collection.discard();
            }
        } catch (RegistryException e) {
            log.error("Error while creating the registry collection at: " + registryPath, e);
        }
    }

    /**
     * Start this DeploymentSynchronizer instance. If the autoCommit and autoCheckout modes are not
     * enabled, this method will do nothing. If such additional features are enabled, this method
     * will schedule the necessary periodic tasks and get the auto-sync tasks up and running.
     */
    public void start() {
        if (!autoCommit && !autoCheckout) {
            return;
        }

        if (log.isDebugEnabled()) {
            log.debug("Starting a synchronizer on file system at: " + filePath);
        }

        try {
            doInitialSyncUp();
            if (autoCheckout && autoCheckoutController != null) {
                autoCheckoutController.initAutoCheckout();
            }
            Runnable task = new AutoSyncTask();
            DeploymentSynchronizationManager.getInstance().
                    scheduleSynchronizationTask(filePath, task, period, period);
        } catch (SynchronizationException e) {
            log.error("Error while performing the initial sync-up on the repository at: " +
                    filePath + ". Auto sync tasks will not be engaged.", e);
        } catch (DeploymentSynchronizerException e) {
            log.error("Error while initializing auto checkout mode on the repository at: " +
                    filePath + ". Auto sync tasks will not be engaged", e);
        }
    }

    public void stop() {
        if (autoCommit || autoCheckout) {
            if (log.isDebugEnabled()) {
                log.debug("Terminating the synchronizer on file system at: " + filePath);
            }
            DeploymentSynchronizationManager.getInstance().cancelSynchronizationTask(filePath);

            if (autoCheckout && autoCheckoutController != null) {
                autoCheckoutController.cleanup();
            }
        }
    }

    /**
     * Commit the artifacts in the file system repository to the registry
     *
     * @throws SynchronizationException If an error occurs while committing the artifacts
     */
    public synchronized void commit() throws SynchronizationException {
        if (log.isDebugEnabled()) {
            log.debug("Committing artifacts at " + filePath + " to the collection at " +
                    registryPath);
        }
        RegistrySynchronizer.checkIn(registry, filePath, false);
        lastCommitTime = System.currentTimeMillis();
    }

    /**
     * Checkout the artifacts stored in the registry to the file system. If the artifacts
     * have already been checked out, an update will be executed instead.
     *
     * @throws SynchronizationException If an error occurs while checking out or updating the resources
     */
    public synchronized void checkout() throws SynchronizationException {
        if (log.isDebugEnabled()) {
            log.debug("Checking out artifacts from " + registryPath + " to the file system " +
                    "at " + filePath);
        }

        if (RegistrySynchronizer.isCheckedOut(filePath)) {
            RegistrySynchronizer.update(registry, filePath, false);
        } else {
            RegistrySynchronizer.checkOut(registry, filePath, registryPath);
        }
        lastCheckoutTime = System.currentTimeMillis();
    }

    public void setAutoCommit(boolean autoCommit) {
        this.autoCommit = autoCommit;
    }

    public void setAutoCheckout(boolean autoCheckout) {
        this.autoCheckout = autoCheckout;
    }

    public void setPeriod(long period) {
        this.period = period;
    }

    public boolean isAutoCommit() {
        return autoCommit;
    }

    public boolean isAutoCheckout() {
        return autoCheckout;
    }

    public String getRegistryPath() {
        return registryPath;
    }

    public long getLastCheckoutTime() {
        return lastCheckoutTime;
    }

    public long getLastCommitTime() {
        return lastCommitTime;
    }

    public AutoCheckoutController getAutoCheckoutController() {
        return autoCheckoutController;
    }

    public void setAutoCheckoutController(AutoCheckoutController autoCheckoutController) {
        this.autoCheckoutController = autoCheckoutController;
    }

    private void doInitialSyncUp() throws SynchronizationException {
        if (autoCommit) {
            if (lastCheckoutTime == -1L) {
                checkout();
            }
            commit();
        }

        if (autoCheckout && lastCheckoutTime == -1L) {
            checkout();
        }
    }

    private class AutoSyncTask implements Runnable {

        private AutoSyncTask() {
            if (log.isDebugEnabled()) {
                log.debug("Initializing auto sync task for the repository at: " + filePath);
            }
        }

        public void run() {
            try {
                if (autoCheckout && autoCheckoutController != null &&
                        autoCheckoutController.checkoutRequested()) {
                    checkout();
                }

                if (autoCommit) {
                    commit();
                }
            } catch (SynchronizationException e) {
                log.error("Registry synchronization error encountered in the repository " +
                        "at: " + filePath, e);
            } catch (Throwable t) {
                log.error("Unexpected runtime error encountered while synchronizing the " +
                        "repository: " + filePath, t);
            }
        }
    }
}
TOP

Related Classes of org.wso2.carbon.deployment.synchronizer.DeploymentSynchronizer$AutoSyncTask

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.