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.module.server;
00026
00027 import java.io.BufferedInputStream;
00028 import java.io.File;
00029 import java.io.FileInputStream;
00030 import java.io.FileNotFoundException;
00031 import java.io.IOException;
00032 import java.io.InputStreamReader;
00033 import java.io.Reader;
00034
00035 import org.openmobileis.common.context.SessionContext;
00036 import org.openmobileis.common.context.SessionContextManager;
00037 import org.openmobileis.common.util.PropertiesManager;
00038 import org.openmobileis.module.core.ProfileModule;
00039 import org.openmobileis.module.core.XmlModuleParser;
00040 import org.openmobileis.synchro.openmsp.OpenMSPException;
00041 import org.openmobileis.synchro.openmsp.protocol.Command;
00042 import org.openmobileis.synchro.openmsp.protocol.ContainerMessage;
00043 import org.openmobileis.synchro.openmsp.protocol.DataItem;
00044 import org.openmobileis.synchro.openmsp.protocol.Element;
00045 import org.openmobileis.synchro.openmsp.protocol.Result;
00046 import org.openmobileis.synchro.openmsp.protocol.Status;
00047 import org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener;
00048 import org.openmobileis.synchro.openmsp.server.util.MemoryFile;
00049 import org.openmobileis.synchro.security.auth.Credential;
00050 import org.xmlpull.v1.XmlPullParserException;
00051
00059 public final class ModuleJarSynchroTarget extends DefaultOpenMSPSynchroTargetListener {
00060
00061 protected ProfileModule listenerModule;
00062 protected File xmlFile;
00063
00067 public ModuleJarSynchroTarget(File xmlFile) throws FileNotFoundException, XmlPullParserException, IOException {
00068 super();
00069 this.xmlFile = xmlFile;
00070 Reader reader = new InputStreamReader(new FileInputStream(xmlFile));
00071 try {
00072 XmlModuleParser parser = new XmlModuleParser(reader);
00073 parser.parse();
00074 listenerModule = parser.getModuletoload();
00075 } finally {
00076 reader.close();
00077 }
00078
00079 }
00080
00081 public ProfileModule getProfilModule() {
00082 return listenerModule;
00083 }
00084
00085
00086
00087
00088 public String getTargetName() {
00089 return listenerModule.getName();
00090 }
00091
00092 public String getVersion() {
00093 return listenerModule.getVersion();
00094 }
00095
00096
00097
00098
00099 protected ContainerMessage processMapCommand(Credential cred, ContainerMessage mapContainer) {
00100 return null;
00101 }
00102
00103
00104
00105
00106 protected void notifySyncAction(long sessionId, Credential cred, Command syncCommand) throws OpenMSPException {
00107 SessionContext context = SessionContextManager.getManager().getSessionContext();
00108 long ns = syncCommand.getSourceSessionID();
00109 context.setAttribute("jarfilens", new Long(ns));
00110 }
00111
00112
00113
00114
00115 protected void processResultCommand(Credential cred, Result resultCommande) throws OpenMSPException {
00116 }
00117
00118
00119
00120
00121 protected void processStatusCommand(Credential cred, Status statusCommande) throws OpenMSPException {
00122 }
00123
00124
00125
00126
00127 protected Status processAddCommand(Credential cred, ContainerMessage addContainer) throws OpenMSPException {
00128 return null;
00129 }
00130
00131
00132
00133
00134 protected Status processDeleteCommand(Credential cred, ContainerMessage deleteContainer) throws OpenMSPException {
00135 return null;
00136 }
00137
00138
00139
00140
00141 protected Status processReplaceCommand(Credential cred, ContainerMessage replaceContainer) throws OpenMSPException {
00142 return null;
00143 }
00144
00145
00146
00147
00148 protected void beginProcessOpenMSpCommand(Credential cred) throws OpenMSPException {
00149 }
00150
00151
00152
00153
00154 protected ContainerMessage[] endProcessOpenMSpCommand() throws OpenMSPException {
00155 return null;
00156 }
00157
00158 private File getModuleTerminalJarFile() {
00159 StringBuffer modrep = new StringBuffer(PropertiesManager.getManager().getProperty("org.openmobileis.bundle.module.repositorypath"));
00160 modrep.append('/');
00161 modrep.append(this.listenerModule.getTerminaljar());
00162 return new File(modrep.toString());
00163 }
00164
00165
00166
00167
00168 protected ContainerMessage[] processGetCommand(Credential cred, ContainerMessage getContainer) throws OpenMSPException {
00169 SessionContext context = SessionContextManager.getManager().getSessionContext();
00170 long synchroFileModifTS = ((Long)context.getAttribute("jarfilens")).longValue();
00171 long newSessionId = synchroFileModifTS;
00172
00173
00174 Element getCommand = getContainer.getElement();
00175 Result resultCommand = new Result(getCommand.getCmdId(), this.getTargetName(), this.getTargetName());
00176 ContainerMessage resultContainer = new ContainerMessage(resultCommand);
00177 try {
00178
00179
00180 File terminaljarfile = this.getModuleTerminalJarFile();
00181 long filens = terminaljarfile.lastModified();
00182 if (xmlFile.lastModified() > filens) filens = xmlFile.lastModified();
00183 if (synchroFileModifTS < filens) {
00184 newSessionId = filens;
00185 this.addFileToSynchro(new MemoryFile("/filedir/"+terminaljarfile.getName(), new BufferedInputStream(new FileInputStream(terminaljarfile))));
00186 DataItem newItem = new DataItem(Element.ITEM, "jarfile", terminaljarfile.getName(), null, null);
00187 resultContainer.add(newItem);
00188 this.addFileToSynchro(new MemoryFile("/filedir/"+xmlFile.getName(), new BufferedInputStream(new FileInputStream(xmlFile))));
00189 newItem = new DataItem(Element.ITEM, "xmlfile", xmlFile.getName(), null, null);
00190 resultContainer.add(newItem);
00191 } else {
00192 DataItem newItem = new DataItem(Element.ITEM, "nofile", "nofile", null, null);
00193 resultContainer.add(newItem);
00194 }
00195 resultCommand.setMetaInformation(Long.toString(newSessionId));
00196 } catch (Exception ex) {
00197 Status status = new Status(getCommand.getCmdId(), Status.STATUS_FAILED);
00198 status.setCmdRef(getCommand.getCmdId());
00199 ContainerMessage statusContainer = new ContainerMessage(status);
00200 ContainerMessage[] ret = new ContainerMessage[1];
00201 ret[0] = statusContainer;
00202 return ret;
00203 }
00204 ContainerMessage[] ret = new ContainerMessage[1];
00205 ret[0] = resultContainer;
00206 return ret;
00207 }
00208
00209 }