00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 package org.openmobileis.database.fastobjectdb.synchro.client;
00026
00027 import java.io.BufferedInputStream;
00028 import java.io.ByteArrayOutputStream;
00029 import java.io.File;
00030 import java.io.FileInputStream;
00031 import java.io.ObjectOutputStream;
00032
00033 import org.openmobileis.common.util.codec.GeneralCoder;
00034 import org.openmobileis.common.util.exception.DatabaseException;
00035 import org.openmobileis.common.util.file.FileUtilities;
00036 import org.openmobileis.common.util.file.ZipUtilities;
00037 import org.openmobileis.common.util.log.LogManager;
00038 import org.openmobileis.database.fastobjectdb.FODBCollectionDescriptor;
00039 import org.openmobileis.database.fastobjectdb.FODBIndexDescriptor;
00040 import org.openmobileis.database.fastobjectdb.FastObjectDB;
00041 import org.openmobileis.database.fastobjectdb.FastObjectDBManager;
00042 import org.openmobileis.database.fastobjectdb.db.FODBCollection;
00043 import org.openmobileis.database.fastobjectdb.db.crypto.FODBCypher;
00044 import org.openmobileis.database.fastobjectdb.db.index.FODBIndex;
00045 import org.openmobileis.synchro.client.SynchroDescriptor;
00046 import org.openmobileis.synchro.journal.JournalLogRenderer;
00047 import org.openmobileis.synchro.openmsp.OpenMSPException;
00048 import org.openmobileis.synchro.openmsp.client.OpenMSPDBSyncListener;
00049 import org.openmobileis.synchro.openmsp.client.db.ImportQueryManager;
00050 import org.openmobileis.synchro.security.auth.Credential;
00051
00059 public final class OpenMSPFODBSyncListener extends OpenMSPDBSyncListener {
00060
00061 private String collectionName;
00062
00063 private FODBImportQueryManager queryManager;
00064
00068 public OpenMSPFODBSyncListener(String collectionName) {
00069 super();
00070 this.collectionName = collectionName;
00071 queryManager = new FODBImportQueryManager(this.collectionName);
00072 }
00073
00074 public void registerSynchroListener(SynchroFODBReturnListener listener) {
00075 this.queryManager.registerSynchroListener(listener);
00076 }
00077
00081 protected String getSendCommandMetaData() {
00082 try {
00083 FODBCollection collection = ((SynchroFastObjectDB) FastObjectDBManager.getCurrentFODB()).getCollection(this.getSyncName());
00084 StringBuffer buff = new StringBuffer(collection.getCollectionObjectClass().getName());
00085
00086 FODBCypher cypher = collection.getCollectionCypher();
00087 if (cypher == null) {
00088 buff.append(":nocypher");
00089 } else {
00090 ByteArrayOutputStream bstr = new ByteArrayOutputStream();
00091 ObjectOutputStream ostr = new ObjectOutputStream(bstr);
00092 ostr.writeObject(cypher);
00093 buff.append(":");
00094 buff.append(new String(GeneralCoder.encodeBase64(bstr.toByteArray())));
00095 }
00096
00097
00098 FODBCollectionDescriptor coldesc = collection.getDescriptor();
00099 try {
00100 ByteArrayOutputStream bstr = new ByteArrayOutputStream();
00101 ObjectOutputStream ostr = new ObjectOutputStream(bstr);
00102 ostr.writeObject(coldesc);
00103 byte[] b = bstr.toByteArray();
00104 buff.append(":");
00105 buff.append(new String(GeneralCoder.encodeBase64(b)));
00106 } catch (Throwable ex) {
00107 LogManager.traceError(0, ex);
00108 }
00109
00110 FODBIndex[] indexList = collection.getCollectionIndexList();
00111 for (int i = 0; i < indexList.length; i++) {
00112 FODBIndexDescriptor descriptor = indexList[i].getIndexDescriptor();
00113 try {
00114 ByteArrayOutputStream bstr = new ByteArrayOutputStream();
00115 ObjectOutputStream ostr = new ObjectOutputStream(bstr);
00116 ostr.writeObject(descriptor);
00117 byte[] b = bstr.toByteArray();
00118 buff.append(":");
00119 buff.append(new String(GeneralCoder.encodeBase64(b)));
00120 } catch (Throwable ex) {
00121 LogManager.traceError(0, ex);
00122 }
00123 }
00124 return buff.toString();
00125 } catch (Throwable ex) {
00126 LogManager.traceError(0, ex);
00127 }
00128 return null;
00129 }
00130
00136 protected void importCompletSynchroDatabaseFile(String importFile) throws DatabaseException {
00137 this.replaceDBFiles(importFile);
00138
00139
00140 SynchroFastObjectDBManager.flushDB();
00141 }
00142
00143 private void replaceDBFiles(String importFile) throws DatabaseException {
00144 FastObjectDB db = FastObjectDBManager.getCurrentFODB();
00145 String dbPath = "" + db.getRootDir() + "/" + db.getName()+ "/";
00146 importFile = FileUtilities.convertFileNameToSystem(importFile);
00147 try {
00148 BufferedInputStream inst = new BufferedInputStream(new FileInputStream(importFile));
00149 try {
00150 ZipUtilities.unZipData(inst, dbPath);
00151 } finally {
00152 inst.close();
00153 File filein = new File(importFile);
00154 filein.delete();
00155 }
00156 } catch (Throwable ex) {
00157 throw new DatabaseException(ex);
00158 }
00159 }
00160
00161
00162
00163
00164 protected String[] getDBDataForAdd(String pdaUID) throws OpenMSPException {
00165 String[] ret = null;
00166 try {
00167 Object obj = ((SynchroFastObjectDB) FastObjectDBManager.getCurrentFODB()).getObjectFromCollectionWithId(this.getSyncName(), pdaUID);
00168 ret = new String[2];
00169 ret[0] = new String(GeneralCoder.encodeBase64(pdaUID.getBytes()));
00170 ByteArrayOutputStream bstr = new ByteArrayOutputStream();
00171 ObjectOutputStream ostr = new ObjectOutputStream(bstr);
00172 ostr.writeObject(obj);
00173 byte[] b = bstr.toByteArray();
00174 ret[1] = new String(GeneralCoder.encodeBase64(b));
00175 } catch (Throwable ex) {
00176 throw new OpenMSPException(ex);
00177 }
00178 return ret;
00179 }
00180
00181
00182
00183
00184 protected String[] getDBDataForReplace(String pdaUID) throws OpenMSPException {
00185 return this.getDBDataForAdd(pdaUID);
00186 }
00187
00188
00189
00190
00191 protected String getSynchroTarget() {
00192 return collectionName;
00193 }
00194
00195
00196
00197
00198 public String getSyncName() {
00199 return collectionName;
00200 }
00201
00202
00203
00204
00205 protected Credential getSyncroCredential() {
00206 return null;
00207 }
00208
00209
00210
00211
00212 protected ImportQueryManager getImportQueryManager() {
00213 return queryManager;
00214 }
00215
00216 protected JournalLogRenderer getJournalLogRenderer() {
00217 return null;
00218 }
00219
00223 public void startSync(Credential cred, SynchroDescriptor synchrodescriptor) throws OpenMSPException {
00224 super.startSync(cred, synchrodescriptor);
00225 SynchroFODBReturnListener listener = ((FODBImportQueryManager)this.getImportQueryManager()).getSynchroReturnListener();
00226 if (listener != null) {
00227 listener.notifySynchroBegin();
00228 }
00229 }
00230
00234 public void endSync() throws OpenMSPException {
00235 super.endSync();
00236 SynchroFODBReturnListener listener = ((FODBImportQueryManager)this.getImportQueryManager()).getSynchroReturnListener();
00237 if (listener != null) {
00238 listener.notifySynchroEnd();
00239 }
00240
00241 }
00242
00243
00244 }