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.embedded.util.SystemAPI;
00040 import org.openmobileis.services.TemplateService;
00041 import org.openmobileis.services.common.ServiceManager;
00042 import org.openmobileis.services.common.ServletTools;
00043 import org.openmobileis.services.navigation.NavigationBarService;
00044 import org.openmobileis.services.security.SessionLoginManager;
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.openmsp.protocol.Status;
00051 import org.openmobileis.synchro.security.auth.Credential;
00052
00053 import freemarker.template.SimpleScalar;
00054 import freemarker.template.TemplateModelRoot;
00055
00063 public final class DirectSynchroService extends TemplateService implements NavigationBarService{
00064 private static final String templatePath="openmischannel"+java.io.File.separator+"synchroresult.htm";
00065 private boolean synchroPending = false;
00066 private SynchroDescriptor descriptor;
00067
00068 class SynchroThread implements Runnable {
00069 String login, password;
00070 SynchroThread(String login, String password) {
00071 this.login = login;
00072 this.password = password;
00073 }
00074
00075 public void run() {
00076 try {
00077 this.doSynchro(login, password);
00078 if (OpenMSPSynchroManager.getManager().getGlobalSynchroStatut() == Status.STATUS_OK) {
00079 SessionLoginManager.getManager().changeSessionPass(password);
00080 }
00081 } catch (Throwable e) {
00082 LogManager.traceError(0, e);
00083 } finally {
00084 synchronized(this) {synchroPending = false;}
00085 Plateform plateform = ApplicationContextManager.getManager().getApplicationContext().getPlateform();
00086 if (plateform.getOS().equals(Plateform.POCKETPCOS)) {
00087 if (PropertiesManager.getManager().getProperty("org.openmobileis.embedded.execprog.browser")!=null) {
00088 String indexurl = PropertiesManager.getManager().getProperty("org.openmobileis.embedded.execprog.browser.args");
00089 if(indexurl != null) {
00090 int index = indexurl.indexOf("//");
00091 String crackurl = indexurl.substring(index+2, indexurl.length());
00092 int index2 = crackurl.indexOf('/');
00093 if (index2 == -1) {
00094 index2 =crackurl.length()-1;
00095 }
00096 indexurl = indexurl.substring(0, index+2+index2);
00097 } else {
00098 indexurl = "http://127.0.0.1:9090";
00099 }
00100
00101 SystemAPI.execProgram(PropertiesManager.getManager().getProperty("org.openmobileis.embedded.execprog.browser"), indexurl+"/services/synchro/direct/"+Long.toString(System.currentTimeMillis())+"?showres=true");
00102 }
00103 }
00104 }
00105
00106 }
00107
00108 private void doSynchro(String login, String password) throws SynchroException {
00109 Credential cred = new Credential(login, password);
00110 SynchroManager.getManager().doSynchro(cred, descriptor);
00111 }
00112 }
00116 public DirectSynchroService() {
00117 super();
00118 try {
00119 String synchroInstallPath = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.direct.installpath");
00120 if (synchroInstallPath == null) {
00121 String propFilePath = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.properties.file.path");
00122 PropertiesManager.getManager().addPropertiesFileFromFilePath(propFilePath);
00123 synchroInstallPath = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.direct.installpath");
00124 }
00125
00126 descriptor = new SynchroDescriptor();
00127 descriptor.addProperty("OpenMSPsynchrotype", "DR");
00128 String useragent = OpenMSPSynchroManager.getManager().getSynchroUserAgent(descriptor);
00129 descriptor.setSynchroConduit(new HttpOpenMSPSynchroConduit(synchroInstallPath ,useragent));
00130
00131 String group = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.direct.group");
00132 descriptor.setSynchroGroup(group);
00133 String url = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.direct.targeturl");
00134 descriptor.setServerURL(url);
00135 } catch (Throwable ex) {
00136 LogManager.traceError(0, "Error during init of DirectSynchroService. No direct synchro available");
00137 LogManager.traceError(0, ex);
00138 }
00139
00140 }
00141
00142
00143
00144
00145 public String runTemplate(HttpServletRequest req, HttpServletResponse res, TemplateModelRoot templateData) throws ServletException, IOException {
00146 String login = req.getParameter("login");
00147 String pwd = req.getParameter("pwd");
00148 String showres = req.getParameter("showres");
00149 if (showres != null) {
00150 LogManager.traceDebug(0, "DirectSyncService showres.");
00151 return this.showResultMessage(req, res, templateData);
00152 }
00153
00154 if ((login == null) || (pwd == null)) {
00155
00156 return null;
00157 }
00158 if (!synchroPending) {
00159 synchronized(this) {synchroPending = true;}
00160
00161 SynchroThread sync = new SynchroThread(login, pwd);
00162 Thread thread = new Thread(sync);
00163 Plateform plateform = ApplicationContextManager.getManager().getApplicationContext().getPlateform();
00164 LogManager.traceInfo(0, "");
00165
00166 if ((plateform.getOS().equals(Plateform.POCKETPCOS)) && (PropertiesManager.getManager().getProperty("org.openmobileis.embedded.execprog.browser")!=null)) {
00167 thread.start();
00168 return this.showWaitMessage(res, templateData);
00169 } else {
00170 thread.run();
00171 return this.showResultMessage(req, res, templateData);
00172 }
00173 } else {
00174 return this.showWaitMessage(res, templateData);
00175 }
00176
00177 }
00178
00179 public String showWaitMessage(HttpServletResponse res, TemplateModelRoot templateData) {
00180 IntlResourceManager resourceManager = IntlResourceManager.getManager();
00181 String title = resourceManager.getLocalizedProperty("org.openmobileis.synchro.DirectSynchroService.SyncStarted");
00182 if (title == null) title = "Synchronizationn started.";
00183 String message = resourceManager.getLocalizedProperty("org.openmobileis.synchro.DirectSynchroService.Wait");
00184 if (message == null) message = "Please wait until the end.";
00185 ServletTools.sendConfirmationPage(title, message, "/index", res);
00186 return null;
00187 }
00188
00189 public String showResultMessage(HttpServletRequest req, HttpServletResponse res, TemplateModelRoot templateData) throws ServletException, IOException {
00190 int status = OpenMSPSynchroManager.getManager().getGlobalSynchroStatut();
00191 String syncMessage = JournalManager.getManager().getErrorMessageForSynchroStatus(status);
00192 templateData.put("syncstatus", new SimpleScalar(syncMessage));
00193 SynchroJournalDisplayService journalservice = new SynchroJournalDisplayService();
00194 journalservice.runTemplate(req, res, templateData);
00195 return DirectSynchroService.templatePath;
00196 }
00197
00198
00199 public String getNavigationBarLabel(HttpServletRequest req) {
00200 return "Synchronisation";
00201 }
00202
00203 public boolean displayFormExitMessage() {
00204 return false;
00205 }
00206
00207 public boolean displayRecursive() {
00208 return false;
00209 }
00210
00211 public String getServiceUri() {
00212 return ServiceManager.getManager().getServiceBaseURI()+"/synchro/direct";
00213 }
00214
00215 }