00001 package org.openmobileis.synchro.openmsp.server.synctarget;
00002
00003 import java.io.ByteArrayInputStream;
00004 import java.io.ByteArrayOutputStream;
00005 import java.io.IOException;
00006 import java.io.ObjectInputStream;
00007 import java.io.ObjectOutputStream;
00008 import java.io.Serializable;
00009
00010 import org.openmobileis.common.util.codec.GeneralCoder;
00011 import org.openmobileis.synchro.openmsp.OpenMSPException;
00012 import org.openmobileis.synchro.openmsp.protocol.Command;
00013 import org.openmobileis.synchro.openmsp.protocol.ContainerMessage;
00014 import org.openmobileis.synchro.openmsp.protocol.DataItem;
00015 import org.openmobileis.synchro.openmsp.protocol.Element;
00016 import org.openmobileis.synchro.openmsp.protocol.Result;
00017 import org.openmobileis.synchro.openmsp.protocol.Status;
00018 import org.openmobileis.synchro.openmsp.server.synctarget.DefaultOpenMSPSynchroTargetListener;
00019 import org.openmobileis.synchro.security.auth.Credential;
00020
00021 public class SynchroMetadataTarget extends DefaultOpenMSPSynchroTargetListener {
00022 private Serializable terminalMetadata;
00023 private Serializable serverMetadata;
00024
00025 public Serializable getTerminalMetadata() {
00026 return terminalMetadata;
00027 }
00028
00029 public void setTerminalMetadata(Serializable terminalMetadata) {
00030 this.terminalMetadata = terminalMetadata;
00031 }
00032
00033 public Serializable getServerMetadata() {
00034 return serverMetadata;
00035 }
00036
00037 public void setServerMetadata(Serializable serverMetadata) {
00038 this.serverMetadata = serverMetadata;
00039 }
00040
00041 protected void beginProcessOpenMSpCommand(Credential cred)
00042 throws OpenMSPException {
00043 }
00044
00045 protected ContainerMessage[] endProcessOpenMSpCommand()
00046 throws OpenMSPException {
00047 return null;
00048 }
00049
00050 public String getTargetName() {
00051 return "FWKsynchrometadata";
00052 }
00053
00054 protected void notifySyncAction(long sessionId, Credential cred,
00055 Command syncCommand) throws OpenMSPException {
00056 }
00057
00058 protected Status processAddCommand(Credential cred,
00059 ContainerMessage addContainer) throws OpenMSPException {
00060 return null;
00061 }
00062
00063 protected Status processDeleteCommand(Credential cred,
00064 ContainerMessage deleteContainer) throws OpenMSPException {
00065 return null;
00066 }
00067
00068 protected ContainerMessage[] processGetCommand(Credential cred,
00069 ContainerMessage cm) throws OpenMSPException {
00070
00071 Command getCommand = (Command) cm.getElement();
00072 Result resultCommand = new Result(getCommand.getCmdId(),
00073 getTargetName(), getTargetName());
00074 ContainerMessage resultContainer = new ContainerMessage(resultCommand);
00075 try {
00076 long synchroSyncNumber = getCommand.getSourceSessionID();
00077 while (cm.hasMoreMessage()) {
00078 DataItem item = (DataItem) cm.nextMessage().getElement();
00079 String data = item.getData();
00080 if (data != null && data.length() > 0) {
00081 this.terminalMetadata = (Serializable) this.unserializeDBObject(data.getBytes());
00082 }
00083 }
00084 DataItem newItem = null;
00085 if (this.serverMetadata == null) {
00086 newItem = new DataItem(Element.ITEM, "noobject", "", null, null);
00087 } else {
00088 newItem = new DataItem(Element.ITEM, "object", new String(this
00089 .serializeDBObject(this.serverMetadata)), null, null);
00090 }
00091 resultContainer.add(newItem);
00092 resultCommand.setMetaInformation(Long.toString(synchroSyncNumber));
00093 } catch (IOException ex) {
00094 throw new OpenMSPException(ex);
00095 } catch (ClassNotFoundException ex) {
00096 throw new OpenMSPException(ex);
00097 }
00098
00099 ContainerMessage[] ret = new ContainerMessage[1];
00100 ret[0] = resultContainer;
00101 return ret;
00102 }
00103
00104 protected ContainerMessage processMapCommand(Credential cred,
00105 ContainerMessage mapContainer) throws OpenMSPException {
00106 return null;
00107 }
00108
00109 protected Status processReplaceCommand(Credential cred,
00110 ContainerMessage replaceContainer) throws OpenMSPException {
00111 return null;
00112 }
00113
00114 protected void processResultCommand(Credential cred, Result resultCommande)
00115 throws OpenMSPException {
00116 }
00117
00118 protected void processStatusCommand(Credential cred, Status statusCommande)
00119 throws OpenMSPException {
00120 }
00121
00122 private byte[] serializeDBObject(Object obj) throws IOException {
00123 ByteArrayOutputStream bstr = new ByteArrayOutputStream();
00124 ObjectOutputStream ostr = new ObjectOutputStream(bstr);
00125 ostr.writeObject(obj);
00126 return GeneralCoder.encodeBase64(bstr.toByteArray());
00127 }
00128
00129 private Object unserializeDBObject(byte[] data) throws IOException,
00130 ClassNotFoundException {
00131 ByteArrayInputStream bstr = new ByteArrayInputStream(GeneralCoder
00132 .decodeBase64(data));
00133 ObjectInputStream ostr = new ObjectInputStream(bstr);
00134 return ostr.readObject();
00135 }
00136
00137 }