KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > bean > TxLockTester


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.aop.bean;
23
24 import org.jboss.logging.Logger;
25 import org.jboss.system.ServiceMBeanSupport;
26
27 import javax.management.MBeanRegistration JavaDoc;
28 import javax.management.MBeanServer JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30
31 /**
32  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
33  * @version $Revision: 37406 $
34  * @see Monitorable
35  */

36 public class TxLockTester
37 extends ServiceMBeanSupport
38 implements TxLockTesterMBean, MBeanRegistration JavaDoc
39 {
40    // Constants ----------------------------------------------------
41
// Attributes ---------------------------------------------------
42
static Logger log = Logger.getLogger(TxLockTester.class);
43    MBeanServer JavaDoc m_mbeanServer;
44
45    // Static -------------------------------------------------------
46

47    // Constructors -------------------------------------------------
48
public TxLockTester()
49    {}
50    
51    // Public -------------------------------------------------------
52

53    // MBeanRegistration implementation -----------------------------------
54
public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name)
55    throws Exception JavaDoc
56    {
57       m_mbeanServer = server;
58       return name;
59    }
60
61    public void postRegister(Boolean JavaDoc registrationDone)
62    {}
63
64    public void preDeregister() throws Exception JavaDoc
65    {}
66
67    public void postDeregister()
68    {}
69
70    protected void startService()
71    throws Exception JavaDoc
72    {
73    }
74
75    protected void stopService()
76    {
77    }
78
79    boolean failed = false;
80
81    public class LockThread implements Runnable JavaDoc
82    {
83       TxLockedPOJO pojo;
84
85       public LockThread(TxLockedPOJO pojo)
86       {
87          this.pojo = pojo;
88       }
89
90       public void run()
91       {
92          try
93          {
94             // A lock should be held
95
pojo.setField(5);
96          }
97          catch (Exception JavaDoc ex)
98          {
99             failed = true;
100             log.error("thread failed", ex);
101          }
102       }
103    }
104
105    public class AnnotatedLockThread implements Runnable JavaDoc
106    {
107       AnnotatedTxLockedPOJO pojo;
108
109       public AnnotatedLockThread(AnnotatedTxLockedPOJO pojo)
110       {
111          this.pojo = pojo;
112       }
113
114       public void run()
115       {
116          try
117          {
118             // A lock should be held
119
pojo.setField(5);
120          }
121          catch (Exception JavaDoc ex)
122          {
123             failed = true;
124             log.error("thread failed", ex);
125          }
126       }
127    }
128
129    public void testXml()
130    {
131       failed = false;
132       try
133       {
134          log.info("TESTING TX LOCK");
135          TxLockedPOJO pojo = new TxLockedPOJO();
136          Thread JavaDoc t = new Thread JavaDoc(new LockThread(pojo));
137          t.start();
138          Thread.sleep(1000);
139          pojo.setField(6);
140          if (failed) throw new RuntimeException JavaDoc("test failed");
141       }
142       catch (Throwable JavaDoc ex)
143       {
144          log.error("failed", ex);
145          throw new RuntimeException JavaDoc(ex.getMessage());
146       }
147    }
148
149    public void testAnnotated()
150    {
151       failed = false;
152       try
153       {
154          log.info("TESTING TX LOCK");
155          AnnotatedTxLockedPOJO pojo = new AnnotatedTxLockedPOJO();
156          Thread JavaDoc t = new Thread JavaDoc(new AnnotatedLockThread(pojo));
157          t.start();
158          Thread.sleep(1000);
159          pojo.setField(6);
160          if (failed) throw new RuntimeException JavaDoc("test failed");
161       }
162       catch (Throwable JavaDoc ex)
163       {
164          log.error("failed", ex);
165          throw new RuntimeException JavaDoc(ex.getMessage());
166       }
167    }
168
169 }
170
171
Popular Tags