KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > txtimer > EJBTimerServiceLocator


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.ejb.txtimer;
23
24 // $Id: EJBTimerServiceLocator.java 43304 2006-04-03 12:28:01Z adrian $
25

26 import javax.ejb.TimerService JavaDoc;
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29
30 import org.jboss.ejb.Container;
31 import org.jboss.logging.Logger;
32 import org.jboss.mx.util.MBeanProxyExt;
33 import org.jboss.mx.util.MBeanServerLocator;
34
35 /**
36  * Locates the EJBTimerService, either as MBean or as local instance.
37  *
38  * It first checks if the EJBTimerServiceImpl is registered with the MBeanServer,
39  * if not it creates a singleton and uses that.
40  *
41  * @author Thomas.Diesler@jboss.org
42  * @author Dimitris.Andreadis@jboss.org
43  * @version $Revision: 43304 $
44  * @since 07-Apr-2004
45  */

46 public class EJBTimerServiceLocator
47 {
48    // logging support
49
private static Logger log = Logger.getLogger(EJBTimerServiceLocator.class);
50
51    private static EJBTimerService ejbTimerService;
52
53    /**
54     * Locates the EJBTimerService, first as MBean, then as singleton
55     */

56    public static EJBTimerService getEjbTimerService()
57    {
58       try
59       {
60          // First try the MBean server
61
MBeanServer JavaDoc server = MBeanServerLocator.locateJBoss();
62          if (server != null && server.isRegistered(EJBTimerService.OBJECT_NAME))
63             ejbTimerService = new MBeanDelegate(server);
64       }
65       catch (Exception JavaDoc ignore)
66       {
67       }
68
69       // This path can be used for standalone test cases
70
if (ejbTimerService == null)
71       {
72          EJBTimerServiceImpl ejbTimerServiceImpl = new EJBTimerServiceImpl();
73          ejbTimerService = ejbTimerServiceImpl;
74          try
75          {
76             ejbTimerServiceImpl.create();
77             ejbTimerServiceImpl.start();
78          }
79          catch (Exception JavaDoc e)
80          {
81             throw new RuntimeException JavaDoc("Cannot start EJBTimerService", e);
82          }
83       }
84       return ejbTimerService;
85    }
86
87    /**
88     * Delegates method calls to the EJBTimerService to the MBean server
89     */

90    public static class MBeanDelegate implements EJBTimerService
91    {
92       private EJBTimerService mbeanEjbTimerService;
93
94       public MBeanDelegate(MBeanServer JavaDoc server)
95       {
96          try
97          {
98             mbeanEjbTimerService = (EJBTimerService)MBeanProxyExt.create(EJBTimerService.class, EJBTimerService.OBJECT_NAME, server);
99          }
100          catch (Exception JavaDoc e)
101          {
102             throw new IllegalStateException JavaDoc("Cannot create EJBTimerService proxy: " + e.getMessage());
103          }
104       }
105
106       public TimerService JavaDoc createTimerService(ObjectName JavaDoc containerId, Object JavaDoc instancePk, Container container)
107               throws IllegalStateException JavaDoc
108       {
109          try
110          {
111             TimerService JavaDoc timerService = mbeanEjbTimerService.createTimerService(containerId, instancePk, container);
112             return timerService;
113          }
114          catch (Exception JavaDoc e)
115          {
116             log.error("Cannot createTimerService", e);
117             return null;
118          }
119       }
120
121       public TimerService JavaDoc createTimerService(ObjectName JavaDoc containerId, Object JavaDoc instancePk, TimedObjectInvoker invoker)
122               throws IllegalStateException JavaDoc
123       {
124          try
125          {
126             TimerService JavaDoc timerService = mbeanEjbTimerService.createTimerService(containerId, instancePk, invoker);
127             return timerService;
128          }
129          catch (Exception JavaDoc e)
130          {
131             log.error("Cannot createTimerService", e);
132             return null;
133          }
134       }
135
136       public TimerService JavaDoc getTimerService(ObjectName JavaDoc containerId, Object JavaDoc instancePk)
137               throws IllegalStateException JavaDoc
138       {
139          try
140          {
141             TimerService JavaDoc timerService = mbeanEjbTimerService.getTimerService(containerId, instancePk);
142             return timerService;
143          }
144          catch (Exception JavaDoc e)
145          {
146             log.error("Cannot getTimerService", e);
147             return null;
148          }
149       }
150
151       public void removeTimerService(ObjectName JavaDoc containerId, Object JavaDoc instancePk)
152               throws IllegalStateException JavaDoc
153       {
154          try
155          {
156             mbeanEjbTimerService.removeTimerService(containerId, instancePk);
157          }
158          catch (Exception JavaDoc e)
159          {
160             log.error("Cannot removeTimerService", e);
161          }
162       }
163       
164       public void removeTimerService(ObjectName JavaDoc containerId, boolean keepState) throws IllegalStateException JavaDoc
165       {
166          try
167          {
168             mbeanEjbTimerService.removeTimerService(containerId, keepState);
169          }
170          catch (Exception JavaDoc e)
171          {
172             log.error("Cannot removeTimerService", e);
173          }
174       }
175       
176       public void restoreTimers(ObjectName JavaDoc containerId, ClassLoader JavaDoc loader) throws IllegalStateException JavaDoc
177       {
178          try
179          {
180             mbeanEjbTimerService.restoreTimers(containerId, loader);
181          }
182          catch (Exception JavaDoc e)
183          {
184             log.error("Cannot restoreTimer", e);
185          }
186       }
187    }
188 }
189
Popular Tags