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.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;}
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");
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);
00113 conduit.setSynchroUserAgent(useragent);
00114 descriptor.setSynchroConduit(conduit);
00115 } else {
00116 ApacheHTTPClientSynchroConduit conduit = new ApacheHTTPClientSynchroConduit();
00117 conduit.setSynchroPath(synchroInstallPath);
00118 conduit.setSynchroUserAgent(useragent);
00119 descriptor.setSynchroConduit(conduit);
00120 }
00121
00122 String group = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.direct.group");
00123 descriptor.setSynchroGroup(group);
00124 String url = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.direct.targeturl");
00125 descriptor.setServerURL(url);
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
00134
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
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) {
00152 synchronized(this) {synchroPending = true;synchroStarted=true;}
00153
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 }