KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > daemon > messageservice > named > NamedQueueManagerImpl


1 /*
2  * MessageService: The message service daemon
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * QueueManagerImpl.java
20  */

21
22 // package path
23
package com.rift.coad.daemon.messageservice.named;
24
25 // java imports
26
import java.rmi.Remote JavaDoc;
27 import java.rmi.RemoteException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Date JavaDoc;
33 import java.util.Set JavaDoc;
34 import java.util.HashSet JavaDoc;
35 import java.util.Vector JavaDoc;
36 import java.util.concurrent.ConcurrentHashMap JavaDoc;
37 import javax.naming.Context JavaDoc;
38 import javax.naming.InitialContext JavaDoc;
39 import javax.transaction.SystemException JavaDoc;
40 import javax.transaction.UserTransaction JavaDoc;
41 import javax.transaction.Status JavaDoc;
42 import javax.transaction.xa.XAException JavaDoc;
43 import javax.transaction.xa.XAResource JavaDoc;
44 import javax.transaction.xa.Xid JavaDoc;
45
46 // logging import
47
import org.apache.log4j.Logger;
48
49 // hibernate imports
50
import org.hibernate.*;
51 import org.hibernate.cfg.*;
52
53 // coadunation imports
54
import com.rift.coad.daemon.messageservice.*;
55 import com.rift.coad.util.transaction.TransactionManager;
56 import com.rift.coad.util.lock.LockRef;
57 import com.rift.coad.util.lock.ObjectLockFactory;
58 import com.rift.coad.daemon.messageservice.db.*;
59 import com.rift.coad.hibernate.util.HibernateUtil;
60 import com.rift.coad.lib.ResourceIndex;
61 import com.rift.coad.lib.Resource;
62 import com.rift.coad.lib.bean.TransactionBeanCache;
63 import com.rift.coad.lib.cache.CacheRegistry;
64
65
66 /**
67  * The implementation of the queue manager object.
68  *
69  * @author Brett Chaldecott
70  */

