00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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.ServiceManager;
00043 import org.openmobileis.services.common.ServletTools;
00044 import org.openmobileis.services.navigation.NavigationBarService;
00045 import org.openmobileis.synchro.client.SynchroDescriptor;
00046 import org.openmobileis.synchro.client.SynchroManager;
00047 import org.openmobileis.synchro.journal.JournalManager;
00048 import org.openmobileis.synchro.openmsp.client.OpenMSPSynchroManager;
00049 import org.openmobileis.synchro.openmsp.client.conduit.HttpOpenMSPSynchroConduit;
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;}
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
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
00140
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
00151 if ((login == null) || (pwd == null)) {
00152
00153 return null;
00154 }
00155 if (!synchroPending) {
00156 synchronized(this) {synchroPending = true;}
00157
00158 SynchroThread sync = new SynchroThread(login, pwd);
00159 Thread thread = new Thread(sync);
00160 Plateform plateform = ApplicationContextManager.getManager().getApplicationContext().getPlateform();
00161
00162 if ((plateform.getOS().equals(Plateform.POCKETPCOS)) && (PropertiesManager.getManager().getProperty("org.openmobileis.embedded.execprog.browser")!=null)) {
00163 thread.start();
00164 return this.showWaitMessage(res, templateData);
00165 } else {
00166 thread.run();
00167 return this.showResultMessage(req, res, templateData);
00168 }
00169 } else {
00170 return this.showWaitMessage(res, templateData);
00171 }
00172
00173 }
00174
00175 public String showWaitMessage(HttpServletResponse res, TemplateModelRoot templateData) {
00176 IntlResourceManager resourceManager = IntlResourceManager.getManager();
00177 String title = resourceManager.getLocalizedProperty("org.openmobileis.synchro.DirectSynchroService.SyncStarted");
00178 if (title == null) title = "Synchronizationn started.";
00179 String message = resourceManager.getLocalizedProperty("org.openmobileis.synchro.DirectSynchroService.Wait");
00180 if (message == null) message = "Please wait until the end.";
00181 ServletTools.sendConfirmationPage(title, message, "/index", res);
00182 return null;
00183 }
00184
00185 public String showResultMessage(HttpServletRequest req, HttpServletResponse res, TemplateModelRoot templateData) throws ServletException, IOException {
00186 int status = OpenMSPSynchroManager.getManager().getGlobalSynchroStatut();
00187 String syncMessage = JournalManager.getManager().getErrorMessageForSynchroStatus(status);
00188 templateData.put("syncstatus", new SimpleScalar(syncMessage));
00189 SynchroJournalDisplayService journalservice = new SynchroJournalDisplayService();
00190 journalservice.runTemplate(req, res, templateData);
00191 return DirectSynchroService.templatePath;
00192 }
00193
00194
00195 public String getNavigationBarLabel(HttpServletRequest req) {
00196 return "Synchronisation";
00197 }
00198
00199 public boolean displayFormExitMessage() {
00200 return false;
00201 }
00202
00203 public boolean displayRecursive() {
00204 return false;
00205 }
00206
00207 public String getServiceUri() {
00208 return ServiceManager.getManager().getServiceBaseURI()+"/synchro/direct";
00209 }
00210
00211 }