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

📄 sync4jstrategy.java

📁 实现了SyncML无线同步协议
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                if (log.isLoggable(Level.FINEST)) {            log.finest("Newly mapped items: " + newlyMappedItems);        }                Am = new ArrayList(); Bm = new ArrayList();        Am.addAll(Arrays.asList(newA      ));         Am.addAll(Arrays.asList(updatedA  ));        Am.addAll(Arrays.asList(deletedA  ));        Bm.addAll(Arrays.asList(newB      ));         Bm.addAll(Arrays.asList(updatedB  ));        Bm.addAll(Arrays.asList(deletedB  ));                if (log.isLoggable(Level.FINEST)) {            log.finest("Am: " + Am);            log.finest("Bm: " + Bm);            log.finest("Am-Bm: " + ListUtils.subtract(Am, Bm));            log.finest("Bm-Am: " + ListUtils.subtract(Bm, Am));        }                //        // Now calculate subsets: AmBm, AmBBm, AAmBm        //        AmBm  = EngineHelper.intersect(Am, Bm);        AmBBm = EngineHelper.buildAmBBm(ListUtils.subtract(Am, Bm), sources[0], principal);        AAmBm = EngineHelper.buildAAmBm(ListUtils.subtract(Bm, Am), sources[1], principal);                if (log.isLoggable(Level.FINEST)) {            log.finest("AmBm:  " + AmBm );            log.finest("AmBBm: " + AmBBm);            log.finest("AAmBm: " + AAmBm);        }                //        // Ready for conflict detection!        //        syncOperations =             checkSyncOperations(principal, Am, Bm, AmBm, AmBBm, AAmBm);                if (log.isLoggable(Level.FINEST)) {            log.finest("operations: " + syncOperations);        }                log.info("Preparation completed.");                return (SyncOperation[])syncOperations.toArray(new SyncOperationImpl[] {});    }    /**     * Implements Synchronizable.sync     *     *      */    public SyncOperationStatus[] sync(SyncOperation[] syncOperations) {        Logger log = Logger.getLogger(LOG_NAME);                log.info("Synchronizing...");                if ((syncOperations == null) || (syncOperations.length == 0)) {            return new SyncOperationStatus[0];        }                ArrayList status = new ArrayList();                SyncOperationStatus[] operationStatus = null;        for(int i=0; i<syncOperations.length; ++i) {            if (log.isLoggable(Level.FINE)) {                log.fine("Executing " + syncOperations[i]);            }                        //            // execSyncOperation can return more than one status for one             // operation when more than one source are involved            //            operationStatus = execSyncOperation((SyncOperationImpl)syncOperations[i]);                        for (int j=0; j<operationStatus.length; ++j) {                status.add(operationStatus[j]);            }  // next j        } // next i                log.fine("Synchronization completed.");        if (log.isLoggable(Level.FINE)) {            log.fine("status: " + status);        }        return (SyncOperationStatus[])status.toArray(new SyncOperationStatus[0]);    }            /**     * Implements Synchronizable.endSync     *     * @see beanblue.sync.event.Synchronizable     */    public void endSync() throws SyncException {        Logger log = Logger.getLogger(LOG_NAME);                log.info("Ending synchronization...");        log.info("Synchronization ended.");    }        // -------------------------------------------------------- Protected methds    /**     * Checks the given SyncItem lists and creates the needed SyncOperations     * following the rules described in the class descriprio and in the .     *     * @param principal who has requested the synchronization     * @param AmBm     * @param AmBBm     * @param AAmBm     *     * @return an ArrayList containing all the collected sync operations     */    protected ArrayList checkSyncOperations(Principal principal,                                            List      Am       ,                                            List      Bm       ,                                            List      AmBm     ,                                            List      AmBBm    ,                                             List      AAmBm    ) {        SyncItemMapping mapping = null;        SyncItem syncItemA = null, syncItemB = null;                ArrayList all        = new ArrayList();        ArrayList operations = new ArrayList();                        // ---------------------------------------------------------------------                all.addAll(AmBm );        all.addAll(AmBBm);        all.addAll(AAmBm);                //        // 1st check: items in both sources        //        Iterator i = all.iterator();        while (i.hasNext()) {            mapping = (SyncItemMapping)i.next();                        syncItemA = mapping.getSyncItemA();            syncItemB = mapping.getSyncItemB();                        operations.add(                checkSyncOperation(                    principal,                     syncItemA,                    syncItemB                )            );            Am.remove(syncItemA);            Bm.remove(syncItemB);        }                //        // 2nd check: items in source A and not in source B        //        i = Am.iterator();        while (i.hasNext()) {           syncItemA = (SyncItem)i.next();                       operations.add(checkSyncOperation(principal, syncItemA, null));        }  // next i                 //        // 3rd check: items in source B and not in source A        //        i = Bm.iterator();        while (i.hasNext()) {           syncItemB = (SyncItem)i.next();                       operations.add(checkSyncOperation(principal, null, syncItemB));        }  // next i                 return operations;    }        /**     * Create a SyncOperation based on the state of the given SyncItem couple.     *     * @param principal     the entity that wnats to do the operation     * @param syncItemA     the SyncItem of the source A - NULL means <i>not existing</i/     * @param syncItemB     the SyncItem of the source B - NULL means <i>not existing</i/     *     * @return the SyncOperation object     */    protected SyncOperation checkSyncOperation(Principal principal,                                               SyncItem  syncItemA,                                                SyncItem  syncItemB) {        if (log.isLoggable(Level.FINE)) {            log.fine( "check: syncItemA: "                    + syncItemA                    + " syncItemB: "                    + syncItemB                    );        }                if (syncItemA == null) {            syncItemA = SyncItemImpl.getNotExistingSyncItem(null);        }        if (syncItemB == null) {            syncItemB = SyncItemImpl.getNotExistingSyncItem(null);        }                switch (syncItemA.getState()) {            //            // NEW            //            case SyncItemState.NEW:                switch (syncItemB.getState()) {                    case SyncItemState.NEW:                        return new SyncConflict(syncItemA, syncItemB,                                                String.valueOf(SyncItemState.NEW) +                                                String.valueOf(SyncItemState.NEW) );                    case SyncItemState.UPDATED:                        return new SyncConflict(syncItemA, syncItemB,                                                String.valueOf(SyncItemState.NEW)     +                                                String.valueOf(SyncItemState.UPDATED) );                   case SyncItemState.DELETED:                        return new SyncConflict(syncItemA, syncItemB,                                                String.valueOf(SyncItemState.NEW)     +                                                String.valueOf(SyncItemState.DELETED) );                   case SyncItemState.SYNCHRONIZED:                        return new SyncConflict(syncItemA, syncItemB,                                                String.valueOf(SyncItemState.NEW)       +                                                String.valueOf(SyncItemState.SYNCHRONIZED) );                   case SyncItemState.NOT_EXISTING:                        return new SyncOperationImpl(principal, syncItemA, syncItemB, SyncOperation.NEW, false, true);                }  // end inner switch                        //            // DELETED            //            case SyncItemState.DELETED:                switch (syncItemB.getState()) {                    case SyncItemState.NEW:                        return new SyncConflict(syncItemA, syncItemB,                                                String.valueOf(SyncItemState.DELETED) +                                                String.valueOf(SyncItemState.NEW)     );                    case SyncItemState.UPDATED:                        return new SyncConflict(syncItemA, syncItemB,                                                String.valueOf(SyncItemState.DELETED) +                                                String.valueOf(SyncItemState.UPDATED) );                                      case SyncItemState.SYNCHRONIZED:                        return new SyncOperationImpl(principal, syncItemA, syncItemB, SyncOperation.DELETE, false, true);                                           case SyncItemState.DELETED:                   case SyncItemState.NOT_EXISTING:                        return new SyncOperationImpl(principal, syncItemB, syncItemA, SyncOperation.NOP, false, false);                }  // end inner switch                        //            // UPDATED            //            case SyncItemState.UPDATED:                switch (syncItemB.getState()) {                    case SyncItemState.NEW:                        return new SyncConflict(syncItemA, syncItemB,                                                String.valueOf(SyncItemState.UPDATED) +                                                String.valueOf(SyncItemState.NEW)     );                    case SyncItemState.UPDATED:                        return new SyncConflict(syncItemA, syncItemB,                                                String.valueOf(SyncItemState.UPDATED) +                                                String.valueOf(SyncItemState.UPDATED) );                    case SyncItemState.DELETED:                        return new SyncConflict(syncItemA, syncItemB,                                                String.valueOf(SyncItemState.UPDATED) +                                                String.valueOf(SyncItemState.DELETED) );                    case SyncItemState.SYNCHRONIZED:                        return new SyncOperationImpl(principal, syncItemA, syncItemB, SyncOperation.UPDATE, false, true);                    case SyncItemState.NOT_EXISTING:                        return new SyncOperationImpl(principal, syncItemA, syncItemB, SyncOperation.NEW, false, true);                }  // end inner switch                       //           // SYNCHRONIZED           //           case SyncItemState.SYNCHRONIZED:                switch (syncItemB.getState()) {                    case SyncItemState.NEW:                        return new SyncConflict(syncItemA, syncItemB,                                                String.valueOf(SyncItemState.SYNCHRONIZED) +                                                String.valueOf(SyncItemState.NEW)       );                    case SyncItemState.UPDATED:                        return new SyncOperationImpl(principal, syncItemA, syncItemB, SyncOperation.UPDATE, true, false);                   case SyncItemState.DELETED:                        return new SyncOperationImpl(principal, syncItemA, syncItemB, SyncOperation.DELETE, true, false);                   case SyncItemState.SYNCHRONIZED:                        return new SyncOperationImpl(principal, syncItemA, syncItemB, SyncOperation.NOP, false, false);                   case SyncItemState.NOT_EXISTING:                        return new SyncOperationImpl(principal, syncItemA, syncItemB, SyncOperation.NEW, false, true);                }  // end inner switch                           //           // NOTEXISITNG           //           case SyncItemState.NOT_EXISTING:                switch (syncItemB.getState()) {                    case SyncItemState.NEW:                    case SyncItemState.UPDATED:                        return new SyncOperationImpl(principal, syncItemA, syncItemB, SyncOperation.NEW, true, false);

⌨️ 快捷键说明

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