⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basesyncsource.java

📁 SyncML的java实现类库 funambol公司发布的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (C) 2006-2007 Funambol
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */

package com.funambol.syncml.client;

import com.funambol.syncml.spds.SourceConfig;
import com.funambol.syncml.spds.SyncItem;
import com.funambol.syncml.spds.SyncSource;
import com.funambol.syncml.spds.SyncException;

import com.funambol.syncml.protocol.SyncFilter;
import com.funambol.syncml.protocol.SyncML;

import com.funambol.util.Log;

/**
 * An abstract implementation of the <i>SyncSource</i> interface, providing
 * the basic framework each SyncSource has to implement. 
 * A developer can choose to extends BaseSyncSource or to implements
 * SyncSource directly if needed.
 *
 * The class BaseSyncSource uses the SyncConfig to store the source 
 * configuration data. With this class is possible to alter the source
 * configuration, which is not permitted by the SyncSource interface.
 */
public abstract class BaseSyncSource implements SyncSource {
    
    //--------------------------------------------------------------- Attributes

    /** SyncSource configuration */
    protected SourceConfig config;

    /** Synchronization filter */
    protected SyncFilter filter;

    /** SyncMode, set by beginSync */
    protected int syncMode;

    // Item lists
    protected SyncItem[] allItems, newItems, updItems, delItems;

    // Lists counters
    protected int allIndex, newIndex, updIndex, delIndex;

    /** The number of items to be sent to the server in the session */
    private int clientItemsNumber;
    
    /** The number of items that the server announced to send in the session */
    private int serverItemsNumber;


    //------------------------------------------------------------- Constructors

    /**
     * BaseSyncSource constructor: initialize source config
     */
    public BaseSyncSource(SourceConfig config) {
        this.config = config;
        
        syncMode = 0;
        
        // Init lists (empty)
        allItems = null;
        newItems = null;
        updItems = null;
        delItems = null;

        // Init counters
        allIndex = newIndex = updIndex = delIndex = 0;

        // Init number of chages counters
        clientItemsNumber = serverItemsNumber = -1;

        filter = null;
    }

    //----------------------------------------------------------- Public Methods

    /**
     * Returns the config of the source. The client can use this method
     * to obtain the config object and change some parameter. A setConfig()
     * must be called to actually change the source configuration.
     *
     * @return the config of the source
     */
    public SourceConfig getConfig() {
        return config;
    }

    /**
     * Returns the config of the source. The client can use this method
     * to change the config the source configuration.
     * This operation should not be done while the sync is in progress.
     *
     * @return the config of the source
     */
    public void setConfig(SourceConfig config) {
        this.config = config;
    }
    
    //------------------------------------------------ SyncSource implementation

    /**
     * Returns the name of the source
     *
     * @return the name of the source
     */
    public String getName() {
        return config.getName();
    }

    /**
     * Returns the source URI
     *
     * @return the absolute URI of the source
     */
    public String getSourceUri() {
        return config.getRemoteUri();
    }

    /**
     * Returns the type of the source.
     * The types are defined as mime-types, for instance * text/x-vcard).
     * @return the type of the source
     */
    public String getType() {
        return config.getType();
    }

    /**
     * Returns the encoding of the source.
     * The encoding can be 'b64' or 'none' only. The standard defines
     * also 'des' and '3des' but they are not implemented in this version
     * of the APIs.
     *
     * @return the encoding of the source
     */
    public String getEncoding() {
        return config.getEncoding();
    }

    /**
     * Returns the preferred sync mode of the source.
     * The preferred sync mode is the one that the SyncManager sends
     * to the server in the initialization phase. The server can respond
     * with a different alert code, to force, for instance, a slow.
     *
     * @return the preferred sync mode for this source
     */
    public int getSyncMode() {
        return config.getSyncMode();
    }

    /**
     * Returns the current filter for this SyncSource.
     */
    public SyncFilter getFilter() {
        return filter;
    }

    /**
     * Set a new filter for this SyncSource
     */
    public void setFilter(SyncFilter filter) {
        this.filter = filter;
    }

    /**
     * Add a new SyncItem to this source backend.
     * The item key after a successful add must contain the local UID,
     * that is used by the engine to send the mappings to the server.
     * The source must then change the item key accordingly before return.
     *
     * @param item the SyncItem to add, with the GUID sent by the server.
     *             The source is resposible to set it to the LUID before
     *             returning a successful status code.
     *
     * @return the status code of the operation. It will be returned to
     *         the server in the response for this item.
     *
     * @throws SyncException if an unrecoverable error occur, to stop the sync
     */
    public abstract int addItem(SyncItem item) throws SyncException ;
    
    /**
     * Update a given SyncItem stored in the source backend.
     *
     * @param item the SyncItem to update. The key of the item is already
     *             the LUID.
     *
     * @return the status code of the operation. It will be returned to
     *         the server in the response for this item.
     *
     * @throws SyncException if an unrecoverable error occur, to stop the sync
     */
    public abstract int updateItem(SyncItem item) throws SyncException ;
    
    /**
     * Delete a SyncItem stored in the source backend.
     *
     * @param key The key of the item to delete.
     *
     * @return the status code of the operation. It will be returned to
     *         the server in the response for this item.
     *
     * @throws SyncException if an unrecoverable error occur, to stop the sync
     */
    public abstract int deleteItem(String key) throws SyncException ;
    
    /** 
     * Returns the next item of the store.
     **/
    public SyncItem getNextItem() throws SyncException {
        if (allItems == null) {
            Log.info("Source "+getName()+": no items to send for slow sync");
            return null;
        }
        
        if (allIndex<allItems.length) {
            Log.info("Source "+getName()+": sending item "
                     +allItems[allIndex].getKey());
            return getItemContent(allItems[allIndex++]);
        }
        else {
            Log.info("Source "+getName()+": no more items to send for slow sync");
            // All Items sent, we can free memory
            allItems = null;
            allIndex = 0;
            return null;
        }
    }
    
    /**
     * Returns the next new item of the store
     * (not yet sent to the server)
     */
    public SyncItem getNextNewItem() throws SyncException {
        if (newItems == null) {
            Log.info("Source "+getName()+": no new items to send");
            return null;
        }
        
        if (newIndex<newItems.length) {
            Log.info("Source "+getName()+": sending item "
                     +newItems[newIndex].getKey());
            return getItemContent(newItems[newIndex++]);
        }
        else {
            Log.info("Source "+getName()+": no more new items to send");
            // All Items sent, we can free memory
            newItems = null;
            newIndex = 0;
            return null;
        }
    }
    
    
    /**
     * Returns the first/next updated item of the store
     * (changed from the last sync)
     */
    public SyncItem getNextUpdatedItem() throws SyncException {
        if (updItems == null) {
            Log.info("Source "+getName()+": no updated items to send");
            return null;
        }
        
        if (updIndex<updItems.length) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -