1 25 26 package org.objectweb.petals.jbi.transport; 27 28 import java.io.File ; 29 import java.io.IOException ; 30 import java.net.ConnectException ; 31 import java.util.ArrayList ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Properties ; 35 36 import javax.naming.Binding ; 37 import javax.naming.Context ; 38 import javax.naming.InitialContext ; 39 import javax.naming.NamingEnumeration ; 40 import javax.naming.NamingException ; 41 42 import org.apache.commons.io.FileUtils; 43 import org.objectweb.joram.client.jms.admin.AdminException; 44 import org.objectweb.joram.client.jms.admin.AdminModule; 45 import org.objectweb.util.monolog.Monolog; 46 import org.objectweb.util.monolog.api.LoggerFactory; 47 48 import org.objectweb.petals.PetalsException; 49 import org.objectweb.petals.jbi.transport.util.JoramConfigurationBuilder; 50 import org.objectweb.petals.kernel.admin.ContainerInformation; 51 import org.objectweb.petals.kernel.admin.ContainerInformation.ContainerState; 52 import org.objectweb.petals.util.JNDIUtil; 53 import org.objectweb.petals.util.LoggingUtil; 54 import org.objectweb.petals.util.PropertyUtil; 55 import org.objectweb.petals.util.SystemUtil; 56 57 import fr.dyade.aaa.agent.AgentServer; 58 59 65 public class JoramAgent { 66 67 protected static final String CONTAINERS_REF = "containers"; 68 69 72 private static final String JORAM_LOCATION = "joram"; 73 74 77 protected long containerUID; 78 79 82 protected String domain; 83 84 87 protected String domainPort; 88 89 92 protected LoggerFactory factory; 93 94 97 protected String fcState; 98 99 102 protected String host; 103 104 107 protected String id; 108 109 112 protected File joramDir; 113 114 117 protected LoggingUtil log; 118 119 122 protected String name; 123 124 127 protected String password; 128 129 132 protected InitialContext rootContext; 133 134 137 protected String tcpPort; 138 139 142 protected String username; 143 144 public JoramAgent(LoggingUtil log) { 145 this.log = log; 146 } 147 148 154 protected List <ContainerInformation> retrieveAllContainersInformation() { 155 log.start(); 156 List <ContainerInformation> containers = new ArrayList <ContainerInformation>(); 157 try { 158 InitialContext rootContext = new InitialContext (PropertyUtil 159 .retrieveJNDIProperties()); 160 if (JNDIUtil.isBound(rootContext, "/", CONTAINERS_REF)) { 161 NamingEnumeration <Binding > enums = rootContext 162 .listBindings(CONTAINERS_REF); 163 while (enums.hasMoreElements()) { 164 Binding binding = (Binding ) enums.next(); 165 ContainerInformation containerInformation = (ContainerInformation) binding 166 .getObject(); 167 if (containerInformation.isStarted()) { 168 containers.add(containerInformation); 169 } 170 } 171 } 172 } catch (NamingException e) { 173 log 174 .debug("Problem while trying to retrieve all the containers information : context may not be initialized"); 175 } 176 log.end(); 177 return containers; 178 } 179 180 183 protected List <ContainerInformation> containers = new ArrayList <ContainerInformation>(); 184 185 188 public void startServer() { 189 190 boolean isInit = false; 191 192 if (!isServerRunning()) { 193 194 try { 195 containers = retrieveAllContainersInformation(); 197 198 initConfiguration(); 201 202 boolean previsouslyStarted = isExistingConfiguration(); 205 206 if (!previsouslyStarted) { 207 log 208 .debug("First start, create a a3servers.xml configuration file."); 209 212 String configurationContent = null; 219 if (containers.size() > 0) { 220 223 log 224 .debug("Retrieve the existing network configuration from one of the running server."); 225 configurationContent = retrieveDistributedConfiguration(); 226 } else { 227 230 log 231 .debug("Create an empty configuration as no server is runnning on the network."); 232 name = id; 233 SystemUtil.setContainerName(id); 234 configurationContent = JoramConfigurationBuilder 236 .buildFullMasterConf(domain, id, name, host, 237 username, password, domainPort, tcpPort, Long 238 .toString(containerUID)); 239 } 240 241 cleanJoramServerNotStoppedCorrectly(); 243 244 FileUtils.writeStringToFile(new File (joramDir 247 + File.separator + "a3servers.xml"), 248 configurationContent, "UTF-8"); 249 250 System.setProperty("fr.dyade.aaa.agent.A3CONF_DIR", 251 joramDir.getAbsolutePath()); 252 253 } else { 254 log 257 .debug("A stored network configuration as been found, restart with it."); 258 } 259 260 initJoramAgentServer(); 262 263 isInit = true; 264 265 startJoramAgentServer(); 267 268 registerContainer(createContainerInformation()); 270 271 log.info("Joram server started at [host:port " + host + ":" 272 + domainPort + ",tcp port " + tcpPort + ", jndi port " 273 + ", domain name " + domain + ", server name " + name 274 + ", server id " + id + "\n"); 275 276 } catch (Exception e) { 277 log.error("Problem while starting the Joram server", e); 278 if (isInit) { 279 fr.dyade.aaa.agent.AgentServer.stop(true); 280 } 281 } 282 } 283 } 284 285 288 public void stopServer() { 289 log.start(); 290 updateContainerState(SystemUtil.getContainerName(), 291 ContainerState.STOPPED); 292 293 if (isServerRunning()) { 294 fr.dyade.aaa.agent.AgentServer.stop(); 295 } 296 log.end(); 297 } 298 299 317 protected void addJoramServer(int id, String host, String domain, 318 int domainPort, String tcpPort) throws ConnectException , AdminException { 319 AdminModule 320 .addServer( 321 id, 322 host, 323 domain, 324 domainPort, 325 name, 326 new String [] {"org.objectweb.joram.mom.proxies.tcp.TcpProxyService"}, 327 new String [] {tcpPort}); 328 } 329 330 335 protected void cleanJoramServerNotStoppedCorrectly() { 336 for (File file : joramDir.listFiles()) { 337 if (file.isDirectory()) { 338 boolean isLock = false; 340 for (File joramFile : file.listFiles()) { 341 if ("lock".equals(joramFile.getName())) { 342 isLock = true; 344 break; 345 } 346 } 347 if (isLock) { 348 try { 351 FileUtils.forceDelete(file); 352 } catch (IOException e) { 353 } 355 } 356 } 357 } 358 } 359 360 366 protected boolean isExistingConfiguration() { 367 return (joramDir != null && joramDir.exists() && joramDir.listFiles().length > 0); 368 } 369 370 376 protected void initConfiguration() { 377 factory = Monolog 379 .instanciateMonologFactory("org.objectweb.petals.util.monolog.wrapper.javalog.LoggerFactory"); 380 381 joramDir = new File (SystemUtil.getPetalsInstallDirectory() 383 + File.separator + JORAM_LOCATION + File.separator); 384 if (!joramDir.exists()) { 385 joramDir.mkdir(); 386 } 387 388 domain = SystemUtil.getJoramDomain(); 390 id = SystemUtil.getJoramId(); 391 name = SystemUtil.getJoramId(); 392 host = SystemUtil.getHost(); 393 394 username = SystemUtil.getJoramUser(); 395 password = SystemUtil.getJoramPassword(); 396 tcpPort = SystemUtil.getJoramTCPPort(); 397 domainPort = SystemUtil.getJoramDomainPort(); 398 containerUID = SystemUtil.getContainerUID(); 399 } 400 401 404 protected void initJoramAgentServer() throws Exception { 405 log.call(); 406 fr.dyade.aaa.agent.AgentServer.init((short) Integer.parseInt(id), 407 joramDir.getAbsolutePath() + File.separator + id, factory); 408 } 409 410 415 protected boolean isServerRunning() { 416 return 4 == fr.dyade.aaa.agent.AgentServer.getStatus(); 417 } 418 419 427 protected boolean isServerRegistered(String serverId) 428 throws ConnectException , AdminException { 429 boolean serverAlreadyExist = false; 430 for (Iterator serverIdsIt = AdminModule.getServersIds().iterator(); serverIdsIt 431 .hasNext() 432 && !serverAlreadyExist;) { 433 String aServerId = serverIdsIt.next().toString(); 434 serverAlreadyExist = (aServerId.equalsIgnoreCase(serverId)); 435 } 436 return serverAlreadyExist; 437 } 438 439 449 protected String retrieveDistributedConfiguration() { 450 String conf = null; 451 452 if (containers.size() > 0) { 453 Iterator <ContainerInformation> containersIt = containers.iterator(); 455 boolean fail = true; 456 while (fail && containersIt.hasNext()) { 457 try { 458 ContainerInformation information = containersIt.next(); 459 log.debug("Trying to connect to Joram server on " 460 + information.getHost() + ":" 461 + information.getJoramTCPPort()); 462 AdminModule.connect(information.getHost(), Integer 463 .parseInt(information.getJoramTCPPort()), information 464 .getJoramLogin(), information.getJoramPassword(), 3); 465 466 conf = AdminModule.getConfiguration(); 468 469 if (!isServerRegistered(id)) { 472 addJoramServer(Integer.parseInt(id), host, domain, 473 Integer.parseInt(domainPort), tcpPort); 474 } 475 AdminModule.disconnect(); 476 477 fail = false; 479 } catch (Exception e) { 480 log.warning("Impossible to connect to Joram, try another", 481 e); 482 } 483 } 484 if (conf != null) { 485 conf = JoramConfigurationBuilder.buildFullConfiguration(conf, 487 domain, id, name, host, username, password, domainPort, 488 tcpPort, Long.toString(containerUID)); 489 } else { 490 throw new RuntimeException ( 491 "Unable to connect to other Petals container, so stop"); 492 } 493 } else { 494 conf = JoramConfigurationBuilder.buildFullMasterConf(domain, id, 495 name, host, username, password, domainPort, tcpPort, Long 496 .toString(containerUID)); 497 } 498 return conf; 499 } 500 501 510 protected InitialContext retrieveInitialContext(Properties outProperties) 511 throws NamingException { 512 return new InitialContext (outProperties); 513 } 514 515 518 protected void startJoramAgentServer() throws Exception { 519 log.call(); 520 fr.dyade.aaa.agent.AgentServer.start(); 521 } 522 523 531 protected void updateContainerState(String containerName, 532 ContainerState state) { 533 log.start(); 534 if (containerName == null) { 535 throw new IllegalArgumentException ( 536 "Container name must be non null"); 537 } 538 if (!(!state.equals(ContainerState.STARTED) || !state 539 .equals(ContainerState.STOPPED))) { 540 throw new IllegalArgumentException ("State must be " 541 + ContainerState.STARTED + " or " + ContainerState.STOPPED); 542 } 543 try { 544 InitialContext rootContext = new InitialContext (PropertyUtil 545 .retrieveJNDIProperties()); 546 if (JNDIUtil.isBound(rootContext, "/", CONTAINERS_REF)) { 547 if (JNDIUtil 548 .isBound(rootContext, CONTAINERS_REF, containerName)) { 549 Context containersContext = (Context ) rootContext 550 .lookup(CONTAINERS_REF); 551 ContainerInformation containerInformation = (ContainerInformation) containersContext 552 .lookup(containerName); 553 containerInformation.setState(state); 554 containersContext.rebind(containerName, 555 containerInformation); 556 } 557 } 558 } catch (Exception e) { 559 log.error("Problem while updating container state", e); 560 } 561 log.end(); 562 } 563 564 569 public void shutdown() throws Exception { 570 571 576 try { 577 AdminModule.connect(host, Integer.parseInt(tcpPort), username, 578 password, 3); 579 AdminModule.removeServer(Integer.parseInt(SystemUtil.getJoramId())); 580 AdminModule.disconnect(); 581 582 removeFromContainersInformation(); 583 } catch (Exception e) { 584 log.error("Problem while connecting to this Joram server", e); 585 } 586 } 587 588 591 protected void removeFromContainersInformation() { 592 log.start(); 593 String containerName = SystemUtil.getContainerName(); 594 try { 595 InitialContext rootContext = new InitialContext (PropertyUtil 596 .retrieveJNDIProperties()); 597 if (JNDIUtil.isBound(rootContext, "/", CONTAINERS_REF)) { 598 if (JNDIUtil 599 .isBound(rootContext, CONTAINERS_REF, containerName)) { 600 Context containersContext = (Context ) rootContext 601 .lookup(CONTAINERS_REF); 602 containersContext.unbind(containerName); 603 } 604 } 605 } catch (Exception e) { 606 log.error("Problem while removing container information", e); 607 } 608 } 609 610 615 protected void registerContainer(ContainerInformation containerInformation) throws PetalsException { 616 log.start(); 617 try { 618 InitialContext rootContext = new InitialContext (PropertyUtil 619 .retrieveJNDIProperties()); 620 if (!JNDIUtil.isBound(rootContext, CONTAINERS_REF, 621 containerInformation.getName())) { 622 Context containerCtx = (Context )rootContext.lookup(CONTAINERS_REF); 623 containerCtx.bind(containerInformation.getName(), 624 containerInformation); 625 } else { 626 Context containerCtx = (Context )rootContext.lookup(CONTAINERS_REF); 627 containerCtx.rebind(containerInformation.getName(), 628 containerInformation); 629 } 630 } catch (NamingException e) { 631 throw new PetalsException( 632 "Error while binding container information", e); 633 } 634 log.end(); 635 } 636 637 644 protected ContainerInformation createContainerInformation() 645 throws PetalsException { 646 log.start(); 647 ContainerInformation containerInformation = new ContainerInformation(); 648 try { 649 containerInformation.setHost(SystemUtil.getHost()); 650 containerInformation.setName(SystemUtil.getContainerName()); 651 containerInformation.setJoramId(SystemUtil.getContainerName()); 652 containerInformation.setJoramDomainPort(SystemUtil 653 .getJoramDomainPort()); 654 containerInformation.setJoramLogin(SystemUtil.getJoramUser()); 655 containerInformation 656 .setJoramPassword(SystemUtil.getJoramPassword()); 657 containerInformation.setJoramTCPPort(SystemUtil.getJoramTCPPort()); 658 containerInformation.setJoramDomain(SystemUtil.getJoramDomain()); 659 660 containerInformation.setJmxPort(SystemUtil.getJmxPort()); 662 containerInformation.setJmxLogin(SystemUtil.getJmxUser()); 663 containerInformation.setJmxPassword(SystemUtil.getJmxPassword()); 664 665 containerInformation.setHtmlPort(SystemUtil.getHtmlPort()); 667 668 containerInformation.setJndiFactory(SystemUtil.getJndiFactory()); 670 containerInformation.setJndiPort(SystemUtil.getJndiPort()); 671 672 containerInformation.setUid(SystemUtil.getContainerUID()); 673 674 containerInformation.setState(ContainerState.STARTED); 675 676 } catch (Exception e) { 677 throw new PetalsException( 678 "Failed to retrieve container informations", e); 679 } 680 log.end(); 681 return containerInformation; 682 } 683 684 } 685 | Popular Tags |