com.funambol.syncml.client
Class BaseSyncSource

java.lang.Object
  extended by com.funambol.syncml.client.BaseSyncSource
All Implemented Interfaces:
SyncSource

public abstract class BaseSyncSource
extends java.lang.Object
implements SyncSource

An abstract implementation of the SyncSource interface, providing the basic framework each SyncSource has to implement. A developer can choose to extends BaseSyncSource or to implements SyncSource directly if needed. The class BaseSyncSource uses the SyncConfig to store the source configuration data. With this class is possible to alter the source configuration, which is not permitted by the SyncSource interface.


Field Summary
protected  int allIndex
           
protected  SyncItem[] allItems
           
protected  SourceConfig config
          SyncSource configuration
protected  int delIndex
           
protected  SyncItem[] delItems
           
protected  SyncFilter filter
          Synchronization filter
protected  int globalStatus
          Status of the sync source summarized in an integer value.
protected  int newIndex
           
protected  SyncItem[] newItems
           
protected  int syncMode
          SyncMode, set by beginSync
protected  int updIndex
           
protected  SyncItem[] updItems
           
 
Fields inherited from interface com.funambol.syncml.spds.SyncSource
ENCODING_B64, ENCODING_NONE, STATUS_CONNECTION_ERROR, STATUS_RECV_ERROR, STATUS_SEND_ERROR, STATUS_SERVER_ERROR, STATUS_SUCCESS
 
Constructor Summary
BaseSyncSource(SourceConfig config)
          BaseSyncSource constructor: initialize source config
 
Method Summary
abstract  int addItem(SyncItem item)
          Add a new SyncItem to this source backend.
 void beginSync(int syncMode)
          Called after SyncManager preparation and initialization just before start the synchronization of the SyncSource.
 void dataReceived(java.lang.String date, int size)
          Default implementation for
abstract  int deleteItem(java.lang.String key)
          Delete a SyncItem stored in the source backend.
 void endSync()
          Called just before committing the synchronization process by the SyncManager.
 int getClientAddNumber()
          Return the number of new items (add) that the client will send during the session.
 int getClientDeleteNumber()
          Return the number of deleted items that the client will send during the session.
 int getClientItemsNumber()
          Return the number of changes that the client will send during the session.
 int getClientReplaceNumber()
          Return the number of replaced items that the client will send during the session.
 SourceConfig getConfig()
          Returns the config of the source.
 java.lang.String getEncoding()
          Returns the encoding of the source.
 SyncFilter getFilter()
          Returns the current filter for this SyncSource.
protected abstract  SyncItem getItemContent(SyncItem item)
          This function gets the item content in the backend database and returns a complete item.
 long getLastAnchor()
          Return the Last Anchor for this source
 com.funambol.util.SyncListener getListener()
          Returns the current listener (or null if not set)
 java.lang.String getName()
          Returns the name of the source
 long getNextAnchor()
          Return the Next Anchor for this source
 SyncItem getNextDeletedItem()
          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)
 SyncItem getNextItem()
          Returns the next item of the store.
 SyncItem getNextNewItem()
          Returns the next new item of the store (not yet sent to the server)
 SyncItem getNextUpdatedItem()
          Returns the first/next updated item of the store (changed from the last sync)
 int getServerItemsNumber()
          Return the number of changes that the server will send during the session.
 java.lang.String getSourceUri()
          Returns the source URI
 int getStatus()
          Returns the status of the sync source.
 int getSyncMode()
          Returns the preferred sync mode of the source.
 java.lang.String getType()
          Returns the type of the source.
protected abstract  void initAllItems()
          In a concrete implementation, this function should search the database for all the items present and store their keys.
protected abstract  void initDelItems()
          In a real implementation, this function should search the database for the deleted items present and store their keys.
protected abstract  void initNewItems()
          In a concrete implementation, this function should search the database for the new items present and store their keys.
protected abstract  void initUpdItems()
          In a real implementation, this function should search the database for the modified items present and store their keys.
 void setConfig(SourceConfig config)
          Sets the config of the source.
 void setFilter(SyncFilter filter)
          Set a new filter for this SyncSource
 void setItemStatus(java.lang.String key, int status)
          Tell the SyncSource the status returned by the server for an Item previously sent.
 void setLastAnchor(long time)
          Set the value of the Last Anchor for this source
 void setListener(com.funambol.util.SyncListener listener)
          Set a sync listener.
 void setNextAnchor(long time)
          Set the value of the Next Anchor for this source
 void setServerItemsNumber(int number)
          Set the number of changes that the server will send during the session.
