1
2
3
4
5
6 package net.kwfgrid.gworkflowdl.protocol.server;
7
8 import net.kwfgrid.gworkflowdl.protocol.IncompatibleVersionsException;
9 import net.kwfgrid.gworkflowdl.protocol.IllegalModificationException;
10 import net.kwfgrid.gworkflowdl.protocol.IModificationHandler;
11 import net.kwfgrid.gworkflowdl.protocol.IMethodCallEncoder;
12 import net.kwfgrid.gworkflowdl.protocol.Protocol;
13 import net.kwfgrid.gworkflowdl.protocol.calls.MethodCallException;
14 import net.kwfgrid.gworkflowdl.protocol.calls.IMethodCall;
15 import net.kwfgrid.gworkflowdl.structure.JdomString;
16 import net.kwfgrid.gworkflowdl.structure.Workflow;
17
18 import java.io.IOException;
19
20 import org.apache.log4j.Logger;
21
22 /***
23 Default implementation of <code>IServerDelegate</code> for a <code>Workflow</code> structure.
24 */
25 public class DefaultServerDelegate implements IServerDelegate {
26 private static final Logger logger = Logger.getLogger(DefaultServerDelegate.class);
27
28 public Object execute(IMethodCall call) throws MethodCallException, IllegalModificationException {
29 IServerRootObject structure = (IServerRootObject)call.getTarget();
30 IMethodCallEncoder encoder = Protocol.getMethodCallEncoder();
31 Object ret = null;
32 String enc = null;
33 int newversion = -1;
34 synchronized(structure.getStructureLock()) {
35 boolean cupdate = structure.getClientUpdateOccured();
36 if (cupdate) {
37 try {
38 enc = encoder.encode(call);
39 } catch (IOException x) {
40 throw new IllegalModificationException("Could not encode method call.", x);
41 }
42 }
43 try {
44 ret = call.execute();
45 } catch (Exception x) {
46 throw new MethodCallException("Could not execute method call.", x);
47 }
48
49 newversion = structure.incrementVersionNumber();
50
51 if (cupdate) {
52 structure.getModificationBuffer().write(newversion, enc);
53 } else {
54 structure.getModificationBuffer().setInitialVersion(newversion);
55 }
56
57 structure.setXMLValid(false);
58
59 return ret;
60 }
61 }
62
63 public String[][] getModificationsForUpdate(IServerRootObject structure, int clientversion) {
64 synchronized (structure.getStructureLock()) {
65 structure.setClientUpdateOccured();
66 ModificationBuffer buffer = structure.getModificationBuffer();
67 if (clientversion < buffer.firstHandleableVersion()) {
68 return new String[][] { { ""+structure.getVersionNumber(), Protocol.IDENTIFIER_FULLUPDATE, structure.getXML() } };
69 } else {
70 return buffer.read(clientversion);
71 }
72 }
73 }
74
75 public int commitModification(IServerRootObject structure, int clientversion, String modification) throws IncompatibleVersionsException, IllegalModificationException {
76 synchronized(structure.getStructureLock()) {
77 if (structure.getVersionNumber() != clientversion)
78 throw new IncompatibleVersionsException("The client's version is incompatible for commit ("+clientversion+" != "+structure.getVersionNumber()+").");
79
80 Protocol.getModificationHandler().handleModification(structure, modification);
81
82 int newversion = structure.incrementVersionNumber();
83
84 if (structure.getClientUpdateOccured()) {
85 structure.getModificationBuffer().write(newversion, modification);
86 } else {
87 structure.getModificationBuffer().setInitialVersion(newversion);
88 }
89
90 structure.setXMLValid(false);
91
92 return newversion;
93 }
94 }
95
96 public String getXML(IServerRootObject structure, String currentxml) {
97 synchronized(structure.getStructureLock()) {
98 if (!structure.isXMLValid()) {
99 try {
100 currentxml = JdomString.workflow2string((Workflow)structure);
101 structure.setXML(currentxml);
102 structure.setXMLValid(true);
103 } catch (IOException x) {
104 logger.error("FATAL: Could not write XML description of workflow.", x);
105 currentxml = "<workflow/>";
106 }
107 }
108 return currentxml;
109 }
110 }
111 }