Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

DirectSynchroService.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.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.context.ApplicationContextManager;
00034 import org.openmobileis.common.context.Plateform;
00035 import org.openmobileis.common.intl.IntlResourceManager;
00036 import org.openmobileis.common.util.PropertiesManager;
00037 import org.openmobileis.common.util.exception.SynchroException;
00038 import org.openmobileis.common.util.log.LogManager;
00039 import org.openmobileis.common.util.log.LogServices;
00040 import org.openmobileis.embedded.util.SystemAPI;
00041 import org.openmobileis.services.TemplateService;
00042 import org.openmobileis.services.common.ServletTools;
00043 import org.openmobileis.services.navigation.NavigationBarService;
00044 import org.openmobileis.synchro.client.SynchroDescriptor;
00045 import org.openmobileis.synchro.client.SynchroManager;
00046 import org.openmobileis.synchro.journal.JournalManager;
00047 import org.openmobileis.synchro.openmsp.client.OpenMSPSynchroManager;
00048 import org.openmobileis.synchro.openmsp.client.conduit.HttpOpenMSPSynchroConduit;
00049 import org.openmobileis.synchro.openmsp.client.conduit.SynchroConduit;
00050 import org.openmobileis.synchro.security.auth.Credential;
00051 
00052 import freemarker.template.SimpleScalar;
00053 import freemarker.template.TemplateModelRoot;
00054 
00062 public final class DirectSynchroService extends TemplateService implements NavigationBarService{
00063   private static final String templatePath="openmischannel"+java.io.File.separator+"synchroresult.htm";
00064   private boolean synchroPending = 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       } catch (Throwable e) {
00078         LogManager.traceError(LogServices.WEBSERVICE, "Error in synchro thread: "+e.getMessage());
00079         LogManager.traceError(0, e);
00080       } finally {
00081         synchronized(this) {synchroPending = false;} //end before start IE
00082         Plateform plateform = ApplicationContextManager.getManager().getApplicationContext().getPlateform();
00083         if (plateform.getOS().equals(Plateform.POCKETPCOS)) {
00084               if (PropertiesManager.getManager().getProperty("org.openmobileis.embedded.execprog.browser")!=null)       {
00085                 String indexurl = PropertiesManager.getManager().getProperty("org.openmobileis.embedded.execprog.browser.args");
00086                 if(indexurl != null)    {
00087                   int index = indexurl.indexOf("//");
00088                   String crackurl = indexurl.substring(index+2, indexurl.length());
00089                   int index2 = crackurl.indexOf('/');
00090                   if (index2 == -1)     {
00091                     index2 =crackurl.length()-1;
00092                   }
00093                   indexurl = indexurl.substring(0, index+2+index2);
00094                 } else  {
00095                   indexurl = "http://127.0.0.1:9090";
00096                 }
00097                 //miss base url not have to crack it.
00098                 SystemAPI.execProgram(PropertiesManager.getManager().getProperty("org.openmobileis.embedded.execprog.browser"), indexurl+"/services/synchro/direct/"+Long.toString(System.currentTimeMillis())+"?showres=true");
00099               }
00100         }
00101       }
00102       
00103     }
00104     
00105     private void doSynchro(String login, String password) throws SynchroException {
00106       Credential cred = new Credential(login, password);   
00107       SynchroManager.getManager().doSynchro(cred, descriptor);
00108     }
00109   }
00113   public DirectSynchroService() {
00114     super();
00115     try {
00116       String synchroInstallPath = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.direct.installpath");
00117       if (synchroInstallPath == null)   {
00118               String propFilePath = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.properties.file.path");
00119               PropertiesManager.getManager().addPropertiesFileFromFilePath(propFilePath);
00120               synchroInstallPath = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.direct.installpath");
00121       }
00122       
00123       descriptor = new SynchroDescriptor();
00124       descriptor.addProperty("OpenMSPsynchrotype", "DR");
00125       String useragent = OpenMSPSynchroManager.getManager().getSynchroUserAgent(descriptor);
00126       descriptor.setSynchroConduit(new HttpOpenMSPSynchroConduit(synchroInstallPath ,useragent));
00127       
00128       String group = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.direct.group");
00129       descriptor.setSynchroGroup(group);
00130       String url = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.direct.targeturl");
00131       descriptor.setServerURL(url);
00132     } catch (Throwable ex)  {
00133       LogManager.traceError(0, "Error during init of DirectSynchroService. No direct synchro available");
00134       LogManager.traceError(0, ex);      
00135     }
00136     
00137   }
00138 
00139   /* (non-Javadoc)
00140    * @see org.openmobileis.services.common.TemplateService#runTemplate(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, freemarker.template.TemplateModelRoot)
00141    */
00142   public String runTemplate(HttpServletRequest req, HttpServletResponse res, TemplateModelRoot templateData) throws ServletException, IOException {
00143     String login = req.getParameter("login");
00144     String pwd = req.getParameter("pwd");
00145     String showres = req.getParameter("showres");
00146     if (showres != null)  {
00147       LogManager.traceDebug(0, "DirectSyncService showres.");
00148       return this.showResultMessage(req, res, templateData);
00149     }
00150    // manage auhentication always ask user password
00151     if ((login == null) || (pwd == null)) {
00152      //return password error authentication required
00153       return null;
00154     }
00155     if (!synchroPending) {// should be synchronized but to synchro at the same time never arrive
00156       synchronized(this) {synchroPending = true;}
00157       // start synchro thread
00158       SynchroThread sync = new SynchroThread(login, pwd);
00159       Thread thread = new Thread(sync);
00160       Plateform plateform = ApplicationContextManager.getManager().getApplicationContext().getPlateform();
00161       if (plateform.getOS().equals(Plateform.POCKETPCOS)) {
00162         thread.start();  
00163         return this.showWaitMessage(res, templateData);
00164      } else  {
00165          thread.run();     
00166         return this.showResultMessage(req, res, templateData);
00167      }                       
00168     } else  {
00169       return this.showWaitMessage(res, templateData);
00170     }
00171     
00172   }
00173   
00174   public String showWaitMessage(HttpServletResponse res, TemplateModelRoot templateData) {
00175     IntlResourceManager resourceManager = IntlResourceManager.getManager();
00176     String title = resourceManager.getLocalizedProperty("org.openmobileis.synchro.DirectSynchroService.SyncStarted");
00177     if (title == null) title = "Synchronizationn started.";
00178     String message = resourceManager.getLocalizedProperty("org.openmobileis.synchro.DirectSynchroService.Wait");
00179     if (message == null) message = "Please wait until the end.";
00180     ServletTools.sendConfirmationPage(title, message, "/index", res);
00181     return null;
00182   }
00183   
00184   public String showResultMessage(HttpServletRequest req, HttpServletResponse res, TemplateModelRoot templateData)  throws ServletException, IOException {
00185     int status = OpenMSPSynchroManager.getManager().getGlobalSynchroStatut();
00186     String syncMessage = JournalManager.getManager().getErrorMessageForSynchroStatus(status);
00187     templateData.put("syncstatus", new SimpleScalar(syncMessage));
00188     SynchroJournalDisplayService journalservice = new SynchroJournalDisplayService();
00189     journalservice.runTemplate(req, res, templateData);
00190     return DirectSynchroService.templatePath;
00191   }
00192 
00193   
00194   public String getNavigationBarLabel(HttpServletRequest req) {
00195     return "Synchronisation";
00196   }
00197   
00198   public boolean displayFormExitMessage() {
00199     return false;
00200   }
00201   
00202   public boolean displayRecursive() {
00203     return false;
00204   }
00205 
00206   /* (non-Javadoc)
00207    * @see org.openmobileis.services.Service#getName()
00208    */
00209   public String getName() {
00210     return "/synchro/direct";
00211   }
00212 
00213 }

Generated on Wed Dec 14 21:05:32 2005 for OpenMobileIS by  doxygen 1.4.4