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.IOException;
00032 import java.io.ObjectOutputStream;
00033
00034 import org.openmobileis.common.util.codec.GeneralCoder;
00035 import org.openmobileis.common.util.exception.DatabaseException;
00036 import org.openmobileis.common.util.file.FileUtilities;
00037 import org.openmobileis.common.util.file.ZipUtilities;
00038 import org.openmobileis.common.util.log.LogManager;
00039 import org.openmobileis.database.fastobjectdb.FODBCollectionDescriptor;
00040 import org.openmobileis.database.fastobjectdb.FODBIndexDescriptor;
00041 import org.openmobileis.database.fastobjectdb.FastObjectDB;
00042 import org.openmobileis.database.fastobjectdb.FastObjectDBManager;
00043 import org.openmobileis.database.fastobjectdb.db.FODBCollection;
00044 import org.openmobileis.database.fastobjectdb.db.crypto.FODBCypher;
00045 import org.openmobileis.database.fastobjectdb.db.index.FODBIndex;
00046 import org.openmobileis.synchro.client.SynchroDescriptor;
00047 import org.openmobileis.synchro.journal.JournalLogRenderer;
00048 import org.openmobileis.synchro.openmsp.OpenMSPException;
00049 import org.openmobileis.synchro.openmsp.client.OpenMSPDBSyncListener;
00050 import org.openmobileis.synchro.openmsp.client.db.ImportQueryManager;
00051 import org.openmobileis.synchro.security.auth.Credential;
00052
00060 public final class OpenMSPFODBSyncListener extends OpenMSPDBSyncListener {
00061
00062 private String collectionName;
00063
00064 private FODBImportQueryManager queryManager;
00065
00069 public OpenMSPFODBSyncListener(String collectionName) {
00070 super();
00071 this.collectionName = collectionName;
00072 queryManager = new FODBImportQueryManager(this.collectionName);
00073 }
00074
00075 public void registerSynchroListener(SynchroFODBReturnListener listener) {
00076 this.queryManager.registerSynchroListener(listener);
00077 }
00078
00082 protected String getSendCommandMetaData() throws OpenMSPException {
00083 try {
00084 StringBuffer buff = new StringBuffer();
00085
00086
00087 SynchroFODBReturnListener listener = ((FODBImportQueryManager)this.getImportQueryManager()).getSynchroReturnListener();
00088 boolean hasMetaData = false;
00089 if (listener != null) {
00090 Object metada = listener.getSendSynchroMetada();
00091 if (metada != null) {
00092 byte[] b = this.serializeObject(metada);
00093 byte[] b2 = GeneralCoder.encodeBase64(b);
00094 buff.append(new String(b2));
00095 hasMetaData = true;
00096 }
00097 }
00098 if (!hasMetaData) {
00099 buff.append("no-metadata");
00100 }
00101 buff.append('%');
00102
00103 FODBCollection collection = ((SynchroFastObjectDB) FastObjectDBManager.getCurrentFODB()).getCollection(this.getSyncName());
00104 buff.append(collection.getCollectionObjectClass().getName());
00105
00106 FODBCypher cypher = collection.getCollectionCypher();
00107 if (cypher == null) {
00108 buff.append(":nocypher");
00109 } else {
00110 buff.append(new String(GeneralCoder.encodeBase64(this.serializeObject(cypher))));
00111 }
00112
00113
00114 FODBCollectionDescriptor coldesc = collection.getDescriptor();
00115 byte[] b = this.serializeObject(coldesc);
00116 buff.append(":");
00117 buff.append(new String(GeneralCoder.encodeBase64(b)));
00118
00119 FODBIndex[] indexList = collection.getCollectionIndexList();
00120 for (int i = 0; i < indexList.length; i++) {
00121 FODBIndexDescriptor descriptor = indexList[i].getIndexDescriptor();
00122 b = this.serializeObject(descriptor);
00123 buff.append(":");
00124 buff.append(new String(GeneralCoder.encodeBase64(b)));
00125 }
00126 return buff.toString();
00127 } catch (Throwable ex) {
00128 LogManager.traceError(0, ex);
00129 throw new OpenMSPException(ex);
00130 }
00131 }
00132
00138 protected void importCompletSynchroDatabaseFile(String importFile) throws DatabaseException {
00139 this.replaceDBFiles(importFile);
00140
00141
00142 SynchroFastObjectDBManager.flushDB();
00143 }
00144
00145 private void replaceDBFiles(String importFile) throws DatabaseException {
00146 FastObjectDB db = FastObjectDBManager.getCurrentFODB();
00147 String dbPath = "" + db.getRootDir() + "/" + db.getName()+ "/";
00148 importFile = FileUtilities.convertFileNameToSystem(importFile);
00149 try {
00150 BufferedInputStream inst = new BufferedInputStream(new FileInputStream(importFile));
00151 try {
00152 ZipUtilities.unZipData(inst, dbPath);
00153 } finally {
00154 inst.close();
00155 File filein = new File(importFile);
00156 filein.delete();
00157 }
00158 } catch (Throwable ex) {
00159 throw new DatabaseException(ex);
00160 }
00161 }
00162
00163
00164
00165
00166 protected String[] getDBDataForAdd(String pdaUID) throws OpenMSPException {
00167 String[] ret = null;
00168 try {
00169 Object obj = ((SynchroFastObjectDB) FastObjectDBManager.getCurrentFODB()).getObjectFromCollectionWithId(this.getSyncName(), pdaUID);
00170 ret = new String[2];
00171 ret[0] = new String(GeneralCoder.encodeBase64(pdaUID.getBytes()));
00172 ByteArrayOutputStream bstr = new ByteArrayOutputStream();
00173 ObjectOutputStream ostr = new ObjectOutputStream(bstr);
00174 ostr.writeObject(obj);
00175 byte[] b = bstr.toByteArray();
00176 ret[1] = new String(GeneralCoder.encodeBase64(b));
00177 } catch (Throwable ex) {
00178 throw new OpenMSPException(ex);
00179 }
00180 return ret;
00181 }
00182
00183
00184
00185
00186 protected String[] getDBDataForReplace(String pdaUID) throws OpenMSPException {
00187 return this.getDBDataForAdd(pdaUID);
00188 }
00189
00190
00191
00192
00193 protected String getSynchroTarget() {
00194 return collectionName;
00195 }
00196
00197
00198
00199
00200 public String getSyncName() {
00201 return collectionName;
00202 }
00203
00204
00205
00206
00207 protected Credential getSyncroCredential() {
00208 return null;
00209 }
00210
00211
00212
00213
00214 protected ImportQueryManager getImportQueryManager() {
00215 return queryManager;
00216 }
00217
00218 protected JournalLogRenderer getJournalLogRenderer() {
00219 return null;
00220 }
00221
00225 public void startSync(Credential cred, SynchroDescriptor synchrodescriptor) throws OpenMSPException {
00226 super.startSync(cred, synchrodescriptor);
00227 SynchroFODBReturnListener listener = ((FODBImportQueryManager)this.getImportQueryManager()).getSynchroReturnListener();
00228 if (listener != null) {
00229 listener.notifySynchroBegin();
00230 }
00231 }
00232
00236 public void endSync() throws OpenMSPException {
00237 super.endSync();
00238 SynchroFODBReturnListener listener = ((FODBImportQueryManager)this.getImportQueryManager()).getSynchroReturnListener();
00239 if (listener != null) {
00240 listener.notifySynchroEnd();
00241 }
00242
00243 }
00244
00245
00253 public void notifySyncDataDone(String pdaID, int actionType) throws OpenMSPException {
00254 SynchroFODBReturnListener listener = ((FODBImportQueryManager)this.getImportQueryManager()).getSynchroReturnListener();
00255 if (listener != null) {
00256 listener.notifyFODBObjectSynchroDone(pdaID);
00257 }
00258 }
00259
00260 private byte[] serializeObject(Object obj) throws IOException {
00261 ByteArrayOutputStream bstr = new ByteArrayOutputStream();
00262 try {
00263 ObjectOutputStream ostr = new ObjectOutputStream(bstr);
00264 ostr.writeObject(obj);
00265
00266
00267 } finally {
00268 bstr.flush();
00269 bstr.close();
00270 }
00271 return bstr.toByteArray();
00272
00273 }
00274
00275 }