KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > mdb > ProducerManagerImpl


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.mdb;
23
24 import java.io.Externalizable JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.ObjectInput JavaDoc;
27 import java.io.ObjectOutput JavaDoc;
28 import java.io.Serializable JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import javax.jms.Connection JavaDoc;
32 import javax.jms.ConnectionFactory JavaDoc;
33 import javax.jms.Destination JavaDoc;
34 import javax.jms.JMSException JavaDoc;
35 import javax.jms.MessageProducer JavaDoc;
36 import javax.jms.ObjectMessage JavaDoc;
37 import javax.jms.Session JavaDoc;
38 import javax.naming.InitialContext JavaDoc;
39 import javax.naming.NamingException JavaDoc;
40 import org.jboss.annotation.ejb.DeliveryMode;
41 import org.jboss.annotation.ejb.MessageProperties;
42 import org.jboss.annotation.ejb.Producer;
43 import org.jboss.aop.advice.Interceptor;
44 import org.jboss.aop.joinpoint.Invocation;
45 import org.jboss.aop.joinpoint.MethodInvocation;
46 import org.jboss.ejb3.EJB3Util;
47 import org.jboss.logging.Logger;
48
49 /**
50  * comment
51  *
52  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
53  */

54 public class ProducerManagerImpl implements ProducerManager, Externalizable JavaDoc, Interceptor
55 {
56    private static final Logger log = Logger.getLogger(ProducerManagerImpl.class);
57    
58    private static final int PERSISTENT = javax.jms.DeliveryMode.PERSISTENT;
59    private static final int NON_PERSISTENT = javax.jms.DeliveryMode.NON_PERSISTENT;
60    
61    protected Producer JavaDoc producer;
62    protected Destination JavaDoc destination;
63    protected String JavaDoc factoryLookup;
64
65    protected int deliveryMode = javax.jms.DeliveryMode.PERSISTENT;
66    protected int timeToLive = 0;
67    protected int priority = 4;
68    protected HashMap JavaDoc methodMap;
69
70    protected transient ConnectionFactory JavaDoc factory;
71    protected transient Connection JavaDoc connection;
72    protected transient Session JavaDoc session;
73    protected transient MessageProducer JavaDoc msgProducer;
74    protected transient String JavaDoc username;
75    protected transient String JavaDoc password;
76    protected transient InitialContext JavaDoc initialContext;
77    protected Hashtable JavaDoc initialContextProperties;
78
79    public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
80    {
81       out.writeObject(producer);
82       out.writeObject(destination);
83       out.writeObject(factoryLookup);
84       out.writeInt(deliveryMode);
85       out.writeInt(timeToLive);
86       out.writeInt(priority);
87       out.writeObject(methodMap);
88       out.writeObject(initialContextProperties);
89       if (factoryLookup == null)
90       {
91          out.writeObject(factory);
92       }
93    }
94
95    public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
96    {
97       producer = (Producer JavaDoc) in.readObject();
98       destination = (Destination JavaDoc) in.readObject();
99       factoryLookup = (String JavaDoc) in.readObject();
100       deliveryMode = in.readInt();
101       timeToLive = in.readInt();
102       priority = in.readInt();
103       methodMap = (HashMap JavaDoc) in.readObject();
104       initialContextProperties = (Hashtable JavaDoc)in.readObject();
105       try
106       {
107          initialContext = EJB3Util.getInitialContext(initialContextProperties);
108       }
109       catch (NamingException JavaDoc e)
110       {
111          throw new RuntimeException JavaDoc(e);
112       }
113       if (factoryLookup != null)
114       {
115          try
116          {
117             factory = (ConnectionFactory JavaDoc) initialContext.lookup(factoryLookup);
118          }
119          catch (NamingException JavaDoc e)
120          {
121             throw new RuntimeException JavaDoc(e);
122          }
123       }
124       else
125       {
126          factory = (ConnectionFactory JavaDoc) in.readObject();
127       }
128    }
129
130    public ProducerManagerImpl(Producer JavaDoc producer, Destination JavaDoc destination, ConnectionFactory JavaDoc factory, DeliveryMode deliveryMode, int timeToLive, int priority, HashMap JavaDoc methodMap, Hashtable JavaDoc initialContextProperties)
131    {
132       this.initialContextProperties = initialContextProperties;
133       try
134       {
135          this.initialContext = EJB3Util.getInitialContext(initialContextProperties);
136       }
137       catch (NamingException JavaDoc e)
138       {
139          throw new RuntimeException JavaDoc(e);
140       }
141       this.producer = producer;
142       this.destination = destination;
143       this.factory = factory;
144       
145       int mode = deliveryMode.ordinal();
146       switch (mode)
147       {
148          case PERSISTENT:
149             this.deliveryMode = javax.jms.DeliveryMode.PERSISTENT;
150             break;
151          case NON_PERSISTENT:
152             this.deliveryMode = javax.jms.DeliveryMode.NON_PERSISTENT;
153             break;
154       }
155       this.timeToLive = timeToLive;
156       this.priority = priority;
157       this.methodMap = methodMap;
158    }
159
160    public ProducerManagerImpl(Producer JavaDoc producer, Destination JavaDoc destination, String JavaDoc factory, DeliveryMode deliveryMode, int timeToLive, int priority, HashMap JavaDoc methodMap, Hashtable JavaDoc initialContextProperties)
161    {
162       this.initialContextProperties = initialContextProperties;
163       try
164       {
165          this.initialContext = EJB3Util.getInitialContext(initialContextProperties);
166       }
167       catch (NamingException JavaDoc e)
168       {
169          throw new RuntimeException JavaDoc(e);
170       }
171       this.producer = producer;
172       this.destination = destination;
173       this.factoryLookup = factory;
174       
175       int mode = deliveryMode.ordinal();
176       switch (mode)
177       {
178          case PERSISTENT:
179             this.deliveryMode = javax.jms.DeliveryMode.PERSISTENT;
180             break;
181          case NON_PERSISTENT:
182             this.deliveryMode = javax.jms.DeliveryMode.NON_PERSISTENT;
183             break;
184       }
185       this.timeToLive = timeToLive;
186       this.priority = priority;
187       this.methodMap = methodMap;
188    }
189
190
191    public ProducerManagerImpl()
192    {
193    }
194
195    public void setUsername(String JavaDoc user)
196    {
197       this.username = user;
198    }
199
200    public void setPassword(String JavaDoc passwd)
201    {
202       this.password = passwd;
203    }
204
205    public void connect() throws JMSException JavaDoc
206    {
207       if (factory == null)
208       {
209          try
210          {
211             factory = (ConnectionFactory JavaDoc) initialContext.lookup(factoryLookup);
212          }
213          catch (NamingException JavaDoc e)
214          {
215             throw new RuntimeException JavaDoc(e);
216          }
217       }
218       if (connection != null) return;
219       if (username != null)
220       {
221          connection = factory.createConnection(username, password);
222       }
223       else
224       {
225          connection = factory.createConnection();
226       }
227       session = connection.createSession(producer.transacted(), producer.acknowledgeMode());
228       msgProducer = session.createProducer(destination);
229       msgProducer.setDeliveryMode(deliveryMode);
230       msgProducer.setTimeToLive(timeToLive);
231       msgProducer.setPriority(priority);
232    }
233
234    public void close() throws JMSException JavaDoc
235    {
236       msgProducer.close();
237       msgProducer = null;
238       session.close();
239       session = null;
240       connection.close();
241       connection = null;
242    }
243
244    public void commit() throws JMSException JavaDoc
245    {
246       session.commit();
247    }
248
249    public void rollback() throws JMSException JavaDoc
250    {
251       session.rollback();
252    }
253
254    public String JavaDoc getName()
255    {
256       return ProducerManager.class.getName();
257    }
258
259    public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
260    {
261       if (session == null)
262       {
263          throw new RuntimeException JavaDoc("You must call connect() on the producer. The JMS session has not been set");
264       }
265       ObjectMessage JavaDoc msg = session.createObjectMessage((Serializable JavaDoc) invocation);
266       MethodInvocation mi = (MethodInvocation) invocation;
267       MessageProperties props = (MessageProperties)methodMap.get(new Long JavaDoc(mi.getMethodHash()));
268       if (props != null)
269       {
270          int del = (props.delivery() == DeliveryMode.PERSISTENT) ? javax.jms.DeliveryMode.PERSISTENT : javax.jms.DeliveryMode.NON_PERSISTENT;
271          msgProducer.send(msg, del, props.priority(), props.timeToLive());
272       }
273       else
274       {
275          msgProducer.send(msg);
276       }
277       return null;
278    }
279 }
280
Popular Tags