com.funambol.syncclient.spds.source
Class FileSystemSyncSource

java.lang.Object
  extended by com.funambol.syncclient.spds.source.FileSystemSyncSource
All Implemented Interfaces:
SyncSource
Direct Known Subclasses:
SIFFileSystemSyncSource, SIFSyncSource

public class FileSystemSyncSource
extends java.lang.Object
implements SyncSource

This class implements a file system SyncSource. IMPORTANT NOTE: on FAT16/32 file system the last modified timestamp of a file is accurate with precision (...) of no more than 2 seconds (all timestamps have even seconds and no milliseconds). For instance, all times between 10:35:02,001 and 10:35:03,999 are rounded to 10:35:04,000. On this filesystem, the risk is to detect a change more times. On EXT2/3 file system the last modified timestamp of a file is accurate with precision (...) of no more than 1 seconds (all timestamps have not milliseconds). For instance, all times between 10:35:03,001 and 10:35:03,999 are truncated to 10:35:03,000. On this filesystem, the risk is to not detect a change because the update time is truncated. To avoid to detect a change more times (on FAT32) and to avoid to not detect a change (on EXT3), we round also the since timestamp according to the underlying FS.

Version:
$Id: FileSystemSyncSource.java,v 1.3 2007-12-22 18:09:17 nichele Exp $

Field Summary
protected  int counter
          Counter used to create the name of the file created Adding an item
static java.lang.String DATABASE_FILE_NAME
           
static java.lang.String DATABASE_HEADER
           
static java.lang.String FORMAT_BASE64
           
protected  boolean isEXT3
           
protected  boolean isFAT32
           
 
Constructor Summary
FileSystemSyncSource()
          Creates a new instance of FileSystemSyncSource
 
Method Summary
 void beginSync(int type)
          Called after SyncManager preparation and initialization just before start the synchronization of the SyncSource.
 void commitSync()
          Called just before committing the synchronization process by the SyncManager.
protected  void emptyDataStore()
          reset the syncDB removing all items
 void endSync()
           
protected  SyncItem[] filterSyncItems(java.security.Principal principal, java.util.Date since, char state)
          Filters the SyncItems in the synchronization database (after a refresh) based on the given principal, last sync timestamp and state (see SyncItemState).
 SyncItem[] getAllSyncItems(java.security.Principal principal)
          Returns all items in the data store belonging to the given principal.
 SyncItem[] getDeletedSyncItems(java.security.Principal principal, java.util.Date since)
          Returns all deleted items belonging to the given principal and deleted after the given point in time.
protected  java.util.Vector getExistingFiles()
           
 java.lang.String getName()
          Returns the name of the source
 SyncItem[] getNewSyncItems(java.security.Principal principal, java.util.Date since)
          Returns all new items belonging to the given principal and created after the given point in time.
 java.lang.String getSourceDirectory()
           
 java.lang.String getSourceDrive()
           
 java.lang.String getSourceURI()
          Getter for property uri.
 java.lang.String getType()
          Returns the type of the source.
protected  java.lang.String getUniqueFileName()
          Returns a unique filename created as: "timestamp-counter"
 SyncItem[] getUpdatedSyncItems(java.security.Principal principal, java.util.Date since)
          Returns all updated items belonging to the given principal and modified after the given point in time.
 boolean isEncode()
           
protected  long lastModifiedFromStateString(java.lang.String state)
           
protected  byte[] readFileContent(java.lang.String fileName)
          Reads the content of the given file.
protected  void removeState(java.security.Principal principal, java.lang.String file)
           
 void removeSyncItem(java.security.Principal principal, SyncItem syncItem)
          Removes a SyncItem given its key.
protected  void restoreSyncDB()
          remove old deleted files from syncDB
protected  long roundTime(long time)
          Rounds the given time according to the underlying FS.
protected  long roundTimeEXT3(long time)
          Rounds the given time according to the approximation done by the EXT3 filesystem.
protected  long roundTimeFAT32(long originalTime)
          Rounds the given time according to the approximation done by the FAT32 filesystem.
 void setEncode(boolean encode)
           
 void setEncode(java.lang.String encode)
           
 void setName(java.lang.String name)
           
 void setSourceDirectory(java.lang.String sourceDirectory)
           
 void setSourceDrive(java.lang.String sourceDrive)
           
 void setSourceURI(java.lang.String sourceURI)
          Setter for property uri.
protected  void setState(java.security.Principal principal, java.lang.String file, char state, long timestamp)
           
 SyncItem setSyncItem(java.security.Principal principal, SyncItem syncItem)
          Replaces an existing SyncItem or adds a new SyncItem if it does not exist.
 void setType(java.lang.String type)
           
protected  char stateFromStateString(java.lang.String state)
           
