DirectSynchroAndWaitService.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  */
00025 package org.openmobileis.synchro.openmsp.client.services;
00026 
00027 import java.io.IOException;
00028 
00029 import javax.servlet.ServletException;
00030 import javax.servlet.http.HttpServletRequest;
00031 import javax.servlet.http.HttpServletResponse;
00032 
00033 import org.openmobileis.common.util.PropertiesManager;
00034 import org.openmobileis.common.util.exception.SynchroException;
00035 import org.openmobileis.common.util.log.LogManager;
00036 import org.openmobileis.services.TemplateService;
00037 import org.openmobileis.services.common.ServiceManager;
00038 import org.openmobileis.services.navigation.NavigationBarService;
00039 import org.openmobileis.services.security.SessionLoginManager;
00040 import org.openmobileis.synchro.client.SynchroDescriptor;
00041 import org.openmobileis.synchro.client.SynchroManager;
00042 import org.openmobileis.synchro.openmsp.client.OpenMSPSynchroManager;
00043 import org.openmobileis.synchro.openmsp.client.conduit.ApacheHTTPClientSynchroConduit;
00044 import org.openmobileis.synchro.openmsp.client.conduit.SynchroConduit;
00045 import org.openmobileis.synchro.openmsp.protocol.Status;
00046 import org.openmobileis.synchro.security.auth.Credential;
00047 
00048 import freemarker.template.TemplateModelRoot;
00049 
00060 public final class DirectSynchroAndWaitService extends TemplateService implements NavigationBarService{
00061   private static final String resulttemplatePath="openmischannel"+java.io.File.separator+"synchroresult.htm";
00062   private static final String waittemplatePath="openmischannel"+java.io.File.separator+"synchrowait.htm";
00063   private boolean synchroPending = false;
00064   private boolean synchroStarted = false;
00065   private SynchroDescriptor descriptor;
00066 
00067   class SynchroThread implements Runnable {
00068     String login, password;
00069     SynchroThread(String login, String password) {
00070       this.login = login;
00071       this.password = password;
00072     }
00073 
00074     public void run() {
00075       try {
00076         this.doSynchro(login, password);
00077         if (OpenMSPSynchroManager.getManager().getGlobalSynchroStatut() == Status.STATUS_OK)    {
00078                 SessionLoginManager.getManager().changeSessionPass(password);
00079         }
00080       } catch (Throwable e) {
00081         LogManager.traceError(0, e);
00082       } finally {
00083         synchronized(this) {synchroPending = false;} //end before start IE
00084       }
00085 
00086     }
00087 
00088     private void doSynchro(String login, String password) throws SynchroException {
00089       Credential cred = new Credential(login, password);
00090       SynchroManager.getManager().doSynchro(cred, descriptor);
00091     }
00092   }
00096   public DirectSynchroAndWaitService() {
00097     super();
00098     try {
00099       String synchroInstallPath = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.direct.installpath");
00100       if (synchroInstallPath == null)   {
00101               String propFilePath = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.properties.file.path");
00102               PropertiesManager.getManager().addPropertiesFileFromFilePath(propFilePath);
00103               synchroInstallPath = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.direct.installpath");
00104       }
00105 
00106       descriptor = new SynchroDescriptor();
00107       descriptor.addProperty("OpenMSPsynchrotype", "DR"); // to say direct synchro not mandatory. Can be use if you have different type of synchronisation. To identify them on the server side.
00108       String useragent = OpenMSPSynchroManager.getManager().getSynchroUserAgent(descriptor);
00109       String conduitClass = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.direct.conduit.class");
00110       if (conduitClass != null) {
00111         SynchroConduit conduit = (SynchroConduit)Class.forName( conduitClass ).newInstance();;
00112         conduit.setSynchroPath(synchroInstallPath);  //path where synchro file are stored
00113         conduit.setSynchroUserAgent(useragent);
00114         descriptor.setSynchroConduit(conduit);
00115       }         else    {
00116         ApacheHTTPClientSynchroConduit conduit = new ApacheHTTPClientSynchroConduit();
00117         conduit.setSynchroPath(synchroInstallPath);  //path where synchro file are stored
00118         conduit.setSynchroUserAgent(useragent); // synchro client user agent. Use to identify the terminal type on the server side
00119         descriptor.setSynchroConduit(conduit);
00120       }
00121 
00122       String group = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.direct.group");
00123       descriptor.setSynchroGroup(group); // group of the user. Use by the server side profil manager to identify the profile to use.
00124       String url = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.direct.targeturl");
00125       descriptor.setServerURL(url);//url of the server synchro service ex:/syncro/openmspservice
00126     } catch (Throwable ex)  {
00127       LogManager.traceError(0, "Error during init of DirectSynchroService. No direct synchro available");
00128       LogManager.traceError(0, ex);
00129     }
00130 
00131   }
00132 
00133   /* (non-Javadoc)
00134    * @see org.openmobileis.services.common.TemplateService#runTemplate(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, freemarker.template.TemplateModelRoot)
00135    */
00136   public String runTemplate(HttpServletRequest req, HttpServletResponse res, TemplateModelRoot templateData) throws ServletException, IOException {
00137     String login = req.getParameter("login");
00138     String pwd = req.getParameter("pwd");
00139     String showres = req.getParameter("showres");
00140     if (showres != null)  {
00141       return this.showResultMessage(req, res, templateData);
00142     }
00143    // manage auhentication always ask user password
00144     if ((login == null) || (pwd == null)) {
00145         if (synchroPending)     {
00146         return this.showWaitMessage(res, templateData);                 
00147         }
00148       synchronized(this) {synchroStarted=false;}
00149       return this.showResultMessage(req, res, templateData);
00150     }
00151     if (!synchroPending && !synchroStarted) {// should be synchronized but to synchro at the same time never arrive
00152       synchronized(this) {synchroPending = true;synchroStarted=true;}
00153               // start synchro thread
00154               SynchroThread sync = new SynchroThread(login, pwd);
00155               Thread thread = new Thread(sync);
00156               thread.start();
00157               return this.showWaitMessage(res, templateData);
00158     } else  {
00159         if (synchroPending)     {
00160         return this.showWaitMessage(res, templateData);                 
00161         }
00162       synchronized(this) {synchroStarted=false;}
00163       return this.showResultMessage(req, res, templateData);
00164     }
00165   }
00166 
00167   public String showWaitMessage(HttpServletResponse res, TemplateModelRoot templateData) {
00168     return DirectSynchroAndWaitService.waittemplatePath;
00169   }
00170 
00171   public String showResultMessage(HttpServletRequest req, HttpServletResponse res, TemplateModelRoot templateData)  throws ServletException, IOException {
00172     SynchroJournalDisplayService journalservice = new SynchroJournalDisplayService();
00173     journalservice.runTemplate(req, res, templateData);
00174     return DirectSynchroAndWaitService.resulttemplatePath;
00175   }
00176 
00177 
00178   public String getNavigationBarLabel(HttpServletRequest req) {
00179     return "Synchronisation";
00180   }
00181 
00182   public boolean displayFormExitMessage() {
00183     return false;
00184   }
00185 
00186   public boolean displayRecursive() {
00187     return false;
00188   }
00189 
00190   public String getServiceUri() {
00191     return ServiceManager.getManager().getServiceBaseURI()+"/synchro/direct";
00192   }
00193 
00194 }

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