1 25 26 package org.objectweb.easybeans.component.joram; 27 28 import java.net.ConnectException ; 29 import java.util.ArrayList ; 30 import java.util.List ; 31 import java.util.Properties ; 32 33 import javax.jms.ConnectionFactory ; 34 import javax.jms.QueueConnectionFactory ; 35 import javax.jms.TopicConnectionFactory ; 36 import javax.naming.InitialContext ; 37 import javax.naming.NamingException ; 38 import javax.resource.ResourceException ; 39 import javax.resource.spi.ActivationSpec ; 40 import javax.resource.spi.BootstrapContext ; 41 import javax.resource.spi.ResourceAdapter ; 42 import javax.resource.spi.ResourceAdapterInternalException ; 43 import javax.resource.spi.XATerminator ; 44 import javax.transaction.TransactionManager ; 45 import javax.transaction.xa.XAException ; 46 47 import org.objectweb.easybeans.component.api.EZBComponentException; 48 import org.objectweb.easybeans.component.itf.JMSComponent; 49 import org.objectweb.easybeans.jca.workmanager.ResourceWorkManager; 50 import org.objectweb.easybeans.log.JLog; 51 import org.objectweb.easybeans.log.JLogFactory; 52 import org.objectweb.easybeans.transaction.JTransactionManager; 53 import org.objectweb.joram.client.connector.ActivationSpecImpl; 54 import org.objectweb.joram.client.connector.JoramAdapter; 55 import org.objectweb.joram.client.jms.ConnectionMetaData; 56 import org.objectweb.joram.client.jms.admin.AdminException; 57 import org.objectweb.joram.client.jms.admin.AdminModule; 58 import org.objectweb.joram.client.jms.tcp.QueueTcpConnectionFactory; 59 import org.objectweb.joram.client.jms.tcp.TcpConnectionFactory; 60 import org.objectweb.joram.client.jms.tcp.TopicTcpConnectionFactory; 61 import org.objectweb.joram.mom.proxies.ConnectionManager; 62 import org.objectweb.joram.mom.proxies.tcp.TcpProxyService; 63 import org.objectweb.jotm.Current; 64 import org.objectweb.util.monolog.Monolog; 65 66 import fr.dyade.aaa.agent.AgentServer; 67 import fr.dyade.aaa.agent.ServerConfigHelper; 68 import fr.dyade.aaa.util.NullTransaction; 69 70 75 public class JoramComponent implements JMSComponent { 76 77 80 private static JLog logger = JLogFactory.getLog(JoramComponent.class); 81 82 83 86 private static final String QUEUE_CONN_FACT_NAME="JQCF"; 87 88 91 private static final String TOPIC_CONN_FACT_NAME="JTCF"; 92 93 96 private static final String CONN_FACT_NAME="JCF"; 97 98 101 private static final int DEFAULT_PORT_NUMBER = 16010; 102 103 106 private int port = DEFAULT_PORT_NUMBER; 107 108 111 private static final String DEFAULT_HOST_NAME = "localhost"; 112 113 116 private String host = DEFAULT_HOST_NAME; 117 118 121 private static final short ID = 0; 122 123 127 private static final String TRANSACTION_PROPERTY = "Transaction"; 128 129 132 private static final String DEFAULT_PERSISTENCE_DIRECTORY = "joram-persistence-s" + ID; 133 134 137 private boolean started = false; 138 139 142 private static final int MIN_THREADS = 1; 143 144 147 private static final int MAX_THREADS = 2; 148 149 152 private static final int THREAD_TIMEOUT = 20; 153 154 157 private JoramAdapter joramAdapter = null; 158 159 162 private List <String > topics = null; 163 164 167 private List <String > queues = null; 168 169 172 private ResourceWorkManager workManager = null; 173 174 177 private InitialContext ictx = null; 178 179 182 public JoramComponent() { 183 topics = new ArrayList <String >(); 184 queues = new ArrayList <String >(); 185 } 186 187 188 193 public void init() throws EZBComponentException { 194 195 } 196 197 201 public void start() throws EZBComponentException { 202 203 Properties properties = new Properties (); 205 properties.put(Monolog.MONOLOG_CLASS_NAME, "org.objectweb.util.monolog.wrapper.javaLog.LoggerFactory"); 206 properties.put("logger.org.objectweb.joram.level", "ERROR"); 207 properties.put("logger.com.scalagent.level", "ERROR"); 208 properties.put("logger.fr.dyade.aaa.level", "ERROR"); 209 Monolog.init(properties); 210 211 212 try { 214 ictx = new InitialContext (); 215 } catch (NamingException e) { 216 throw new EZBComponentException("Cannot create an initial context.", e); 217 } 218 219 220 TransactionManager transactionManager = JTransactionManager.getTransactionManager(); 221 workManager = new ResourceWorkManager(transactionManager, MIN_THREADS, MAX_THREADS, THREAD_TIMEOUT); 222 223 XATerminator xaTerminator = null; 225 try { 226 xaTerminator = ((Current) transactionManager).getXATerminator(); 227 } catch (XAException e) { 228 throw new EZBComponentException("Cannot get the XA terminator", e); 229 } 230 231 BootstrapContext bootstrapContext = new JoramBootstrapContext(workManager, xaTerminator); 232 233 joramAdapter = new JoramAdapter(); 235 236 joramAdapter.setHostName(host); 238 joramAdapter.setServerPort(new Integer (port)); 239 240 joramAdapter.setCollocatedServer(Boolean.FALSE); 242 243 System.setProperty(TRANSACTION_PROPERTY, NullTransaction.class.getName()); 245 246 try { 248 AgentServer.init(ID, DEFAULT_PERSISTENCE_DIRECTORY, null); 249 } catch (Exception e) { 250 throw new EZBComponentException("Cannot initialize a new collocated Joram server", e); 251 } 252 253 254 try { 256 AgentServer.start(); 257 } catch (Exception e) { 258 throw new EZBComponentException("Cannot start collocated Joram server", e); 259 } 260 261 try { 263 new ServerConfigHelper(false).addService(ID, ConnectionManager.class.getName(), "root root"); 264 } catch (Exception e) { 265 throw new EZBComponentException("Cannot add connection manager service", e); 266 } 267 try { 269 new ServerConfigHelper(false).addService(ID, TcpProxyService.class.getName(), String.valueOf(port)); 270 } catch (Exception e) { 271 throw new EZBComponentException("Cannot add TcpProxy service", e); 272 } 273 274 275 276 try { 278 connectToCollocated(); 279 } catch (JoramException e) { 280 throw new EZBComponentException("Cannot connect to the collocated server", e); 281 } 282 283 284 try { 286 joramAdapter.createUser("anonymous", "anonymous"); 287 } catch (AdminException e) { 288 throw new EZBComponentException("Cannot create anonymous user", e); 289 290 } 291 292 293 try { 295 joramAdapter.start(bootstrapContext); 296 } catch (ResourceAdapterInternalException e) { 297 throw new EZBComponentException("Cannot start the resource adapter of JORAM", e); 298 } 299 300 try { 302 createConnectionFactories(); 303 } catch (JoramException e) { 304 throw new EZBComponentException("Cannot create connection factories", e); 305 } 306 307 308 try { 310 createInitialTopics(); 311 } catch (JoramException e) { 312 throw new EZBComponentException("Cannot create initial topics", e); 313 } 314 315 try { 316 createInitialQueues(); 317 } catch (JoramException e) { 318 throw new EZBComponentException("Cannot create initial queues", e); 319 } 320 321 ActivationSpec activationSpec = new ActivationSpecImpl(); 323 try { 324 activationSpec.setResourceAdapter(joramAdapter); 325 } catch (ResourceException e) { 326 throw new EZBComponentException("Cannot set resource adapter on activation spec object", e); 327 } 328 try { 329 ictx.rebind("joramActivationSpec", activationSpec); 330 } catch (NamingException e) { 331 throw new EZBComponentException("Cannot bind activation spec object", e); 332 } 333 334 logger.info("Joram version ''{0}'' started on {1}:{2}.", ConnectionMetaData.providerVersion, host, String.valueOf(port)); 335 started = true; 336 } 337 338 342 public void stop() throws EZBComponentException { 343 if (!started) { 344 throw new IllegalStateException ("Cannot stop a server as it was not started"); 345 } 346 347 joramAdapter.stop(); 349 350 disconnectFromCollocated(); 352 353 AgentServer.stop(); 355 356 workManager.stopThreads(); 358 } 359 360 365 private void connectToCollocated() throws JoramException { 366 try { 367 AdminModule.collocatedConnect("root", "root"); 368 } catch (ConnectException e) { 369 throw new JoramException("Cannot connect to the collocated server for the administration", e); 370 } catch (AdminException e) { 371 throw new JoramException("Cannot connect to the collocated server for the administration", e); 372 } 373 } 374 375 381 private void disconnectFromCollocated() { 382 AdminModule.disconnect(); 383 } 384 385 389 private void createConnectionFactories() throws JoramException { 390 joramAdapter.createCF("CF"); 392 joramAdapter.createQCF("QCF"); 393 joramAdapter.createTCF("TCF"); 394 395 ConnectionFactory jcf = null; 397 TopicConnectionFactory jtcf = null; 398 QueueConnectionFactory jqcf = null; 399 400 String name = CONN_FACT_NAME; 401 try { 402 jcf = TcpConnectionFactory.create(host, port); 403 ictx.rebind(name, jcf); 404 } catch (NamingException e) { 405 throw new JoramException("Cannot create a factory with the name '" + name + "'.", e); 406 } 407 408 name = QUEUE_CONN_FACT_NAME; 409 try { 410 jqcf = QueueTcpConnectionFactory.create(host, port); 411 ictx.rebind(name, jqcf); 412 } catch (NamingException e) { 413 throw new JoramException("Cannot create a factory with the name '" + name + "'.", e); 414 } 415 416 name = TOPIC_CONN_FACT_NAME; 417 try { 418 jtcf = TopicTcpConnectionFactory.create(host, port); 419 ictx.rebind(name, jtcf); 420 } catch (NamingException e) { 421 throw new JoramException("Cannot create a factory with the name '" + name + "'.", e); 422 } 423 } 424 425 430 private void createTopic(final String name) throws JoramException { 431 try { 432 joramAdapter.createTopic(name); 433 } catch (AdminException e) { 434 throw new JoramException("Cannot create a topic with the name '" + name + "'.", e); 435 } 436 } 437 438 443 private void createQueue(final String name) throws JoramException { 444 try { 445 joramAdapter.createQueue(name); 446 } catch (AdminException e) { 447 throw new JoramException("Cannot create a queue with the name '" + name + "'.", e); 448 } 449 } 450 451 455 private void createInitialTopics() throws JoramException { 456 for (String topic : topics) { 457 createTopic(topic); 458 } 459 } 460 461 465 private void createInitialQueues() throws JoramException { 466 for (String queue : queues) { 467 createQueue(queue); 468 } 469 } 470 471 475 public void setQueues(final List <String > queues) { 476 this.queues = queues; 477 } 478 479 483 public void setTopics(final List <String > topics) { 484 this.topics = topics; 485 } 486 487 488 492 public ResourceAdapter getResourceAdapter() { 493 return joramAdapter; 494 } 495 496 497 501 public void setHostname(final String host) { 502 this.host = host; 503 } 504 505 506 510 public void setPort(final int port) { 511 this.port = port; 512 } 513 514 515 } 516 | Popular Tags |