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.aop.bean;
023:
024: import org.jboss.logging.Logger;
025: import org.jboss.system.ServiceMBeanSupport;
026:
027: import javax.management.MBeanRegistration;
028: import javax.management.MBeanServer;
029: import javax.management.ObjectName;
030:
031: /**
032: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
033: * @version $Revision: 57211 $
034: * @see Monitorable
035: */
036: public class TxLockTester extends ServiceMBeanSupport implements
037: TxLockTesterMBean, MBeanRegistration {
038: // Constants ----------------------------------------------------
039: // Attributes ---------------------------------------------------
040: static Logger log = Logger.getLogger(TxLockTester.class);
041: MBeanServer m_mbeanServer;
042:
043: // Static -------------------------------------------------------
044:
045: // Constructors -------------------------------------------------
046: public TxLockTester() {
047: }
048:
049: // Public -------------------------------------------------------
050:
051: // MBeanRegistration implementation -----------------------------------
052: public ObjectName preRegister(MBeanServer server, ObjectName name)
053: throws Exception {
054: m_mbeanServer = server;
055: return name;
056: }
057:
058: public void postRegister(Boolean registrationDone) {
059: }
060:
061: public void preDeregister() throws Exception {
062: }
063:
064: public void postDeregister() {
065: }
066:
067: protected void startService() throws Exception {
068: }
069:
070: protected void stopService() {
071: }
072:
073: boolean failed = false;
074:
075: public class LockThread implements Runnable {
076: TxLockedPOJO pojo;
077:
078: public LockThread(TxLockedPOJO pojo) {
079: this .pojo = pojo;
080: }
081:
082: public void run() {
083: try {
084: // A lock should be held
085: pojo.setField(5);
086: } catch (Exception ex) {
087: failed = true;
088: log.error("thread failed", ex);
089: }
090: }
091: }
092:
093: public class AnnotatedLockThread implements Runnable {
094: AnnotatedTxLockedPOJO pojo;
095:
096: public AnnotatedLockThread(AnnotatedTxLockedPOJO pojo) {
097: this .pojo = pojo;
098: }
099:
100: public void run() {
101: try {
102: // A lock should be held
103: pojo.setField(5);
104: } catch (Exception ex) {
105: failed = true;
106: log.error("thread failed", ex);
107: }
108: }
109: }
110:
111: public void testXml() {
112: failed = false;
113: try {
114: log.info("TESTING TX LOCK");
115: TxLockedPOJO pojo = new TxLockedPOJO();
116: Thread t = new Thread(new LockThread(pojo));
117: t.start();
118: Thread.sleep(1000);
119: pojo.setField(6);
120: if (failed)
121: throw new RuntimeException("test failed");
122: } catch (Throwable ex) {
123: log.error("failed", ex);
124: throw new RuntimeException(ex.getMessage());
125: }
126: }
127:
128: public void testAnnotated() {
129: failed = false;
130: try {
131: log.info("TESTING TX LOCK");
132: AnnotatedTxLockedPOJO pojo = new AnnotatedTxLockedPOJO();
133: Thread t = new Thread(new AnnotatedLockThread(pojo));
134: t.start();
135: Thread.sleep(1000);
136: pojo.setField(6);
137: if (failed)
138: throw new RuntimeException("test failed");
139: } catch (Throwable ex) {
140: log.error("failed", ex);
141: throw new RuntimeException(ex.getMessage());
142: }
143: }
144:
145: }
|