KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jms > jca > MessageListenerSpec


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jms.jca;
31
32 import com.caucho.log.Log;
33 import com.caucho.util.L10N;
34
35 import javax.jms.JMSException JavaDoc;
36 import javax.resource.ResourceException JavaDoc;
37 import javax.resource.spi.ActivationSpec JavaDoc;
38 import javax.resource.spi.InvalidPropertyException JavaDoc;
39 import javax.resource.spi.ResourceAdapter JavaDoc;
40 import javax.resource.spi.endpoint.MessageEndpointFactory JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.logging.Level JavaDoc;
43 import java.util.logging.Logger JavaDoc;
44
45 /**
46  * The JMS MessageListener configuration specification.
47  */

48 public class MessageListenerSpec implements ActivationSpec JavaDoc {
49   private static final L10N L = new L10N(MessageListenerSpec.class);
50   private static final Logger JavaDoc log = Log.open(MessageListenerSpec.class);
51
52   private ResourceAdapterImpl _ra;
53   
54   private MessageEndpointFactory JavaDoc _factory;
55
56   private ArrayList JavaDoc<MessageListenerTask> _endpoints =
57     new ArrayList JavaDoc<MessageListenerTask>();
58   
59   /**
60    * Returns the associated ResourceAdapter
61    */

62   public ResourceAdapter JavaDoc getResourceAdapter()
63   {
64     return _ra;
65   }
66   
67   /**
68    * Associate this JavaBean with a resource adapter java bean.
69    *
70    * This method is called exactly once.
71    */

72   public void setResourceAdapter(ResourceAdapter JavaDoc ra)
73     throws ResourceException JavaDoc
74   {
75     if (! (ra instanceof ResourceAdapterImpl))
76       throw new ResourceException JavaDoc(L.l("'{0}' is not a valid resource adapter for the JMS MessageListenerSpec",
77                       ra.getClass().getName()));
78
79     _ra = (ResourceAdapterImpl) ra;
80   }
81
82   /**
83    * Sets the endpoint factory.
84    */

85   void setEndpointFactory(MessageEndpointFactory JavaDoc factory)
86   {
87     _factory = factory;
88   }
89
90   /**
91    * Validates the configuration.
92    */

93   public void validate()
94     throws InvalidPropertyException JavaDoc
95   {
96   }
97
98   /**
99    * Starts the listener.
100    */

101   void start()
102     throws ResourceException JavaDoc
103   {
104     try {
105       ListenerEndpoint endpoint;
106       endpoint = (ListenerEndpoint) _factory.createEndpoint(null);
107
108       MessageListenerTask task = new MessageListenerTask(_ra, endpoint);
109
110       _endpoints.add(task);
111
112       _ra.getWorkManager().startWork(task);
113     } catch (JMSException JavaDoc e) {
114       throw new ResourceException JavaDoc(e);
115     }
116   }
117
118   /**
119    * Stops the listener.
120    */

121   void stop()
122     throws JMSException JavaDoc
123   {
124     for (int i = 0; i < _endpoints.size(); i++) {
125       MessageListenerTask task = _endpoints.get(i);
126
127       try {
128     task.release();
129       } catch (Throwable JavaDoc e) {
130     log.log(Level.WARNING, e.toString(), e);
131       }
132     }
133
134     _endpoints.clear();
135   }
136 }
137
138
Popular Tags