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

LogSyncService.java

00001 /*
00002  *        OpenMobileIS - a free Java Framework for mobile applications
00003  *
00004  *   Copyright (C) 2002  Philippe Delrieu.
00005  *
00006  *   This program is free software; you can redistribute it and/or
00007  *   modify it under the terms of the GNU General Public
00008  *   License as published by the Free Software Foundation; either
00009  *   version 2 of the License, or (at your option) any later version.
00010  *
00011  *   This program is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *   General Public License for more details.
00015  *
00016  *   You should have received a copy of the GNU Library General Public
00017  *   License along with this library; if not, write to the Free
00018  *   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  * 
00020  * Philippe Delrieu kept the rigth to distribute all code Copyrighted by philippe Delrieu
00021  *  under other licence term even commercial one.
00022  *  
00023  *  Modifications :
00024  *  2002 Creation P.Delrieu
00025  * 
00026  */
00027 
00028 package org.openmobileis.modules.common.log.server;
00029 
00030 import java.io.BufferedOutputStream;
00031 import java.io.File;
00032 import java.io.FileOutputStream;
00033 import java.io.OutputStream;
00034 
00035 import org.openmobileis.common.context.SessionContext;
00036 import org.openmobileis.common.context.SessionContextManager;
00037 import org.openmobileis.common.util.codec.GeneralCoder;
00038 import org.openmobileis.common.util.log.LogManager;
00039 import org.openmobileis.synchro.openmsp.OpenMSPException;
00040 import org.openmobileis.synchro.openmsp.protocol.AbstractCommand;
00041 import org.openmobileis.synchro.openmsp.protocol.ContainerMessage;
00042 import org.openmobileis.synchro.openmsp.protocol.DataItem;
00043 import org.openmobileis.synchro.openmsp.protocol.Element;
00044 import org.openmobileis.synchro.openmsp.protocol.Status;
00045 import org.openmobileis.synchro.openmsp.server.synctarget.OpenMSPSynchroTargetListener;
00046 import org.openmobileis.synchro.openmsp.server.synctarget.SyncTargetAnswer;
00047 import org.openmobileis.synchro.security.auth.Credential;
00048 
00058 public final class LogSyncService implements OpenMSPSynchroTargetListener {
00059 
00060   public LogSyncService() {
00061     // load mail connectors
00062   }
00063 
00064  public SyncTargetAnswer processCommand(Credential cred, ContainerMessage containerMessage) throws OpenMSPException {
00065     AbstractCommand syncCommand = (AbstractCommand) containerMessage.getElement();
00066     int commandType = syncCommand.getElementType();
00067     Status status = new Status(syncCommand.getCmdId(), Status.STATUS_WRONG_FORMAT);
00068    if (commandType == Element.SYNC) {
00069       // process incomming data
00070       while (containerMessage.hasMoreMessage()) {
00071         ContainerMessage commandContainer = containerMessage.nextMessage();
00072         Element command = commandContainer.getElement();
00073         if (command.getElementType() == Element.ADD) {
00074           this.processAddCommand(commandContainer);
00075           status = new Status(syncCommand.getCmdId(), Status.STATUS_OK);
00076         }
00077       }
00078     }
00079     SyncTargetAnswer answer = new SyncTargetAnswer();
00080     status.setCmdRef(syncCommand.getCmdId());
00081     ContainerMessage[] returnContainer = new ContainerMessage[1];
00082     returnContainer[0] = new ContainerMessage(status);
00083     answer.containerMessage = returnContainer;
00084     return answer;
00085   }
00086 
00087   private Status processAddCommand(ContainerMessage addContainer) {
00088     try {
00089       SessionContext context = SessionContextManager.getManager().getSessionContext();
00090       String rep = context.getUserId();
00091       String basePath = System.getProperty("user.dir");
00092       Status resStat = null;
00093       String filePath = basePath + File.separator + "pdalogs" + File.separator + rep + File.separator;
00094       while (addContainer.hasMoreMessage()) {
00095         DataItem item = (DataItem) addContainer.nextMessage().getElement();
00096         String fileName = item.getMetaInformation();
00097         byte[] fileData = item.getData().getBytes();
00098         fileData = GeneralCoder.decodeBase64(fileData);
00099         File newFile = new File(filePath);
00100         if (!newFile.exists()) {
00101           newFile.mkdirs();
00102         }
00103         OutputStream destFile = new BufferedOutputStream(new FileOutputStream(filePath + File.separator + fileName, true));
00104         try {
00105           destFile.write(fileData);
00106         } finally {
00107           destFile.flush();
00108           destFile.close();
00109         }
00110       }
00111       resStat = new Status(addContainer.getElement().getCmdId(), Status.STATUS_OK);
00112       return resStat;
00113     } catch (Throwable ex) {
00114       LogManager.traceError(0, ex);
00115       return new Status(addContainer.getElement().getCmdId(), Status.STATUS_FAILED);
00116     }
00117   }
00118 
00119   /* (non-Javadoc)
00120    * @see org.openmobileis.server.synchro.safeserver.services.DefaultSafeServerService#getServiceName()
00121    */
00122   public String getTargetName() {
00123     return "FWKSyncLogsManager";
00124   }
00125 
00126 }

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