Package org.jayasoft.woj.server.services.synchronization

Source Code of org.jayasoft.woj.server.services.synchronization.AbstractSynchronizationService

/*
* This file is subject to the licence found in LICENCE.TXT in the root directory of the project.
* Copyright Jayasoft 2005 - All rights reserved
*
* #SNAPSHOT#
*/
package org.jayasoft.woj.server.services.synchronization;

import java.util.Timer;
import java.util.TimerTask;

import org.apache.log4j.Logger;
import org.jayasoft.woj.common.model.Server;
import org.jayasoft.woj.common.model.content.ModuleDescriptor;
import org.jayasoft.woj.common.model.security.Key;
import org.jayasoft.woj.common.services.ServerServicesProvider;
import org.jayasoft.woj.common.services.ServiceException;
import org.jayasoft.woj.server.WOJServer;
import org.jayasoft.woj.server.services.AbstractWOJService;
import org.jayasoft.woj.server.services.SynchronizationService;

import fr.jayasoft.commons.logging.LoggingManager;

public abstract class AbstractSynchronizationService extends AbstractWOJService implements SynchronizationService {
    private static final String SYNC_SERVERS_PERIOD = "synchronization.sync.servers.period";

    private static final String LAST_MD_SYNC = "synchronization.last.md.sync";

    private static Logger LOGGER = LoggingManager.getLogger(AbstractSynchronizationService.class.getName());
   
    private Timer _timer;
    private long _syncServersPeriod;

    private long _lastMDSync;
   
    public void activate() {
        super.activate();
        setLastMDSync(getConfiguration().getLong(LAST_MD_SYNC, 0));
        setSyncServersPeriod(getConfiguration().getLong(SYNC_SERVERS_PERIOD, 1000 * 60 * 60));
    }

    public void syncWith(Server server, boolean force) {
        LOGGER.info("syncing with "+server);
        syncPrivateKey(server, force);
        syncServers(server, force);
        syncModuleDescriptors(server, force);
    }
   
    public void syncPrivateKey(Server server, boolean force) {
        LOGGER.info("syncing private key with "+server);
        try {
            Key newPrivateKey = ServerServicesProvider.get(server).getSynchronizationService().getPrivateKey();
            if (newPrivateKey != null) {
                WOJServer.getInstance().getServerManagementService().setNewPrivateKey(newPrivateKey);
            }
        } catch (ServiceException e) {
            LOGGER.warn("a problem occured while synchronizing private key with "+server, e);
        }       
    }

    public void syncServers(Server server, boolean force) {
        LOGGER.info("syncing servers with "+server);
        try {
           
            Server[] servers = ServerServicesProvider.get(server).getSynchronizationService().getServers();
            if (servers != null) {
               
                WOJServer.getInstance().getServerManagementService().updateServers(servers);
            }
        } catch (ServiceException e) {
            LOGGER.warn("a problem occured while synchronizing servers with "+server, e);
        }
    }

    public void syncModuleDescriptors(Server server, boolean force) {
        LOGGER.info("syncing module descriptors with "+server);
        try {
            ModuleDescriptor[] mds = ServerServicesProvider.get(server).getSynchronizationService().getModuleDescriptorsSince(getLastMDSync());
            if (mds != null) {
                if (mds.length == 0) {
                    LOGGER.info("syncing module descriptors with "+server+": nothing changed");
                } else {
                    LOGGER.info("syncing module descriptors with "+server+": "+mds.length+" module(s) changed");
                    WOJServer.getInstance().getModuleManagementService().updateModuleDescriptors(mds);
                    setLastMDSync(getMostRecent(mds));
                    getConfiguration().save();
                }
            }
        } catch (ServiceException e) {
            LOGGER.warn("a problem occured while synchronizing servers with "+server, e);
        }
    }

    private long getMostRecent(ModuleDescriptor[] mds) {
        long mostRecent = 0;
        for (int i = 0; i < mds.length; i++) {
            if (mds[i].getTimestamp() > mostRecent) {
                mostRecent = mds[i].getTimestamp();
            }
        }
        return mostRecent;
    }

    private void refreshTimer() {
        if (_timer != null) {
            _timer.cancel();
        }
        _timer = new Timer(true);
        _timer.schedule(new TimerTask() {
            public void run() {
                syncServers();
            }

        }, getSyncServersPeriod(), getSyncServersPeriod());
    }

    public long getSyncServersPeriod() {
        return _syncServersPeriod;
    }

    public void setSyncServersPeriod(long period) {
        _syncServersPeriod = period;
        getConfiguration().setLong(SYNC_SERVERS_PERIOD, period);
        refreshTimer();
    }

    public void startup() {
    }

    private long getLastMDSync() {
        return _lastMDSync;
    }

    private void setLastMDSync(long lastMDSync) {
        _lastMDSync = lastMDSync;
        getConfiguration().setLong(LAST_MD_SYNC, lastMDSync);
    }

   
}
TOP

Related Classes of org.jayasoft.woj.server.services.synchronization.AbstractSynchronizationService

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.