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-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
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.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       //add cypher if any
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       //manage collection descriptor
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     //reload db to take into account file change
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   /* (non-Javadoc)
00161    * @see org.openmobileis.synchro.openmsp.client.OpenMSPDBSyncListener#getDBDataForAdd(java.lang.String)
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   /* (non-Javadoc)
00181    * @see org.openmobileis.synchro.openmsp.client.OpenMSPDBSyncListener#getDBDataForReplace(java.lang.String)
00182    */
00183   protected String[] getDBDataForReplace(String pdaUID) throws OpenMSPException {
00184     return this.getDBDataForAdd(pdaUID);
00185   }
00186 
00187   /* (non-Javadoc)
00188    * @see org.openmobileis.synchro.openmsp.client.OpenMSPDBSyncListener#getSynchroTarget()
00189    */
00190   protected String getSynchroTarget() {
00191     return collectionName;
00192   }
00193 
00194   /* (non-Javadoc)
00195    * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#getSyncName()
00196    */
00197   public String getSyncName() {
00198     return collectionName;
00199   }
00200 
00201   /* (non-Javadoc)
00202    * @see org.openmobileis.synchro.openmsp.client.OpenMSPDBSyncListener#getSyncroCredential()
00203    */
00204   protected Credential getSyncroCredential() {
00205     return null;
00206   }
00207 
00208   /* (non-Javadoc)
00209    * @see org.openmobileis.synchro.openmsp.client.OpenMSPDBSyncListener#getImportQueryManager()
00210    */
00211   protected ImportQueryManager getImportQueryManager() {
00212     return queryManager;
00213   }
00214 
00215   protected JournalLogRenderer getJournalLogRenderer() {
00216     return null;
00217   }
00218 
00219 }

Generated on Wed Dec 14 21:05:34 2005 for OpenMobileIS by  doxygen 1.4.4