ModuleJarSynchroTarget.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2008 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
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  */
00025 package org.openmobileis.module.server;
00026 
00027 import java.io.File;
00028 import java.io.FileNotFoundException;
00029 import java.io.IOException;
00030 
00031 import org.openmobileis.common.util.log.LogManager;
00032 import org.openmobileis.module.core.ProfileModule;
00033 import org.openmobileis.synchro.openmsp.OpenMSPException;
00034 import org.openmobileis.synchro.openmsp.protocol.Command;
00035 import org.openmobileis.synchro.openmsp.protocol.ContainerMessage;
00036 import org.openmobileis.synchro.openmsp.protocol.DataItem;
00037 import org.openmobileis.synchro.openmsp.protocol.Element;
00038 import org.openmobileis.synchro.openmsp.protocol.Result;
00039 import org.openmobileis.synchro.openmsp.protocol.Status;
00040 import org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener;
00041 import org.openmobileis.synchro.openmsp.server.util.MemoryFile;
00042 import org.openmobileis.synchro.security.auth.Credential;
00043 import org.xmlpull.v1.XmlPullParserException;
00044 
00052 public final class ModuleJarSynchroTarget extends DefaultOpenMSPSynchroTargetListener {
00053   
00054   protected ProfileModule listenerModule;
00055   //protected File xmlFile;
00056   protected byte[] terminaljar;
00057   protected long moduleJarLastModified;
00058   
00059   public ModuleJarSynchroTarget(ProfileModule module) throws FileNotFoundException, XmlPullParserException, IOException {
00060         this.listenerModule = module;
00061   }
00062   
00063   public ProfileModule getProfilModule() {
00064     return listenerModule;
00065   }
00066   
00067   public void setTerminalJar(byte[] jar, long lastModified)     {
00068         this.terminaljar = jar;
00069         this.moduleJarLastModified = lastModified;
00070   }
00071   
00072   /* (non-Javadoc)
00073    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#getTargetName()
00074    */
00075   public String getTargetName() {
00076     return listenerModule.getName();
00077   }
00078   
00079   public String getVersion()    {
00080     return listenerModule.getVersion();
00081   }
00082 
00083   /* (non-Javadoc)
00084    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#processMapCommand(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00085    */
00086   protected ContainerMessage processMapCommand(Credential cred, ContainerMessage mapContainer) {
00087     return null;
00088   }
00089 
00090   /* (non-Javadoc)
00091    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#notifySyncAction(long, org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.Command)
00092    */
00093   protected void notifySyncAction(long sessionId, Credential cred, Command syncCommand) throws OpenMSPException {
00094  /*   SessionContext context = SessionContextManager.getManager().getSessionContext();
00095     long ns = syncCommand.getSourceSessionID();
00096     context.setAttribute("jarfilens", new Long(ns)); //jarfilemodiftime
00097     */
00098   }
00099 
00100   /* (non-Javadoc)
00101    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#processResultCommand(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.Result)
00102    */
00103   protected void processResultCommand(Credential cred, Result resultCommande) throws OpenMSPException {
00104   }
00105 
00106   /* (non-Javadoc)
00107    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#processStatusCommand(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.Status)
00108    */
00109   protected void processStatusCommand(Credential cred, Status statusCommande) throws OpenMSPException {
00110   }
00111 
00112   /* (non-Javadoc)
00113    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#processAddCommand(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00114    */
00115   protected Status processAddCommand(Credential cred, ContainerMessage addContainer) throws OpenMSPException {
00116     return null;
00117   }
00118 
00119   /* (non-Javadoc)
00120    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#processDeleteCommand(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00121    */
00122   protected Status processDeleteCommand(Credential cred, ContainerMessage deleteContainer) throws OpenMSPException {
00123     return null;
00124   }
00125 
00126   /* (non-Javadoc)
00127    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#processReplaceCommand(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00128    */
00129   protected Status processReplaceCommand(Credential cred, ContainerMessage replaceContainer) throws OpenMSPException {
00130       return null;
00131   }
00132 
00133   /* (non-Javadoc)
00134    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#beginProcessOpenMSpCommand(org.openmobileis.synchro.security.auth.Credential)
00135    */
00136   protected void beginProcessOpenMSpCommand(Credential cred) throws OpenMSPException {
00137   }
00138 
00139   /* (non-Javadoc)
00140    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#endProcessOpenMSpCommand()
00141    */
00142   protected ContainerMessage[] endProcessOpenMSpCommand() throws OpenMSPException {
00143     return null;
00144   }
00145 
00146   /* (non-Javadoc)
00147    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#processGetCommand(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00148    */
00149   protected ContainerMessage[] processGetCommand(Credential cred, ContainerMessage getContainer) throws OpenMSPException {
00150      //construct return command
00151         Command getCommand = (Command)getContainer.getElement();
00152     long synchroFileModifTS =  getCommand.getSourceSessionID();
00153     long newSessionId = synchroFileModifTS;
00154     Result resultCommand = new Result(getCommand.getCmdId(), this.getTargetName(), this.getTargetName());
00155     ContainerMessage resultContainer = new ContainerMessage(resultCommand);
00156     try {   
00157       if (synchroFileModifTS < this.moduleJarLastModified)    { //manage file modification.
00158         newSessionId = this.moduleJarLastModified;
00159         this.addFileToSynchro(new MemoryFile("/filedir/"+listenerModule.getName()+".jar", this.terminaljar));
00160         DataItem newItem = new DataItem(Element.ITEM, "jarfile", listenerModule.getName()+".jar", null, null);
00161         resultContainer.add(newItem);
00162         this.addFileToSynchro(new MemoryFile("/filedir/"+listenerModule.getName()+".xml", listenerModule.toXML().getBytes()));
00163         newItem = new DataItem(Element.ITEM, "xmlfile",listenerModule.getName()+".xml", null, null);
00164         resultContainer.add(newItem);
00165       } else  {
00166         DataItem newItem = new DataItem(Element.ITEM, "nofile", "nofile", null, null);
00167         resultContainer.add(newItem);
00168       }
00169       resultCommand.setMetaInformation(Long.toString(newSessionId));
00170     } catch (Exception ex)  {
00171         LogManager.traceError(0, ex);
00172       Status status = new Status(getCommand.getCmdId(), Status.STATUS_FAILED);
00173       status.setCmdRef(getCommand.getCmdId());
00174       ContainerMessage statusContainer = new ContainerMessage(status);
00175       ContainerMessage[] ret = new ContainerMessage[1];
00176       ret[0] = statusContainer;
00177       return ret;
00178     }
00179     ContainerMessage[] ret = new ContainerMessage[1];
00180     ret[0] = resultContainer;
00181     return ret;
00182   }
00183 
00184 }

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