ModuleJarSynchroTarget.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 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.BufferedInputStream;
00028 import java.io.File;
00029 import java.io.FileInputStream;
00030 import java.io.FileNotFoundException;
00031 import java.io.IOException;
00032 import java.io.InputStreamReader;
00033 import java.io.Reader;
00034 
00035 import org.openmobileis.common.context.SessionContext;
00036 import org.openmobileis.common.context.SessionContextManager;
00037 import org.openmobileis.common.util.PropertiesManager;
00038 import org.openmobileis.module.core.ProfileModule;
00039 import org.openmobileis.module.core.XmlModuleParser;
00040 import org.openmobileis.synchro.openmsp.OpenMSPException;
00041 import org.openmobileis.synchro.openmsp.protocol.Command;
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.Result;
00046 import org.openmobileis.synchro.openmsp.protocol.Status;
00047 import org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener;
00048 import org.openmobileis.synchro.openmsp.server.util.MemoryFile;
00049 import org.openmobileis.synchro.security.auth.Credential;
00050 import org.xmlpull.v1.XmlPullParserException;
00051 
00059 public final class ModuleJarSynchroTarget extends DefaultOpenMSPSynchroTargetListener {
00060   
00061   protected ProfileModule listenerModule;
00062   protected File xmlFile;
00063 
00067   public ModuleJarSynchroTarget(File xmlFile) throws FileNotFoundException, XmlPullParserException, IOException {
00068     super();
00069     this.xmlFile = xmlFile;
00070     Reader reader = new InputStreamReader(new FileInputStream(xmlFile));
00071     try {
00072       XmlModuleParser parser = new XmlModuleParser(reader);
00073       parser.parse();
00074       listenerModule = parser.getModuletoload();
00075    } finally   {
00076       reader.close();
00077     }
00078     //validate parsing
00079  }
00080   
00081   public ProfileModule getProfilModule() {
00082     return listenerModule;
00083   }
00084   
00085   /* (non-Javadoc)
00086    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#getTargetName()
00087    */
00088   public String getTargetName() {
00089     return listenerModule.getName();
00090   }
00091   
00092   public String getVersion()    {
00093     return listenerModule.getVersion();
00094   }
00095 
00096   /* (non-Javadoc)
00097    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#processMapCommand(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00098    */
00099   protected ContainerMessage processMapCommand(Credential cred, ContainerMessage mapContainer) {
00100     return null;
00101   }
00102 
00103   /* (non-Javadoc)
00104    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#notifySyncAction(long, org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.Command)
00105    */
00106   protected void notifySyncAction(long sessionId, Credential cred, Command syncCommand) throws OpenMSPException {
00107     SessionContext context = SessionContextManager.getManager().getSessionContext();
00108     long ns = syncCommand.getSourceSessionID();
00109     context.setAttribute("jarfilens", new Long(ns)); //jarfilemodiftime
00110   }
00111 
00112   /* (non-Javadoc)
00113    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#processResultCommand(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.Result)
00114    */
00115   protected void processResultCommand(Credential cred, Result resultCommande) throws OpenMSPException {
00116   }
00117 
00118   /* (non-Javadoc)
00119    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#processStatusCommand(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.Status)
00120    */
00121   protected void processStatusCommand(Credential cred, Status statusCommande) throws OpenMSPException {
00122   }
00123 
00124   /* (non-Javadoc)
00125    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#processAddCommand(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00126    */
00127   protected Status processAddCommand(Credential cred, ContainerMessage addContainer) throws OpenMSPException {
00128     return null;
00129   }
00130 
00131   /* (non-Javadoc)
00132    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#processDeleteCommand(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00133    */
00134   protected Status processDeleteCommand(Credential cred, ContainerMessage deleteContainer) throws OpenMSPException {
00135     return null;
00136   }
00137 
00138   /* (non-Javadoc)
00139    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#processReplaceCommand(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00140    */
00141   protected Status processReplaceCommand(Credential cred, ContainerMessage replaceContainer) throws OpenMSPException {
00142       return null;
00143   }
00144 
00145   /* (non-Javadoc)
00146    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#beginProcessOpenMSpCommand(org.openmobileis.synchro.security.auth.Credential)
00147    */
00148   protected void beginProcessOpenMSpCommand(Credential cred) throws OpenMSPException {
00149   }
00150 
00151   /* (non-Javadoc)
00152    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#endProcessOpenMSpCommand()
00153    */
00154   protected ContainerMessage[] endProcessOpenMSpCommand() throws OpenMSPException {
00155     return null;
00156   }
00157   
00158   private File getModuleTerminalJarFile()   {
00159    StringBuffer modrep = new StringBuffer(PropertiesManager.getManager().getProperty("org.openmobileis.bundle.module.repositorypath"));
00160    modrep.append('/');
00161    modrep.append(this.listenerModule.getTerminaljar());
00162    return new File(modrep.toString());
00163   }
00164 
00165   /* (non-Javadoc)
00166    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#processGetCommand(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00167    */
00168   protected ContainerMessage[] processGetCommand(Credential cred, ContainerMessage getContainer) throws OpenMSPException {
00169     SessionContext context = SessionContextManager.getManager().getSessionContext();
00170     long synchroFileModifTS =  ((Long)context.getAttribute("jarfilens")).longValue();
00171     long newSessionId = synchroFileModifTS;
00172     
00173     //construct return command
00174     Element getCommand = getContainer.getElement();
00175     Result resultCommand = new Result(getCommand.getCmdId(), this.getTargetName(), this.getTargetName());
00176     ContainerMessage resultContainer = new ContainerMessage(resultCommand);
00177     try {
00178     
00179       //validate module file modification date.
00180       File terminaljarfile = this.getModuleTerminalJarFile();
00181       long filens = terminaljarfile.lastModified();
00182       if (xmlFile.lastModified() > filens) filens = xmlFile.lastModified();
00183       if (synchroFileModifTS < filens)    { //manage file modification.
00184         newSessionId = filens;
00185         this.addFileToSynchro(new MemoryFile("/filedir/"+terminaljarfile.getName(), new BufferedInputStream(new FileInputStream(terminaljarfile))));
00186         DataItem newItem = new DataItem(Element.ITEM, "jarfile", terminaljarfile.getName(), null, null);
00187         resultContainer.add(newItem);
00188         this.addFileToSynchro(new MemoryFile("/filedir/"+xmlFile.getName(), new BufferedInputStream(new FileInputStream(xmlFile))));
00189         newItem = new DataItem(Element.ITEM, "xmlfile", xmlFile.getName(), null, null);
00190         resultContainer.add(newItem);
00191       } else  {
00192         DataItem newItem = new DataItem(Element.ITEM, "nofile", "nofile", null, null);
00193         resultContainer.add(newItem);
00194       }
00195       resultCommand.setMetaInformation(Long.toString(newSessionId));
00196     } catch (Exception ex)  {
00197       Status status = new Status(getCommand.getCmdId(), Status.STATUS_FAILED);
00198       status.setCmdRef(getCommand.getCmdId());
00199       ContainerMessage statusContainer = new ContainerMessage(status);
00200       ContainerMessage[] ret = new ContainerMessage[1];
00201       ret[0] = statusContainer;
00202       return ret;
00203     }
00204     ContainerMessage[] ret = new ContainerMessage[1];
00205     ret[0] = resultContainer;
00206     return ret;
00207   }
00208 
00209 }

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