📄 configmanager.java
字号:
* @return true if upgrade was necessary.
*/
public static boolean upgrade() {
try {
confstore.open(CONFIGNAME);
} catch (Exception e) {
// if the config does not exist, this is not un upgrade
return false;
}
try {
int size = confstore.size();
DataInputStream in = confstore.retrieveBytes(1);
int firstInt = in.readInt();
// This is kind of tricky, but as we have no global configuration
// version information we are forced to play dirty
if (firstInt != 4) {
// This is not an hashtable, upgrade is reguired
return false;
}
Log.info("Configuration needs to be upgraded");
MailClientConfig mcc = new MailClientConfig();
SourceConfig msc = new SourceConfig();
SourceConfig csc = new SourceConfig();
SyncClientConfig scc = new SyncClientConfig();
for(int i=0; i<4; i++ ){
String name = in.readUTF();
int recordId = in.readInt();
if (name.equals("MailConfig")) {
confstore.retrieve(recordId, mcc);
} else if (name.equals("source.mail")) {
confstore.retrieve(recordId, msc);
} else if (name.equals("source.contact")) {
confstore.retrieve(recordId, csc);
} else if (name.equals("syncml.config")) {
confstore.retrieve(recordId, scc);
} else {
Log.error("Cannot upgrade configuration. Unknown config " + name);
return false;
}
}
initConfigurationStorage(mcc, msc, csc, scc, null);
return true;
} catch (Exception ex) {
// This should never happen... but if it does we must delete the
// configuration and ask the user to create a new one
Log.error("Cannot upgrade configuration");
initConfigurationStorage(null, null, null, null, null);
return false;
}
}
//--------------------------------------------------------- Private Methods
// retrieve account and configuration settings from JAD attributes
private static MailClientConfig getConfigFromJad() {
Log.info("MailClientConfig from JAD or default\n");
MailClientConfig configFromJad = new MailClientConfig();
configFromJad.setMailAccount(getMailAccountFromJad());
int logLevel = getLogLevelFromJAD();
configFromJad.setLogLevel(logLevel);
String pollInterval = UIController.appProperties.get(AppProperties.POLL_INTERVAL_ATTR);
if (pollInterval != null && !pollInterval.equals("") ) {
try {
int pollIntervalInt = Integer.parseInt(pollInterval);
configFromJad.setPollInterval(pollIntervalInt);
} catch(NumberFormatException nfe) {
Log.error("Error parsing pollInterval attribute: "
+ nfe.toString());
}
}
boolean flag = false;
String enableScheduler = UIController.appProperties.get(AppProperties.ENABLE_SCHEDULER_ATTR);
if (enableScheduler != null && !enableScheduler.equals("") ) {
flag = StringUtil.getBooleanValue(enableScheduler);
configFromJad.enableScheduler(flag);
}
flag = StringUtil.getBooleanValue(
UIController.appProperties.get(AppProperties.ENABLE_SMS_LISTENER_ATTR));
//#ifdef isBlackberry
//# configFromJad.enableSmsListener(true);
//#else
configFromJad.enableSmsListener(flag);
//#endif
flag = StringUtil.getBooleanValue(
UIController.appProperties.get(AppProperties.ENABLE_DELETE_PROPAGATION));
configFromJad.enableDeletePropagation(flag);
return configFromJad;
}
private static int getLogLevelFromJAD() {
String jadStr = UIController.appProperties.get(AppProperties.LOGLEVEL_ATTR);
//#ifdef isBlackberry
//# jadStr = "DEBUG";
//#endif
if ((jadStr == null) || (jadStr.equals(""))) {
return Log.DISABLED;
} else if (StringUtil.equalsIgnoreCase(jadStr, "DISABLED")) {
return Log.DISABLED;
} else if (StringUtil.equalsIgnoreCase(jadStr, "ERROR")) {
return Log.ERROR;
} else if (StringUtil.equalsIgnoreCase(jadStr, "INFO")) {
return Log.INFO;
} else if (StringUtil.equalsIgnoreCase(jadStr, "DEBUG")) {
return Log.DEBUG;
} else if (StringUtil.equalsIgnoreCase(jadStr, "TRACE")) {
return Log.TRACE;
} else {
return Log.ERROR;
}
}
// get account settings from JAD
private static Account getMailAccountFromJad() {
Account mailAccount = new Account();
String user = UIController.appProperties.get(AppProperties.USER_ATTR);
if (StringUtil.isNullOrEmpty(user)) {
Log.info("No user defined in JAD file");
} else {
Log.info("User got from JAD: " + "[" + user + "]");
mailAccount.setUser(user);
}
String url = UIController.appProperties.get(AppProperties.URL_ATTR);
if (StringUtil.isNullOrEmpty(url)) {
Log.info("No url defined in JAD file");
} else {
Log.info("Url got from JAD: " + "[" + url + "]");
mailAccount.setUrl(url);
}
String password = UIController.appProperties.get(AppProperties.PASSWORD_ATTR);
if (StringUtil.isNullOrEmpty(password)) {
Log.info("No password defined in JAD file");
} else {
Log.info("Password got from JAD: " + "[" + password + "]");
mailAccount.setPassword(password);
}
String name = UIController.appProperties.get(AppProperties.NAME_ATTR);
if (StringUtil.isNullOrEmpty(name)) {
Log.info("No name defined in JAD file");
} else {
Log.info("Name got from JAD: " + "[" + name + "]");
mailAccount.setName(name);
}
String address = UIController.appProperties.get(AppProperties.ADDRESS_ATTR);
if (StringUtil.isNullOrEmpty(address)) {
Log.info("No address defined in JAD file");
} else {
Log.info("Address got from JAD: " + "[" + address + "]");
mailAccount.setAddress(address);
}
String remoteURI = UIController.appProperties.get(AppProperties.REMOTEURI_ATTR);
if (StringUtil.isNullOrEmpty(remoteURI)) {
Log.info("No remoteURI defined in JAD file");
} else {
Log.info("RemoteURI got from JAD: " + "[" + remoteURI + "]");
mailAccount.setRemoteURI(remoteURI);
}
String pimRemoteURI = UIController.appProperties.get(AppProperties.PIM_REMOTEURI_ATTR);
if (StringUtil.isNullOrEmpty(pimRemoteURI)) {
Log.info("No pimRemoteURI defined in JAD file");
} else {
Log.info("pimRemoteURI got from JAD: " + "[" + pimRemoteURI + "]");
mailAccount.setPimRemoteURI(pimRemoteURI);
}
return mailAccount;
}
/**
* Get the device ID specifird in the JAD, if present.
* The device ID can be obtained from the JAD to avoid the
* generation of a new dev-id each time the application is installed
* without keeping the old configurration.
* It is mainly intented for debug purposes, but the portal can also
* set this value to have always the same id for a registered device.
*/
private static void initDeviceIdFromJad()
throws ConfigException, IOException {
//Get SyncClientConfig for SyncML specific parameters...
SyncClientConfig syncClientConf = new SyncClientConfig();
//get the device ID from the JAD (if available)
String devID = UIController.appProperties.get(AppProperties.DEVID_ATTR);
if (StringUtil.isNullOrEmpty(devID)) {
Log.info("No device ID defined in JAD file");
} else {
Log.info("Device ID got from JAD: " + "[" + devID + "]");
// ... set device ID in the SyncClientConfig object...
syncClientConf.setDeviceId(devID);
save(SyncClientConfig.CONFIGNAME, syncClientConf);
}
}
private static int getId(String name) {
int configId = 0;
if (name.equals("MailConfig")) {
configId = MAIL_CLIENT_CONFIG;
} else if (name.equals("source.mail")) {
configId = MAIL_SOURCE_CONFIG;
} else if (name.equals("source.contact")) {
configId = CONTACT_SOURCE_CONFIG;
} else if (name.equals("syncml.config")) {
configId = SYNCML_CONFIG;
} else if (name.equals("push.config")) {
configId = PUSH_CONFIG;
}
return configId;
}
private static int loadRmsSize() {
int size = 512*1024;
try {
AbstractRecordStore rs = AbstractRecordStore.openRecordStore(MailClientConfig.NAME, true);
size = rs.getSizeAvailable();
rs.closeRecordStore();
} catch (RecordStoreNotOpenException ex) {
Log.error("RecordStoreNotOpenException: " + ex.getMessage());
ex.printStackTrace();
} catch (RecordStoreException ex) {
Log.error("RecordStoreException: " + ex.getMessage());
ex.printStackTrace();
}
return size;
}
private static void recoverConfig(boolean firstrun)
throws ConfigException, IOException {
// If we are here then we need to re-init the config because either it
// does not exist or it is unreadable
initConfigurationStorage(null,null,null,null,null);
mailClientConfig = getConfigFromJad();
int size =0;
if (firstrun) {
size = loadRmsSize();
} else {
size = 512*1024;
}
mailClientConfig.setRmsStoreSize(size);
saveConfig(mailClientConfig);
// init the SyncClientConfig with the device id from jad, if present
// otherwise, a default one will be generated.
initDeviceIdFromJad();
}
/**
* This method creates the necessary record for storing the J2ME client
* configuration.
*
* @param mcc the MailClientConfig or null if it does not exist (the record
* will be created empty and the load method will report is as non existing)
*
* @param msc the mail SourceConfig or null if it does not exist (the record
* will be created empty and the load method will report is as non existing)
*
* @param csc the contact SourceConfig or null if it does not exist (the record
* will be created empty and the load method will report is as non existing)
*
* @param scc the SyncClientConfig or null if it does not exist (the record
* will be created empty and the load method will report is as non existing)
*/
private static void initConfigurationStorage(MailClientConfig mcc,
SourceConfig msc,
SourceConfig csc,
SyncClientConfig scc,
PushConfig pcfg) {
try {
confstore.close();
AbstractRecordStore.deleteRecordStore(CONFIGNAME);
} catch (Exception e) {
// If the config store does not exist, this is OK
}
try {
// Now recreate it empty
confstore.create(CONFIGNAME);
// And write four records (one for each config)
if (mcc == null) {
confstore.createEmptyRecord();
} else {
confstore.store(mcc);
}
if (msc == null) {
confstore.createEmptyRecord();
} else {
confstore.store(msc);
}
if (csc == null) {
confstore.createEmptyRecord();
} else {
confstore.store(csc);
}
if (scc == null) {
confstore.createEmptyRecord();
} else {
confstore.store(scc);
}
if (pcfg == null) {
confstore.createEmptyRecord();
} else {
confstore.store(pcfg);
}
} catch (Exception e) {
Log.error("Cannot init configuration");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -