📄 syncclient.java
字号:
/*
* Copyright (C) 2006 Funambol
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.funambol.mailclient.sm;
import com.funambol.mailclient.config.ConfigException;
import com.funambol.mailclient.config.ConfigManager;
import com.funambol.mailclient.Account;
import com.funambol.mailclient.sm.SyncClientConfig;
import com.funambol.syncml.protocol.SyncML;
import com.funambol.syncml.spds.SourceConfig;
import com.funambol.syncml.spds.SyncConfig;
import com.funambol.syncml.spds.SyncException;
import com.funambol.syncml.spds.SyncManager;
import com.funambol.syncml.spds.SyncSource;
import com.funambol.util.Log;
import java.io.IOException;
/**
* A class that control and unify the sync process on the client side
*/
public class SyncClient {
/**
* SyncManager class declaration:
* managed by a Singleton pattern in order to avoid memory leaks and
* manage concurrent sync processes
*/
private SyncManager sm;
/** Source configuration */
private SourceConfig sc;
/** Instance of this class*/
private static SyncClient instance;
/**
* Default (private) constructor:
*/
private SyncClient() {
try {
sm = getSyncManager(ConfigManager.getConfig().getMailAccount());
} catch (ConfigException ex) {
Log.error(ex.toString());
}
}
/**
* Private constructor:
* @param a the given account
*/
private SyncClient(Account a) {
sm = getSyncManager(a);
}
public static SyncClient getSyncClient(Account a) throws SyncException {
if (instance==null) {
instance = new SyncClient(a);
} else {
Log.info("[getSyncClient] SyncClient not null, sync in progress.");
throw new SyncException(
SyncException.CONCURRENCE_ERROR, "Sync in progress");
}
return instance;
}
public static SyncClient getSyncClient() throws SyncClientException {
if (instance==null) {
throw new SyncClientException("SyncClient instance not initialized yet");
} else {
return instance;
}
}
public static void dispose() {
instance=null;
}
/**
* Start the sync using the SyncSource, and with the default
* sync mode configured for that source.
* @param ss is the sync source to be passed to the syncManager
*/
public void sync(SyncSource ss) throws SyncException {
sync(ss, ss.getSyncMode());
}
/**
* Start the sync using the SyncSource, with a given sync mode.
* @param ss is the sync source to be passed to the syncManager
* @param mode is the type of sync required for client
*/
public void sync(SyncSource ss, int mode) throws SyncException {
if (mode == SyncML.ALERT_CODE_NONE) {
Log.info("SyncCLient: source not active.");
return;
}
try {
//do sync
sm.sync(ss, mode);
//save sync source configuration
saveSourceConfig();
} catch(ConfigException ce) {
Log.error(this, ce.toString());
throw new SyncException(
SyncException.STORAGE_ERROR,
"Error saving source state:" + ce.toString());
} catch(IOException ioe) {
Log.error(this, ioe.toString());
throw new SyncException(
SyncException.STORAGE_ERROR,
"Error saving source state:" + ioe.toString());
}
}
/**
* Factory method for the SourceConfig.
* Loads and return the correct SouorceConfig depending on the source name.
*
* @param name the source name
* @param type the source type
* @param remoteUri the remore URI for the source
*/
public SourceConfig loadSourceConfig(String name, String type, String remoteUri) {
// create a new SourceConfig with the given paramenters
sc = new SourceConfig(name, type, remoteUri);
// The sources used by the mail client are all not encoded.
sc.setEncoding(SyncSource.ENCODING_NONE);
// Load the config from RMS.
try {
ConfigManager.load("source." + sc.getName(), sc);
sc.setRemoteUri(remoteUri);
Log.debug("Config for source" + sc.getName() + " loaded.");
} catch(ConfigException ce) {
Log.error("Source configuration not found, save default.");
try {
ConfigManager.save("source." + sc.getName(), sc);
} catch (Exception e) {
Log.error("loadSourceConfig, can't save default conf.");
Log.error(" source: "+sc.getName()+" error: "+e.toString());
}
} catch (IOException ex) {
ex.printStackTrace();
Log.error("loadSourceConfig: "+ex.toString());
}
return sc;
}
private SyncManager getSyncManager(Account a) {
// Init a SyncConfig with the parameters get from Account
SyncConfig syncConfig = new SyncConfig();
syncConfig.syncUrl = a.getUrl();
syncConfig.userName = a.getUser();
syncConfig.password = a.getPassword();
syncConfig.userAgent = createUserAgent();
//#ifdef http_resp.force_cookies
syncConfig.forceCookies = true;
//#endif
Log.info("HTTP Device-Agent header: " + syncConfig.userAgent);
// Get SyncClientConfig for Syncml specific parameters
SyncClientConfig conf = new SyncClientConfig();
try {
ConfigManager.load(SyncClientConfig.CONFIGNAME, conf);
} catch (Exception ce) {
Log.error("Error reading SyncClientConfig, using defaults.");
// Get a new device Id, it's the first use of the client
// on the device.
conf.generateDeviceId();
}
// Set device id in the devinfo config class
syncConfig.deviceConfig.devID = conf.getDeviceId();
syncConfig.lastServerUrl = conf.getLastSyncUrl();
conf.setLastSyncUrl(syncConfig.syncUrl);
// TODO: check exception handling!!!!!!!!!
try {
// set if the communications with the server must be compressed
syncConfig.compress = ConfigManager.getConfig().isCompress();
// Get a new instance of SyncManager
sm = new SyncManager(syncConfig);
ConfigManager.save(SyncClientConfig.CONFIGNAME, conf);
} catch (Exception e) {
Log.error("Error saving SyncClientConfig Config: " + e.toString());
}
return sm;
}
// Creates UserAgent to use in HTTP requests
private String createUserAgent() {
StringBuffer sbAgent = new StringBuffer();
sbAgent.append(System.getProperty("microedition.platform"));
sbAgent.append(" FJM ").append(ConfigManager.getClientName());
sbAgent.append(" v. ").append(ConfigManager.getClientVersion());
sbAgent.append(" ").append(System.getProperty("microedition.profiles"));
sbAgent.append(" ").append(System.getProperty("microedition.configuration"));
return sbAgent.toString();
}
/**
* Save the given source config.
*
* @param sc the MailSourceConfig to save.
*
* @throws ConfigException if the config is not present
* @throws IOException if an error occurred during the deserialization
*/
public void saveSourceConfig()
throws ConfigException, IOException {
// The source config is stored in a record named "source.<sourcename>"
ConfigManager.save("source." + sc.getName(), sc);
Log.info("[SyncClient] config for source " + sc.getName() + " saved.");
}
public static boolean isBusy() {
return instance==null?false:true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -