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