KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > audit > beans > AuditSessionBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cmp2.audit.beans;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.PreparedStatement JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.ResultSetMetaData JavaDoc;
28 import java.sql.Timestamp JavaDoc;
29 import java.sql.Types JavaDoc;
30 import java.util.Date JavaDoc;
31
32 import javax.ejb.CreateException JavaDoc;
33 import javax.ejb.EJBException JavaDoc;
34 import javax.ejb.SessionBean JavaDoc;
35 import javax.ejb.SessionContext JavaDoc;
36 import javax.naming.InitialContext JavaDoc;
37 import javax.security.auth.login.LoginContext JavaDoc;
38 import javax.sql.DataSource JavaDoc;
39
40 import org.jboss.test.cmp2.audit.interfaces.ApplicationCallbackHandler;
41 import org.jboss.test.cmp2.audit.interfaces.Audit;
42 import org.jboss.test.cmp2.audit.interfaces.AuditHome;
43 import org.jboss.test.cmp2.audit.interfaces.AuditMapped;
44 import org.jboss.test.cmp2.audit.interfaces.AuditMappedHome;
45
46 /**
47  * Session facade for audit testing.
48  *
49  * @author Adrian.Brock@HappeningTimes.com
50  * @version $Revision: 42494 $
51  */

52 public class AuditSessionBean
53    implements SessionBean JavaDoc
54 {
55    private static final int FULL = 1;
56    private static final int CREATE = 2;
57    private static final int UPDATE = 3;
58    private static final int CREATE_CHANGED_NAMES = 4;
59    private static final int UPDATE_CHANGED_NAMES = 5;
60    private static final int CREATE_MAPPED = 6;
61    private static final int UPDATE_MAPPED = 7;
62
63    private static String JavaDoc QUERY_FULL = "select audit_created_by, audit_created_time, audit_updated_by, audit_updated_time"
64                                       + " from cmp2_audit where id = ?";
65    private static String JavaDoc QUERY_CREATE = "select audit_created_by, audit_created_time"
66                                         + " from cmp2_audit where id = ?";
67    private static String JavaDoc QUERY_UPDATE = "select audit_updated_by, audit_updated_time"
68                                         + " from cmp2_audit where id = ?";
69    private static String JavaDoc QUERY_CREATE_CHANGED_NAMES = "select createdby, createdtime"
70                                                       + " from cmp2_audit_changednames where id = ?";
71    private static String JavaDoc QUERY_UPDATE_CHANGED_NAMES = "select updatedby, updatedtime"
72                                                       + " from cmp2_audit_changednames where id = ?";
73    private static String JavaDoc QUERY_CREATE_MAPPED = "select createdby, createdtime"
74                                                + " from cmp2_audit_mapped where id = ?";
75    private static String JavaDoc QUERY_UPDATE_MAPPED = "select updatedby, updatedtime"
76                                                + " from cmp2_audit_mapped where id = ?";
77
78    public void createAudit(String JavaDoc id)
79    {
80       try
81       {
82          LoginContext JavaDoc login = ApplicationCallbackHandler.login("audituser1", "user1");
83          try
84          {
85             AuditHome home = getAuditEJB();
86             home.create(id);
87          }
88          finally
89          {
90             if (login != null)
91                login.logout();
92          }
93       }
94       catch (Exception JavaDoc e)
95       {
96          throw new EJBException JavaDoc(e);
97       }
98    }
99
100    public void updateAudit(String JavaDoc id, String JavaDoc stringValue)
101    {
102       try
103       {
104          LoginContext JavaDoc login = ApplicationCallbackHandler.login("audituser2", "user2");
105          try
106          {
107             AuditHome home = getAuditEJB();
108             Audit audit = home.findByPrimaryKey(id);
109             audit.setStringValue(stringValue);
110          }
111          finally
112          {
113             if (login != null)
114                login.logout();
115          }
116       }
117       catch (Exception JavaDoc e)
118       {
119          throw new EJBException JavaDoc(e);
120       }
121    }
122
123    public void updateAuditWithClear(String JavaDoc id, String JavaDoc stringValue)
124    {
125       try
126       {
127          LoginContext JavaDoc login = ApplicationCallbackHandler.login("old-client-login", "audituser2", "user2");
128          try
129          {
130             AuditHome home = getAuditEJB();
131             Audit audit = home.findByPrimaryKey(id);
132             audit.setStringValue(stringValue);
133          }
134          finally
135          {
136             if (login != null)
137                login.logout();
138          }
139       }
140       catch (Exception JavaDoc e)
141       {
142          throw new EJBException JavaDoc(e);
143       }
144    }
145
146    public void createAuditChangedNames(String JavaDoc id)
147    {
148       try
149       {
150          LoginContext JavaDoc login = ApplicationCallbackHandler.login("audituser1", "user1");
151          try
152          {
153             AuditHome home = getAuditChangedNamesEJB();
154             home.create(id);
155          }
156          finally
157          {
158             if (login != null)
159                login.logout();
160          }
161       }
162       catch (Exception JavaDoc e)
163       {
164          throw new EJBException JavaDoc(e);
165       }
166    }
167
168    public void updateAuditChangedNames(String JavaDoc id, String JavaDoc stringValue)
169    {
170       try
171       {
172          LoginContext JavaDoc login = ApplicationCallbackHandler.login("audituser2", "user2");
173          try
174          {
175             AuditHome home = getAuditChangedNamesEJB();
176             Audit audit = home.findByPrimaryKey(id);
177             audit.setStringValue(stringValue);
178          }
179          finally
180          {
181             if (login != null)
182                login.logout();
183          }
184       }
185       catch (Exception JavaDoc e)
186       {
187          throw new EJBException JavaDoc(e);
188       }
189    }
190
191    public void createAuditMapped(String JavaDoc id)
192    {
193       try
194       {
195          LoginContext JavaDoc login = ApplicationCallbackHandler.login("audituser1", "user1");
196          try
197          {
198             AuditMappedHome home = getAuditMappedEJB();
199             home.create(id);
200          }
201          finally
202          {
203             if (login != null)
204                login.logout();
205          }
206       }
207       catch (Exception JavaDoc e)
208       {
209          throw new EJBException JavaDoc(e);
210       }
211    }
212
213    public void updateAuditMapped(String JavaDoc id, String JavaDoc stringValue)
214    {
215       try
216       {
217          LoginContext JavaDoc login = ApplicationCallbackHandler.login("audituser2", "user2");
218          try
219          {
220             AuditMappedHome home = getAuditMappedEJB();
221             AuditMapped audit = home.findByPrimaryKey(id);
222             audit.setStringValue(stringValue);
223          }
224          finally
225          {
226             if (login != null)
227                login.logout();
228          }
229       }
230       catch (Exception JavaDoc e)
231       {
232          throw new EJBException JavaDoc(e);
233       }
234    }
235
236    public void createAuditMappedChangedFields(String JavaDoc id, String JavaDoc user, long time)
237    {
238       try
239       {
240          LoginContext JavaDoc login = ApplicationCallbackHandler.login("audituser1", "user1");
241          try
242          {
243             AuditMappedHome home = getAuditMappedEJB();
244             AuditMapped audit = home.create(id);
245             audit.setCreatedBy(user);
246             audit.setCreatedTime(new Date JavaDoc(time));
247          }
248          finally
249          {
250             if (login != null)
251                login.logout();
252          }
253       }
254       catch (Exception JavaDoc e)
255       {
256          throw new EJBException JavaDoc(e);
257       }
258    }
259
260    public void updateAuditMappedChangedFields(String JavaDoc id, String JavaDoc stringValue, String JavaDoc user, long time)
261    {
262       try
263       {
264          LoginContext JavaDoc login = ApplicationCallbackHandler.login("audituser2", "user2");
265          try
266          {
267             AuditMappedHome home = getAuditMappedEJB();
268             AuditMapped audit = home.findByPrimaryKey(id);
269             audit.setStringValue(stringValue);
270             audit.setUpdatedBy(user);
271             audit.setUpdatedTime(new Date JavaDoc(time));
272          }
273          finally
274          {
275             if (login != null)
276                login.logout();
277          }
278       }
279       catch (Exception JavaDoc e)
280       {
281          throw new EJBException JavaDoc(e);
282       }
283    }
284
285    public String JavaDoc fullAuditCheck(String JavaDoc id, String JavaDoc user, long beginTime, long endTime)
286    {
287       try
288       {
289          AuditData auditData = getAuditData(id, FULL);
290          if (user.equals(auditData.createdBy) == false)
291             return "Expected created by to be set to " + user + " during the test but got " + auditData.createdBy;
292          if (auditData.createdTime < beginTime || auditData.createdTime > endTime)
293             return "Expected created time to be set between " +
294                    beginTime + "-" + endTime + " during the test but got " + auditData.createdTime;
295          if (user.equals(auditData.updatedBy) == false)
296             return "Expected updated by to be set to " + user + " during the test but got " + auditData.updatedBy;
297          if (auditData.updatedTime < beginTime || auditData.updatedTime > endTime)
298             return "Expected updated time to be set between " +
299                    beginTime + "-" + endTime + " during the test but got " + auditData.updatedTime;
300          return null;
301       }
302       catch (Exception JavaDoc e)
303       {
304          throw new EJBException JavaDoc(e);
305       }
306    }
307
308    public String JavaDoc createAuditCheck(String JavaDoc id, String JavaDoc user, long beginTime, long endTime)
309    {
310       return createCheck(id, CREATE, user, beginTime, endTime);
311    }
312
313    public String JavaDoc updateAuditCheck(String JavaDoc id, String JavaDoc user, long beginTime, long endTime)
314    {
315       return updateCheck(id, UPDATE, user, beginTime, endTime);
316    }
317
318    public String JavaDoc createAuditChangedNamesCheck(String JavaDoc id, String JavaDoc user, long beginTime, long endTime)
319    {
320       return createCheck(id, CREATE_CHANGED_NAMES, user, beginTime, endTime);
321    }
322
323    public String JavaDoc updateAuditChangedNamesCheck(String JavaDoc id, String JavaDoc user, long beginTime, long endTime)
324    {
325       return updateCheck(id, UPDATE_CHANGED_NAMES, user, beginTime, endTime);
326    }
327
328    public String JavaDoc createAuditMappedCheck(String JavaDoc id, String JavaDoc user, long beginTime, long endTime)
329    {
330       String JavaDoc failure = createCheck(id, CREATE_MAPPED, user, beginTime, endTime);
331       if (failure != null)
332          return failure;
333
334       try
335       {
336          LoginContext JavaDoc login = ApplicationCallbackHandler.login("audituser1", "user1");
337          try
338          {
339             AuditMappedHome home = getAuditMappedEJB();
340             AuditMapped audit = home.findByPrimaryKey(id);
341             if (user.equals(audit.getCreatedBy()) == false)
342                return "Expected getter to return user from test";
343             long time = audit.getCreatedTime().getTime();
344             if (time < beginTime || time > endTime)
345                return "Expected getter to return time from test";
346
347             return null;
348          }
349          finally
350          {
351             if (login != null)
352                login.logout();
353          }
354       }
355       catch (Exception JavaDoc e)
356       {
357          throw new EJBException JavaDoc(e);
358       }
359    }
360
361    public String JavaDoc updateAuditMappedCheck(String JavaDoc id, String JavaDoc user, long beginTime, long endTime)
362    {
363       String JavaDoc failure = updateCheck(id, UPDATE_MAPPED, user, beginTime, endTime);
364       if (failure != null)
365          return failure;
366
367       try
368       {
369          LoginContext JavaDoc login = ApplicationCallbackHandler.login("audituser1", "user1");
370          try
371          {
372             AuditMappedHome home = getAuditMappedEJB();
373             AuditMapped audit = home.findByPrimaryKey(id);
374             if (user.equals(audit.getUpdatedBy()) == false)
375                return "Expected getter to return user from test";
376             long time = audit.getUpdatedTime().getTime();
377             if (time < beginTime || time > endTime)
378                return "Expected getter to return time from test";
379
380             return null;
381          }
382          finally
383          {
384             if (login != null)
385                login.logout();
386          }
387       }
388       catch (Exception JavaDoc e)
389       {
390          throw new EJBException JavaDoc(e);
391       }
392    }
393
394    public String JavaDoc createCheck(String JavaDoc id, int type, String JavaDoc user, long beginTime, long endTime)
395    {
396       try
397       {
398          AuditData auditData = getAuditData(id, type);
399          if (user.equals(auditData.createdBy) == false)
400             return "Expected created by to be set to " + user + " during the test but got " + auditData.createdBy;
401          if (auditData.createdTime < beginTime || auditData.createdTime > endTime)
402             return "Expected created time to be set between " +
403                    beginTime + "-" + endTime + " during the test but got " + auditData.createdTime;
404          return null;
405       }
406       catch (Exception JavaDoc e)
407       {
408          throw new EJBException JavaDoc(e);
409       }
410    }
411
412    public String JavaDoc updateCheck(String JavaDoc id, int type, String JavaDoc user, long beginTime, long endTime)
413    {
414       try
415       {
416          AuditData auditData = getAuditData(id, type);
417          if (user.equals(auditData.updatedBy) == false)
418             return "Expected updated by to be set to " + user + " during the test but got " + auditData.updatedBy;
419          if (auditData.updatedTime < beginTime || auditData.updatedTime > endTime)
420             return "Expected updated time to be set between " +
421                    beginTime + "-" + endTime + " during the test but got " + auditData.updatedTime;
422          return null;
423       }
424       catch (Exception JavaDoc e)
425       {
426          throw new EJBException JavaDoc(e);
427       }
428    }
429
430    public void ejbCreate()
431       throws CreateException JavaDoc
432    {
433    }
434    
435    public void setSessionContext(SessionContext JavaDoc ctx)
436    {
437    }
438     
439    public void ejbActivate()
440    {
441    }
442     
443    public void ejbPassivate()
444    {
445    }
446     
447    public void ejbRemove()
448    {
449    }
450
451    private AuditData getAuditData(String JavaDoc id, int type)
452       throws Exception JavaDoc
453    {
454       Connection JavaDoc c = getDataSource().getConnection();
455       PreparedStatement JavaDoc s = null;
456       try
457       {
458          switch (type)
459          {
460          case FULL:
461             s = c.prepareStatement(QUERY_FULL);
462             break;
463          case CREATE:
464             s = c.prepareStatement(QUERY_CREATE);
465             break;
466          case UPDATE:
467             s = c.prepareStatement(QUERY_UPDATE);
468             break;
469          case CREATE_CHANGED_NAMES:
470             s = c.prepareStatement(QUERY_CREATE_CHANGED_NAMES);
471             break;
472          case UPDATE_CHANGED_NAMES:
473             s = c.prepareStatement(QUERY_UPDATE_CHANGED_NAMES);
474             break;
475          case CREATE_MAPPED:
476             s = c.prepareStatement(QUERY_CREATE_MAPPED);
477             break;
478          case UPDATE_MAPPED:
479             s = c.prepareStatement(QUERY_UPDATE_MAPPED);
480             break;
481 // case CREATE_UNSECURED:
482
// s = c.prepareStatement(QUERY_CREATE_UNSECURED);
483
// break;
484
// case UPDATE_UNSECURED:
485
// s = c.prepareStatement(QUERY_UPDATE_UNSECURED);
486
// break;
487
}
488          s.setString(1, id);
489          ResultSet JavaDoc r = s.executeQuery();
490          r.next();
491
492          switch (type)
493          {
494          case FULL:
495             return new AuditData(r.getString(1), getTimestamp(r, 2),
496                                  r.getString(3), getTimestamp(r, 4));
497          case CREATE:
498          case CREATE_CHANGED_NAMES:
499          case CREATE_MAPPED:
500             return new AuditData(r.getString(1), getTimestamp(r, 2), null, 0);
501          case UPDATE:
502          case UPDATE_CHANGED_NAMES:
503          case UPDATE_MAPPED:
504             return new AuditData(null, 0, r.getString(1), getTimestamp(r, 2));
505          }
506
507          return null;
508       }
509       finally
510       {
511          if (s != null)
512             s.close();
513          if (c != null)
514             c.close();
515       }
516    }
517
518    private AuditHome getAuditEJB()
519       throws Exception JavaDoc
520    {
521       return (AuditHome) new InitialContext JavaDoc().lookup("java:comp/env/ejb/AuditEJB");
522    }
523
524    private AuditHome getAuditChangedNamesEJB()
525       throws Exception JavaDoc
526    {
527       return (AuditHome) new InitialContext JavaDoc().lookup("java:comp/env/ejb/AuditChangedNamesEJB");
528    }
529
530    private AuditMappedHome getAuditMappedEJB()
531       throws Exception JavaDoc
532    {
533       return (AuditMappedHome) new InitialContext JavaDoc().lookup("java:comp/env/ejb/AuditMappedEJB");
534    }
535
536    private DataSource JavaDoc getDataSource()
537       throws Exception JavaDoc
538    {
539       return (DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:comp/env/jdbc/DataSource");
540    }
541
542    private static long getTimestamp(ResultSet JavaDoc r, int index)
543       throws Exception JavaDoc
544    {
545       ResultSetMetaData JavaDoc metaData = r.getMetaData();
546       switch (metaData.getColumnType(index))
547       {
548       case Types.DATE:
549          return r.getDate(index).getTime();
550       case Types.TIMESTAMP:
551          Timestamp JavaDoc timestamp = r.getTimestamp(index);
552          long time = timestamp.getTime();
553          if (time % 1000 == 0)
554             time += timestamp.getNanos() / 1000000;
555          return time;
556       }
557       return -1;
558    }
559
560    public static class AuditData
561    {
562       public String JavaDoc createdBy;
563       public long createdTime;
564       public String JavaDoc updatedBy;
565       public long updatedTime;
566
567       public AuditData(String JavaDoc createdBy, long createdTime, String JavaDoc updatedBy, long updatedTime)
568       {
569          this.createdBy = createdBy;
570          this.createdTime = createdTime;
571          this.updatedBy = updatedBy;
572          this.updatedTime = updatedTime;
573       }
574    }
575 }
576
Popular Tags