c046af590ba3e96ce7123acb843c6a33a1f5fab7
[vamsas.git] / src / org / vamsas / client / simpleclient / IdFactory.java
1 /**
2  * 
3  */
4 package org.vamsas.client.simpleclient;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.vamsas.client.ClientHandle;
9 import org.vamsas.client.SessionHandle;
10 import org.vamsas.client.UserHandle;
11 import org.vamsas.client.VorbaId;
12 import org.vamsas.client.VorbaIdFactory;
13 import org.vamsas.objects.utils.document.VersionEntries;
14
15 import java.util.Hashtable;
16 import java.util.zip.CRC32;
17 /**
18  * Simplest VorbaId constructor
19  * @author jimp
20  *
21  */
22 public class IdFactory extends VorbaIdFactory {
23   static Log log = LogFactory.getLog(IdFactory.class);
24   private SessionHandle session=null;
25   private ClientHandle client;
26   private UserHandle user;
27   private CRC32 unique=new CRC32(); // used to attempt a unique but predictable stream for IDs
28   private String idstring;
29   int sequence=0; // incrementing value for next new ID
30   /**
31    * 
32    */
33   public IdFactory() {
34     super();
35     // TODO Auto-generated constructor stub
36   }
37   
38   /**
39    * @param session
40    * @param client
41    * @param user
42    */
43   protected IdFactory(SessionHandle session, ClientHandle client, UserHandle user) {
44     super();
45     this.session = session;
46     this.client = client;
47     this.user = user;
48     unique.reset();
49     unique.update(new Object[] { session, client, user}.toString().getBytes());
50     idstring = "vamsas:"+session.getSessionUrn()+":"+unique.getValue()+":"+client.getClientUrn()+"/";
51     sequence=0;
52     extantids=new Hashtable();
53   }
54
55   /* (non-Javadoc)
56    * @see org.vamsas.client.VorbaIdFactory#makeVorbaId()
57    */
58   public VorbaId makeVorbaId() {
59     if (session==null)
60       throw new Error("makeVorbaId called on improperly initialised IdFactory object!");
61     String newidstring;
62     do {
63       if (sequence!=0) {
64         sequence++;
65       } else {
66         idstring+="1/";
67       }
68       newidstring=idstring+Integer.toString(sequence);
69     } while (extantids.containsKey(newidstring));
70     VorbaId id = newId(newidstring);
71     return id;
72   }
73
74   /* (non-Javadoc)
75    * @see org.vamsas.client.VorbaIdFactory#setSession(org.vamsas.client.SessionHandle)
76    */
77   protected void setSession(SessionHandle sessionhandle) {
78     if (sessionhandle!=null)
79       session=sessionhandle;
80     else
81       log.warn("setSession(null) called.");
82   }
83
84   /* (non-Javadoc)
85    * @see org.vamsas.client.VorbaIdFactory#getSessionHandle()
86    */
87   public SessionHandle getSessionHandle() {
88     return session;
89   }
90
91   /* (non-Javadoc)
92    * @see org.vamsas.client.VorbaIdFactory#setClient(org.vamsas.client.ClientHandle)
93    */
94   protected void setClient(ClientHandle appHandle) {
95     if (appHandle!=null)
96       client = appHandle;
97     else
98       log.warn("setClient(null) called.");
99   }
100
101   /* (non-Javadoc)
102    * @see org.vamsas.client.VorbaIdFactory#getClientHandle()
103    */
104   public ClientHandle getClientHandle() {
105     return client;
106   }
107
108   /* (non-Javadoc)
109    * @see org.vamsas.client.VorbaIdFactory#setUser(org.vamsas.client.UserHandle)
110    */
111   protected void setUser(UserHandle userHandle) {
112   if (userHandle!=null)
113     user = userHandle;
114   else
115     log.warn("setUser(null) called.");
116   }
117
118   /* (non-Javadoc)
119    * @see org.vamsas.client.VorbaIdFactory#getUserHandle()
120    */
121   public UserHandle getUserHandle() {
122     return user;
123   }
124   /**
125    * Convenience method used for default behaviour in testing 
126    * and any anonymous internal vamsasDocument unmarshalling
127    * @param clientname
128    * @return
129    */
130   protected  static IdFactory getDummyFactory(String clientname) {
131     if (clientname==null)
132       clientname="org.vamsas.client.simpleclient.IdFactory";
133     return new IdFactory(new SessionHandle("dummy.session"), 
134         new ClientHandle(clientname,VersionEntries.latestVersion()), 
135         new UserHandle("arnoldUser", "none"));
136   }
137 }