1 21 22 package com.rift.coad.daemon.messageservice.named; 24 25 import java.rmi.Remote ; 27 import java.rmi.RemoteException ; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 import java.util.HashMap ; 31 import java.util.Map ; 32 import java.util.Date ; 33 import java.util.Set ; 34 import java.util.HashSet ; 35 import java.util.Vector ; 36 import java.util.concurrent.ConcurrentHashMap ; 37 import javax.naming.Context ; 38 import javax.naming.InitialContext ; 39 import javax.transaction.SystemException ; 40 import javax.transaction.UserTransaction ; 41 import javax.transaction.Status ; 42 import javax.transaction.xa.XAException ; 43 import javax.transaction.xa.XAResource ; 44 import javax.transaction.xa.Xid ; 45 46 import org.apache.log4j.Logger; 48 49 import org.hibernate.*; 51 import org.hibernate.cfg.*; 52 53 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 71 public class NamedQueueManagerImpl implements QueueManager { 72 73 74 private static NamedQueueManagerImpl singleton = null; 76 77 protected Logger log = 79 Logger.getLogger(NamedQueueManagerImpl.class.getName()); 80 81 private Map queues = new HashMap (); 83 84 87 public NamedQueueManagerImpl() throws MessageServiceException { 88 singleton = this; 89 } 90 91 92 101 public synchronized NamedQueue getNamedQueue(String name) throws RemoteException , 102 MessageServiceException { 103 try { 104 Boolean value = (Boolean )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 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 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 162 public synchronized boolean checkForNamedQueue(String name, boolean create) 163 throws MessageServiceException { 164 try { 165 Boolean value = (Boolean )queues.get(name); 166 if (value != null) { 167 return value.booleanValue(); 168 } 169 Session session = HibernateUtil. 170 getInstance(MessageServiceImpl.class).getSession(); 171 List 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 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 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 225 private void addCheckEntry(String name, boolean queueType) { 226 queues.put(name,new Boolean (queueType)); 227 } 228 } 229 | Popular Tags |