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

📄 enginehelper.java

📁 实现了SyncML无线同步协议
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                                              ModificationCommand cmd          ,                                              char                state        ,                                              long                timestamp    ) {        Item[] items = (Item[])cmd.getItems().toArray(new Item[0]);        if ((items == null) || (items.length == 0)) {            return new SyncItem[0];        }        SyncItem[] syncItems = new SyncItem[items.length];        Data   d       = null;        String content = null, key = null, mappedKey = null;        for (int i=0; i<items.length; ++i) {            d = items[i].getData();            content = (d != null) ? d.getData() : "";            key       = items[i].getSource().getLocURI();            mappedKey = clientMapping.getMappedValueForLuid(key);            //            // NOTE: for the purpose of sync items comparison, the mappedKey,            // when not null, becomes the real item key, whilst the old key            // becomes the mapped key.            //            if (mappedKey != null) {                String k = mappedKey;                mappedKey = key;                key = k;            }            syncItems[i] =                new SyncItemImpl(syncSource, key, mappedKey, state);            syncItems[i].setProperty(                new SyncProperty(SyncItemHelper.PROPERTY_COMMAND, cmd)            );            syncItems[i].setProperty(                new SyncProperty(SyncItem.PROPERTY_BINARY_CONTENT, content.getBytes())            );            syncItems[i].setProperty(                new SyncProperty(SyncItem.PROPERTY_TIMESTAMP, new Timestamp(timestamp))            );        }        return syncItems;    }    /**     * Calculate the intersection between the given lists, returning a new list     * of <i>SyncItemMapping</i>s each containing the corresponding item couple.     * Items are matched using the <i>get()</i> method of the <i>List</i> object,     * therefore the rules for matching items are the ones specified by tha     * <i>List</i> interface.     *     * @param a the first list - NOT NULL     * @param b the seconf list - NOT NULL     *     * @return the intersection mapping list.     */    public static List intersect(final List a, final List b) {        int       n   = 0;        SyncItem  itemA = null;        ArrayList ret = new ArrayList();        SyncItemMapping mapping = null;        Iterator i = a.iterator();        while(i.hasNext()) {            itemA = (SyncItem)i.next();            n = b.indexOf(itemA);            if (n >= 0) {                mapping = new SyncItemMapping(itemA.getKey());                mapping.setMapping(itemA, (SyncItem)b.get(n));                ret.add(mapping);            }        }        return ret;    }    /**     * Calculate and build the cross set Am(B-Bm) that is the set of synchronized     * items of the source B interested by the modifications of source A.     *     * @param items source A items     * @param source the B source     * @param principal the pricipal data are related to     *     * @return a list of <i>SyncItemMapping</i> representing the item set     */    public static List buildAmBBm(List       items    ,                                  SyncSource source   ,                                  Principal  principal) {        SyncItem itemA, itemB;        SyncItemMapping mapping = null;        ArrayList ret = new ArrayList();        Iterator i = items.iterator();        while (i.hasNext()) {            itemA = (SyncItem)i.next();            try {                itemB = source.getSyncItemFromId(principal, itemA.getKey());                if (itemB != null) {                    itemB.setState(SyncItemState.SYNCHRONIZED);                    mapping = new SyncItemMapping(itemA.getKey());                    mapping.setMapping(itemA, itemB);                    ret.add(mapping);                }            } catch (SyncSourceException e) {                String msg = "Error retrieving an item by its key "                           + itemA.getKey()                           + " from source "                           + source                           + ": "                           + e.getMessage();                log.severe(msg);                log.throwing("EngineHelper", "buildAmBBm", e);            }        }        return ret;    }     /**     * Calculate and build the cross set (A-Am)Bm that is the set of synchronized     * items of the source A interested by the modifications of source B.     *     * @param items source B items     * @param source the A source     * @param principal the pricipal data are related to     *     * @return a list of <i>SyncItemMapping</i> representing the item set     */    public static List buildAAmBm(List       items    ,                                  SyncSource source   ,                                  Principal  principal) {        SyncItem itemA, itemB;        SyncItemMapping mapping = null;        ArrayList ret = new ArrayList();        Iterator i = items.iterator();        while (i.hasNext()) {            itemB = (SyncItem)i.next();            try {                itemA = source.getSyncItemFromId(principal, itemB.getKey());                if (itemA != null) {                    itemA.setState(SyncItemState.SYNCHRONIZED);                    mapping = new SyncItemMapping(itemB.getKey());                    mapping.setMapping(itemA, itemB);                    ret.add(mapping);                }            } catch (SyncSourceException e) {                String msg = "Error retrieving an item by its key "                           + itemB.getKey()                           + " from source "                           + source                           + ": "                           + e.getMessage();                log.severe(msg);                log.throwing("EngineHelper", "buildAmBBm", e);            }        }        return ret;    }    /**     * Update the mapping given an array of operations. This is used for     * mappings sent by the client.     *     * @param clientMappings the client mappings for current synchronization     * @param operations the operation performed     * @param slowSync is true if slow synchronization     *     */    public static void updateClientMappings(Map             clientMappings,                                            SyncOperation[] operations    ,                                            boolean         slowSync      )    throws MappingException {        ClientMapping clientMapping = null;        String guid = null, luid = null;        SyncSource clientSource = null;        for (int i=0; ((operations != null) && (i<operations.length)); ++i) {            //            // Ignore conflicts            //            if (operations[i] instanceof SyncConflict) {                continue;            }            //            // Ignore NOPs if fast sync            //            if (!slowSync && (operations[i].getOperation() == SyncOperation.NOP)) {                continue;            }            clientSource = operations[i].getSyncItemA().getSyncSource();            if (clientSource == null) {                //                // clientSource is null for unexsisting items                //                continue;            }            clientMapping = (ClientMapping)clientMappings.get(clientSource.getSourceURI());            //            //this is the case of slow sync and without mappings into db            //            if (slowSync && operations[i].getOperation() != SyncOperation.DELETE) {                if (operations[i].getSyncItemA() != null &&                    operations[i].getSyncItemB() != null) {                    guid = operations[i].getSyncItemB().getKey().getKeyAsString();                    SyncItemKey sikA = operations[i].getSyncItemA().getMappedKey();                    if (sikA != null) {                        luid = sikA.getKeyAsString();                    } else {                        luid = operations[i].getSyncItemA().getKey().getKeyAsString();                    }                    clientMapping.updateMapping(luid , guid);                    continue;                }            } else if (operations[i].getOperation() == SyncOperation.DELETE) {                guid = operations[i].getSyncItemB().getKey().getKeyAsString();                clientMapping.removeMappedValuesForGuid(guid);            }        }    }    /**     * Updates the LUID-GUIDmapping for the client modifications (that is the     * items directlry inserted or removed by the server).     *     * @param clientMappings the client mappings for current synchronization     * @param status the status of the performed operations     * @param slowSync is true if slow synchronization     *     */    public static void updateServerMappings(Map                  clientMappings,                                            SyncOperationStatus[] status       ,                                            boolean slowSync                   )    throws MappingException {        char op;        ClientMapping clientMapping = null;        String luid = null, guid = null;        SyncSource serverSource = null;        SyncOperation operation;        for (int i=0; ((status != null) && (i<status.length)); ++i) {            operation = status[i].getOperation();            op = operation.getOperation();            //            // Ignore conflicts            //            if (operation instanceof SyncConflict) {                continue;            }            //            // Ignore NOPs if fast sync            //            if (!slowSync && op == SyncOperation.NOP) {                continue;            }            serverSource = status[i].getSyncSource();            if (serverSource == null) {                //                // clientSource is null for unexsisting items                //                continue;            }            clientMapping = (ClientMapping)clientMappings.get(serverSource.getSourceURI());            //            //this is the case of slow sync and without mappings into db            //            if (slowSync &&                operation.getOperation() != SyncOperation.DELETE &&                operation.getOperation() != SyncOperation.NEW) {                if (operation.getSyncItemA() != null &&                    operation.getSyncItemB() != null) {                    guid = operation.getSyncItemB().getKey().getKeyAsString();                    SyncItemKey sikA = operation.getSyncItemA().getMappedKey();                    if (sikA != null) {                        luid = sikA.getKeyAsString();                    } else {                        luid = operation.getSyncItemA().getKey().getKeyAsString();                    }                    clientMapping.updateMapping(luid , guid);                    continue;                }            } else if(op == SyncOperation.DELETE) {                    guid = operation.getSyncItemB().getKey().getKeyAsString();                    clientMapping.removeMappedValuesForGuid(guid);            } else if (op == SyncOperation.NEW) {                if (operation.isBOperation()) {                    luid = operation.getSyncItemA().getKey().getKeyAsString();                    guid = operation.getSyncItemB().getKey().getKeyAsString();                    clientMapping.updateMapping(luid, guid);                }            }        }    }    /**     * Creates a <i>DataStore</i> from a <i>SyncSourceInfo</i>     *     * @param uri the source URI     * @param info the <i>SyncSourceInfo</i>     *     * @return the corresponding <i>DataStore</i>     */    public static DataStore toDataStore(String uri, SyncSourceInfo info) {        ContentTypeInfo[] supportedContents = null;        ContentTypeInfo   preferredContent  = null;        if (info != null) {            ContentType[] supportedTypes = info.getSupportedTypes();            ContentType   preferredType  = info.getPreferredType() ;            supportedContents =                new ContentTypeInfo[supportedTypes.length];            for (int i=0; (i<supportedTypes.length); ++i) {                supportedContents[i] = new ContentTypeInfo (                    supportedTypes[i].type,                    supportedTypes[i].version                );            }            preferredContent =  new ContentTypeInfo(                preferredType.type,                preferredType.version            );        }        return new DataStore(            new SourceRef(uri),            null,            32,            preferredContent ,            supportedContents,            preferredContent ,            supportedContents,            null,            new SyncCap(SyncType.ALL_SYNC_TYPES)        );    }    /**     * Resets the item state of the items passed in the given collection to     * the <i>synchronized</i> state.     *     * @param items items collection     */    public static void resetState(Collection items) {        if (items == null) {            return;        }        Iterator i = items.iterator();        while (i.hasNext()) {            Object obj = i.next();            if (obj != null) {                ((SyncItem)obj).setState(SyncItemState.SYNCHRONIZED);            }        }    }}class StatusKey {    public ModificationCommand  cmd = null;    public int statusCode = 0;    public StatusKey(ModificationCommand cmd, int statusCode) {        this.cmd = cmd;        this.statusCode = statusCode;    }    public boolean equals(Object o){        if ((o != null) && (o instanceof StatusKey)) {            return (((StatusKey)o).cmd.getCmdID().equals(this.cmd.getCmdID()))                && (((StatusKey)o).statusCode == this.statusCode);        }        return false;    }    public int hashCode() {        return ( String.valueOf(cmd.getCmdID().getCmdID())               + String.valueOf(statusCode)               ).hashCode();    }}

⌨️ 快捷键说明

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