protected  java.util.Properties updateSyncDatabase(java.security.Principal principal, long since)
          The synchronization database is stored in a file whose name is given by the value of the constant DATABASE_FILE_NAME prefixed by the user name and the device id.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DATABASE_FILE_NAME

public static final java.lang.String DATABASE_FILE_NAME
See Also:
Constant Field Values

DATABASE_HEADER

public static final java.lang.String DATABASE_HEADER
See Also:
Constant Field Values

FORMAT_BASE64

public static final java.lang.String FORMAT_BASE64
See Also:
Constant Field Values

counter

protected int counter
Counter used to create the name of the file created Adding an item


isFAT32

protected boolean isFAT32

isEXT3

protected boolean isEXT3
Constructor Detail

FileSystemSyncSource

public FileSystemSyncSource()
Creates a new instance of FileSystemSyncSource

Method Detail

setSourceDrive

public void setSourceDrive(java.lang.String sourceDrive)

getSourceDrive

public java.lang.String getSourceDrive()

setSourceDirectory

public void setSourceDirectory(java.lang.String sourceDirectory)

getSourceDirectory

public java.lang.String getSourceDirectory()

getName

public java.lang.String getName()
Description copied from interface: SyncSource
Returns the name of the source

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

setName

public void setName(java.lang.String name)

getType

public java.lang.String getType()
Description copied from interface: SyncSource
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

setType

public void setType(java.lang.String type)

isEncode

public boolean isEncode()

setEncode

public void setEncode(boolean encode)

setEncode

public void setEncode(java.lang.String encode)

getSourceURI

public java.lang.String getSourceURI()
Getter for property uri.

Specified by:
getSourceURI in interface SyncSource
Returns:
Value of property uri.

setSourceURI

public void setSourceURI(java.lang.String sourceURI)
Setter for property uri.

Parameters:
sourceURI - New value of property uri.

beginSync

public void beginSync(int type)
               throws SyncException
Description copied from interface: SyncSource
Called after SyncManager preparation and initialization just before start the synchronization of the SyncSource.

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

getAllSyncItems

public SyncItem[] getAllSyncItems(java.security.Principal principal)
                           throws SyncException
Description copied from interface: SyncSource
Returns all items in the data store belonging to the given principal.

Specified by:
getAllSyncItems in interface SyncSource
Parameters:
principal - not used, always null
Returns:
an array of all SyncItems stored in this source. If there are no items an empty array is returned.
Throws:
SyncException - in case of error (for instance if the underlying data store runs into problems)

getDeletedSyncItems

public SyncItem[] getDeletedSyncItems(java.security.Principal principal,
                                      java.util.Date since)
                               throws SyncException
Description copied from interface: SyncSource
Returns all deleted items belonging to the given principal and deleted after the given point in time.

Specified by:
getDeletedSyncItems in interface SyncSource
Parameters:
principal - the principal for which the data has to be considered Null means all principals
since - consider the changes since this point in time. Null means all items regardless when they were changed.
Returns:
an array of keys containing the SyncItem's key of the deleted items after the last synchronizazion. If there are no deleted items an empty array is returned.
Throws:
SyncException - in case of error (for instance if the underlying data store runs into problems)

getNewSyncItems

public SyncItem[] getNewSyncItems(java.security.Principal principal,
                                  java.util.Date since)
                           throws SyncException
Description copied from interface: SyncSource
Returns all new items belonging to the given principal and created after the given point in time.

Specified by:
getNewSyncItems in interface SyncSource
Parameters:
principal - not used, always null
since - consider the changes since this point in . Null means all items regardless when they were changed.
Returns:
an array of items containing representing the newly created items. If there are no new items an empty array MUST BE returned.
Throws:
SyncException - in case of error (for instance if the underlying data store runs into problems)

getUpdatedSyncItems

public SyncItem[] getUpdatedSyncItems(java.security.Principal principal,
                                      java.util.Date since)
                               throws SyncException
Description copied from interface: SyncSource
Returns all updated items belonging to the given principal and modified after the given point in time.

Specified by:
getUpdatedSyncItems in interface SyncSource
Parameters:
principal - not used, always null
since - consider the changes since this point in time. Null means all items regardless when they were changed.
Returns:
an array of keys containing the SyncItem's key of the updated items after the last synchronizazion. It MUST NOT return null for no keys, but instad an empty array.
Throws:
SyncException

removeSyncItem

public void removeSyncItem(java.security.Principal principal,
                           SyncItem syncItem)
                    throws SyncException
Description copied from interface: SyncSource
Removes a SyncItem given its key.

Specified by:
removeSyncItem in interface SyncSource
Parameters:
principal - not used, always null
syncItem - the item to remove
Throws:
SyncException - in case of error (for instance if the underlying data store runs into problems)

