View Javadoc

1   /*
2    * Copyright (c) 2005, The K-Wf Grid Consortium
3    * Fraunhofer Institute for Computer Architecture and Software Technology
4    * See http://www.kwfgrid.eu and http://www.first.fraunhofer.de for more details.
5    */
6   package net.kwfgrid.gworkflowdl.protocol.client;
7   
8   import net.kwfgrid.gworkflowdl.protocol.IncompatibleVersionsException;
9   import net.kwfgrid.gworkflowdl.protocol.IllegalModificationException;
10  import net.kwfgrid.gworkflowdl.protocol.IMethodCallEncoder;
11  import net.kwfgrid.gworkflowdl.protocol.Protocol;
12  import net.kwfgrid.gworkflowdl.protocol.service.IStructureService;
13  import net.kwfgrid.gworkflowdl.protocol.calls.MethodCallException;
14  import net.kwfgrid.gworkflowdl.protocol.calls.IMethodCall;
15  import net.kwfgrid.gworkflowdl.protocol.structure.ProtocolWorkflow;
16  import net.kwfgrid.gworkflowdl.structure.WorkflowFormatException;
17  import net.kwfgrid.gworkflowdl.structure.CapacityException;
18  
19  import java.rmi.RemoteException;
20  import java.io.IOException;
21  
22  /***
23     Default implementation of <code>IClientDelegate</code>.
24   */
25  public class DefaultClientDelegate implements IClientDelegate {
26      private IStructureService _servicestub;
27      private String _userID;
28  
29      /***
30         Constructor.
31       @param servicestub The service stub to contact the server.
32        * @param userID
33       */
34      public DefaultClientDelegate(IStructureService servicestub, String userID) {
35  	    _servicestub = servicestub;
36          _userID = userID;
37      }
38  
39      public Object execute(IMethodCall call) throws MethodCallException, IllegalModificationException, IncompatibleVersionsException {
40  	IClientRootObject structure = (IClientRootObject)call.getTarget();
41  	IMethodCallEncoder encoder = Protocol.getMethodCallEncoder();
42  	String enc = null;
43  	synchronized(structure.getStructureLock()) {
44  	    try {
45  		enc = encoder.encode(call);
46  	    } catch (IOException x) {
47  		IllegalModificationException ix = new IllegalModificationException("Could not encode method call.");
48  		ix.initCause(x);
49  		throw ix;
50  	    }
51  	    try {
52  		commit(structure, enc);
53  	    } catch (RemoteException x) {
54  		IllegalModificationException ix = new IllegalModificationException("Could not commit changes. Exception on server-side.");
55  		ix.initCause(x);
56  		throw ix;		
57  	    }
58  	    try {
59  		return call.execute();
60  	    } catch (Exception x) {
61  		throw new MethodCallException("Could not invoke method call. Structure invalid and needs to be checked out again.", x);
62  	    }
63  	}
64      }
65  
66      public void commit(IClientRootObject structure, String modification) throws IllegalModificationException, IncompatibleVersionsException, RemoteException {
67         synchronized (structure.getStructureLock()) {
68             int newversion = _servicestub.commitModification(structure.getID(), structure.getVersionNumber(), modification, _userID);
69  	   structure.setVersionNumber(newversion);
70         }
71      }
72      
73      public void update(IClientRootObject structure) throws IllegalModificationException, RemoteException {
74  	try {
75  	    synchronized (structure.getStructureLock()) {
76  		String[][] updates = _servicestub.getModificationsForUpdate(structure.getID(), structure.getVersionNumber(), _userID);
77  		/// VERSION, COMMAND, CONTENT
78  		for (int i=0; i<updates.length; i++) {
79  		    String version = updates[i][0];
80  		    String command = updates[i][1];
81  		    String content = updates[i][2];
82  		    handleUpdate(structure, updates[i][1], updates[i][2]);
83  		    structure.setVersionNumber(Integer.parseInt(updates[i][0]));
84  		}
85  	    }   
86  	} catch (NumberFormatException x) {
87  	    throw new IllegalModificationException("Could not parse version number as int.", x);
88  	}
89      }
90  
91      private void handleUpdate(IClientRootObject structure, String command, String content) throws IllegalModificationException {
92  	if (Protocol.IDENTIFIER_FULLUPDATE.equals(command)) {
93  	    try {
94  		((ProtocolWorkflow)structure).__fromXML(content);
95  	    } catch (WorkflowFormatException x) {
96  		throw new IllegalModificationException("Could not parse workflow description.", x);
97  	    } catch (CapacityException x) {
98  		throw new IllegalModificationException("Could not parse workflow description.", x);
99  	    }
100 	} else if (Protocol.IDENTIFIER_MODIFICATION.equals(command))  {
101 	    Protocol.getModificationHandler().handleModification(structure, content);
102 	} else {
103 	    throw new IllegalModificationException("Unknown update command: "+command);
104 	}	
105     }
106 }