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
00026
00027
00028
00029 package org.openmobileis.modules.common.log.server;
00030
00031 import java.io.BufferedOutputStream;
00032 import java.io.File;
00033 import java.io.FileOutputStream;
00034 import java.io.OutputStream;
00035
00036 import org.openmobileis.common.context.SessionContext;
00037 import org.openmobileis.common.context.SessionContextManager;
00038 import org.openmobileis.common.util.codec.GeneralCoder;
00039 import org.openmobileis.common.util.log.LogManager;
00040 import org.openmobileis.synchro.openmsp.OpenMSPException;
00041 import org.openmobileis.synchro.openmsp.protocol.AbstractCommand;
00042 import org.openmobileis.synchro.openmsp.protocol.ContainerMessage;
00043 import org.openmobileis.synchro.openmsp.protocol.DataItem;
00044 import org.openmobileis.synchro.openmsp.protocol.Element;
00045 import org.openmobileis.synchro.openmsp.protocol.Status;
00046 import org.openmobileis.synchro.openmsp.server.synctarget.OpenMSPSynchroTargetListener;
00047 import org.openmobileis.synchro.openmsp.server.synctarget.SyncTargetAnswer;
00048 import org.openmobileis.synchro.security.auth.Credential;
00049
00059 public final class LogSyncService implements OpenMSPSynchroTargetListener {
00060 private String basePath = null;
00061
00062 public LogSyncService() {
00063 basePath = System.getProperty("user.dir");
00064 }
00065
00066 public void setLogBasePath(String path) {
00067 LogManager.traceInfo(0, "LogSyncService setLogBasePath :"+path);
00068 this.basePath = path;
00069 }
00070
00071 public SyncTargetAnswer processCommand(Credential cred, ContainerMessage containerMessage) throws OpenMSPException {
00072 AbstractCommand syncCommand = (AbstractCommand) containerMessage.getElement();
00073 int commandType = syncCommand.getElementType();
00074 Status status = new Status(syncCommand.getCmdId(), Status.STATUS_WRONG_FORMAT);
00075 if (commandType == Element.SYNC) {
00076
00077 while (containerMessage.hasMoreMessage()) {
00078 ContainerMessage commandContainer = containerMessage.nextMessage();
00079 Element command = commandContainer.getElement();
00080 if (command.getElementType() == Element.ADD) {
00081 this.processAddCommand(commandContainer);
00082 status = new Status(syncCommand.getCmdId(), Status.STATUS_OK);
00083 }
00084 }
00085 }
00086 SyncTargetAnswer answer = new SyncTargetAnswer();
00087 status.setCmdRef(syncCommand.getCmdId());
00088 ContainerMessage[] returnContainer = new ContainerMessage[1];
00089 returnContainer[0] = new ContainerMessage(status);
00090 answer.containerMessage = returnContainer;
00091 return answer;
00092 }
00093
00094 private Status processAddCommand(ContainerMessage addContainer) {
00095 try {
00096 SessionContext context = SessionContextManager.getManager().getSessionContext();
00097 String rep = context.getUserId();
00098 Status resStat = null;
00099 String filePath = basePath + File.separator + "pdalogs" + File.separator + rep;
00100 while (addContainer.hasMoreMessage()) {
00101 DataItem item = (DataItem) addContainer.nextMessage().getElement();
00102 String fileName = item.getMetaInformation();
00103 byte[] fileData = item.getData().getBytes();
00104 fileData = GeneralCoder.decodeBase64(fileData);
00105 File newFile = new File(filePath);
00106 if (!newFile.exists()) {
00107 newFile.mkdirs();
00108 }
00109 OutputStream destFile = new BufferedOutputStream(new FileOutputStream(filePath + File.separator + fileName, true));
00110 try {
00111 destFile.write(fileData);
00112 } finally {
00113 destFile.flush();
00114 destFile.close();
00115 }
00116 }
00117 resStat = new Status(addContainer.getElement().getCmdId(), Status.STATUS_OK);
00118 return resStat;
00119 } catch (Throwable ex) {
00120 LogManager.traceError(0, ex);
00121 return new Status(addContainer.getElement().getCmdId(), Status.STATUS_FAILED);
00122 }
00123 }
00124
00125
00126
00127
00128 public String getTargetName() {
00129 return "FWKSyncLogsManager";
00130 }
00131
00132 }