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