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.file.FileUtilities;
00035 import org.openmobileis.common.util.file.ZipUtilities;
00036 import org.openmobileis.common.util.log.LogManager;
00037 import org.openmobileis.database.DatabaseException;
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.journal.JournalLogRenderer;
00046 import org.openmobileis.synchro.openmsp.OpenMSPException;
00047 import org.openmobileis.synchro.openmsp.client.OpenMSPDBSyncListener;
00048 import org.openmobileis.synchro.openmsp.client.db.ImportQueryManager;
00049 import org.openmobileis.synchro.security.auth.Credential;
00050
00058 public final class OpenMSPFODBSyncListener extends OpenMSPDBSyncListener {
00059
00060 private String collectionName;
00061
00062 private FODBImportQueryManager queryManager;
00063
00067 public OpenMSPFODBSyncListener(String collectionName) {
00068 super();
00069 this.collectionName = collectionName;
00070 queryManager = new FODBImportQueryManager(this.collectionName);
00071 }
00072
00073 public void registerSynchroListener(SynchroFODBReturnListener listener) {
00074 this.queryManager.registerSynchroListener(listener);
00075 }
00076
00080 protected String getSendCommandMetaData() {
00081 try {
00082 FODBCollection collection = ((SynchroFastObjectDB) FastObjectDBManager.getCurrentFODB()).getCollection(this.getSyncName());
00083 StringBuffer buff = new StringBuffer(collection.getCollectionObjectClass().getName());
00084
00085 FODBCypher cypher = collection.getCollectionCypher();
00086 if (cypher == null) {
00087 buff.append(":nocypher");
00088 } else {
00089 ByteArrayOutputStream bstr = new ByteArrayOutputStream();
00090 ObjectOutputStream ostr = new ObjectOutputStream(bstr);
00091 ostr.writeObject(cypher);
00092 buff.append(":");
00093 buff.append(new String(GeneralCoder.encodeBase64(bstr.toByteArray())));
00094 }
00095
00096
00097 FODBCollectionDescriptor coldesc = collection.getDescriptor();
00098 try {
00099 ByteArrayOutputStream bstr = new ByteArrayOutputStream();
00100 ObjectOutputStream ostr = new ObjectOutputStream(bstr);
00101 ostr.writeObject(coldesc);
00102 byte[] b = bstr.toByteArray();
00103 buff.append(":");
00104 buff.append(new String(GeneralCoder.encodeBase64(b)));
00105 } catch (Throwable ex) {
00106 LogManager.traceError(0, ex);
00107 }
00108
00109 FODBIndex[] indexList = collection.getCollectionIndexList();
00110 for (int i = 0; i < indexList.length; i++) {
00111 FODBIndexDescriptor descriptor = indexList[i].getIndexDescriptor();
00112 try {
00113 ByteArrayOutputStream bstr = new ByteArrayOutputStream();
00114 ObjectOutputStream ostr = new ObjectOutputStream(bstr);
00115 ostr.writeObject(descriptor);
00116 byte[] b = bstr.toByteArray();
00117 buff.append(":");
00118 buff.append(new String(GeneralCoder.encodeBase64(b)));
00119 } catch (Throwable ex) {
00120 LogManager.traceError(0, ex);
00121 }
00122 }
00123 return buff.toString();
00124 } catch (Throwable ex) {
00125 LogManager.traceError(0, ex);
00126 }
00127 return null;
00128 }
00129
00135 protected void importCompletSynchroDatabaseFile(String importFile) throws DatabaseException {
00136 this.replaceDBFiles(importFile);
00137
00138
00139 SynchroFastObjectDBManager.flushDB();
00140 }
00141
00142 private void replaceDBFiles(String importFile) throws DatabaseException {
00143 FastObjectDB db = FastObjectDBManager.getCurrentFODB();
00144 String dbPath = "" + db.getRootDir() + "/" + db.getName()+ "/";
00145 importFile = FileUtilities.convertFileNameToSystem(importFile);
00146 try {
00147 BufferedInputStream inst = new BufferedInputStream(new FileInputStream(importFile));
00148 try {
00149 ZipUtilities.unZipData(inst, dbPath);
00150 } finally {
00151 inst.close();
00152 File filein = new File(importFile);
00153 filein.delete();
00154 }
00155 } catch (Throwable ex) {
00156 throw new DatabaseException(ex);
00157 }
00158 }
00159
00160
00161
00162
00163 protected String[] getDBDataForAdd(String pdaUID) throws OpenMSPException {
00164 String[] ret = null;
00165 try {
00166 Object obj = ((SynchroFastObjectDB) FastObjectDBManager.getCurrentFODB()).getObjectFromCollectionWithId(this.getSyncName(), pdaUID);
00167 ret = new String[2];
00168 ret[0] = pdaUID;
00169 ByteArrayOutputStream bstr = new ByteArrayOutputStream();
00170 ObjectOutputStream ostr = new ObjectOutputStream(bstr);
00171 ostr.writeObject(obj);
00172 byte[] b = bstr.toByteArray();
00173 ret[1] = new String(GeneralCoder.encodeBase64(b));
00174 } catch (Throwable ex) {
00175 throw new OpenMSPException(ex);
00176 }
00177 return ret;
00178 }
00179
00180
00181
00182
00183 protected String[] getDBDataForReplace(String pdaUID) throws OpenMSPException {
00184 return this.getDBDataForAdd(pdaUID);
00185 }
00186
00187
00188
00189
00190 protected String getSynchroTarget() {
00191 return collectionName;
00192 }
00193
00194
00195
00196
00197 public String getSyncName() {
00198 return collectionName;
00199 }
00200
00201
00202
00203
00204 protected Credential getSyncroCredential() {
00205 return null;
00206 }
00207
00208
00209
00210
00211 protected ImportQueryManager getImportQueryManager() {
00212 return queryManager;
00213 }
00214
00215 protected JournalLogRenderer getJournalLogRenderer() {
00216 return null;
00217 }
00218
00219 }