setSyncItem

public SyncItem setSyncItem(java.security.Principal principal,
                            SyncItem syncItem)
                     throws SyncException
Description copied from interface: SyncSource
Replaces an existing SyncItem or adds a new SyncItem if it does not exist. The item is also returned giving the opportunity to the source to modify its content and return the updated item (i.e. updating the id to the GUID).

Specified by:
setSyncItem in interface SyncSource
Parameters:
principal - not used, always null
syncItem - the item to replace/add
Returns:
the inserted/updated item
Throws:
SyncException - in case of error (for instance if the underlying data store runs into problems)

commitSync

public void commitSync()
Description copied from interface: SyncSource
Called just before committing the synchronization process by the SyncManager. If an error is detected and

Specified by:
commitSync in interface SyncSource

updateSyncDatabase

protected java.util.Properties updateSyncDatabase(java.security.Principal principal,
                                                  long since)
The synchronization database is stored in a file whose name is given by the value of the constant DATABASE_FILE_NAME prefixed by the user name and the device id. For example:
   guest.FunambolTest.sync.db
 
The database is a property file where each entry has the following format:
 [filename]=[state][lastmodified_timestamp]
 
For example:
readme.txt=U98928743098094

updateSyncDatabase works as follows:

 1.    Read the existing database (if it exists)
 2.    Scan the source directory getting all files in the directory
 3.    For each file f in the source directory
 3.1.    If f is already in the database
 3.1.1.    If f has been modified after the lastmodified_timestamp stored into the database
 3.1.1.1.    Set the state of the file to UPDATE and store the new lastmodified_timestamp
 3.2.    Else
 3.2.1.    Add f to the database setting its state to NEW and store the lastmodified_timestamp
 3.    End For each
 4.    For each file f in the database
 4.1.    If f does not exist in the source directory
 4.1.1.    Set the state to DELETED
 5.    End For each
 

At the end of the process, the updated database is saved and than returned.

Returns:
the updated databse. In case of error, an error message is traced and an empty Properties object is returned.

lastModifiedFromStateString

protected long lastModifiedFromStateString(java.lang.String state)

stateFromStateString

protected char stateFromStateString(java.lang.String state)

getExistingFiles

protected java.util.Vector getExistingFiles()
                                     throws java.io.IOException
Throws:
java.io.IOException

setState

protected void setState(java.security.Principal principal,
                        java.lang.String file,
                        char state,
                        long timestamp)

removeState

protected void removeState(java.security.Principal principal,
                           java.lang.String file)

filterSyncItems

protected SyncItem[] filterSyncItems(java.security.Principal principal,
                                     java.util.Date since,
                                     char state)
                              throws SyncException
Filters the SyncItems in the synchronization database (after a refresh) based on the given principal, last sync timestamp and state (see SyncItemState). If state is equals to UNKNOWN all items are returned.
Note that the current implementation ignores the principal: data do not depend on users.

Parameters:
principal - principal. null means any
since - last sync timestamp. null neans since ever
state - the state to use as filter
Returns:
an array of SyncItem objects whose state is equal to the given state.
Throws:
SyncException

readFileContent

protected byte[] readFileContent(java.lang.String fileName)
Reads the content of the given file.

Parameters:
fileName - the name of the file to read
Returns:
the file content as a byte[]

restoreSyncDB

protected void restoreSyncDB()
                      throws SyncException
remove old deleted files from syncDB

Throws:
SyncException

emptyDataStore

protected void emptyDataStore()
                       throws SyncException
reset the syncDB removing all items

Throws:
SyncException

roundTime

protected long roundTime(long time)
Rounds the given time according to the underlying FS. If this is EXT3 the time is rounded calling roundTimeEXT3 otherwise if this is FAT32 the time is rounded calling roundTimeFAT32.

Parameters:
time - the time to round
Returns:
long the time rounded

roundTimeEXT3

protected long roundTimeEXT3(long time)
Rounds the given time according to the approximation done by the EXT3 filesystem. The milliseconds are simply truncated.

Parameters:
time - the time to round
Returns:
long the time rounded

roundTimeFAT32

protected long roundTimeFAT32(long originalTime)
Rounds the given time according to the approximation done by the FAT32 filesystem. In the FAT32 all times haven't milliseconds and have only even seconds. So, for instance, all times between 10:35:32,001 and 10:35:33,999 are rounded in 10:35:34,000.

Parameters:
time - the time to round
Returns:
long the time rounded

getUniqueFileName

protected java.lang.String getUniqueFileName()
Returns a unique filename created as: "timestamp-counter"

Returns:
String


Copyright © 2010 Funambol. All Rights Reserved.