package org.biomoby.registry.sync;

import java.util.Properties;
import java.util.logging.Logger;

import org.biomoby.client.CentralDigestImpl;
import org.biomoby.registry.sync.logging.MobySecondaryLogging;
import org.biomoby.shared.MobyException;

/**
 * Factory class to enable the access to the Central implementation of Biomoby. The static class provides access to the
 * local and the master central.
 * 
 * @author groscurt
 */
public final class CentralFactory {
    private static CentralDigestImpl masterCentral;
    private static CentralDigestImpl localCentral;

    private static Logger logger = Logger.getLogger( MobySecondaryLogging.LOGGER_NAME );

    private CentralFactory() {
    }

    /**
     * Initialise the master and the local centrals. The URLs and URIs for the centrals are given by the configuration.
     * 
     * @param configuration
     *            the information about the endpoints of the centrals
     * @throws MobyException
     *             whether the initialisation of the centrals failed
     */
    public static void init( Properties configuration ) throws MobyException {
        masterCentral = new CentralDigestImpl( configuration.getProperty( "central.url" ), configuration
                .getProperty( "central.uri" ) );
        logger.fine( "Initialise the master central " + masterCentral );

        localCentral = new CentralDigestImpl( configuration.getProperty( "local.url" ), configuration
                .getProperty( "local.uri" ) );
        logger.fine( "Initialise the local central " + localCentral );
    }

    /**
     * Returns the local central. This central will be updated in the process
     * 
     * @return the local central
     * @throws MobyException
     *             whether the local central is not initialised
     */
    public static CentralDigestImpl getLocalCentral() throws MobyException {
        if ( localCentral == null ) {
            throw new MobyException( "The local central was not initialised correctly !" );
        }
        return localCentral;
    }

    /**
     * Returns the master central. This central will be the central to get the update information from
     * 
     * @return the master central
     * @throws MobyException
     *             whether the master central is not initialised
     */
    public static CentralDigestImpl getMasterCentral() throws MobyException {
        if ( masterCentral == null ) {
            throw new MobyException( "The master central was not initialised correctly !" );
        }
        return masterCentral;
    }
}