71 public class NamedQueueManagerImpl implements QueueManager {
72     
73     
74     // the queue manager singleton method
75
private static NamedQueueManagerImpl singleton = null;
76     
77     // the logger reference
78
protected Logger log =
79             Logger.getLogger(NamedQueueManagerImpl.class.getName());
80     
81     // private member variables
82
private Map JavaDoc queues = new HashMap JavaDoc();
83     
84     /**
85      * Creates a new instance of QueueManagerImpl
86      */

87     public NamedQueueManagerImpl() throws MessageServiceException {
88         singleton = this;
89     }
90     
91     
92     /**
93      * This method returns the queue specified by the name. If the queue does
94      * not exist it gets created.
95      *
96      * @return The queue identified by the name.
97      * @param name The name of the queue to retrieve.
98      * @exception RemoteException
99      * @exception MessageServiceException
100      */

101     public synchronized NamedQueue getNamedQueue(String JavaDoc name) throws RemoteException JavaDoc,
102             MessageServiceException {
103         try {
104             Boolean JavaDoc value = (Boolean JavaDoc)queues.get(name);
105             if (value != null) {
106                 if (value.booleanValue()) {
107                     return new NamedQueueImpl(name);
108                 } else {
109                     log.error("This is not a named queue [" + name +
110                             "].");
111                     throw new MessageServiceException
112                             ("This is not a named queue [" + name +
113                             "].");
114                 }
115             }
116             Session session = HibernateUtil.
117                     getInstance(MessageServiceImpl.class).getSession();
118             List JavaDoc list = session.createQuery("FROM MessageQueue AS queue " +
119                     "WHERE queue.messageQueueName = ?").setString(0,name).list();
120             NamedQueueImpl queue = new NamedQueueImpl(name);
121             if (list.size() == 1) {
122                 com.rift.coad.daemon.messageservice.db.MessageQueue dbQueue =
123                         (com.rift.coad.daemon.messageservice.db.MessageQueue)
124                         list.get(0);
125                 if ((dbQueue.getNamed() == null) ||
126                         (dbQueue.getNamed() == 0)) {
127                     addCheckEntry(name, false);
128                     log.error("This is not a named queue [" + name +
129                             "].");
130                     throw new MessageServiceException
131                             ("This is not a named queue [" + name +
132                             "].");
133                 }
134                 addCheckEntry(name, true);
135                 return queue;
136             }
137             com.rift.coad.daemon.messageservice.db.MessageQueue dbQueue = new
138                     com.rift.coad.daemon.messageservice.db.MessageQueue(name);
139             dbQueue.setNamed(1);
140             session.persist(dbQueue);
141             addCheckEntry(name, true);
142             return queue;
143         } catch (MessageServiceException ex) {
144             throw ex;
145         } catch (Exception JavaDoc ex) {
146             log.error("Failed to retrieve the named message queue [" +
147                     name + "] : " + ex.getMessage(),ex);
148             throw new MessageServiceException
149                     ("Failed to retrieve the named message queue [" +
150                     name + "] : " + ex.getMessage(),ex);
151         }
152     }
153     
154     
155     /**
156      * This method returns true if the queue with the specified name exists.
157      *
158      * @return TRUE if found, FALSE if not.
159      * @param name The name of the queue to check for.
160      * @exception MessageServiceException
161      */

162     public synchronized boolean checkForNamedQueue(String JavaDoc name, boolean create)
163     throws MessageServiceException {
164         try {
165             Boolean JavaDoc value = (Boolean JavaDoc)queues.get(name);
166             if (value != null) {
167                 return value.booleanValue();
168             }
169             Session session = HibernateUtil.
170                     getInstance(MessageServiceImpl.class).getSession();
171             List JavaDoc list = session.createQuery("FROM MessageQueue AS queue " +
172                     "WHERE queue.messageQueueName = ?").setString(0,name).list();
173             if (list.size() == 1) {
174                 com.rift.coad.daemon.messageservice.db.MessageQueue queue =
175                         (com.rift.coad.daemon.messageservice.db.MessageQueue)
176                         list.get(0);
177                 if ((queue.getNamed() != null) && (queue.getNamed() == 1)) {
178                     addCheckEntry(name, true);
179                     return true;
180                 } else {
181                     addCheckEntry(name, false);
182                     return false;
183                 }
184             } else if (create) {
185                 com.rift.coad.daemon.messageservice.db.MessageQueue dbQueue = new
186                         com.rift.coad.daemon.messageservice.db.MessageQueue(name);
187                 dbQueue.setNamed(1);
188                 session.persist(dbQueue);
189                 addCheckEntry(name, true);
190                 return true;
191             }
192             return false;
193         } catch (Exception JavaDoc ex) {
194             log.error("Failed to check for the named queue [" +
195                     name + "] : " + ex.getMessage(),ex);
196             throw new MessageServiceException
197                     ("Failed to check for the named queue [" +
198                     name + "] : " + ex.getMessage(),ex);
199         }
200     }
201     
202     
203     /**
204      * This method returns a reference to the singleton instance.
205      *
206      * @return A reference to the singleton instance.
207      * @exception MessageServiceException
208      */

209     public static synchronized NamedQueueManagerImpl getInstance() throws
210             MessageServiceException {
211         if (singleton == null) {
212             throw new MessageServiceException(
213                     "Message service singleton not initialized");
214         }
215         return singleton;
216     }
217     
218     
219     /**
220      * This method addes a check flag value to the memory queues list
221      *
222      * @param name The name to add.
223      * @praam the type of queue. TRUE if named
224      */

225     private void addCheckEntry(String JavaDoc name, boolean queueType) {
226         queues.put(name,new Boolean JavaDoc(queueType));
227     }
228 }
229
Popular Tags