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.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
00066 public final class FODBOpenMSPSynchroTargetWrapper extends OpenMSPDBSynchroTargetListener {
00067 private FODBSyncTarget syncService;
00068
00069 private String pregenerateddbpath;
00070
00076 public FODBOpenMSPSynchroTargetWrapper(FODBSyncTarget syncService, String pregenerateddbpath) {
00077 this(syncService);
00078 this.pregenerateddbpath = pregenerateddbpath;
00079 }
00080
00085 public FODBOpenMSPSynchroTargetWrapper(FODBSyncTarget syncService) {
00086 super();
00087
00088 this.syncService = syncService;
00089 }
00090
00091
00092
00093
00094
00095
00096 public String getTargetName() {
00097 return syncService.getCollectionName();
00098 }
00099
00100
00101
00102
00103
00104
00105 protected void connect(Credential cred) throws UserNotFoundException, ServiceException {
00106 syncService.connect(cred);
00107
00108 }
00109
00110
00111
00112
00113
00114
00115 protected void disconnect() {
00116 syncService.disconnect();
00117
00118 }
00119
00120
00121
00122
00123
00124
00125 protected SynchroConflicResolver getSynchroConflicResolver() {
00126 return syncService.getConflicResolver();
00127 }
00128
00129
00130
00131
00132
00133
00134 protected SynchroAtomicObject[] getAllModifiedAtomicObjectSince(long syncNumber) throws OpenMSPException {
00135 return syncService.getAllModifiedAtomicObjectSince(syncNumber);
00136 }
00137
00138
00139
00140
00141
00142
00143 protected void updateTargetWithSynchroObject(Object syncObject) throws OpenMSPException {
00144 syncService.updateCollectionObject(syncObject);
00145 }
00146
00147
00148
00149
00150
00151
00152 protected void deleteTargetForSynchroObjectId(String uid) throws OpenMSPException {
00153 syncService.deleteCollectionObject(uid);
00154
00155 }
00156
00157
00158
00159
00160
00161
00162 protected Object getTargetObjectWithId(String uid) throws OpenMSPException {
00163 return syncService.getCollectionObjectWithId(uid);
00164 }
00165
00172 protected int getUpdateMaxNbRow() {
00173 return syncService.getUpdateMaxNbRow();
00174 }
00175
00179 protected void processSyncActionMetaData(String metadata) throws ServiceException {
00180 StringTokenizer token = new StringTokenizer(metadata, ":");
00181 if (token.hasMoreTokens()) {
00182 String collectionName = token.nextToken();
00183 SessionContext context = SessionContextManager.getManager().getSessionContext();
00184 context.setAttribute("FODBSyncServiceWrapper%class", collectionName);
00185
00186
00187 String encodedcypher = token.nextToken();
00188 if (encodedcypher.equals("nocypher")) {
00189 context.removeAttribute("FODBSyncServiceWrapper%cypher");
00190 } else {
00191 try {
00192 ByteArrayInputStream bstr = new ByteArrayInputStream(GeneralCoder.decodeBase64(encodedcypher.getBytes()));
00193 ObjectInputStream ostr = new ObjectInputStream(bstr);
00194 FODBCypher cypher = (FODBCypher) ostr.readObject();
00195 context.setAttribute("FODBSyncServiceWrapper%cypher", cypher);
00196 } catch (Throwable ex) {
00197 throw new ServiceException(ex);
00198 }
00199 }
00200
00201
00202 String coldess = token.nextToken();
00203 try {
00204 ByteArrayInputStream bstr = new ByteArrayInputStream(GeneralCoder.decodeBase64(coldess.getBytes()));
00205 ObjectInputStream ostr = new ObjectInputStream(bstr);
00206 FODBCollectionDescriptor coldesc = (FODBCollectionDescriptor) ostr.readObject();
00207 context.setAttribute("FODBSyncServiceWrapper%coldesc", coldesc);
00208 } catch (Throwable ex) {
00209 throw new ServiceException(ex);
00210 }
00211
00212 int nbToken = token.countTokens();
00213 FODBIndexDescriptor[] descList = new FODBIndexDescriptor[nbToken];
00214 int i = 0;
00215 while (token.hasMoreTokens()) {
00216 String descriptor = token.nextToken();
00217 try {
00218 ByteArrayInputStream bstr = new ByteArrayInputStream(GeneralCoder.decodeBase64(descriptor.getBytes()));
00219 ObjectInputStream ostr = new ObjectInputStream(bstr);
00220 FODBIndexDescriptor desc = (FODBIndexDescriptor) ostr.readObject();
00221 descList[i] = desc;
00222 } catch (Throwable ex) {
00223 throw new ServiceException(ex);
00224 }
00225 i++;
00226 }
00227 context.setAttribute("FODBSyncServiceWrapper%desclist", descList);
00228 }
00229
00230 }
00231
00232
00233
00234
00235
00236
00237 protected OpenMISFile[] getDatabaseImportFiles() throws ServiceException {
00238 if (this.pregenerateddbpath == null) {
00239 return this.generateImportDatabaseFiles();
00240 } else {
00241 return this.getPregenerateDatabaseFiles();
00242 }
00243 }
00244
00245 private synchronized OpenMISFile[] getPregenerateDatabaseFiles() throws ServiceException {
00246 try {
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259 File dbFiles = new File(this.pregenerateddbpath);
00260 File[] colFiles = dbFiles.listFiles(new FilenameFilter() {
00261 public boolean accept(File arg0, String arg1) {
00262 return arg1.endsWith(".col");
00263 }
00264 });
00265 OpenMISFile[] retDbFiles = new OpenMISFile[colFiles.length];
00266 String dbPath = this.pregenerateddbpath + File.separatorChar;
00267 String filePath = null;
00268 for (int i = 0; i < retDbFiles.length; i++) {
00269 String fileColName = colFiles[i].getName();
00270 filePath = dbPath + fileColName;
00271 InputStream file = new BufferedInputStream(new FileInputStream(filePath));
00272 retDbFiles[i] = new ZipEntryMemoryFile("/" + fileColName, file);
00273 ((ZipEntryMemoryFile) retDbFiles[i]).setZipFileName("/dbfiles/" + this.getTargetName() + ".zip");
00274 file.close();
00275 }
00276
00277 return retDbFiles;
00278 } catch (Throwable ex) {
00279 throw new ServiceException(ex);
00280 }
00281 }
00282
00283 private OpenMISFile[] generateImportDatabaseFiles() throws ServiceException {
00284 try {
00285 String installPath = PropertiesManager.getManager().getProperty("org.openmobileis.database.fastobjectdb.synchro.server.generateddb.installpath");
00286 if (installPath == null) {
00287 installPath = System.getProperty("user.dir") + File.separator + "fodbgen";
00288
00289 }
00290 SessionContext context = SessionContextManager.getManager().getSessionContext();
00291 String userId = context.getUserId();
00292 StringBuffer buff = new StringBuffer(installPath).append(File.separatorChar);
00293 buff.append("temp").append(File.separatorChar).append(userId);
00294 String dbRootPath = buff.toString();
00295 buff.append(File.separatorChar).append("fodb");
00296 String dbPath = buff.toString();
00297 buff.append(File.separatorChar).append(syncService.getCollectionName()).append(".col");
00298 String filePath = buff.toString();
00299
00300 File toDelFile = new File(filePath);
00301 if (toDelFile.exists()) {
00302 toDelFile.delete();
00303 toDelFile = new File(dbPath + File.separatorChar + "fodb.db");
00304 toDelFile.delete();
00305 }
00306 FastObjectDB db = FastObjectDB.open(dbRootPath, "fodb");
00307 try {
00308
00309 FODBCollectionDescriptor coldesc = (FODBCollectionDescriptor) context.getAttribute("FODBSyncServiceWrapper%coldesc");
00310 db.createCollection(coldesc);
00311
00312
00313 FODBCypher cypher = (FODBCypher) context.getAttribute("FODBSyncServiceWrapper%cypher");
00314 if (cypher != null) {
00315 FODBCollection col = db.getCollection(syncService.getCollectionName());
00316 col.setCollectionCypher(cypher);
00317 }
00318 FODBIndexDescriptor[] descList = (FODBIndexDescriptor[]) context.getAttribute("FODBSyncServiceWrapper%desclist");
00319 for (int i = 0; i < descList.length; i++) {
00320 if (descList[i] != null) {
00321 db.addIndex(syncService.getCollectionName(), descList[i]);
00322 }
00323 }
00324
00325 Array objList = syncService.getAllCollectionObject();
00326 for (int i = 0; i < objList.size(); i++) {
00327 db.add(syncService.getCollectionName(), objList.get(i));
00328 }
00329 syncService.updateSynchroDB(db);
00330
00331 FODBCollection[] colList = db.getDatabaseCollectionArray();
00332 OpenMISFile[] retDbFiles = new OpenMISFile[colList.length];
00333 dbPath = dbPath + File.separatorChar;
00334 for (int i = 0; i < retDbFiles.length; i++) {
00335 String fileColName = colList[i].getName() + ".col";
00336 filePath = dbPath + fileColName;
00337 InputStream file = new BufferedInputStream(new FileInputStream(filePath));
00338 retDbFiles[i] = new ZipEntryMemoryFile("/" + fileColName, file);
00339 ((ZipEntryMemoryFile) retDbFiles[i]).setZipFileName("/dbfiles/" + this.getTargetName() + ".zip");
00340 file.close();
00341 }
00342 return retDbFiles;
00343 } finally {
00344 toDelFile = new File(dbPath);
00345 File[] files = toDelFile.listFiles();
00346 for (int i = 0; i < files.length; i++) {
00347 files[i].delete();
00348 }
00349 }
00350 } catch (Throwable ex) {
00351 throw new ServiceException(ex);
00352 }
00353 }
00354
00355 }