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.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.IOException;
00033 import java.io.InputStream;
00034 import java.io.ObjectInputStream;
00035 import java.util.StringTokenizer;
00036
00037 import org.openmobileis.common.context.SessionContext;
00038 import org.openmobileis.common.context.SessionContextManager;
00039 import org.openmobileis.common.user.UserNotFoundException;
00040 import org.openmobileis.common.util.PropertiesManager;
00041 import org.openmobileis.common.util.UniqueIdGenerator;
00042 import org.openmobileis.common.util.codec.GeneralCoder;
00043 import org.openmobileis.common.util.collection.Array;
00044 import org.openmobileis.common.util.exception.BadDataFormatException;
00045 import org.openmobileis.common.util.exception.ServiceException;
00046 import org.openmobileis.common.util.log.LogManager;
00047 import org.openmobileis.database.fastobjectdb.FODBCollectionDescriptor;
00048 import org.openmobileis.database.fastobjectdb.FODBIndexDescriptor;
00049 import org.openmobileis.database.fastobjectdb.FastObjectDB;
00050 import org.openmobileis.database.fastobjectdb.db.FODBCollection;
00051 import org.openmobileis.database.fastobjectdb.db.crypto.FODBCypher;
00052 import org.openmobileis.database.fastobjectdb.db.exception.FODBException;
00053 import org.openmobileis.synchro.algo.replication.SynchroAtomicObject;
00054 import org.openmobileis.synchro.algo.replication.SynchroConflicResolver;
00055 import org.openmobileis.synchro.openmsp.OpenMSPException;
00056 import org.openmobileis.synchro.openmsp.server.synctarget.OpenMSPDBSynchroTargetListener;
00057 import org.openmobileis.synchro.openmsp.server.util.FileStoreZipEntryFile;
00058 import org.openmobileis.synchro.openmsp.server.util.MemoryZipEntryFile;
00059 import org.openmobileis.synchro.openmsp.server.util.OpenMISFile;
00060 import org.openmobileis.synchro.openmsp.server.util.ZipEntryFile;
00061 import org.openmobileis.synchro.security.auth.Credential;
00062
00073 public final class FODBOpenMSPSynchroTargetWrapper extends OpenMSPDBSynchroTargetListener {
00074 private FODBSyncTarget syncService;
00075
00076 private String pregenerateddbpath;
00077
00083 public FODBOpenMSPSynchroTargetWrapper(FODBSyncTarget syncService, String pregenerateddbpath) {
00084 this(syncService);
00085 this.pregenerateddbpath = pregenerateddbpath;
00086 }
00087
00092 public FODBOpenMSPSynchroTargetWrapper(FODBSyncTarget syncService) {
00093 super();
00094
00095 this.syncService = syncService;
00096 }
00097
00098
00099
00100
00101
00102
00103 public String getTargetName() {
00104 return syncService.getCollectionName();
00105 }
00106
00107
00108
00109
00110
00111
00112 protected void connect(Credential cred) throws UserNotFoundException, ServiceException {
00113 syncService.connect(cred);
00114
00115 }
00116
00117
00118
00119
00120
00121
00122 protected void disconnect() {
00123 syncService.disconnect();
00124
00125 }
00126
00127
00128
00129
00130
00131
00132 protected SynchroConflicResolver getSynchroConflicResolver() {
00133 return syncService.getConflicResolver();
00134 }
00135
00136
00137
00138
00139
00140
00141 protected SynchroAtomicObject[] getAllModifiedAtomicObjectSince(long syncNumber) throws OpenMSPException {
00142 return syncService.getAllModifiedAtomicObjectSince(syncNumber);
00143 }
00144
00145
00146
00147
00148
00149
00150 protected void updateTargetWithSynchroObject(Object syncObject, long updateSynchroNumber) throws OpenMSPException {
00151 syncService.updateCollectionObject(syncObject, updateSynchroNumber);
00152 }
00153
00154
00155
00156
00157
00158
00159 protected void deleteTargetForSynchroObjectId(String uid, long updateSynchroNumber) throws OpenMSPException {
00160 syncService.deleteCollectionObject(uid, updateSynchroNumber);
00161
00162 }
00163
00164
00165
00166
00167
00168
00169 protected Object getTargetObjectWithId(String uid) throws OpenMSPException {
00170 return syncService.getCollectionObjectWithId(uid);
00171 }
00172
00179 protected int getUpdateMaxNbRow() {
00180 return syncService.getUpdateMaxNbRow();
00181 }
00182
00186 protected void processSyncActionMetaData(String metadata) throws OpenMSPException {
00187 try {
00188
00189 int index = metadata.indexOf('%');
00190 if (index != -1) {
00191 String userdata = metadata.substring(0, index);
00192 if (!userdata.startsWith("no-metadata")) {
00193 Object userdataok = this.unserializeData(userdata);
00194 this.syncService.setSendSynchroMetaData(userdataok);
00195 }
00196 if (index+1 < metadata.length())
00197 metadata = metadata.substring(index+1, metadata.length());
00198 }
00199
00200 StringTokenizer token = new StringTokenizer(metadata, ":");
00201 if (token.hasMoreTokens()) {
00202 String collectionName = token.nextToken();
00203 SessionContext context = SessionContextManager.getManager().getSessionContext();
00204 context.setAttribute("FODBSyncServiceWrapper%class", collectionName);
00205
00206
00207 String encodedcypher = token.nextToken();
00208 if (encodedcypher.equals("nocypher")) {
00209 context.removeAttribute("FODBSyncServiceWrapper%cypher");
00210 } else {
00211 FODBCypher cypher = (FODBCypher) this.unserializeData(encodedcypher);
00212 context.setAttribute("FODBSyncServiceWrapper%cypher", cypher);
00213 }
00214
00215
00216 String coldess = token.nextToken();
00217 FODBCollectionDescriptor coldesc = (FODBCollectionDescriptor) this.unserializeData(coldess);
00218 context.setAttribute("FODBSyncServiceWrapper%coldesc", coldesc);
00219
00220 int nbToken = token.countTokens();
00221 FODBIndexDescriptor[] descList = new FODBIndexDescriptor[nbToken];
00222 int i = 0;
00223 while (token.hasMoreTokens()) {
00224 String descriptor = token.nextToken();
00225 FODBIndexDescriptor desc = (FODBIndexDescriptor) this.unserializeData(descriptor);
00226 descList[i] = desc;
00227 i++;
00228 }
00229 context.setAttribute("FODBSyncServiceWrapper%desclist", descList);
00230 }
00231 } catch (IOException ex) {
00232 throw new OpenMSPException(ex);
00233 } catch (ClassNotFoundException ex) {
00234 throw new OpenMSPException(ex);
00235 }
00236
00237 }
00238
00239 private Object unserializeData(String data) throws IOException, ClassNotFoundException {
00240 byte[] b = GeneralCoder.decodeBase64(data.getBytes());
00241 ByteArrayInputStream bstr = new ByteArrayInputStream(b);
00242 try {
00243 ObjectInputStream ostr = new ObjectInputStream(bstr);
00244 return ostr.readObject();
00245 } finally {
00246 bstr.close();
00247 }
00248 }
00249
00250
00251
00252
00253
00254
00255 protected OpenMISFile[] getDatabaseImportFiles() throws OpenMSPException {
00256 if (this.pregenerateddbpath == null) {
00257 return this.generateImportDatabaseFiles();
00258 } else {
00259 return this.getPregenerateDatabaseFiles();
00260 }
00261 }
00262
00263 private synchronized OpenMISFile[] getPregenerateDatabaseFiles() throws OpenMSPException {
00264 try {
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 File dbFiles = new File(this.pregenerateddbpath);
00278 File[] colFiles = dbFiles.listFiles(new FilenameFilter() {
00279 public boolean accept(File arg0, String arg1) {
00280 return arg1.endsWith(".col");
00281 }
00282 });
00283 OpenMISFile[] retDbFiles = new OpenMISFile[colFiles.length];
00284 for (int i = 0; i < retDbFiles.length; i++) {
00285 String fileColName = colFiles[i].getName();
00286 retDbFiles[i] = new FileStoreZipEntryFile(this.pregenerateddbpath, "/" + fileColName);
00287 ((ZipEntryFile) retDbFiles[i]).setZipFileName("/dbfiles/" + this.getTargetName() + ".zip");
00288 }
00289
00290 return retDbFiles;
00291 } catch (IOException ex) {
00292 throw new OpenMSPException(ex);
00293 }
00294 }
00295
00296 private OpenMISFile[] generateImportDatabaseFiles() throws OpenMSPException {
00297 LogManager.traceDebug(0, "FODBOpenMSPSynchroTargetWrapper enter generateImportDatabaseFiles");
00298 try {
00299 String installPath = PropertiesManager.getManager().getProperty("org.openmobileis.database.fastobjectdb.synchro.server.generateddb.installpath");
00300 if (installPath == null) {
00301 installPath = System.getProperty("user.dir") + File.separator + "fodbgen";
00302
00303 }
00304 SessionContext context = SessionContextManager.getManager().getSessionContext();
00305
00306 String newis = Long.toString(UniqueIdGenerator.getManager().getNewID());
00307 StringBuffer buff = new StringBuffer(installPath).append(File.separatorChar);
00308 buff.append("temp").append(File.separatorChar).append(newis);
00309 String dirPath = buff.toString();
00310 String dbRootPath = buff.toString();
00311 buff.append(File.separatorChar).append("fodb");
00312 String dbPath = buff.toString();
00313 buff.append(File.separatorChar).append(syncService.getCollectionName()).append(".col");
00314 String filePath = buff.toString();
00315 File toDelFile = new File(filePath);
00316 if (toDelFile.exists()) {
00317 LogManager.traceDebug(0, "FODBOpenMSPSynchroTargetWrapper generateImportDatabaseFiles File exist DELETE :"+filePath);
00318 toDelFile.delete();
00319 toDelFile = new File(dbPath + File.separatorChar + "fodb.db");
00320 toDelFile.delete();
00321 }
00322 LogManager.traceDebug(0, "FODBOpenMSPSynchroTargetWrapper generateImportDatabaseFiles Open DB path :"+dbRootPath);
00323 FastObjectDB db = FastObjectDB.open(dbRootPath, "fodb");
00324 try {
00325
00326 FODBCollectionDescriptor coldesc = (FODBCollectionDescriptor) context.getAttribute("FODBSyncServiceWrapper%coldesc");
00327 db.createCollection(coldesc);
00328
00329
00330 FODBCypher cypher = (FODBCypher) context.getAttribute("FODBSyncServiceWrapper%cypher");
00331 if (cypher != null) {
00332 FODBCollection col = db.getCollection(syncService.getCollectionName());
00333 col.setCollectionCypher(cypher);
00334 }
00335 FODBIndexDescriptor[] descList = (FODBIndexDescriptor[]) context.getAttribute("FODBSyncServiceWrapper%desclist");
00336 for (int i = 0; i < descList.length; i++) {
00337 if (descList[i] != null) {
00338 db.addIndex(syncService.getCollectionName(), descList[i]);
00339 }
00340 }
00341
00342 Array objList = syncService.getAllCollectionObject();
00343 for (int i = 0; i < objList.size(); i++) {
00344 db.add(syncService.getCollectionName(), objList.get(i));
00345 }
00346 syncService.updateSynchroDB(db);
00347
00348 FODBCollection[] colList = db.getDatabaseCollectionArray();
00349 OpenMISFile[] retDbFiles = new OpenMISFile[colList.length];
00350 dbPath = dbPath + File.separatorChar;
00351 for (int i = 0; i < retDbFiles.length; i++) {
00352 String fileColName = colList[i].getName() + ".col";
00353 filePath = dbPath + fileColName;
00354 InputStream file = new BufferedInputStream(new FileInputStream(filePath));
00355 retDbFiles[i] = new MemoryZipEntryFile("/" + fileColName, file);
00356 ((ZipEntryFile) retDbFiles[i]).setZipFileName("/dbfiles/" + this.getTargetName() + ".zip");
00357 file.close();
00358 }
00359 return retDbFiles;
00360 } finally {
00361 toDelFile = new File(dbPath);
00362 File[] files = toDelFile.listFiles();
00363 for (int i = 0; i < files.length; i++) {
00364 files[i].delete();
00365 }
00366
00367 File todeldir = new File(dirPath+"/fodb");
00368 todeldir.delete();
00369 todeldir = new File(dirPath);
00370 todeldir.delete();
00371 }
00372 } catch (ClassNotFoundException ex) {
00373 throw new OpenMSPException(ex);
00374 } catch (BadDataFormatException ex) {
00375 throw new OpenMSPException(ex);
00376 } catch (FODBException ex) {
00377 throw new OpenMSPException(ex);
00378 } catch (IOException ex) {
00379 throw new OpenMSPException(ex);
00380 }
00381 }
00382
00383 }