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   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       // process incomming data
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   /* (non-Javadoc)
00126    * @see org.openmobileis.server.synchro.safeserver.services.DefaultSafeServerService#getServiceName()
00127    */
00128   public String getTargetName() {
00129     return "FWKSyncLogsManager";
00130   }
00131 
00132 }

Generated on Mon Jan 11 21:19:15 2010 for OpenMobileIS by  doxygen 1.5.4