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

📄 dummysyncsource.java

📁 SyncML部分资料
💻 JAVA
字号:
/* * Funambol is a mobile platform developed by Funambol, Inc. * Copyright (C) 2003 - 2007 Funambol, Inc. * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU Affero General Public License version 3 as published by * the Free Software Foundation with the addition of the following permission * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS. * * 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 Affero General Public License * along with this program; if not, see http://www.gnu.org/licenses or write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301 USA. * * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com. * * The interactive user interfaces in modified source and object code versions * of this program must display Appropriate Legal Notices, as required under * Section 5 of the GNU Affero General Public License version 3. * * In accordance with Section 7(b) of the GNU Affero General Public License * version 3, these Appropriate Legal Notices must retain the display of the * "Powered by Funambol" logo. If the display of the logo is not reasonably * feasible for technical reasons, the Appropriate Legal Notices must display * the words "Powered by Funambol". */package com.funambol.examples.engine.source;import java.io.Serializable;import java.security.Principal;import java.sql.Timestamp;import java.util.ArrayList;import com.funambol.framework.logging.FunambolLogger;import com.funambol.framework.logging.FunambolLoggerFactory;import com.funambol.framework.engine.source.*;import com.funambol.framework.engine.*;import com.funambol.framework.tools.beans.LazyInitBean;/** * This class implements a dummy <i>SyncSource</i> that just displays the calls * to its methods * * @version $Id: DummySyncSource.java,v 1.9 2007/11/28 12:15:00 nichele Exp $ */public class DummySyncSource extends AbstractSyncSourceimplements SyncSource, Serializable, LazyInitBean {    // -------------------------------------------------------------- Properties    // ------------------------------------------------------------ Private data    protected Principal principal = null;    private SyncItem[] allItems     = null;    private SyncItem[] newItems     = null;    private SyncItem[] deletedItems = null;    private SyncItem[] updatedItems = null;    private FunambolLogger log = FunambolLoggerFactory.getLogger("funambol.dummy");    private String itemType = null;    // ------------------------------------------------------------ Constructors    /** Creates a new instance of AbstractSyncSource */    public DummySyncSource() {    }    // ---------------------------------------------------------- Public methods    public void init() {        newItems     = new SyncItem[] {                           createItem("10",                                      "This is an NEW item",                                      SyncItemState.NEW)                       };        deletedItems = new SyncItem[] {                           createItem("20",                                      "This is an DELETED item",                                      SyncItemState.DELETED)                       };        updatedItems = new SyncItem[] {                           createItem("30",                                      "This is an UPDATED item",                                      SyncItemState.UPDATED)                       };        allItems = new SyncItem[newItems.length + updatedItems.length + 1];        allItems[0] = createItem("40",                                 "This is an unchanged item",                                 SyncItemState.SYNCHRONIZED);        allItems[1] = newItems[0];        allItems[2] = updatedItems[0];        SyncSourceInfo info = getInfo();        ContentType[] contentTypes = null;        if (info != null) {            contentTypes = info.getSupportedTypes();            if (contentTypes != null && contentTypes.length > 0) {                itemType = contentTypes[0].getType();            }        }    }    /**     * Returns a string representation of this object.     *     * @return a string representation of this object.     */    public String toString() {        StringBuffer sb = new StringBuffer(super.toString());        sb.append(" - {name: ").append(getName()     );        sb.append(" uri: "    ).append(getSourceURI());        sb.append('}'         );        return sb.toString();    }    /**     * SyncSource's beginSync()     *     * @param context the context of the sync     */    public void beginSync(SyncContext context)    throws SyncSourceException {        //        // Reset counters        //        super.beginSync(context);        this.principal = context.getPrincipal();    }    /*     * @see SyncSource     */    public SyncItemKey[] getAllSyncItemKeys() throws SyncSourceException {        if (log.isInfoEnabled()) {            log.info("getAllSyncItemKeys(" +                               principal   +                               ")");        }        return extractKeys(allItems);    }    /*     * @see SyncSource     */    public SyncItemKey[] getNewSyncItemKeys(Timestamp since, Timestamp until)    throws SyncSourceException {        if (log.isInfoEnabled()) {            log.info("getNewSyncItemKeys(" +                               principal   +                               " , "       +                               since       +                               " , "       +                               until       +                               ")");        }        return extractKeys(newItems);    }    /*     * @see SyncSource     */    public SyncItemKey[] getDeletedSyncItemKeys(Timestamp since, Timestamp until)    throws SyncSourceException {        if (log.isInfoEnabled()) {            log.info("getDeletedSyncItemKeys(" +                               principal          +                               " , "              +                               since              +                               " , "              +                               until              +                               ")");        }        return extractKeys(deletedItems);    }    /*     * @see SyncSource     */    public SyncItemKey[] getUpdatedSyncItemKeys(Timestamp since, Timestamp until)    throws SyncSourceException {        if (log.isInfoEnabled()) {            log.info("getUpdatedSyncItemKeys(" +                               principal          +                               " , "              +                               since              +                               " , "              +                               until              +                               ")");        }        return extractKeys(updatedItems);    }    /*     * @see SyncSource     */    public SyncItem getSyncItemFromId(SyncItemKey syncItemKey)    throws SyncSourceException {        if (log.isInfoEnabled()) {            log.info("getSyncItemsFromId(" +                               principal             +                               ", "                  +                               syncItemKey           +                               ")");        }        for(int i = 0; i < allItems.length; i++) {            if(allItems[i].getKey().equals(syncItemKey)) {                allItems[i].setType(itemType);                return allItems[i];            }        }        return null;    }    public void removeSyncItem(SyncItemKey syncItemKey, Timestamp time)    throws SyncSourceException {        if (log.isInfoEnabled()) {            log.info("removeSyncItem(" +                               principal    +                               " , "        +                               syncItemKey  +                               " , "        +                               time         +                               ")");        }        ArrayList list = new ArrayList();        for(int i = 0; i < allItems.length; i++) {            if(allItems[i].equals(syncItemKey) != true) {                list.add(allItems[i]);            }        }        allItems = (SyncItem[])list.toArray(new SyncItem[0]);    }    /*     * @see SyncSource     */    public void removeSyncItem(SyncItemKey syncItemKey,                               Timestamp   time       ,                               boolean     softDelete )    throws SyncSourceException {        if(softDelete) {            // implement soft deletion        } else {            removeSyncItem(syncItemKey, time);        }    }    /*     * @see SyncSource     */    public SyncItem updateSyncItem(SyncItem syncItem)    throws SyncSourceException {        if (log.isInfoEnabled()) {            log.info("updateSyncItem("                     +                               principal                          +                               " , "                              +                               syncItem.getKey().getKeyAsString() +                               ")");        }        SyncItem itemOnServer = getSyncItemFromId(syncItem.getKey());        if(itemOnServer != null) {            itemOnServer = syncItem;        }        syncItem.setType(itemType);        return syncItem;    }    /**     * @see SyncSource     */    public SyncItem addSyncItem(SyncItem syncItem) throws SyncSourceException {        if (log.isInfoEnabled()) {            log.info("addSyncItem("                     +                               principal                          +                               " , "                              +                               syncItem.getKey().getKeyAsString() +                               ")");        }        SyncItem itemOnServer = getSyncItemFromId(syncItem.getKey());        if(itemOnServer == null) {            ArrayList list = new ArrayList();            for(int i = 0; i < allItems.length; i++) {                list.add(allItems[i]);            }            list.add(syncItem);            allItems = (SyncItem[])list.toArray(new SyncItem[0]);        }        syncItem.setType(itemType);        return syncItem;    }    /**     * @see SyncSource     */    public SyncItemKey[] getSyncItemKeysFromTwin(SyncItem syncItem)    throws SyncSourceException {        return new SyncItemKey[0];    }    /**     * @see SyncSource     */    public void setOperationStatus(String operation, int statusCode, SyncItemKey[] keys) {        StringBuffer message = new StringBuffer("Received status code '");        message.append(statusCode).append("' for a '").append(operation).append("'").                append(" for this items: ");        for (int i = 0; i < keys.length; i++) {            message.append("\n- " + keys[i].getKeyAsString());        }        if (log.isInfoEnabled()) {            log.info(message.toString());        }    }    // --------------------------------------------------------- Private methods    private SyncItem createItem(String id, String content, char state) {        if (log.isTraceEnabled()) {            log.trace("Creating an sync item with the folowing properties: { "                       + " key = " + id                       + ", content = " + content                       + ", type = "    + itemType                       + ", state = "   + state + " }");        }        SyncItem item = new SyncItemImpl(this, id, state);        item.setContent(content.getBytes());        item.setType(itemType);        return item;    }    private SyncItemKey[] extractKeys(SyncItem[] items) {        SyncItemKey[] keys = new SyncItemKey[items.length];        for (int i=0; i<items.length; ++i) {            keys[i] = items[i].getKey();        }        return keys;    }}

⌨️ 快捷键说明

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