OpenMSPFODBSyncListener.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
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       //manage  SynchroFODBReturnListener synchro meta data
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       //add cypher if any
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       //manage collection descriptor
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             //reload db to take into account file change
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   /* (non-Javadoc)
00164    * @see org.openmobileis.synchro.openmsp.client.OpenMSPDBSyncListener#getDBDataForAdd(java.lang.String)
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()));  //encode UID because if some special caractere like ' or ; is present sync data can't be decoded.
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   /* (non-Javadoc)
00184    * @see org.openmobileis.synchro.openmsp.client.OpenMSPDBSyncListener#getDBDataForReplace(java.lang.String)
00185    */
00186   protected String[] getDBDataForReplace(String pdaUID) throws OpenMSPException {
00187     return this.getDBDataForAdd(pdaUID);
00188   }
00189 
00190   /* (non-Javadoc)
00191    * @see org.openmobileis.synchro.openmsp.client.OpenMSPDBSyncListener#getSynchroTarget()
00192    */
00193   protected String getSynchroTarget() {
00194     return collectionName;
00195   }
00196 
00197   /* (non-Javadoc)
00198    * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#getSyncName()
00199    */
00200   public String getSyncName() {
00201     return collectionName;
00202   }
00203 
00204   /* (non-Javadoc)
00205    * @see org.openmobileis.synchro.openmsp.client.OpenMSPDBSyncListener#getSyncroCredential()
00206    */
00207   protected Credential getSyncroCredential() {
00208     return null;
00209   }
00210 
00211   /* (non-Javadoc)
00212    * @see org.openmobileis.synchro.openmsp.client.OpenMSPDBSyncListener#getImportQueryManager()
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  //     if (obj instanceof Serializable)  ostr.writeObject(obj);
00266  //     else ((Externalizable)obj).writeExternal(ostr);
00267     } finally   {
00268       bstr.flush();
00269       bstr.close();
00270     }
00271     return bstr.toByteArray();
00272 
00273   }
00274 
00275 }

Generated on Mon Dec 4 11:03:28 2006 for OpenMobileIS by  doxygen 1.5.1-p1