LogSyncService.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  Creation P.Delrieu
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 
00061   public LogSyncService() {
00062     // load mail connectors
00063   }
00064 
00065  public SyncTargetAnswer processCommand(Credential cred, ContainerMessage containerMessage) throws OpenMSPException {
00066     AbstractCommand syncCommand = (AbstractCommand) containerMessage.getElement();
00067     int commandType = syncCommand.getElementType();
00068     Status status = new Status(syncCommand.getCmdId(), Status.STATUS_WRONG_FORMAT);
00069    if (commandType == Element.SYNC) {
00070       // process incomming data
00071       while (containerMessage.hasMoreMessage()) {
00072         ContainerMessage commandContainer = containerMessage.nextMessage();
00073         Element command = commandContainer.getElement();
00074         if (command.getElementType() == Element.ADD) {
00075           this.processAddCommand(commandContainer);
00076           status = new Status(syncCommand.getCmdId(), Status.STATUS_OK);
00077         }
00078       }
00079     }
00080     SyncTargetAnswer answer = new SyncTargetAnswer();
00081     status.setCmdRef(syncCommand.getCmdId());
00082     ContainerMessage[] returnContainer = new ContainerMessage[1];
00083     returnContainer[0] = new ContainerMessage(status);
00084     answer.containerMessage = returnContainer;
00085     return answer;
00086   }
00087 
00088   private Status processAddCommand(ContainerMessage addContainer) {
00089     try {
00090       SessionContext context = SessionContextManager.getManager().getSessionContext();
00091       String rep = context.getUserId();
00092       String basePath = System.getProperty("user.dir");
00093       Status resStat = null;
00094       String filePath = basePath + File.separator + "pdalogs" + File.separator + rep;
00095       while (addContainer.hasMoreMessage()) {
00096         DataItem item = (DataItem) addContainer.nextMessage().getElement();
00097         String fileName = item.getMetaInformation();
00098         byte[] fileData = item.getData().getBytes();
00099         fileData = GeneralCoder.decodeBase64(fileData);
00100         File newFile = new File(filePath);
00101         if (!newFile.exists()) {
00102           newFile.mkdirs();
00103         }
00104         OutputStream destFile = new BufferedOutputStream(new FileOutputStream(filePath + File.separator + fileName, true));
00105         try {
00106           destFile.write(fileData);
00107         } finally {
00108           destFile.flush();
00109           destFile.close();
00110         }
00111       }
00112       resStat = new Status(addContainer.getElement().getCmdId(), Status.STATUS_OK);
00113       return resStat;
00114     } catch (Throwable ex) {
00115       LogManager.traceError(0, ex);
00116       return new Status(addContainer.getElement().getCmdId(), Status.STATUS_FAILED);
00117     }
00118   }
00119 
00120   /* (non-Javadoc)
00121    * @see org.openmobileis.server.synchro.safeserver.services.DefaultSafeServerService#getServiceName()
00122    */
00123   public String getTargetName() {
00124     return "FWKSyncLogsManager";
00125   }
00126 
00127 }

Generated on Mon Dec 4 11:03:27 2006 for OpenMobileIS by  doxygen 1.5.1-p1