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

FODBOpenMSPSynchroTargetWrapper.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.server;
00026 
00027 import java.io.BufferedInputStream;
00028 import java.io.ByteArrayInputStream;
00029 import java.io.File;
00030 import java.io.FileInputStream;
00031 import java.io.FilenameFilter;
00032 import java.io.InputStream;
00033 import java.io.ObjectInputStream;
00034 import java.util.StringTokenizer;
00035 
00036 import org.openmobileis.common.context.SessionContext;
00037 import org.openmobileis.common.context.SessionContextManager;
00038 import org.openmobileis.common.user.UserNotFoundException;
00039 import org.openmobileis.common.util.PropertiesManager;
00040 import org.openmobileis.common.util.codec.GeneralCoder;
00041 import org.openmobileis.common.util.collection.Array;
00042 import org.openmobileis.common.util.exception.ServiceException;
00043 import org.openmobileis.database.fastobjectdb.FODBCollectionDescriptor;
00044 import org.openmobileis.database.fastobjectdb.FODBIndexDescriptor;
00045 import org.openmobileis.database.fastobjectdb.FastObjectDB;
00046 import org.openmobileis.database.fastobjectdb.db.FODBCollection;
00047 import org.openmobileis.database.fastobjectdb.db.crypto.FODBCypher;
00048 import org.openmobileis.synchro.algo.replication.SynchroAtomicObject;
00049 import org.openmobileis.synchro.algo.replication.SynchroConflicResolver;
00050 import org.openmobileis.synchro.openmsp.OpenMSPException;
00051 import org.openmobileis.synchro.openmsp.server.synctarget.OpenMSPDBSynchroTargetListener;
00052 import org.openmobileis.synchro.openmsp.server.util.OpenMISFile;
00053 import org.openmobileis.synchro.openmsp.server.util.ZipEntryMemoryFile;
00054 import org.openmobileis.synchro.security.auth.Credential;
00055 
00064 public final class FODBOpenMSPSynchroTargetWrapper extends OpenMSPDBSynchroTargetListener {
00065   private FODBSyncTarget syncService;
00066   private String pregenerateddbpath;
00067 
00072   public FODBOpenMSPSynchroTargetWrapper(FODBSyncTarget syncService, String pregenerateddbpath) {
00073     this(syncService);
00074     this.pregenerateddbpath = pregenerateddbpath;
00075  }
00076 
00081   public FODBOpenMSPSynchroTargetWrapper(FODBSyncTarget syncService) {
00082     super();
00083     //init service data
00084     this.syncService = syncService;
00085   }
00086 
00087   /* (non-Javadoc)
00088    * @see org.openmobileis.synchro.openmsp.server.synctarget.OpenMSPDBSynchroTargetListener#getTargetName()
00089    */
00090   public String getTargetName() {
00091     return syncService.getCollectionName();
00092   }
00093 
00094   /* (non-Javadoc)
00095    * @see org.openmobileis.synchro.openmsp.server.synctarget.OpenMSPDBSynchroTargetListener#connect(org.openmobileis.synchro.security.auth.Credential)
00096    */
00097   protected void connect(Credential cred) throws UserNotFoundException, ServiceException {
00098     syncService.connect(cred);
00099 
00100   }
00101 
00102   /* (non-Javadoc)
00103    * @see org.openmobileis.synchro.openmsp.server.synctarget.OpenMSPDBSynchroTargetListener#disconnect()
00104    */
00105   protected void disconnect() {
00106     syncService.disconnect();
00107 
00108   }
00109 
00110   /* (non-Javadoc)
00111    * @see org.openmobileis.synchro.openmsp.server.synctarget.OpenMSPDBSynchroTargetListener#getSynchroConflicResolver()
00112    */
00113   protected SynchroConflicResolver getSynchroConflicResolver() {
00114      return syncService.getConflicResolver();
00115   }
00116 
00117   /* (non-Javadoc)
00118    * @see org.openmobileis.synchro.openmsp.server.synctarget.OpenMSPDBSynchroTargetListener#getAllModifiedAtomicObjectSince(long)
00119    */
00120   protected SynchroAtomicObject[] getAllModifiedAtomicObjectSince(long syncNumber) throws OpenMSPException {
00121     return syncService.getAllModifiedAtomicObjectSince(syncNumber);
00122   }
00123 
00124   /* (non-Javadoc)
00125    * @see org.openmobileis.synchro.openmsp.server.synctarget.OpenMSPDBSynchroTargetListener#updateTargetWithSynchroObject(java.lang.Object)
00126    */
00127   protected void updateTargetWithSynchroObject(Object syncObject) throws OpenMSPException {
00128     syncService.updateCollectionObject(syncObject);
00129   }
00130 
00131   /* (non-Javadoc)
00132    * @see org.openmobileis.synchro.openmsp.server.synctarget.OpenMSPDBSynchroTargetListener#deleteTargetForSynchroObjectId(java.lang.String)
00133    */
00134   protected void deleteTargetForSynchroObjectId(String uid) throws OpenMSPException {
00135     syncService.deleteCollectionObject(uid);
00136 
00137   }
00138 
00139   /* (non-Javadoc)
00140    * @see org.openmobileis.synchro.openmsp.server.synctarget.OpenMSPDBSynchroTargetListener#getTargetObjectWithId(java.lang.String)
00141    */
00142   protected Object getTargetObjectWithId(String uid) throws OpenMSPException {
00143     return syncService.getCollectionObjectWithId(uid);
00144   }
00145 
00152   protected int getUpdateMaxNbRow() {
00153     return syncService.getUpdateMaxNbRow();
00154   }
00155  
00159   protected void processSyncActionMetaData(String metadata) throws ServiceException {
00160     StringTokenizer token = new StringTokenizer(metadata, ":");
00161     if (token.hasMoreTokens())  {
00162       String collectionName = token.nextToken();
00163       SessionContext context = SessionContextManager.getManager().getSessionContext();
00164       context.setAttribute("FODBSyncServiceWrapper%class", collectionName);
00165       
00166       //manage cypher
00167       String encodedcypher = token.nextToken();
00168       if (encodedcypher.equals("nocypher")) {
00169         context.removeAttribute("FODBSyncServiceWrapper%cypher");
00170       } else  {
00171         try {
00172           ByteArrayInputStream bstr = new ByteArrayInputStream(GeneralCoder.decodeBase64(encodedcypher.getBytes()));
00173           ObjectInputStream ostr = new ObjectInputStream(bstr);
00174           FODBCypher cypher =  (FODBCypher)ostr.readObject();
00175           context.setAttribute("FODBSyncServiceWrapper%cypher", cypher);
00176         } catch (Throwable ex)  {
00177           throw new ServiceException(ex);
00178         }
00179       }     
00180       
00181       //manage collection descriptor
00182       String coldess = token.nextToken();
00183       try {
00184         ByteArrayInputStream bstr = new ByteArrayInputStream(GeneralCoder.decodeBase64(coldess.getBytes()));
00185         ObjectInputStream ostr = new ObjectInputStream(bstr);
00186         FODBCollectionDescriptor coldesc =  (FODBCollectionDescriptor)ostr.readObject();
00187         context.setAttribute("FODBSyncServiceWrapper%coldesc", coldesc);
00188     } catch (Throwable ex)  {
00189       throw new ServiceException(ex);
00190     }
00191      
00192       int nbToken = token.countTokens();
00193       FODBIndexDescriptor[] descList = new FODBIndexDescriptor[nbToken];
00194       int i=0;
00195       while (token.hasMoreTokens()) {
00196         String descriptor = token.nextToken();
00197         try {
00198             ByteArrayInputStream bstr = new ByteArrayInputStream(GeneralCoder.decodeBase64(descriptor.getBytes()));
00199             ObjectInputStream ostr = new ObjectInputStream(bstr);
00200             FODBIndexDescriptor desc =  (FODBIndexDescriptor)ostr.readObject();
00201             descList[i] = desc;
00202         } catch (Throwable ex)  {
00203           throw new ServiceException(ex);
00204         }
00205         i++;
00206       }
00207       context.setAttribute("FODBSyncServiceWrapper%desclist", descList);
00208     }
00209     
00210   }
00211 
00212 
00213   /* (non-Javadoc)
00214    * @see org.openmobileis.server.synchro.safeserver.services.DefaultDBService#getDatabaseImportFiles()
00215    */
00216   protected OpenMISFile[] getDatabaseImportFiles() throws ServiceException {
00217     if (this.pregenerateddbpath == null)  {
00218       return this.generateImportDatabaseFiles();
00219     } else  {
00220       return this.getPregenerateDatabaseFiles();
00221     }
00222   }
00223   
00224   private synchronized OpenMISFile[] getPregenerateDatabaseFiles()  throws ServiceException {
00225     try {
00226       //get database name
00227       /*
00228       int index  = this.pregenerateddbpath.indexOf(File.separatorChar);
00229       String databasename = null;
00230       if (index != -1)  { to do that db must temporary copied.
00231         databasename = this.pregenerateddbpath.substring(index+1, this.pregenerateddbpath.length());
00232         String dbRootPath =  this.pregenerateddbpath.substring(0, index);
00233         FastObjectDB db = FastObjectDB.open(dbRootPath, "databasename");
00234         syncService.updateSynchroDB(db);      
00235       } */
00236       
00237       //get db collection files
00238       File dbFiles = new File(this.pregenerateddbpath);
00239       File[] colFiles = dbFiles.listFiles(new FilenameFilter() {
00240         public boolean accept(File arg0, String arg1) {
00241           return arg1.endsWith(".col");
00242         }
00243       }); 
00244       OpenMISFile[] retDbFiles = new OpenMISFile[colFiles.length];
00245       String dbPath = this.pregenerateddbpath+File.separatorChar;
00246       String filePath = null;
00247       for (int i=0; i<retDbFiles.length;i++)  {
00248         String fileColName =colFiles[i].getName(); 
00249         filePath = dbPath+fileColName;
00250         InputStream file = new BufferedInputStream(new FileInputStream(filePath));
00251         retDbFiles[i] = new ZipEntryMemoryFile("/"+fileColName, file);
00252         ((ZipEntryMemoryFile)retDbFiles[i]).setZipFileName("/dbfiles/"+this.getTargetName()+".zip");
00253         file.close();
00254       }
00255       
00256       return retDbFiles;
00257     } catch (Throwable ex)  {
00258       throw new ServiceException(ex);
00259     }
00260   }
00261   
00262   private OpenMISFile[] generateImportDatabaseFiles() throws ServiceException {
00263     try {
00264       String installPath = PropertiesManager.getManager().getProperty("org.openmobileis.database.fastobjectdb.synchro.server.generateddb.installpath");
00265       if (installPath == null)  {
00266         installPath = System.getProperty("user.dir")+File.separator+"fodbgen"; // default Path
00267       }
00268       SessionContext context  = SessionContextManager.getManager().getSessionContext();
00269       String userId = context.getUserId();
00270       StringBuffer buff = new StringBuffer(installPath).append(File.separatorChar);
00271       buff.append("temp").append(File.separatorChar).append(userId);
00272       String dbRootPath = buff.toString();
00273       buff.append(File.separatorChar).append("fodb");
00274       String dbPath = buff.toString();
00275       buff.append(File.separatorChar).append(syncService.getCollectionName()).append(".col");
00276       String filePath = buff.toString();
00277       
00278       File toDelFile = new File(filePath);
00279       if (toDelFile.exists()) {
00280         toDelFile.delete();
00281         toDelFile = new File(dbPath+File.separatorChar+"fodb.db");
00282         toDelFile.delete();
00283       }
00284       FastObjectDB db = FastObjectDB.open(dbRootPath, "fodb");
00285       //create collection index
00286       FODBCollectionDescriptor coldesc =  (FODBCollectionDescriptor)context.getAttribute("FODBSyncServiceWrapper%coldesc");
00287       db.createCollection(coldesc);
00288       
00289       //manage cypher
00290       FODBCypher cypher = (FODBCypher)context.getAttribute("FODBSyncServiceWrapper%cypher");
00291       if (cypher != null) {
00292         FODBCollection col = db.getCollection(syncService.getCollectionName());
00293         col.setCollectionCypher(cypher);
00294       }
00295       FODBIndexDescriptor[] descList = (FODBIndexDescriptor[])context.getAttribute("FODBSyncServiceWrapper%desclist");
00296       for (int i=0; i<descList.length; i++) {
00297         if (descList[i] != null)  {
00298           db.addIndex(syncService.getCollectionName(), descList[i]);
00299         }
00300       }
00301       
00302       Array objList = syncService.getAllCollectionObject();
00303       for (int i=0; i<objList.size(); i++)  {
00304         db.add(syncService.getCollectionName(), objList.get(i));
00305       }
00306       syncService.updateSynchroDB(db);
00307       
00308       FODBCollection[] colList = db.getDatabaseCollectionArray();
00309       OpenMISFile[] retDbFiles = new OpenMISFile[colList.length];
00310       dbPath = dbPath+File.separatorChar;
00311       for (int i=0; i<retDbFiles.length;i++)  {
00312         String fileColName =colList[i].getName()+".col"; 
00313         filePath = dbPath+fileColName;
00314         InputStream file = new BufferedInputStream(new FileInputStream(filePath));
00315         retDbFiles[i] = new ZipEntryMemoryFile("/"+fileColName, file);
00316         ((ZipEntryMemoryFile)retDbFiles[i]).setZipFileName("/dbfiles/"+this.getTargetName()+".zip");
00317         file.close();
00318         toDelFile = new File(filePath);
00319         if (toDelFile.exists()) {
00320           toDelFile.delete();
00321         }
00322       }
00323       toDelFile = new File(dbPath+File.separatorChar+"fodb.db");
00324       if (toDelFile.exists()) {
00325         toDelFile.delete();
00326       }
00327       return retDbFiles;
00328     } catch (Throwable ex)  {
00329       throw new ServiceException(ex);
00330     }
00331   }
00332 
00333 }

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