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

📄 basesyncsource.java

📁 SyncML的java实现类库 funambol公司发布的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            Log.info("Source "+getName()+": sending item "
                     +updItems[updIndex].getKey());
            return getItemContent(updItems[updIndex++]);
        }
        else {
            Log.info("Source "+getName()+": no more updated items to send");
            // All Items sent, we can free memory
            updItems = null;
            updIndex = 0;
            return null;
        }
    }
    
    /** 
     * Returns a SyncItem containing the key of the first/next
     * deleted item of the store (locally removed after the last sync,
     * but not yet deleted on server)
     */
    public SyncItem getNextDeletedItem() throws SyncException {
        if (delItems == null) {
            Log.info("Source "+getName()+": no deleted items to send");
            return null;
        }
        
        if (delIndex<delItems.length) {
            Log.info("Source "+getName()+": sending item "
                     +delItems[delIndex].getKey());
            // No need to get the content here
            return delItems[delIndex++];
        }
        else {
            Log.info("Source "+getName()+": no more deletetd items to send");
            // All Items sent, we can free memory
            delItems = null;
            delIndex = 0;
            return null;
        }
    } 
    
    /**
     * Tell the SyncSource the status returned by the server 
     * for an Item previously sent.
     * This is a dummy implementation that just logs the status.
     * A concrete implementation can override this method to perform
     * some checks on the received status.
     *
     * @param key the key of the item
     * @param status the status code received for that item
     *
     * @throws SyncException if the SyncSource wants to stop the sync
     */
    public void setItemStatus(String key, int status)
    throws SyncException {
        Log.info("Status " + status + "for item " + key + "from server.");
    }

    /**
     * Return the number of changes that the client will send during the
     * session. This method, after the beginSync() call, should return
     * the number of items to be sent to the server.
     *
     * The number of changes is computed by initXXXItems() during beginSync().
     *
     * @return number of items to sent, or -1 if unknown
     */
    public int getClientItemsNumber() {
        return clientItemsNumber;
    }

    /**
     * Return the number of changes that the server will send during the
     * session. This method, after the beginSync() call, should return
     * the number of items to be sent to the server.
     *
     * @return number of changes from the server, or -1 if not announced.
     */
    public int getServerItemsNumber() {
        return serverItemsNumber;
    }

    /**
     * Set the number of changes that the server will send during the
     * session. This method is called by the engine to notify the Source
     * of the number of changes announced by the server. If the server
     * does not announce the number of changes, the engine will call
     * this method with parameter -1.
     *
     * @param number of changes from the server, or -1 if not announced.
     */
    public void setServerItemsNumber(int number) {
        serverItemsNumber = number;
    }

    /**
     * Default implementation for
     */
    public void dataReceived(String date, int size) {
        Log.info("Received " + size + "bytes.");
    }


    /** 
     * Return the Last Anchor for this source
     */
    public long getLastAnchor() {
        return config.getLastAnchor();
    }
    
    /** 
     * Set the value of the Last Anchor for this source
     */
    public void setLastAnchor(long time) {
        config.setLastAnchor(time);
    }
    
    /** 
     * Return the Next Anchor for this source
     */
    public long getNextAnchor() {
        return config.getNextAnchor();
    }
    
    /** 
     * Set the value of the Next Anchor for this source
     */
    public void setNextAnchor(long time) {
        config.setNextAnchor(time);
    }

    /**
     * Called after SyncManager preparation and initialization just before start
     * the synchronization of the SyncSource.
     *
     * @param syncMode the synchronization type: one of the values in
     *                 sync4j.framework.core.AlertCode
     *
     * @throws SyncException in case of error. This will stop the sync process
     */
    public void beginSync(int syncMode) throws SyncException {
        Log.info("Begin sync for source '" + getName() +
                 "' with mode " + syncMode);

        // Init lists
        switch(syncMode) {
            case SyncML.ALERT_CODE_SLOW:
            case SyncML.ALERT_CODE_REFRESH_FROM_CLIENT:
                // A refresh from client is like a slow here
                initAllItems();
                allIndex = 0;
                // Init number of changes counter
                clientItemsNumber = (allItems != null) ? allItems.length : 0 ;
                break;
            case SyncML.ALERT_CODE_FAST:
            case SyncML.ALERT_CODE_ONE_WAY_FROM_CLIENT:
                // A one way from client is like a fast here
                initNewItems();
                initUpdItems();
                initDelItems();
                newIndex = updIndex = delIndex = 0;
                // Init number of changes counter
                clientItemsNumber = (newItems != null) ? newItems.length : 0 ;
                clientItemsNumber += (updItems != null) ? updItems.length : 0 ;
                clientItemsNumber += (delItems != null) ? delItems.length : 0 ;
                break;
            case SyncML.ALERT_CODE_ONE_WAY_FROM_SERVER:
                // No modifications to send (it's not
                // strictly necessary to reset the lists,
                // because the engine will not ask items to
                // the SyncSource, but it's good to do it)
                newItems = null;
                updItems = null;
                delItems = null;
                newIndex = updIndex = delIndex = 0;
                // Init number of changes counter
                clientItemsNumber = 0;
                break;
            case SyncML.ALERT_CODE_REFRESH_FROM_SERVER:
                // In this case, the SyncSource should
                // delete all the items in the database
                // (possibly asking the user before that)
                // No modifications to send.
                newItems = null;
                updItems = null;
                delItems = null;
                newIndex = updIndex = delIndex = 0;
                // Init number of changes counter
                clientItemsNumber = 0;
                break;
            default:
                throw new SyncException(SyncException.SERVER_ERROR,
                                        "SyncSource "+getName()+
                                        ": invalid sync mode "+getSyncMode());
        }

        this.syncMode = syncMode;
    }

    /**
     * Called just before committing the synchronization process by the
     * SyncManager. The SyncSource can stop the commit phase raising an
     * exception here.
     *
     * @throws SyncException in case of error, to stop the commit.
     */
    public void endSync() throws SyncException  {
        Log.info("End sync for source " + getName());
        // Release resources
        allItems = newItems = updItems = delItems = null;
        allIndex = newIndex = updIndex = delIndex = 0;
    }

    /* ----------------------------------------------------------------------
     * The following methods must be implemented by the concrete 
     * implementation of BaseSyncSource to perform the real modification
     * detection, based on the source type.
     */

    /**
     * In a concrete implementation, this function should search the database
     * for all the items present and store their keys.
     *
     * @throws SyncException implementation can throw a SyncException 
     *                       to stop the sync on fatal errors.
     */
    protected abstract void initAllItems() throws SyncException;

    /**
     * In a concrete implementation, this function should search the database
     * for the new items present and store their keys.
     *
     * @throws SyncException implementation can throw a SyncException 
     *                       to stop the sync on fatal errors.
     */
    protected abstract void initNewItems() throws SyncException;

    /**
     * In a real implementation, this function should search the database
     * for the modified items present and store their keys.
     * The policy to detect a change can vary from one source to another:
     * from generating a CRC to keep the status in a field of the item in
     * the backend database.
     *
     * @throws SyncException implementation can throw a SyncException 
     *                       to stop the sync on fatal errors.
     */
    protected abstract void initUpdItems() throws SyncException ;

    /**
     * In a real implementation, this function should search the database
     * for the deleted items present and store their keys.
     * The policy to detect a deleted item can vary from one source to another:
     * from keeping a list of items after the last sync to keep the items with
     * a deleted flag and then remove them after the successful deletion on
     * the server.
     *
     * @throws SyncException implementation can throw a SyncException 
     *                       to stop the sync on fatal errors.
     */
    protected abstract void initDelItems() throws SyncException ;

    /**
     * This function gets the item content in the backend database and
     * returns a complete item. The parameter item is marked final because
     * should not be used for the filled item: it is a reference to the
     * array entry, and filling it would cause the array to keep all the
     * filled items at the end (the gc will not dispose them). <p>
     * The content of the item depends also from the encoding of this
     * SyncSource:
     * <li> if the encoding is <i>none</i>, it must be a String, converted
     *      with getBytes(), so the engine will send it unchanged.
     * <li> if the encoding is <i>b64</i>, the content can be binary, and the 
     *      type should be set accordingly, so that the receiving source
     *      can handle it. In this way, the binary content is transferred
     *      encoded in the SyncML message. This encoding can be applied to
     *      a test item too, to avoid problems with charset or other, like
     *      what is done with the SIF format.
     */
    protected abstract SyncItem getItemContent(final SyncItem item)
    throws SyncException ;

}

⌨️ 快捷键说明

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