1 22 package org.objectweb.petals.binding.jms; 23 24 import javax.jms.Connection ; 25 import javax.jms.ConnectionFactory ; 26 import javax.jms.Destination ; 27 import javax.jms.JMSException ; 28 import javax.jms.JMSSecurityException ; 29 import javax.jms.MessageConsumer ; 30 import javax.jms.MessageProducer ; 31 import javax.jms.Session ; 32 import javax.naming.Context ; 33 import javax.naming.InitialContext ; 34 import javax.naming.NamingException ; 35 36 import org.objectweb.petals.component.common.PEtALSComponentSDKException; 37 import org.objectweb.petals.tools.jbicommon.util.StringHelper; 38 39 46 public class JMSConnection { 47 48 public enum Role { 49 JMS_PRODUCER, JMS_CONSUMER 50 }; 51 52 private Context context; 53 54 private String connectionFactoryName; 55 56 private String destinationName; 57 58 private String user; 59 60 private String password; 61 62 private Connection connection; 63 64 private Session session; 65 66 private Destination destination; 67 68 private MessageConsumer messageConsumer; 69 70 private MessageProducer messageProducer; 71 72 private boolean transacted; 73 74 private boolean started = false; 75 76 private Role role; 77 78 99 public JMSConnection(Context jndiContext, String connectionFactoryName, 100 String destinationName, boolean transacted, String user, 101 String password, Role role) { 102 this.context = jndiContext; 103 this.connectionFactoryName = connectionFactoryName; 104 this.destinationName = destinationName; 105 this.user = user; 106 this.password = password; 107 this.transacted = transacted; 108 this.role = role; 109 } 110 111 118 protected void init() throws PEtALSComponentSDKException { 119 120 checkJNDIAccess(context); 121 122 ConnectionFactory connFact = lookupConnectionFactory(context, 123 connectionFactoryName); 124 125 destination = lookupDestination(context, destinationName); 126 127 connection = createConnection(connFact, user, password); 128 129 try { 130 context.close(); 131 } catch (NamingException e) { 132 throw new PEtALSComponentSDKException("JNDI context exception.", e); 133 } 134 } 135 136 144 public void start() throws PEtALSComponentSDKException { 145 146 if (!started) { 147 148 init(); 149 150 try { 151 session = connection.createSession(transacted, 152 Session.AUTO_ACKNOWLEDGE); 153 154 if (Role.JMS_CONSUMER.equals(role)) { 155 try{ 156 messageConsumer = session.createConsumer(destination); 157 } catch (JMSSecurityException e) { 158 throw new PEtALSComponentSDKException("Can not start JMS consumer, READ right not granted.", e); 159 } 160 161 } else if (Role.JMS_PRODUCER.equals(role)) { 162 messageProducer = session.createProducer(destination); 163 164 } else { 165 throw new PEtALSComponentSDKException("Unknown JMS Role :" 166 + role); 167 168 } 169 connection.start(); 170 171 started = true; 172 173 } catch (JMSException e) { 174 throw new PEtALSComponentSDKException( 175 "Can not start the JMS consumer/producer.", e); 176 } 177 178 } else { 179 throw new PEtALSComponentSDKException( 180 "JMS connection already started for the '" + destinationName 181 + "' destination."); 182 183 } 184 } 185 186 192 public void stop() throws PEtALSComponentSDKException { 193 try { 194 if (messageConsumer != null) { 195 messageConsumer.close(); 196 messageConsumer = null; 197 } 198 if (messageProducer != null) { 199 messageProducer.close(); 200 messageProducer = null; 201 } 202 if (session != null) { 203 session.close(); 204 session = null; 205 } 206 if (connection != null) { 207 connection.close(); 208 connection = null; 209 } 210 started = false; 211 } catch (JMSException e) { 212 throw new PEtALSComponentSDKException( 213 "Can no stop the JMS connection.", e); 214 } 215 } 216 217 public MessageConsumer getMessageConsumer() { 218 return messageConsumer; 219 } 220 221 public MessageProducer getMessageProducer() { 222 return messageProducer; 223 } 224 225 public Session getSession() { 226 return session; 227 } 228 229 public Role getRole() { 230 return role; 231 } 232 233 public String getDestinationName() { 234 return destinationName; 235 } 236 237 public boolean isTransacted() { 238 return transacted; 239 } 240 241 249 private static void checkJNDIAccess(Context ctx) 250 throws PEtALSComponentSDKException { 251 try { 252 ctx.listBindings(""); 254 } catch (NamingException e) { 255 String providerUrl = "[JNDI provider URL not found]"; 256 try { 257 providerUrl = ctx.getEnvironment().get( 258 InitialContext.PROVIDER_URL).toString(); 259 } catch (NamingException e1) { 260 throw new PEtALSComponentSDKException( 261 "Exception during JNDI Context access.", e); 262 } 263 throw new PEtALSComponentSDKException( 264 "The JNDI can not be accessed at " + providerUrl); 265 } 266 } 267 268 279 private static ConnectionFactory lookupConnectionFactory(Context ctx, 280 String connectionFactoryName) throws PEtALSComponentSDKException { 281 ConnectionFactory connFact = null; 282 try { 283 connFact = (ConnectionFactory ) ctx.lookup(connectionFactoryName); 284 } catch (NamingException e) { 285 throw new PEtALSComponentSDKException("ConnectionFactory '" 286 + connectionFactoryName + "' not found in the JNDI context.", e); 287 } 288 return connFact; 289 } 290 291 302 private static Destination lookupDestination(Context ctx, 303 String destinationName) throws PEtALSComponentSDKException { 304 Destination dest = null; 305 try { 306 dest = (Destination ) ctx.lookup(destinationName); 307 } catch (NamingException e) { 308 throw new PEtALSComponentSDKException("Destination '" 309 + destinationName + "' not found in the JNDI context.", e); 310 } 311 return dest; 312 } 313 314 330 private static Connection createConnection( 331 ConnectionFactory connectionFactory, String user, String password) 332 throws PEtALSComponentSDKException { 333 Connection conn = null; 334 try { 335 if (StringHelper.isNullOrEmpty(user)) { 336 conn = connectionFactory.createConnection(); 337 } else { 338 conn = connectionFactory.createConnection(user, password); 339 } 340 341 } catch (JMSSecurityException e) { 342 throw new PEtALSComponentSDKException( 343 "Invalid user/password to connect to the JMS connectionFactory.", 344 e); 345 } catch (JMSException e) { 346 throw new PEtALSComponentSDKException( 347 "JMS connectionFactory error.", e); 348 } 349 return conn; 350 } 351 } | Popular Tags |