4 package uk.ac.vamsas.client.simpleclient;
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.vamsas.objects.utils.document.VersionEntries;
10 import uk.ac.vamsas.client.ClientHandle;
11 import uk.ac.vamsas.client.SessionHandle;
12 import uk.ac.vamsas.client.UserHandle;
13 import uk.ac.vamsas.client.Vobject;
14 import uk.ac.vamsas.client.VorbaId;
15 import uk.ac.vamsas.client.VorbaIdFactory;
17 import java.util.Hashtable;
18 import java.util.zip.CRC32;
20 * Simplest VorbaId constructor
24 public class IdFactory extends VorbaIdFactory {
25 static Log log = LogFactory.getLog(IdFactory.class);
26 private SessionHandle session=null;
27 private ClientHandle client;
28 private UserHandle user;
29 private CRC32 unique=new CRC32(); // used to attempt a unique but predictable stream for IDs
30 private String idstring;
31 int sequence=1; // incrementing value for next new ID
37 // TODO Auto-generated constructor stub
45 protected IdFactory(SessionHandle session, ClientHandle client, UserHandle user) {
47 this.session = session;
51 unique.update(new Object[] { session, client, user}.toString().getBytes());
52 // TODO: Ensure format of URNs and use standard composition methods.
53 idstring = client.getClientName()+":"+unique.getValue()+".";
54 extantids=new Hashtable();
55 this.extanthashv=new Hashtable();
58 * values for keys in this hash can be used to reference the uk.ac.vamsas.client.Vobject instance for the VorbaId string.
59 * @return the hash of all VorbaIds
61 protected Hashtable getVorbaIdHash() {
65 * values for keys in this hash are Vobjhash objects created for each Vobj with a VorbaId
66 * after this factory has been used to write a vamsas archive.
67 * @return the hash of all VorbaIds and their hash values.
69 protected Hashtable getVobjhashVals() {
73 * @see uk.ac.vamsas.client.VorbaIdFactory#makeVorbaId()
75 public VorbaId makeVorbaId(Vobject vobject) {
77 throw new Error("makeVorbaId called on improperly initialised IdFactory Vobject!");
78 if (!vobject.isRegisterable())
79 throw new Error("makeVorbaId called on unregisterable object.");
80 if (vobject.isRegistered())
81 throw new Error("makeVorbaId called on already registered object.");
90 newidstring=idstring+Integer.toString(sequence);
91 } while (extantids.containsKey(newidstring));
92 extantids.put(newidstring, vobject); // hash the Vobject by its new Id
93 VorbaId id = newId(newidstring); // VorbaId.hash()==newidstring.hash() so we can still recover vobject
98 * @see uk.ac.vamsas.client.VorbaIdFactory#setSession(uk.ac.vamsas.client.SessionHandle)
100 protected void setSession(SessionHandle sessionhandle) {
101 if (sessionhandle!=null)
102 session=sessionhandle;
104 log.warn("setSession(null) called.");
108 * @see uk.ac.vamsas.client.VorbaIdFactory#getSessionHandle()
110 public SessionHandle getSessionHandle() {
115 * @see uk.ac.vamsas.client.VorbaIdFactory#setClient(uk.ac.vamsas.client.ClientHandle)
117 protected void setClient(ClientHandle appHandle) {
121 log.warn("setClient(null) called.");
125 * @see uk.ac.vamsas.client.VorbaIdFactory#getClientHandle()
127 public ClientHandle getClientHandle() {
132 * @see uk.ac.vamsas.client.VorbaIdFactory#setUser(uk.ac.vamsas.client.UserHandle)
134 protected void setUser(UserHandle userHandle) {
135 if (userHandle!=null)
138 log.warn("setUser(null) called.");
142 * @see uk.ac.vamsas.client.VorbaIdFactory#getUserHandle()
144 public UserHandle getUserHandle() {
148 * Convenience method used for default behaviour in testing
149 * and any anonymous internal vamsasDocument unmarshalling
153 protected static IdFactory getDummyFactory(String clientname) {
154 if (clientname==null)
155 clientname="uk.ac.vamsas.client.simpleclient.IdFactory";
156 return new IdFactory(new SessionHandle("dummy.session"),
157 new ClientHandle(clientname,VersionEntries.latestVersion()),
158 new UserHandle(clientname, "Arnold User's Inc."));