/** * */ package org.vamsas.client.simpleclient; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.vamsas.client.ClientHandle; import org.vamsas.client.SessionHandle; import org.vamsas.client.UserHandle; import org.vamsas.client.VorbaId; import org.vamsas.client.VorbaIdFactory; import org.vamsas.objects.utils.document.VersionEntries; import java.util.Hashtable; import java.util.zip.CRC32; /** * Simplest VorbaId constructor * @author jimp * */ public class IdFactory extends VorbaIdFactory { static Log log = LogFactory.getLog(IdFactory.class); private SessionHandle session=null; private ClientHandle client; private UserHandle user; private CRC32 unique=new CRC32(); // used to attempt a unique but predictable stream for IDs private String idstring; int sequence=1; // incrementing value for next new ID /** * */ public IdFactory() { super(); // TODO Auto-generated constructor stub } /** * @param session * @param client * @param user */ protected IdFactory(SessionHandle session, ClientHandle client, UserHandle user) { super(); this.session = session; this.client = client; this.user = user; unique.reset(); unique.update(new Object[] { session, client, user}.toString().getBytes()); // TODO: Ensure format of URNs and use standard composition methods. idstring = client.getClientUrn()+":"+unique.getValue()+"/"; extantids=new Hashtable(); } /* (non-Javadoc) * @see org.vamsas.client.VorbaIdFactory#makeVorbaId() */ public VorbaId makeVorbaId() { if (session==null) throw new Error("makeVorbaId called on improperly initialised IdFactory object!"); String newidstring; do { if (sequence>0) { sequence++; } else { idstring+="1/"; sequence=1; } newidstring=idstring+Integer.toString(sequence); } while (extantids.containsKey(newidstring)); VorbaId id = newId(newidstring); return id; } /* (non-Javadoc) * @see org.vamsas.client.VorbaIdFactory#setSession(org.vamsas.client.SessionHandle) */ protected void setSession(SessionHandle sessionhandle) { if (sessionhandle!=null) session=sessionhandle; else log.warn("setSession(null) called."); } /* (non-Javadoc) * @see org.vamsas.client.VorbaIdFactory#getSessionHandle() */ public SessionHandle getSessionHandle() { return session; } /* (non-Javadoc) * @see org.vamsas.client.VorbaIdFactory#setClient(org.vamsas.client.ClientHandle) */ protected void setClient(ClientHandle appHandle) { if (appHandle!=null) client = appHandle; else log.warn("setClient(null) called."); } /* (non-Javadoc) * @see org.vamsas.client.VorbaIdFactory#getClientHandle() */ public ClientHandle getClientHandle() { return client; } /* (non-Javadoc) * @see org.vamsas.client.VorbaIdFactory#setUser(org.vamsas.client.UserHandle) */ protected void setUser(UserHandle userHandle) { if (userHandle!=null) user = userHandle; else log.warn("setUser(null) called."); } /* (non-Javadoc) * @see org.vamsas.client.VorbaIdFactory#getUserHandle() */ public UserHandle getUserHandle() { return user; } /** * Convenience method used for default behaviour in testing * and any anonymous internal vamsasDocument unmarshalling * @param clientname * @return */ protected static IdFactory getDummyFactory(String clientname) { if (clientname==null) clientname="org.vamsas.client.simpleclient.IdFactory"; return new IdFactory(new SessionHandle("dummy.session"), new ClientHandle(clientname,VersionEntries.latestVersion()), new UserHandle("arnoldUser", "none")); } }