KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > ServiceDelegateWrapper


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.ejb3;
23
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26
27 import javax.ejb.TimerService JavaDoc;
28
29 import org.jboss.ejb3.statistics.InvocationStatistics;
30
31 import org.jboss.system.ServiceMBeanSupport;
32
33 /**
34  * Comment
35  *
36  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
37  * @version $Revision: 57901 $
38  */

39 public class ServiceDelegateWrapper extends ServiceMBeanSupport implements ServiceDelegateWrapperMBean
40 {
41    private Object JavaDoc delegate;
42    private Method JavaDoc createMethod;
43    private Method JavaDoc startMethod;
44    private Method JavaDoc stopMethod;
45    private Method JavaDoc destroyMethod;
46
47
48    public ServiceDelegateWrapper(Object JavaDoc delegate)
49    {
50       this.delegate = delegate;
51       try
52       {
53          createMethod = delegate.getClass().getMethod("create");
54       }
55       catch (NoSuchMethodException JavaDoc ignored)
56       {
57       }
58       try
59       {
60          startMethod = delegate.getClass().getMethod("start");
61       }
62       catch (NoSuchMethodException JavaDoc ignored)
63       {
64       }
65       try
66       {
67          stopMethod = delegate.getClass().getMethod("stop");
68       }
69       catch (NoSuchMethodException JavaDoc ignored)
70       {
71       }
72       try
73       {
74          destroyMethod = delegate.getClass().getMethod("destroy");
75       }
76       catch (NoSuchMethodException JavaDoc ignored)
77       {
78       }
79
80    }
81
82    @Override JavaDoc
83    protected void createService() throws Exception JavaDoc
84    {
85       super.createService();
86       try
87       {
88          if (createMethod != null) createMethod.invoke(delegate);
89       }
90       catch (InvocationTargetException JavaDoc e)
91       {
92          Throwable JavaDoc t = e.getCause();
93          if (t instanceof Exception JavaDoc) throw (Exception JavaDoc)t;
94          else throw new RuntimeException JavaDoc(t);
95       }
96    }
97
98    @Override JavaDoc
99    protected void startService() throws Exception JavaDoc
100    {
101       super.startService();
102       try
103       {
104          if (startMethod != null) startMethod.invoke(delegate);
105       }
106       catch (InvocationTargetException JavaDoc e)
107       {
108          Throwable JavaDoc t = e.getCause();
109          if (t instanceof Exception JavaDoc) throw (Exception JavaDoc)t;
110          else throw new RuntimeException JavaDoc(t);
111       }
112    }
113
114    @Override JavaDoc
115    protected void stopService() throws Exception JavaDoc
116    {
117       super.stopService();
118       try
119       {
120          if (stopMethod != null) stopMethod.invoke(delegate);
121       }
122       catch (InvocationTargetException JavaDoc e)
123       {
124          Throwable JavaDoc t = e.getCause();
125          if (t instanceof Exception JavaDoc) throw (Exception JavaDoc)t;
126          else throw new RuntimeException JavaDoc(t);
127       }
128
129    }
130
131    @Override JavaDoc
132    protected void destroyService() throws Exception JavaDoc
133    {
134       super.destroyService();
135       try
136       {
137          if (destroyMethod != null) destroyMethod.invoke(delegate);
138       }
139       catch (InvocationTargetException JavaDoc e)
140       {
141          Throwable JavaDoc t = e.getCause();
142          if (t instanceof Exception JavaDoc) throw (Exception JavaDoc)t;
143          else throw new RuntimeException JavaDoc(t);
144       }
145    }
146    
147    // FIXME: this is here for EJBTHREE-630, re-establishing timers
148
public TimerService JavaDoc getTimerService(Object JavaDoc pKey)
149    {
150       return ((Container) delegate).getTimerService(pKey);
151    }
152    
153    public InvocationStatistics getInvokeStats()
154    {
155       return ((Container) delegate).getInvokeStats();
156    }
157 }
158
Popular Tags