001: /*
002: * MessageService: The message service daemon
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * QueueManagerImpl.java
020: */
021:
022: // package path
023: package com.rift.coad.daemon.messageservice.named;
024:
025: // java imports
026: import java.rmi.Remote;
027: import java.rmi.RemoteException;
028: import java.util.ArrayList;
029: import java.util.List;
030: import java.util.HashMap;
031: import java.util.Map;
032: import java.util.Date;
033: import java.util.Set;
034: import java.util.HashSet;
035: import java.util.Vector;
036: import java.util.concurrent.ConcurrentHashMap;
037: import javax.naming.Context;
038: import javax.naming.InitialContext;
039: import javax.transaction.SystemException;
040: import javax.transaction.UserTransaction;
041: import javax.transaction.Status;
042: import javax.transaction.xa.XAException;
043: import javax.transaction.xa.XAResource;
044: import javax.transaction.xa.Xid;
045:
046: // logging import
047: import org.apache.log4j.Logger;
048:
049: // hibernate imports
050: import org.hibernate.*;
051: import org.hibernate.cfg.*;
052:
053: // coadunation imports
054: import com.rift.coad.daemon.messageservice.*;
055: import com.rift.coad.util.transaction.TransactionManager;
056: import com.rift.coad.util.lock.LockRef;
057: import com.rift.coad.util.lock.ObjectLockFactory;
058: import com.rift.coad.daemon.messageservice.db.*;
059: import com.rift.coad.hibernate.util.HibernateUtil;
060: import com.rift.coad.lib.ResourceIndex;
061: import com.rift.coad.lib.Resource;
062: import com.rift.coad.lib.bean.TransactionBeanCache;
063: import com.rift.coad.lib.cache.CacheRegistry;
064:
065: /**
066: * The implementation of the queue manager object.
067: *
068: * @author Brett Chaldecott
069: */
070: public class NamedQueueManagerImpl implements QueueManager {
071:
072: // the queue manager singleton method
073: private static NamedQueueManagerImpl singleton = null;
074:
075: // the logger reference
076: protected Logger log = Logger.getLogger(NamedQueueManagerImpl.class
077: .getName());
078:
079: // private member variables
080: private Map queues = new HashMap();
081:
082: /**
083: * Creates a new instance of QueueManagerImpl
084: */
085: public NamedQueueManagerImpl() throws MessageServiceException {
086: singleton = this ;
087: }
088:
089: /**
090: * This method returns the queue specified by the name. If the queue does
091: * not exist it gets created.
092: *
093: * @return The queue identified by the name.
094: * @param name The name of the queue to retrieve.
095: * @exception RemoteException
096: * @exception MessageServiceException
097: */
098: public synchronized NamedQueue getNamedQueue(String name)
099: throws RemoteException, MessageServiceException {
100: try {
101: Boolean value = (Boolean) queues.get(name);
102: if (value != null) {
103: if (value.booleanValue()) {
104: return new NamedQueueImpl(name);
105: } else {
106: log.error("This is not a named queue [" + name
107: + "].");
108: throw new MessageServiceException(
109: "This is not a named queue [" + name + "].");
110: }
111: }
112: Session session = HibernateUtil.getInstance(
113: MessageServiceManager.class).getSession();
114: List list = session.createQuery(
115: "FROM MessageQueue AS queue "
116: + "WHERE queue.messageQueueName = ?")
117: .setString(0, name).list();
118: NamedQueueImpl queue = new NamedQueueImpl(name);
119: if (list.size() == 1) {
120: com.rift.coad.daemon.messageservice.db.MessageQueue dbQueue = (com.rift.coad.daemon.messageservice.db.MessageQueue) list
121: .get(0);
122: if ((dbQueue.getNamed() == null)
123: || (dbQueue.getNamed() == 0)) {
124: addCheckEntry(name, false);
125: log.error("This is not a named queue [" + name
126: + "].");
127: throw new MessageServiceException(
128: "This is not a named queue [" + name + "].");
129: }
130: addCheckEntry(name, true);
131: return queue;
132: }
133: com.rift.coad.daemon.messageservice.db.MessageQueue dbQueue = new com.rift.coad.daemon.messageservice.db.MessageQueue(
134: name);
135: dbQueue.setNamed(1);
136: session.persist(dbQueue);
137: addCheckEntry(name, true);
138: return queue;
139: } catch (MessageServiceException ex) {
140: throw ex;
141: } catch (Exception ex) {
142: log.error("Failed to retrieve the named message queue ["
143: + name + "] : " + ex.getMessage(), ex);
144: throw new MessageServiceException(
145: "Failed to retrieve the named message queue ["
146: + name + "] : " + ex.getMessage(), ex);
147: }
148: }
149:
150: /**
151: * This method returns true if the queue with the specified name exists.
152: *
153: * @return TRUE if found, FALSE if not.
154: * @param name The name of the queue to check for.
155: * @exception MessageServiceException
156: */
157: public synchronized boolean checkForNamedQueue(String name,
158: boolean create) throws MessageServiceException {
159: try {
160: Boolean value = (Boolean) queues.get(name);
161: if (value != null) {
162: return value.booleanValue();
163: }
164: Session session = HibernateUtil.getInstance(
165: MessageServiceManager.class).getSession();
166: List list = session.createQuery(
167: "FROM MessageQueue AS queue "
168: + "WHERE queue.messageQueueName = ?")
169: .setString(0, name).list();
170: if (list.size() == 1) {
171: com.rift.coad.daemon.messageservice.db.MessageQueue queue = (com.rift.coad.daemon.messageservice.db.MessageQueue) list
172: .get(0);
173: if ((queue.getNamed() != null)
174: && (queue.getNamed() == 1)) {
175: addCheckEntry(name, true);
176: return true;
177: } else {
178: addCheckEntry(name, false);
179: return false;
180: }
181: } else if (create) {
182: com.rift.coad.daemon.messageservice.db.MessageQueue dbQueue = new com.rift.coad.daemon.messageservice.db.MessageQueue(
183: name);
184: dbQueue.setNamed(1);
185: session.persist(dbQueue);
186: addCheckEntry(name, true);
187: return true;
188: }
189: return false;
190: } catch (Exception ex) {
191: log.error("Failed to check for the named queue [" + name
192: + "] : " + ex.getMessage(), ex);
193: throw new MessageServiceException(
194: "Failed to check for the named queue [" + name
195: + "] : " + ex.getMessage(), ex);
196: }
197: }
198:
199: /**
200: * This method returns a reference to the singleton instance.
201: *
202: * @return A reference to the singleton instance.
203: * @exception MessageServiceException
204: */
205: public static synchronized NamedQueueManagerImpl getInstance()
206: throws MessageServiceException {
207: if (singleton == null) {
208: throw new MessageServiceException(
209: "Message service singleton not initialized");
210: }
211: return singleton;
212: }
213:
214: /**
215: * This method addes a check flag value to the memory queues list
216: *
217: * @param name The name to add.
218: * @praam the type of queue. TRUE if named
219: */
220: private void addCheckEntry(String name, boolean queueType) {
221: queues.put(name, new Boolean(queueType));
222: }
223: }
|