1 25 26 27 package org.objectweb.jonas_jms; 28 29 import java.util.StringTokenizer ; 30 import java.util.Vector ; 31 32 import javax.jms.ConnectionFactory ; 33 import javax.jms.Queue ; 34 import javax.jms.QueueConnectionFactory ; 35 import javax.jms.Topic ; 36 import javax.jms.TopicConnectionFactory ; 37 import javax.jms.XAConnectionFactory ; 38 import javax.jms.XAQueueConnectionFactory ; 39 import javax.jms.XATopicConnectionFactory ; 40 import javax.naming.InitialContext ; 41 import javax.naming.NamingException ; 42 43 import org.objectweb.common.Env; 44 import org.objectweb.jonas_jms.api.JmsAdministration; 45 import org.objectweb.joram.client.jms.admin.AdminModule; 46 import org.objectweb.joram.client.jms.admin.User; 47 import org.objectweb.joram.client.jms.local.XALocalConnectionFactory; 48 import org.objectweb.joram.client.jms.local.XAQueueLocalConnectionFactory; 49 import org.objectweb.joram.client.jms.local.XATopicLocalConnectionFactory; 50 import org.objectweb.joram.client.jms.tcp.QueueTcpConnectionFactory; 51 import org.objectweb.joram.client.jms.tcp.TcpConnectionFactory; 52 import org.objectweb.joram.client.jms.tcp.TopicTcpConnectionFactory; 53 import org.objectweb.joram.client.jms.tcp.XAQueueTcpConnectionFactory; 54 import org.objectweb.joram.client.jms.tcp.XATcpConnectionFactory; 55 import org.objectweb.joram.client.jms.tcp.XATopicTcpConnectionFactory; 56 import org.objectweb.util.monolog.api.BasicLevel; 57 58 import fr.dyade.aaa.agent.AgentServer; 59 60 72 public class JmsAdminForJoram implements JmsAdministration { 73 74 private static String QUEUE_CONN_FACT_NAME="JQCF"; 75 private static String TOPIC_CONN_FACT_NAME="JTCF"; 76 private static String CONN_FACT_NAME="JCF"; 77 private boolean collocatedmom = false; 78 private InitialContext ictx = null; 79 private Vector namelist = new Vector (); 80 private XAConnectionFactory xacf = null; 81 private XATopicConnectionFactory xatcf = null; 82 private XAQueueConnectionFactory xaqcf = null; 83 private ConnectionFactory jcf = null; 84 private TopicConnectionFactory jtcf = null; 85 private QueueConnectionFactory jqcf = null; 86 private org.objectweb.joram.client.jms.admin.User user = null; 87 private org.objectweb.joram.client.jms.admin.User jonasUser = null; 88 private String host = null; 89 private int port; 90 91 95 public JmsAdminForJoram() { 96 } 97 98 102 112 private void startMOM() throws Exception { 113 TraceJms.logger.log(BasicLevel.DEBUG,""); 114 115 try { 116 String TRANSACTION_PROP = "Transaction"; 118 String s1 = System.getProperty(TRANSACTION_PROP); 119 if (s1 == null) { 120 System.setProperty(TRANSACTION_PROP, "fr.dyade.aaa.util.NullTransaction"); 121 } 122 123 String A3CONF_DIR_PROP = "fr.dyade.aaa.agent.A3CONF_DIR"; 125 String a3config_dir = System.getProperty(A3CONF_DIR_PROP); 126 if (a3config_dir == null) { 127 System.setProperty(A3CONF_DIR_PROP, System.getProperty("jonas.base") 128 + System.getProperty("file.separator") + "conf"); 129 } 130 131 if(TraceJms.isDebug()) 132 TraceJms.logger.log(BasicLevel.DEBUG,"a3config_dir= "+a3config_dir); 133 short serverId = 0; 137 AgentServer.init(serverId,"s0", TraceJms.getLoggerFactory()); 138 AgentServer.start(); 139 140 Thread.sleep(10); 142 143 short sid = AgentServer.getServerId(); host = AgentServer.getHostname(sid); port = 16010; 148 try { 149 String s = AgentServer.getServiceArgs(sid, "org.objectweb.joram.mom.proxies.tcp.TcpProxyService"); 150 StringTokenizer st = new StringTokenizer (s); 151 port = Integer.parseInt(st.nextToken()); 152 } catch (NumberFormatException exc) { 153 TraceJms.logger.log(BasicLevel.ERROR,"exception: "+exc); 154 } catch (Exception e) { 155 TraceJms.logger.log(BasicLevel.ERROR,"MOM exception:"+e); 156 throw e; 157 } 158 TraceJms.logger.log(BasicLevel.INFO, "starting MOM on host " + host + ", port " + port); 159 160 } catch (Exception ex) { 161 TraceJms.logger.log(BasicLevel.ERROR,"exception: "+ex); 162 throw ex; 163 } 164 } 165 166 170 179 public void start(boolean collocated, String url) throws Exception { 180 181 if (collocated) { 183 collocatedmom = true; 184 startMOM(); 185 } 186 187 ictx = new InitialContext (); 189 190 if (host != null) { 192 TraceJms.logger.log(BasicLevel.INFO, "starting JmsAdmin with host " + host + ", port " + port); 194 AdminModule.collocatedConnect("root", "root"); 195 } else if (url != null && url.length() > 0) { 196 int indexOfHost = url.indexOf("//") + 2; 198 int indexOfPort = url.indexOf(":", indexOfHost) + 1; 199 host = url.substring(indexOfHost, indexOfPort - 1); 200 port = Integer.parseInt(url.substring(indexOfPort)); 201 TraceJms.logger.log(BasicLevel.WARN, "starting JmsAdmin with host " + host + ", port " + port); 202 AdminModule.connect(host, port, "root", "root", 100); 203 } else { 204 TraceJms.logger.log(BasicLevel.WARN, "starting JmsAdmin with default url"); 206 AdminModule.connect("root", "root", 100); 207 } 208 209 user = User.create("anonymous", "anonymous"); 210 jonasUser = User.create("jonas", "jonas"); 211 212 if (collocated) { 214 xacf = XALocalConnectionFactory.create(); 215 xatcf = XATopicLocalConnectionFactory.create(); 216 xaqcf = XAQueueLocalConnectionFactory.create(); 217 } 218 else { 219 xacf = XATcpConnectionFactory.create(host, port); 220 xatcf = XATopicTcpConnectionFactory.create(host, port); 221 xaqcf = XAQueueTcpConnectionFactory.create(host, port); 222 } 223 224 225 String name = CONN_FACT_NAME; 227 try { 228 jcf = TcpConnectionFactory.create(host, port); 229 ictx.rebind(name, jcf); 230 namelist.addElement(name); 231 } catch (NamingException e) { 232 TraceJms.logger.log(BasicLevel.ERROR,"cannot register "+name); 233 } 234 235 name = QUEUE_CONN_FACT_NAME; 236 try { 237 jqcf = QueueTcpConnectionFactory.create(host, port); 238 ictx.rebind(name, jqcf); 239 namelist.addElement(name); 240 } catch (NamingException e) { 241 TraceJms.logger.log(BasicLevel.ERROR,"cannot register "+name); 242 } 243 244 name = TOPIC_CONN_FACT_NAME; 245 try { 246 jtcf = TopicTcpConnectionFactory.create(host, port); 247 ictx.rebind(name, jtcf); 248 namelist.addElement(name); 249 } catch (NamingException e) { 250 TraceJms.logger.log(BasicLevel.ERROR,"cannot register "+name); 251 } 252 } 253 254 257 public void stop() { 258 if ( Env.isJAVA4() ){ 259 try { 261 AdminModule.disconnect(); 262 } catch (Exception ex) { 263 TraceJms.logger.log(BasicLevel.ERROR, "problem for closing admin object " + ex); 264 } 265 if (collocatedmom) { 266 AgentServer.stop(); 267 if(TraceJms.isDebug()) 268 TraceJms.logger.log(BasicLevel.DEBUG, "agentServer stopped"); 269 } 270 } 271 try { 273 ictx.unbind(CONN_FACT_NAME); 274 ictx.unbind(QUEUE_CONN_FACT_NAME); 275 ictx.unbind(TOPIC_CONN_FACT_NAME); 276 if(TraceJms.isDebug()) 277 TraceJms.logger.log(BasicLevel.DEBUG,"unbind connection factories"); 278 } catch(Exception ex) { 279 TraceJms.logger.log(BasicLevel.ERROR,"cannot unbind connection factories"); 280 } 281 } 282 283 284 287 public XAConnectionFactory getXAConnectionFactory() { 288 return xacf; 289 } 290 291 294 public XATopicConnectionFactory getXATopicConnectionFactory() { 295 return xatcf; 296 } 297 298 301 public XAQueueConnectionFactory getXAQueueConnectionFactory() { 302 return xaqcf; 303 } 304 305 306 309 public Queue createQueue(String name) throws Exception { 310 TraceJms.logger.log(BasicLevel.DEBUG,""); 311 org.objectweb.joram.client.jms.Queue queue = null; 312 try { 313 queue = (org.objectweb.joram.client.jms.Queue) ictx.lookup(name); 314 } catch (Exception e) { 315 queue = org.objectweb.joram.client.jms.Queue.create(name); 316 ictx.rebind(name, queue); 317 } 318 queue.setWriter(user); 319 queue.setReader(user); 320 queue.setWriter(jonasUser); 321 queue.setReader(jonasUser); 322 return queue; 323 } 324 325 328 public Topic createTopic(String name) throws Exception { 329 TraceJms.logger.log(BasicLevel.DEBUG,""); 330 org.objectweb.joram.client.jms.Topic topic = null; 331 try { 332 topic = (org.objectweb.joram.client.jms.Topic) ictx.lookup(name); 333 } catch (Exception e) { 334 topic = org.objectweb.joram.client.jms.Topic.create(name); 335 ictx.rebind(name, topic); 336 } 337 topic.setWriter(user); 338 topic.setReader(user); 339 topic.setWriter(jonasUser); 340 topic.setReader(jonasUser); 341 return topic; 342 } 343 344 347 public void deleteDestination(String name) throws Exception { 348 TraceJms.logger.log(BasicLevel.DEBUG,""); 349 try { 350 org.objectweb.joram.client.jms.Destination dest = (org.objectweb.joram.client.jms.Destination) ictx.lookup(name); 351 dest.delete(); 352 } catch (Exception e) { 353 } 354 } 355 356 361 public int getPendingMessages(javax.jms.Queue queue) throws Exception { 362 return ((org.objectweb.joram.client.jms.Queue) queue).getPendingMessages(); 363 } 364 365 370 public int getPendingRequests(javax.jms.Queue queue) throws Exception { 371 return ((org.objectweb.joram.client.jms.Queue) queue).getPendingRequests(); 372 } 373 374 379 public int getSubscriptions(javax.jms.Topic topic) throws Exception { 380 return ((org.objectweb.joram.client.jms.Topic) topic).getSubscriptions(); 381 } 382 } 383 | Popular Tags |