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

Generated on Mon Jan 14 17:29:48 2008 for OpenMobileIS by  doxygen 1.5.4