001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cmp2.audit.beans;
023:
024: import java.sql.Connection;
025: import java.sql.PreparedStatement;
026: import java.sql.ResultSet;
027: import java.sql.ResultSetMetaData;
028: import java.sql.Timestamp;
029: import java.sql.Types;
030: import java.util.Date;
031:
032: import javax.ejb.CreateException;
033: import javax.ejb.EJBException;
034: import javax.ejb.SessionBean;
035: import javax.ejb.SessionContext;
036: import javax.naming.InitialContext;
037: import javax.security.auth.login.LoginContext;
038: import javax.sql.DataSource;
039:
040: import org.jboss.test.cmp2.audit.interfaces.ApplicationCallbackHandler;
041: import org.jboss.test.cmp2.audit.interfaces.Audit;
042: import org.jboss.test.cmp2.audit.interfaces.AuditHome;
043: import org.jboss.test.cmp2.audit.interfaces.AuditMapped;
044: import org.jboss.test.cmp2.audit.interfaces.AuditMappedHome;
045:
046: /**
047: * Session facade for audit testing.
048: *
049: * @author Adrian.Brock@HappeningTimes.com
050: * @version $Revision: 57211 $
051: */
052: public class AuditSessionBean implements SessionBean {
053: private static final int FULL = 1;
054: private static final int CREATE = 2;
055: private static final int UPDATE = 3;
056: private static final int CREATE_CHANGED_NAMES = 4;
057: private static final int UPDATE_CHANGED_NAMES = 5;
058: private static final int CREATE_MAPPED = 6;
059: private static final int UPDATE_MAPPED = 7;
060:
061: private static String QUERY_FULL = "select audit_created_by, audit_created_time, audit_updated_by, audit_updated_time"
062: + " from cmp2_audit where id = ?";
063: private static String QUERY_CREATE = "select audit_created_by, audit_created_time"
064: + " from cmp2_audit where id = ?";
065: private static String QUERY_UPDATE = "select audit_updated_by, audit_updated_time"
066: + " from cmp2_audit where id = ?";
067: private static String QUERY_CREATE_CHANGED_NAMES = "select createdby, createdtime"
068: + " from cmp2_audit_changednames where id = ?";
069: private static String QUERY_UPDATE_CHANGED_NAMES = "select updatedby, updatedtime"
070: + " from cmp2_audit_changednames where id = ?";
071: private static String QUERY_CREATE_MAPPED = "select createdby, createdtime"
072: + " from cmp2_audit_mapped where id = ?";
073: private static String QUERY_UPDATE_MAPPED = "select updatedby, updatedtime"
074: + " from cmp2_audit_mapped where id = ?";
075:
076: public void createAudit(String id) {
077: try {
078: LoginContext login = ApplicationCallbackHandler.login(
079: "audituser1", "user1");
080: try {
081: AuditHome home = getAuditEJB();
082: home.create(id);
083: } finally {
084: if (login != null)
085: login.logout();
086: }
087: } catch (Exception e) {
088: throw new EJBException(e);
089: }
090: }
091:
092: public void updateAudit(String id, String stringValue) {
093: try {
094: LoginContext login = ApplicationCallbackHandler.login(
095: "audituser2", "user2");
096: try {
097: AuditHome home = getAuditEJB();
098: Audit audit = home.findByPrimaryKey(id);
099: audit.setStringValue(stringValue);
100: } finally {
101: if (login != null)
102: login.logout();
103: }
104: } catch (Exception e) {
105: throw new EJBException(e);
106: }
107: }
108:
109: public void updateAuditWithClear(String id, String stringValue) {
110: try {
111: LoginContext login = ApplicationCallbackHandler.login(
112: "old-client-login", "audituser2", "user2");
113: try {
114: AuditHome home = getAuditEJB();
115: Audit audit = home.findByPrimaryKey(id);
116: audit.setStringValue(stringValue);
117: } finally {
118: if (login != null)
119: login.logout();
120: }
121: } catch (Exception e) {
122: throw new EJBException(e);
123: }
124: }
125:
126: public void createAuditChangedNames(String id) {
127: try {
128: LoginContext login = ApplicationCallbackHandler.login(
129: "audituser1", "user1");
130: try {
131: AuditHome home = getAuditChangedNamesEJB();
132: home.create(id);
133: } finally {
134: if (login != null)
135: login.logout();
136: }
137: } catch (Exception e) {
138: throw new EJBException(e);
139: }
140: }
141:
142: public void updateAuditChangedNames(String id, String stringValue) {
143: try {
144: LoginContext login = ApplicationCallbackHandler.login(
145: "audituser2", "user2");
146: try {
147: AuditHome home = getAuditChangedNamesEJB();
148: Audit audit = home.findByPrimaryKey(id);
149: audit.setStringValue(stringValue);
150: } finally {
151: if (login != null)
152: login.logout();
153: }
154: } catch (Exception e) {
155: throw new EJBException(e);
156: }
157: }
158:
159: public void createAuditMapped(String id) {
160: try {
161: LoginContext login = ApplicationCallbackHandler.login(
162: "audituser1", "user1");
163: try {
164: AuditMappedHome home = getAuditMappedEJB();
165: home.create(id);
166: } finally {
167: if (login != null)
168: login.logout();
169: }
170: } catch (Exception e) {
171: throw new EJBException(e);
172: }
173: }
174:
175: public void updateAuditMapped(String id, String stringValue) {
176: try {
177: LoginContext login = ApplicationCallbackHandler.login(
178: "audituser2", "user2");
179: try {
180: AuditMappedHome home = getAuditMappedEJB();
181: AuditMapped audit = home.findByPrimaryKey(id);
182: audit.setStringValue(stringValue);
183: } finally {
184: if (login != null)
185: login.logout();
186: }
187: } catch (Exception e) {
188: throw new EJBException(e);
189: }
190: }
191:
192: public void createAuditMappedChangedFields(String id, String user,
193: long time) {
194: try {
195: LoginContext login = ApplicationCallbackHandler.login(
196: "audituser1", "user1");
197: try {
198: AuditMappedHome home = getAuditMappedEJB();
199: AuditMapped audit = home.create(id);
200: audit.setCreatedBy(user);
201: audit.setCreatedTime(new Date(time));
202: } finally {
203: if (login != null)
204: login.logout();
205: }
206: } catch (Exception e) {
207: throw new EJBException(e);
208: }
209: }
210:
211: public void updateAuditMappedChangedFields(String id,
212: String stringValue, String user, long time) {
213: try {
214: LoginContext login = ApplicationCallbackHandler.login(
215: "audituser2", "user2");
216: try {
217: AuditMappedHome home = getAuditMappedEJB();
218: AuditMapped audit = home.findByPrimaryKey(id);
219: audit.setStringValue(stringValue);
220: audit.setUpdatedBy(user);
221: audit.setUpdatedTime(new Date(time));
222: } finally {
223: if (login != null)
224: login.logout();
225: }
226: } catch (Exception e) {
227: throw new EJBException(e);
228: }
229: }
230:
231: public String fullAuditCheck(String id, String user,
232: long beginTime, long endTime) {
233: try {
234: AuditData auditData = getAuditData(id, FULL);
235: if (user.equals(auditData.createdBy) == false)
236: return "Expected created by to be set to " + user
237: + " during the test but got "
238: + auditData.createdBy;
239: if (auditData.createdTime < beginTime
240: || auditData.createdTime > endTime)
241: return "Expected created time to be set between "
242: + beginTime + "-" + endTime
243: + " during the test but got "
244: + auditData.createdTime;
245: if (user.equals(auditData.updatedBy) == false)
246: return "Expected updated by to be set to " + user
247: + " during the test but got "
248: + auditData.updatedBy;
249: if (auditData.updatedTime < beginTime
250: || auditData.updatedTime > endTime)
251: return "Expected updated time to be set between "
252: + beginTime + "-" + endTime
253: + " during the test but got "
254: + auditData.updatedTime;
255: return null;
256: } catch (Exception e) {
257: throw new EJBException(e);
258: }
259: }
260:
261: public String createAuditCheck(String id, String user,
262: long beginTime, long endTime) {
263: return createCheck(id, CREATE, user, beginTime, endTime);
264: }
265:
266: public String updateAuditCheck(String id, String user,
267: long beginTime, long endTime) {
268: return updateCheck(id, UPDATE, user, beginTime, endTime);
269: }
270:
271: public String createAuditChangedNamesCheck(String id, String user,
272: long beginTime, long endTime) {
273: return createCheck(id, CREATE_CHANGED_NAMES, user, beginTime,
274: endTime);
275: }
276:
277: public String updateAuditChangedNamesCheck(String id, String user,
278: long beginTime, long endTime) {
279: return updateCheck(id, UPDATE_CHANGED_NAMES, user, beginTime,
280: endTime);
281: }
282:
283: public String createAuditMappedCheck(String id, String user,
284: long beginTime, long endTime) {
285: String failure = createCheck(id, CREATE_MAPPED, user,
286: beginTime, endTime);
287: if (failure != null)
288: return failure;
289:
290: try {
291: LoginContext login = ApplicationCallbackHandler.login(
292: "audituser1", "user1");
293: try {
294: AuditMappedHome home = getAuditMappedEJB();
295: AuditMapped audit = home.findByPrimaryKey(id);
296: if (user.equals(audit.getCreatedBy()) == false)
297: return "Expected getter to return user from test";
298: long time = audit.getCreatedTime().getTime();
299: if (time < beginTime || time > endTime)
300: return "Expected getter to return time from test";
301:
302: return null;
303: } finally {
304: if (login != null)
305: login.logout();
306: }
307: } catch (Exception e) {
308: throw new EJBException(e);
309: }
310: }
311:
312: public String updateAuditMappedCheck(String id, String user,
313: long beginTime, long endTime) {
314: String failure = updateCheck(id, UPDATE_MAPPED, user,
315: beginTime, endTime);
316: if (failure != null)
317: return failure;
318:
319: try {
320: LoginContext login = ApplicationCallbackHandler.login(
321: "audituser1", "user1");
322: try {
323: AuditMappedHome home = getAuditMappedEJB();
324: AuditMapped audit = home.findByPrimaryKey(id);
325: if (user.equals(audit.getUpdatedBy()) == false)
326: return "Expected getter to return user from test";
327: long time = audit.getUpdatedTime().getTime();
328: if (time < beginTime || time > endTime)
329: return "Expected getter to return time from test";
330:
331: return null;
332: } finally {
333: if (login != null)
334: login.logout();
335: }
336: } catch (Exception e) {
337: throw new EJBException(e);
338: }
339: }
340:
341: public String createCheck(String id, int type, String user,
342: long beginTime, long endTime) {
343: try {
344: AuditData auditData = getAuditData(id, type);
345: if (user.equals(auditData.createdBy) == false)
346: return "Expected created by to be set to " + user
347: + " during the test but got "
348: + auditData.createdBy;
349: if (auditData.createdTime < beginTime
350: || auditData.createdTime > endTime)
351: return "Expected created time to be set between "
352: + beginTime + "-" + endTime
353: + " during the test but got "
354: + auditData.createdTime;
355: return null;
356: } catch (Exception e) {
357: throw new EJBException(e);
358: }
359: }
360:
361: public String updateCheck(String id, int type, String user,
362: long beginTime, long endTime) {
363: try {
364: AuditData auditData = getAuditData(id, type);
365: if (user.equals(auditData.updatedBy) == false)
366: return "Expected updated by to be set to " + user
367: + " during the test but got "
368: + auditData.updatedBy;
369: if (auditData.updatedTime < beginTime
370: || auditData.updatedTime > endTime)
371: return "Expected updated time to be set between "
372: + beginTime + "-" + endTime
373: + " during the test but got "
374: + auditData.updatedTime;
375: return null;
376: } catch (Exception e) {
377: throw new EJBException(e);
378: }
379: }
380:
381: public void ejbCreate() throws CreateException {
382: }
383:
384: public void setSessionContext(SessionContext ctx) {
385: }
386:
387: public void ejbActivate() {
388: }
389:
390: public void ejbPassivate() {
391: }
392:
393: public void ejbRemove() {
394: }
395:
396: private AuditData getAuditData(String id, int type)
397: throws Exception {
398: Connection c = getDataSource().getConnection();
399: PreparedStatement s = null;
400: try {
401: switch (type) {
402: case FULL:
403: s = c.prepareStatement(QUERY_FULL);
404: break;
405: case CREATE:
406: s = c.prepareStatement(QUERY_CREATE);
407: break;
408: case UPDATE:
409: s = c.prepareStatement(QUERY_UPDATE);
410: break;
411: case CREATE_CHANGED_NAMES:
412: s = c.prepareStatement(QUERY_CREATE_CHANGED_NAMES);
413: break;
414: case UPDATE_CHANGED_NAMES:
415: s = c.prepareStatement(QUERY_UPDATE_CHANGED_NAMES);
416: break;
417: case CREATE_MAPPED:
418: s = c.prepareStatement(QUERY_CREATE_MAPPED);
419: break;
420: case UPDATE_MAPPED:
421: s = c.prepareStatement(QUERY_UPDATE_MAPPED);
422: break;
423: // case CREATE_UNSECURED:
424: // s = c.prepareStatement(QUERY_CREATE_UNSECURED);
425: // break;
426: // case UPDATE_UNSECURED:
427: // s = c.prepareStatement(QUERY_UPDATE_UNSECURED);
428: // break;
429: }
430: s.setString(1, id);
431: ResultSet r = s.executeQuery();
432: r.next();
433:
434: switch (type) {
435: case FULL:
436: return new AuditData(r.getString(1),
437: getTimestamp(r, 2), r.getString(3),
438: getTimestamp(r, 4));
439: case CREATE:
440: case CREATE_CHANGED_NAMES:
441: case CREATE_MAPPED:
442: return new AuditData(r.getString(1),
443: getTimestamp(r, 2), null, 0);
444: case UPDATE:
445: case UPDATE_CHANGED_NAMES:
446: case UPDATE_MAPPED:
447: return new AuditData(null, 0, r.getString(1),
448: getTimestamp(r, 2));
449: }
450:
451: return null;
452: } finally {
453: if (s != null)
454: s.close();
455: if (c != null)
456: c.close();
457: }
458: }
459:
460: private AuditHome getAuditEJB() throws Exception {
461: return (AuditHome) new InitialContext()
462: .lookup("java:comp/env/ejb/AuditEJB");
463: }
464:
465: private AuditHome getAuditChangedNamesEJB() throws Exception {
466: return (AuditHome) new InitialContext()
467: .lookup("java:comp/env/ejb/AuditChangedNamesEJB");
468: }
469:
470: private AuditMappedHome getAuditMappedEJB() throws Exception {
471: return (AuditMappedHome) new InitialContext()
472: .lookup("java:comp/env/ejb/AuditMappedEJB");
473: }
474:
475: private DataSource getDataSource() throws Exception {
476: return (DataSource) new InitialContext()
477: .lookup("java:comp/env/jdbc/DataSource");
478: }
479:
480: private static long getTimestamp(ResultSet r, int index)
481: throws Exception {
482: ResultSetMetaData metaData = r.getMetaData();
483: switch (metaData.getColumnType(index)) {
484: case Types.DATE:
485: return r.getDate(index).getTime();
486: case Types.TIMESTAMP:
487: Timestamp timestamp = r.getTimestamp(index);
488: long time = timestamp.getTime();
489: if (time % 1000 == 0)
490: time += timestamp.getNanos() / 1000000;
491: return time;
492: }
493: return -1;
494: }
495:
496: public static class AuditData {
497: public String createdBy;
498: public long createdTime;
499: public String updatedBy;
500: public long updatedTime;
501:
502: public AuditData(String createdBy, long createdTime,
503: String updatedBy, long updatedTime) {
504: this.createdBy = createdBy;
505: this.createdTime = createdTime;
506: this.updatedBy = updatedBy;
507: this.updatedTime = updatedTime;
508: }
509: }
510: }
|