abstract  int updateItem(SyncItem item)
          Update a given SyncItem stored in the source backend.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

config

protected SourceConfig config
SyncSource configuration


filter

protected SyncFilter filter
Synchronization filter


syncMode

protected int syncMode
SyncMode, set by beginSync


allItems

protected SyncItem[] allItems

newItems

protected SyncItem[] newItems

updItems

protected SyncItem[] updItems

delItems

protected SyncItem[] delItems

allIndex

protected int allIndex

newIndex

protected int newIndex

updIndex

protected int updIndex

delIndex

protected int delIndex

globalStatus

protected int globalStatus
Status of the sync source summarized in an integer value. See constants defined in SyncSource

Constructor Detail

BaseSyncSource

public BaseSyncSource(SourceConfig config)
BaseSyncSource constructor: initialize source config

Method Detail

getConfig

public SourceConfig getConfig()
Returns the config of the source. The client can use this method to obtain the config object and change some parameter. A setConfig() must be called to actually change the source configuration.

Returns:
the config of the source

setConfig

public void setConfig(SourceConfig config)
Sets the config of the source. The client can use this method to change the config of the source configuration. This operation should not be done while the sync is in progress.


getName

public java.lang.String getName()
Returns the name of the source

Specified by:
getName in interface SyncSource
Returns:
the name of the source

getSourceUri

public java.lang.String getSourceUri()
Returns the source URI

Specified by:
getSourceUri in interface SyncSource
Returns:
the absolute URI of the source

getType

public java.lang.String getType()
Returns the type of the source. The types are defined as mime-types, for instance * text/x-vcard).

Specified by:
getType in interface SyncSource
Returns:
the type of the source

getEncoding

public java.lang.String getEncoding()
Returns the encoding of the source. The encoding can be 'b64' or 'none' only. The standard defines also 'des' and '3des' but they are not implemented in this version of the APIs.

Specified by:
getEncoding in interface SyncSource
Returns:
the encoding of the source

getSyncMode

public int getSyncMode()
Returns the preferred sync mode of the source. The preferred sync mode is the one that the SyncManager sends to the server in the initialization phase. The server can respond with a different alert code, to force, for instance, a slow.

Specified by:
getSyncMode in interface SyncSource
Returns:
the preferred sync mode for this source

getFilter

public SyncFilter getFilter()
Returns the current filter for this SyncSource.

Specified by:
getFilter in interface SyncSource

setFilter

public void setFilter(SyncFilter filter)
Set a new filter for this SyncSource

Specified by:
setFilter in interface SyncSource

addItem

public abstract int addItem(SyncItem item)
                     throws SyncException
Add a new SyncItem to this source backend. The item key after a successful add must contain the local UID, that is used by the engine to send the mappings to the server. The source must then change the item key accordingly before return.

Specified by:
addItem in interface SyncSource
Parameters:
item - the SyncItem to add, with the GUID sent by the server. The source is resposible to set it to the LUID before returning a successful status code.
Returns:
the status code of the operation. It will be returned to the server in the response for this item.
Throws:
SyncException - if an unrecoverable error occur, to stop the sync

updateItem

public abstract int updateItem(SyncItem item)
                        throws SyncException
Update a given SyncItem stored in the source backend.

Specified by:
updateItem in interface SyncSource
Parameters:
item - the SyncItem to update. The key of the item is already the LUID.
Returns:
the status code of the operation. It will be returned to the server in the response for this item.
Throws:
SyncException - if an unrecoverable error occur, to stop the sync

deleteItem

public abstract int deleteItem(java.lang.String key)
                        throws SyncException
Delete a SyncItem stored in the source backend.

Specified by:
deleteItem in interface SyncSource
Parameters:
key - The key of the item to delete.
Returns:
the status code of the operation. It will be returned to the server in the response for this item.
Throws:
SyncException - if an unrecoverable error occur, to stop the sync

getNextItem

public SyncItem getNextItem()
                     throws SyncException
Returns the next item of the store.

Specified by:
getNextItem in interface SyncSource
Throws:
SyncException

getNextNewItem

public SyncItem getNextNewItem()
                        throws SyncException
Returns the next new item of the store (not yet sent to the server)

Specified by:
getNextNewItem in interface SyncSource
Returns:
the first new item, in a SyncItem object, or null if no new items are present.
Throws:
SyncException

getNextUpdatedItem

public SyncItem getNextUpdatedItem()
                            throws SyncException
Returns the first/next updated item of the store (changed from the last sync)

