📄 syncdocument.java
字号:
/**
* @(#)$RCSfile: SyncDocument.java,v $ $Revision: 1.2 $
*
* ====================================================================
* Copyright 2001, Reaxion Corp.,
* 11418 105th PL NE, Kirkland, WA, 98033, USA
* All rights reserved.
* ====================================================================
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the Tequila SyncML.
*
* The Initial Developer of the Original Code is Reaxion Corp.
* All Rights Reserved.
*/
package com.reaxion.tequila.syncml.sync.doc;
import java.util.*;
import com.reaxion.tequila.syncml.*;
import com.reaxion.tequila.syncml.sync.*;
import com.reaxion.tequila.syncml.sync.stack.*;
import com.reaxion.tequila.syncml.xml.*;
import com.reaxion.tequila.syncml.util.*;
/**
* This class represents a XML documend synchronization.
* The instance of this class leaves only through the session of synchronization.
* It should be created before starting the synchronization
* and removed after finishing the process of synchronization
*
* @version $1.0$
* @author Oleg A. Oksyuk
*/
public abstract class SyncDocument implements ISyncDocument
{
protected String source;
protected String target;
protected boolean isSyncFinished = false;
protected boolean headerRespNeeded;
/**
* Called from endPackage method
*
* @return the ennumeration of commands that should be send
* in a reply for this package
*/
protected abstract Enumeration getEndPackageCommands();
/**
* Called from createSync method
*
* @return the list of Sync commands for current document
*/
protected abstract Enumeration getSyncCommands();
/**
* Called from performCommand method when Alert command is received
*/
protected abstract void performAlert(SyncAlert alert);
/**
*
* Called from performCommand method when Sync command is received
*/
protected abstract Enumeration performSync(SyncSync sync);
/**
* Called when performStatus method when non-OK status received
*/
protected abstract void performNonOKStatus(SyncStatus status);
/**
* performs Sync finish action
*/
public abstract void endSync();
protected short scenario;
private IXMLSyncDocument xmlSyncDocument;
protected DocumentStack stack;
protected Date syncDate;
protected String currMsgId;
protected int nMsg = 0;
protected boolean syncFromPerformed = false;
protected boolean syncToPerformed = false;
public SyncDocument(IXMLSyncDocument aXmlSyncDocument, short aScenario)
{
xmlSyncDocument = aXmlSyncDocument;
log("init");
scenario = aScenario;
syncDate = new Date();
if (null != xmlSyncDocument) {
stack = new DocumentStack(xmlSyncDocument.getDbName());
}
}
//getters
public DocumentStack getDocumentStack()
{
return stack;
}
public String getDbName()
{
return xmlSyncDocument.getDbName();
}
//Package related methods
/**
* Called when we begin to process a new package
*
* @param aMsgId package message id.
*/
public Enumeration startPackage(String aMsgId, boolean aHeaderRespNeeded)
{
log("startPackage("+aMsgId+") nMsg="+nMsg);
currMsgId = aMsgId;
headerRespNeeded = aHeaderRespNeeded;
++nMsg;
return new Vector().elements();
}
/**
* Called for each Command (Status/Alert/Sync) with this document as a target
* Do nothing except call appropriate method for different commands
*
* @param inCommand command to perform.
* @return the ennumeration of reply commands
*/
public Enumeration performCommand(SyncCmdIdOwner inCommand)
{
log("performCommand("+inCommand.getName()+")...");
Enumeration outCommands = null;
if (SyncCommandNames.ALERT.equals(inCommand.getName()))
{
performAlert((SyncAlert) inCommand);
}
else if (SyncCommandNames.SYNC.equals(inCommand.getName()))
{
outCommands = performSync((SyncSync) inCommand);
}
else if (SyncCommandNames.STATUS.equals(inCommand.getName()))
{
performStatus((SyncStatus) inCommand);
}
else
{
throw new SyncException("Wrong command");
}
log("performCommand("+inCommand.getName()+") OK");
return outCommands;
}
/**
* Called when we end to process a package
* Check if replies where received ob all commands in previos packages end
* call method getEndPackageCommands()
*
* @return the ennumeration of commands that should be send
* in a reply for this package
*/
public Enumeration endPackage()
{
log("endPackage");
if ((null != stack) && (stack.hasNonRepliedCommands()))
{
throw new SyncException("Get replies on not all commands");
}
return getEndPackageCommands();
}
/**
* Called when a new package put to stack
* Don't call this method after it return true
*
* @return if the document is finished to Sync
* if yes, performs 'finish action'
*/
public boolean tryToEndSync()
{
log("tryToEndSync");
if (isSyncFinished)
{
throw new SyncException("A synchronization is already finished for this document");
}
if ((null == stack) || (!stack.hasNonRepliedCommands()))
{
endSync();
isSyncFinished = true;
}
return isSyncFinished;
}
/**
* @return if the synchronization is finished for the document
*/
public boolean isSyncFinished()
{
return isSyncFinished;
}
//Command related methods
/**
* Called for each Status Command with this document as a target
* Set reply on this command in stack and call performNonOKStatus
* if the status code is other that OK
*
* @param status Status command to perform.
*/
protected void performStatus(SyncStatus status)
{
if (0 == scenario)
{
throw new SyncException("Document is not alerted");
}
String msgRef = status.getMsgRef().getData();
String cmdRef = status.getCmdRef().getData();
StackCommand comm = stack.getCommand(msgRef, cmdRef);
if (null == comm)
{
throw new SyncException("Received Status on wrong command");
}
short code = status.getCode();
comm.setReplyStatus(code);
if (SyncStatuses.OK != code)
{
performNonOKStatus(status);
}
}
/**
* Apply received Sync command for this document
*
* @param sync Sync command to perform.
* @return the ennumeration of reply commands
*/
protected Enumeration purePerformSync(SyncSync sync, boolean needToSync)
{
log("purePerformSync()");
if (syncFromPerformed)
{
throw new SyncException("This document was already sync from");
}
Enumeration statuses;
SyncStatus status;
try
{
statuses = CommandPerformer.applySync(xmlSyncDocument, currMsgId,
sync, needToSync, headerRespNeeded);
xmlSyncDocument.save();
}
catch (Exception e)
{
Log.printEx(e);
throw new InternalException(e.getMessage());
}
syncFromPerformed = true;
return statuses;
}
/**
* Create Put command for this document
*
* @return Sync command with Put command in it
*/
protected SyncSync createPut()
{
log("createPut");
if (syncToPerformed)
{
throw new SyncException("This document was already sync to");
}
SyncPut put = new SyncPut(new SyncTarget(new SyncLocUri(xmlSyncDocument.getDbName())),
xmlSyncDocument.getDocument(), !ISyncServer.COMMAND_RESP_NEEDED);
SyncSync sync = new SyncSync(new SyncTarget(new SyncLocUri(xmlSyncDocument.getDbName())),
null, put, !ISyncServer.SYNC_RESP_NEEDED);
syncToPerformed = true;
return sync;
}
/**
* Create Sync command for this document
*
* @return Sync command
*/
protected SyncSync createSync()
{
log("createSync");
if (syncToPerformed)
{
throw new SyncException("This document was already sync to");
}
Enumeration commands = getSyncCommands();
if (!commands.hasMoreElements())
{
return null;
}
SyncSync sync = new SyncSync(new SyncTarget(new SyncLocUri(xmlSyncDocument.getDbName())),
null, null, !ISyncServer.SYNC_RESP_NEEDED);
while (commands.hasMoreElements())
{
sync.addCommand((SyncSyncCommand) commands.nextElement());
}
syncToPerformed = true;
return sync;
}
/**
* All debug logs for child classes should be print through this method
*
* @param msg message to log
*/
protected void log(String msg)
{
StringBuffer res = new StringBuffer(" ");
res.append(this.getClass().getName());
res.append("[");
if (null != xmlSyncDocument)
{
res.append(xmlSyncDocument.getDbName());
}
else
{
res.append("null");
}
res.append("].");
res.append(msg);
Log.println(res);
}
}
/* -----------------------------------------------------------------------------
* Change log:
* -----------------------------------------------------------------------------
* $Log: SyncDocument.java,v $
* Revision 1.2 2001/10/17 15:27:42 OlegO
* changed comments for better javadoc
*
* Revision 1.1.1.1 2001/10/11 13:13:32 OlegO
* no message
*
* Revision 1.2 2001/07/27 07:44:52 OlegO
* no message
*
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -