KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > timerservice > jboss > JBossTimerServiceFactory


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.timerservice.jboss;
23
24 import javax.ejb.TimerService JavaDoc;
25 import javax.management.ObjectName JavaDoc;
26
27 import org.jboss.ejb.txtimer.EJBTimerService;
28 import org.jboss.ejb3.EJBContainer;
29 import org.jboss.ejb3.timerservice.TimedObjectInvoker;
30 import org.jboss.ejb3.timerservice.TimerServiceFactory;
31 import org.jboss.logging.Logger;
32 import org.jboss.mx.util.MBeanProxyExt;
33 import org.jboss.mx.util.MBeanServerLocator;
34
35 /**
36  * Factory to create timer services which use the JBoss EJB Timer Service.
37  *
38  * @author <a HREF="mailto:carlo.dewolf@jboss.com">Carlo de Wolf</a>
39  * @version $Revision: $
40  */

41 public class JBossTimerServiceFactory extends TimerServiceFactory
42 {
43    private static Logger log = Logger.getLogger(JBossTimerServiceFactory.class);
44    
45    /* (non-Javadoc)
46     * @see org.jboss.ejb3.timerservice.TimerServiceFactory#createTimerService(javax.management.ObjectName, org.jboss.ejb3.timerservice.TimedObjectInvoker)
47     */

48    @Override JavaDoc
49    public TimerService JavaDoc createTimerService(ObjectName JavaDoc containerId, TimedObjectInvoker invoker)
50    {
51       TimerService JavaDoc timerService = null;
52       try
53       {
54          EJBTimerService service = getEJBTimerService();
55          TimerService JavaDoc delegate = service.createTimerService(containerId, null, invoker);
56          timerService = new TimerServiceFacade(containerId, delegate);
57       }
58       catch (Exception JavaDoc e)
59       {
60          //throw new EJBException("Could not create timer service", e);
61
if (log.isTraceEnabled())
62          {
63             log.trace("Unable to initialize timer service", e);
64          }
65          else
66          {
67             log.trace("Unable to initialize timer service");
68          }
69       }
70       return timerService;
71    }
72
73    protected EJBTimerService getEJBTimerService()
74    {
75       return (EJBTimerService) MBeanProxyExt.create(EJBTimerService.class, EJBTimerService.OBJECT_NAME, MBeanServerLocator.locateJBoss());
76    }
77    
78    /* (non-Javadoc)
79     * @see org.jboss.ejb3.timerservice.TimerServiceFactory#removeTimerService(javax.ejb.TimerService)
80     */

81    @Override JavaDoc
82    public void removeTimerService(TimerService JavaDoc timerService)
83    {
84       removeTimerService(((TimerServiceFacade) timerService).getContainerId());
85    }
86
87    protected void removeTimerService(ObjectName JavaDoc containerId)
88    {
89       try
90       {
91          EJBTimerService service = getEJBTimerService();
92          service.removeTimerService(containerId, true);
93       }
94       catch (Exception JavaDoc e)
95       {
96          //throw new EJBException("Could not remove timer service", e);
97
if (log.isTraceEnabled())
98          {
99             log.trace("Unable to initialize timer service", e);
100          }
101          else
102          {
103             log.trace("Unable to initialize timer service");
104          }
105       }
106    }
107    
108    public void restoreTimerService(TimerService JavaDoc aTimerService)
109    {
110       if (aTimerService == null)
111       {
112          log.warn("TIMER SERVICE IS NOT INSTALLED");
113          return;
114       }
115       TimerServiceFacade timerService = (TimerServiceFacade) aTimerService;
116       EJBContainer container = timerService.getContainer();
117       // FIXME: do not assume that a TimedObjectInvoker is an EJBContainer
118
ClassLoader JavaDoc loader = container.getClassloader();
119       
120       getEJBTimerService().restoreTimers(timerService.getContainerId(), loader);
121    }
122 }
123
Popular Tags