Specified by:
getNextUpdatedItem in interface SyncSource
Returns:
the first updated item, in a SyncItem object, or null if no updated items are present.
Throws:
SyncException

getNextDeletedItem

public SyncItem getNextDeletedItem()
                            throws SyncException
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)

Specified by:
getNextDeletedItem in interface SyncSource
Returns:
the first deleted item, in a SyncItem object, or null if no deleted items are present.
Throws:
SyncException

setItemStatus

public void setItemStatus(java.lang.String key,
                          int status)
                   throws SyncException
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.

Specified by:
setItemStatus in interface SyncSource
Parameters:
key - the key of the item
status - the status code received for that item
Throws:
SyncException - if the SyncSource wants to stop the sync

getClientItemsNumber

public int getClientItemsNumber()
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().

Specified by:
getClientItemsNumber in interface SyncSource
Returns:
number of items to sent, or -1 if unknown

getClientAddNumber

public int getClientAddNumber()
Return the number of new items (add) that the client will send during the session. This method, after the beginSync() call, should return the number of new items to be sent to the server. The number of changes is computed by initXXXItems() during beginSync().

Specified by:
getClientAddNumber in interface SyncSource
Returns:
number of items to sent, or -1 if unknown

getClientReplaceNumber

public int getClientReplaceNumber()
Return the number of replaced items that the client will send during the session. This method, after the beginSync() call, should return the number of replaced items to be sent to the server. The number of changes is computed by initXXXItems() during beginSync().

Specified by:
getClientReplaceNumber in interface SyncSource
Returns:
number of items to sent, or -1 if unknown

getClientDeleteNumber

public int getClientDeleteNumber()
Return the number of deleted items that the client will send during the session. This method, after the beginSync() call, should return the number of delted items to be sent to the server. The number of changes is computed by initXXXItems() during beginSync().

Specified by:
getClientDeleteNumber in interface SyncSource
Returns:
number of items to sent, or -1 if unknown

getServerItemsNumber

public int getServerItemsNumber()
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.

Specified by:
getServerItemsNumber in interface SyncSource
Returns:
number of changes from the server, or -1 if not announced.

setServerItemsNumber

public void setServerItemsNumber(int number)
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.

Specified by:
setServerItemsNumber in interface SyncSource
Parameters:
number - of changes from the server, or -1 if not announced.

dataReceived

public void dataReceived(java.lang.String date,
                         int size)
Default implementation for

Specified by:
dataReceived in interface SyncSource

getLastAnchor

public long getLastAnchor()
Return the Last Anchor for this source

Specified by:
getLastAnchor in interface SyncSource

setLastAnchor

public void setLastAnchor(long time)
Set the value of the Last Anchor for this source

Specified by:
setLastAnchor in interface SyncSource

getNextAnchor

public long getNextAnchor()
Return the Next Anchor for this source

Specified by:
getNextAnchor in interface SyncSource

setNextAnchor

public void setNextAnchor(long time)
Set the value of the Next Anchor for this source

Specified by:
setNextAnchor in interface SyncSource

beginSync

public void beginSync(int syncMode)
               throws SyncException
Called after SyncManager preparation and initialization just before start the synchronization of the SyncSource.

Specified by:
beginSync in interface SyncSource
Parameters:
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

endSync

public void endSync()
             throws SyncException
Called just before committing the synchronization process by the SyncManager. The SyncSource can stop the commit phase raising an exception here.

Specified by:
endSync in interface SyncSource
Throws:
SyncException - in case of error, to stop the commit.

setListener

public void setListener(com.funambol.util.SyncListener listener)
Set a sync listener.

Specified by:
setListener in interface SyncSource
Parameters:
listener - the listener or null to remove it

getListener

public com.funambol.util.SyncListener getListener()
Returns the current listener (or null if not set)

Specified by:
getListener in interface SyncSource

getStatus

public int getStatus()
Returns the status of the sync source. The status is encoded as a bit mask of the STATUS_* values

Specified by:
getStatus in interface SyncSource

initAllItems

protected abstract void initAllItems()
                              throws SyncException
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.

initNewItems

protected abstract void initNewItems()
                              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.

initUpdItems

protected abstract void initUpdItems()
                              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.

initDelItems

protected abstract void initDelItems()
                              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.

getItemContent

protected abstract SyncItem getItemContent(SyncItem item)
                                    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).

The content of the item depends also from the encoding of this SyncSource:

  • if the encoding is none, it must be a String, converted with getBytes(), so the engine will send it unchanged.
  • if the encoding is b64, 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.

    Throws:
    SyncException


  • Copyright © 2006 Funambol.