Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

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.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       //add cypher if any
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       //manage collection descriptor
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             //reload db to take into account file change
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   /* (non-Javadoc)
00162    * @see org.openmobileis.synchro.openmsp.client.OpenMSPDBSyncListener#getDBDataForAdd(java.lang.String)
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()));  //encode UID because if some special caractere like ' or ; is present sync data can't be decoded.
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   /* (non-Javadoc)
00182    * @see org.openmobileis.synchro.openmsp.client.OpenMSPDBSyncListener#getDBDataForReplace(java.lang.String)
00183    */
00184   protected String[] getDBDataForReplace(String pdaUID) throws OpenMSPException {
00185     return this.getDBDataForAdd(pdaUID);
00186   }
00187 
00188   /* (non-Javadoc)
00189    * @see org.openmobileis.synchro.openmsp.client.OpenMSPDBSyncListener#getSynchroTarget()
00190    */
00191   protected String getSynchroTarget() {
00192     return collectionName;
00193   }
00194 
00195   /* (non-Javadoc)
00196    * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#getSyncName()
00197    */
00198   public String getSyncName() {
00199     return collectionName;
00200   }
00201 
00202   /* (non-Javadoc)
00203    * @see org.openmobileis.synchro.openmsp.client.OpenMSPDBSyncListener#getSyncroCredential()
00204    */
00205   protected Credential getSyncroCredential() {
00206     return null;
00207   }
00208 
00209   /* (non-Javadoc)
00210    * @see org.openmobileis.synchro.openmsp.client.OpenMSPDBSyncListener#getImportQueryManager()
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 }

Generated on Mon Jul 10 10:29:31 2006 for OpenMobileIS by  doxygen 1.4.4