FullAppUpdateSynchroTarget.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.bundle.update.server;
00026 
00027 import java.io.File;
00028 import java.io.FileNotFoundException;
00029 import java.io.IOException;
00030 import java.util.Iterator;
00031 import java.util.jar.Attributes;
00032 import java.util.jar.JarFile;
00033 
00034 import org.openmobileis.bundle.update.terminal.ApplicationUpdateJarUtil;
00035 import org.openmobileis.common.util.log.LogManager;
00036 import org.openmobileis.synchro.openmsp.OpenMSPException;
00037 import org.openmobileis.synchro.openmsp.protocol.Command;
00038 import org.openmobileis.synchro.openmsp.protocol.ContainerMessage;
00039 import org.openmobileis.synchro.openmsp.protocol.DataItem;
00040 import org.openmobileis.synchro.openmsp.protocol.Element;
00041 import org.openmobileis.synchro.openmsp.protocol.Result;
00042 import org.openmobileis.synchro.openmsp.protocol.Status;
00043 import org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener;
00044 import org.openmobileis.synchro.openmsp.server.util.MemoryFile;
00045 import org.openmobileis.synchro.security.auth.Credential;
00046 import org.xmlpull.v1.XmlPullParserException;
00047 
00055 public final class FullAppUpdateSynchroTarget extends DefaultOpenMSPSynchroTargetListener {
00056   
00057   protected File currentjarFile;
00058   //protected File xmlFile;
00059   protected byte[] terminaljar;
00060   
00061   public FullAppUpdateSynchroTarget(File jarfile) throws FileNotFoundException, XmlPullParserException, IOException {
00062         this.currentjarFile = jarfile;
00063   }
00064   
00065   /* (non-Javadoc)
00066    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#getTargetName()
00067    */
00068   public String getTargetName() {
00069     return ApplicationUpdateJarUtil.FULL_APP_UPDATE_NAME;
00070   }
00071 
00072   /* (non-Javadoc)
00073    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#processMapCommand(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00074    */
00075   protected ContainerMessage processMapCommand(Credential cred, ContainerMessage mapContainer) {
00076     return null;
00077   }
00078 
00079   /* (non-Javadoc)
00080    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#notifySyncAction(long, org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.Command)
00081    */
00082   protected void notifySyncAction(long sessionId, Credential cred, Command syncCommand) throws OpenMSPException {
00083   }
00084 
00085   /* (non-Javadoc)
00086    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#processResultCommand(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.Result)
00087    */
00088   protected void processResultCommand(Credential cred, Result resultCommande) throws OpenMSPException {
00089   }
00090 
00091   /* (non-Javadoc)
00092    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#processStatusCommand(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.Status)
00093    */
00094   protected void processStatusCommand(Credential cred, Status statusCommande) throws OpenMSPException {
00095   }
00096 
00097   /* (non-Javadoc)
00098    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#processAddCommand(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00099    */
00100   protected Status processAddCommand(Credential cred, ContainerMessage addContainer) throws OpenMSPException {
00101     return null;
00102   }
00103 
00104   /* (non-Javadoc)
00105    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#processDeleteCommand(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00106    */
00107   protected Status processDeleteCommand(Credential cred, ContainerMessage deleteContainer) throws OpenMSPException {
00108     return null;
00109   }
00110 
00111   /* (non-Javadoc)
00112    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#processReplaceCommand(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00113    */
00114   protected Status processReplaceCommand(Credential cred, ContainerMessage replaceContainer) throws OpenMSPException {
00115       return null;
00116   }
00117 
00118   /* (non-Javadoc)
00119    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#beginProcessOpenMSpCommand(org.openmobileis.synchro.security.auth.Credential)
00120    */
00121   protected void beginProcessOpenMSpCommand(Credential cred) throws OpenMSPException {
00122   }
00123 
00124   /* (non-Javadoc)
00125    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#endProcessOpenMSpCommand()
00126    */
00127   protected ContainerMessage[] endProcessOpenMSpCommand() throws OpenMSPException {
00128     return null;
00129   }
00130 
00131   /* (non-Javadoc)
00132    * @see org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener#processGetCommand(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00133    */
00134   protected ContainerMessage[] processGetCommand(Credential cred, ContainerMessage getContainer) throws OpenMSPException {
00135      //construct return command
00136         Command getCommand = (Command)getContainer.getElement();
00137     DataItem item = (DataItem) getContainer.nextMessage().getElement();
00138     try {   
00139             String pdaversion = item.getData();
00140             if (pdaversion == null) pdaversion="";
00141             String serverversion = this.readJarVersion(this.currentjarFile);
00142             Result resultCommand = new Result(getCommand.getCmdId(), this.getTargetName(), this.getTargetName());
00143             ContainerMessage resultContainer = new ContainerMessage(resultCommand);
00144             LogManager.traceDebug(0, "FullAppUpdateSynchroTarget processGetCommand pdaversion :"+pdaversion);
00145             LogManager.traceDebug(0, "FullAppUpdateSynchroTarget processGetCommand serverversion :"+serverversion);
00146       if (!pdaversion.equals(serverversion))    { //manage file modification.
00147             LogManager.traceDebug(0, "FullAppUpdateSynchroTarget processGetCommand UPDATE PDA");
00148         MemoryFile mf = new MemoryFile(this.currentjarFile);
00149         mf.setFileName(ApplicationUpdateJarUtil.APPLICATION_JAR_FILENAME);
00150         this.addFileToSynchro(mf);
00151         DataItem newItem = new DataItem(Element.ITEM, "jarfile", ApplicationUpdateJarUtil.APPLICATION_JAR_FILENAME, null, null);
00152         resultContainer.add(newItem);
00153       } else  {
00154         DataItem newItem = new DataItem(Element.ITEM, "nofile", "nofile", null, null);
00155         resultContainer.add(newItem);
00156       }
00157       ContainerMessage[] ret = new ContainerMessage[1];
00158       ret[0] = resultContainer;
00159       return ret;
00160    } catch (Exception ex)  {
00161         LogManager.traceError(0, ex);
00162       Status status = new Status(getCommand.getCmdId(), Status.STATUS_FAILED);
00163       status.setCmdRef(getCommand.getCmdId());
00164       ContainerMessage statusContainer = new ContainerMessage(status);
00165       ContainerMessage[] ret = new ContainerMessage[1];
00166       ret[0] = statusContainer;
00167       return ret;
00168     }
00169   }
00170   
00171   private String readJarVersion(File file)      throws IOException{
00172         String version = "";
00173                 if (file.exists()) {
00174                         JarFile jarFile = new JarFile(file);
00175                         try {
00176                                 if (jarFile.getManifest() != null) {
00177                                         Iterator iter = jarFile.getManifest().getMainAttributes().keySet().iterator();
00178                                         while (iter.hasNext()) {
00179                                                 Attributes.Name attribute = (Attributes.Name) iter.next();
00180                                                 if (attribute.equals(ApplicationUpdateJarUtil.VERSIONNAME)) {
00181                                                         version = jarFile.getManifest().getMainAttributes().getValue(attribute);
00182                                                 }
00183                                         }
00184                                 }
00185                         } finally {
00186                                 if (jarFile != null) {
00187                                         try {
00188                                                 jarFile.close();
00189                                         } catch (IOException ioe) {
00190                                         }
00191                                 }
00192                         }
00193                 }
00194                 return version;
00195   }
00196 
00197 }

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